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
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.


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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 106
notReplace  challenge 53 116
NotAlone Challenge 20 79
Annoying "thing" blocks my view 4 80
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now