Setting Focus to another Application

Posted on 2000-02-24
Last Modified: 2013-11-20
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.  
Question by:TBursch
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 2555740
What about simply using

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

SetForegorundWindow ( hWnd);

(error chacking omitted for brevity)


Author Comment

ID: 2556136
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,
LVL 10

Expert Comment

ID: 2556230
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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2556344
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,

Expert Comment

ID: 2557107
****In the lines of jkr ****
in that case U can use
HWND hWnd = FindWindow ("WindowClassName", NULL);

Hope this helps

Expert Comment

ID: 2557156
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.


Expert Comment

ID: 2557163
u can also use "EnumWindows" to get the handle that u want, then use
hope this help

Author Comment

ID: 2566319
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?

Accepted Solution

coreyfrost earned 100 total points
ID: 2567830
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 (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.


Author Comment

ID: 2576950
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.

Expert Comment

ID: 2582136
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.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question