Friday, February 7, 2014

Switching Windows in Windows

Had a request to ping pong between two applications running in windows.

So, when the user clicks a button from application A, it fires this executable (see below).  This program looks to see if the other application B is running, if so, it switches to application B and puts it in the foreground of the user's session.  if application B is not running, it start application B.  For this post, i'm only showing the process from A to B.  You can simply reuse this code to switch from B to A.

Microsoft's task manager gives you some insight into what needs to be done.  An application running in windows is called a "process".

From a windows desktop application, the program interrogates all processes for a particular process name, e.g. "IntuifaceComposer".

If the process exists, the program obtains a window handle and puts the application in the UI's foreground by using a function in dll import, user32.dll, SetForegroundWindow().

Otherwise, if a new application is needed, the program calls the Start method of Process and passes the file location of the application.



No comments:

Post a Comment