Blog Archives

AppActivate In C#

AppActivate, which many of you may recognize from VB activates a window for an application which is already running, bringing the window of that app to the foreground.

In C#, there is no exact equivalent to this function, which leaves two alternatives (see link at the bottom for code examples):

Option 1 (recommended):
Use SetForeGroundWindow, described in my similar post (scroll down a bit for the code examples).

Option 2:
Add a reference to Microsoft.VisualBasic and call Interactive.AppActivate.

Although Option 2 is a little simpler/faster if you are not familiar with or ready yet to delve into Win32 API, it is not a recommended solution as there can be potential side effects of this approach.

For code examples of both options, see my similarly titled post below and scroll through for code references.

References
Start a Process in the Foreground in C# .Net Without AppActivate https://ronniediaz.com/2011/05/03/start-a-process-in-the-foreground-in-c-net-without-appactivate/

Start a Process in the Foreground in C# .NET without AppActivate

In my particular scenario, I was using System.Diagnostics.Process to start another application, compiled separately, which communicated with the original app which launched the process and used Windows inter-process communication (IPC) to share information across applications using my own protocol.

I won’t delve into the specifics of the above setup, but I did find it interesting that what seemed so simple to do in VB .Net (migrating an old app to C#) was potentially much more complex in C#.

(In VB I was using the function AppActivate).

So my initial Google search for “process.start foreground” led me to a codeproject site that described Process.Start and then 4 links down I discovered an experts exchange link which helped me conceptually reword my search to “bring window into foreground” and led to the discovery on MSDN of the SetForegroundWindow Function.

Reading this definitely started to bring back some old memories of COM and Win32 API development, so I was sure I was beginning to overcomplicated things and went back to my searches to see if I could replace the way I was previously using in VB.

I started searching for “AppActivate alternatives in C#”, but this too was to no avail, as most places seemed to simply suggest something like:

(add reference to Microsoft.Visualbasic first)

using Microsoft.VisualBasic;

void Main() {
Microsoft.VisualBasic.Interaction.AppActivate((System.Diagnostics.Process.GetCurrentProcess().Id));
}

Technically the above does work, but caused a strange “Process 5212′ was not found” error on exit of the second application.

So I dusted off the old C++ Win32 API notes from storage, cracked open a redbull and set to work. To my surprise, the end result was actually very simple. See below:

Basic code to SetForeGroundWindow(don’t forget to add interop reference)

using System.Runtime.InteropServices;

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

void Main() {
IntPtr splashwindow = FindWindowByCaption(IntPtr.Zero, "Title of Form");
                SetForegroundWindow(splashwindow);
}

You might be tempted to use something like MainWindowHandle of Diagnostics.GetCurrentProcess, but be advised this does not always return the correct Window handle you are looking for.

See articles related articles for any questions or issues on:

References
Inter-process communication, http://en.wikipedia.org/wiki/Inter-process_communication
Code Project
Experts-Exchange
MSDN (SetForegroundWindow Function), http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx
MSDN (AppActivate), http://msdn.microsoft.com/en-us/library/dyz95fhy(v=vs.80).aspx
MSDN (Process.GetCurrentProcess), http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx#Y200
Bytes.com (AppActivate), http://bytes.com/topic/c-sharp/answers/244216-appactivate-function-c
Vaibhav Gaikwad (SetForeGroundWindow), http://geekswithblogs.net/vaibhavgaikwad/archive/2007/11/01/116510.aspx
VBForums (AppActivate), http://www.vbforums.com/archive/index.php/t-386106.html