Solved

Setting Focus to another Application

Posted on 2000-02-24
11
334 Views
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.  
0
Comment
Question by:TBursch
11 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
What about simply using

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

SetForegorundWindow ( hWnd);

(error chacking omitted for brevity)

?
0
 

Author Comment

by:TBursch
Comment Utility
jkr,
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,
tom
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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.

0
 

Author Comment

by:TBursch
Comment Utility
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,
tom
0
 
LVL 4

Expert Comment

by:inpras
Comment Utility
Hi
****In the lines of jkr ****
in that case U can use
HWND hWnd = FindWindow ("WindowClassName", NULL);

Hope this helps
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:coreyfrost
Comment Utility
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:
 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nProcessID );
(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.


0
 

Expert Comment

by:johnxi
Comment Utility
u can also use "EnumWindows" to get the handle that u want, then use
"SetForegorundWindow"
hope this help
0
 

Author Comment

by:TBursch
Comment Utility
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?
tom
0
 

Accepted Solution

by:
coreyfrost earned 100 total points
Comment Utility
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.


0
 

Author Comment

by:TBursch
Comment Utility
coreyfrost,
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.
tom
 
0
 

Expert Comment

by:coreyfrost
Comment Utility
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:

pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

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.

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 92
sumDigits challenge 9 96
fizzArray2 challenge 1 56
Change to event 1 72
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

19 Experts available now in Live!

Get 1:1 Help Now