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

Advertisements

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on May 3, 2011, in Programming & Development and tagged , , , , , , , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: