• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Setting Focus to another Application

Using VC++ and NT, how can I set input
focus to another running application,
like AppActivcate in VB.  Looking for a
method to switch between applicatons using software.  One application is a commercial package that we are not able to modify.  
1 Solution
What about simply using

HWND hWnd = FindWindow ( NULL, "<Window's title>");

SetForegorundWindow ( hWnd);

(error chacking omitted for brevity)

TBurschAuthor Commented:
Thanks for your quick respones.
FindWinow() looked promising, but ...
both applications change the Window Title, the string that shows up on the title bar, by appending the working file name to the application name.  So, the Window Title changes each time the applications are started.

I was hopping to find a way to use the application name, that seems to stay constant.

Thanks again,
Are you the author of both applications?

Are the separate instances of the same paplication or two completely different applications?

If you have source code for both, then you could get them both to write their main window handle to a shared file/memory.  Then they could talk to each other and activate one another etc.

If two instances of the same app, this would be a little easier.  But not necessary.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

TBurschAuthor Commented:
The applications are completely different.   We are the authors of one of them.   The other is a third party product that we only have the executable for.

Somthing seems to be jamming my responses back to ExpertsExchange, sorry for the delay.

Thanks for the thoughts,
****In the lines of jkr ****
in that case U can use
HWND hWnd = FindWindow ("WindowClassName", NULL);

Hope this helps
If the application's main window doesn't have a unique class name try the following. This will only work on NT, Win95/98 requires toolhelp functions which NT doesn't provide. There are a couple of examples of how to provide this for both platforms on the MSDN. I don't have access to NT so I am just providing a rough outline.

1) Enumerate all top level windows with the function EnumWindows.

2) For each window, call the function GetWindowThreadProcessID to get its process ID.

3) Open the process with a call to the function OpenProcess as follows:
(where nProcessID is the value obtained via the call to GetWindowThreadProcessID)

4) Obtain a list of all modules associated with this process with a call to the function EnumProcessModules. The 0 element in the array of module handles filled by this function will be the EXE.

5) Make a call to GetModuleFileNameEx to obtain the name of the application. This function is preferred over GetModuleFileName as it can get this information about a separate process.

6) If the file name is the application you want to set the focus to, make a call to SetForegroundWindow with its handle.

u can also use "EnumWindows" to get the handle that u want, then use
hope this help
TBurschAuthor Commented:
EnumWindows()looks promising, and am trying to write some code to work out the details, but am having trouble locating the PSAPI.DLL and the required header file.  I am assuming it is named psapi.h.    Should I be able to find the DLL in the Development Platform release disks?
My apologies! I didn't notice in my sources PSAPI.DLL isn't included with the system. Here is an excerpt from the MSDN article "HOWTO: Enumerate Applications in Win32" which tells where to go to get this files.

The Windows NT approach to creating a list of processes uses functions from the PSAPI.DLL. The PSAPI.DLL file is distributed with the Platform SDK, which is available at http://www.microsoft.com/msdn/sdk (path: \MSSDK\bin\winnt\psapi.dll). The PSAPI.H and PSAPI.LIB files that you need are also included with the Platform SDK (paths: \Mssdk\samples\sdktools\image\winnt\pfmon\psapi.h and \Mssdk\samples\sdktools\image\winnt\pfmon\psapi.lib).

To use the functions in the PSAPI.DLL, add the PSAPI.LIB file to your project, and include the PSAPI.H file with any modules that call the PSAPI.DLL functions. Remember to distribute the PSAPI.DLL file with any executable that uses it, as it is not currently distributed with the operating system.

TBurschAuthor Commented:
Well it was a little struggle, but I found the DLL and .h, and with the help of the article got some code working.  As it turns out what I needed was to get the process.   My code works most of the time ... there is still some uncertanty in the function that I haven't worked out yet, but I am almost there.   Would like to have the programs come up full screen ... are coming up minimized now.  I'm sure there must be a function to do that.
Thanks much for your help, corey frost.
For a windowed application with a resizable frame you can make a call to the Win32 API function ShowWindow. Since you have the handle to main window that you want to set focus to, you could make this call before you change the focus:
::ShowWindow( hWndOfOtherApplication, SW_MAXIMIZE );

If you want your MFC application to initially appear maximized, you'll need to make some changes to your implementation of CWinApp::InitInstance(). The AppWizard will most likely have added these lines near the end of the function:


Just replace m_nCmdShow to SW_MAXIMIZED. The value of m_nCmdShow is only referenced once during the life of the application so don't bother changing it.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now