Solved

Find main window title from process ID?

Posted on 2001-08-03
5
442 Views
Last Modified: 2013-12-03
I am launching an external application from my program.  If an instance of that application is still running, I want to bring it to the top instead of launching a new instance of that same process.

I call CreateProcess and receive a PROCESS_INFORMATION structure back.  This gives me a process ID, a process handle, a thread ID, and a thread handle.  The only other information I have about the external app is its path and file name.

My plan is this:  I want to go from the process/thread information to the (HWND) handle to the external application's main window.  From there, I want to get the window's title and store it in a static variable.  Then on subsequent attempts to launch this external application, I will first try a FindWindow(sWindowTitle) and if that succeeds, I'll SetForegroundWindow on it, otherwise, I'll launch the process again.

The problem is, I can't find a way to get the main window handle from only process and thread IDs and handles.
Does anyone know a good way to do this?
0
Comment
Question by:ris
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6350308
The following code illustrates how to do it:

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam) :


LPSTR GetWindowTitleFromPID ( DWORD dwProcessId)
{
g_hwnd = NULL;
g_nFound = 0;

EnumWindows (  FindHwndFromPID,    ( LPARAM) dwProcessId));

if ( g_nFound)  // we found one...
     return ( g_acTitle);

// nothing found :-(

return (NULL);
}

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam)
{
   DWORD   dwPID2Find  =   ( DWORD) lParam;
   DWORD   dwPID       =   0;

   if  (   GetWindowThreadProcessId    (   hwnd,   &dwPID))
       {
           if  (   dwPID   ==  dwPID2Find)
               {
                   g_hwnd  =   hwnd;

                   g_ nFound = GetWindowText ( g_hwnd, g_acTitle, MAX_SIZE);

                   return  (   FALSE);
               }
       }

   return  (   TRUE);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 6350322
Ooops, forgot some declarations:

// globals
HWND  g_hwnd;
int  g_nFound;
char g_acTitle[ MAX_SIZE];

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam) :


LPSTR GetWindowTitleFromPID ( DWORD dwProcessId)
{
g_hwnd = NULL;
g_nFound = 0;

EnumWindows (  FindHwndFromPID,    ( LPARAM) dwProcessId));

if ( g_nFound)  // we found one...
     return ( g_acTitle);

// nothing found :-(

return (NULL);
}

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam)
{
   DWORD   dwPID2Find  =   ( DWORD) lParam;
   DWORD   dwPID       =   0;

   if  (   GetWindowThreadProcessId    (   hwnd,   &dwPID))
       {
           if  (   dwPID   ==  dwPID2Find)
               {
                   g_hwnd  =   hwnd;

                   g_ nFound = GetWindowText ( g_hwnd, g_acTitle, MAX_SIZE);

                   return  (   FALSE);
               }
       }

   return  (   TRUE);
}

BTW: This was from a PAQ I answered in '99(http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10132929), so you can consider the solution to be 'validated'.
0
 
LVL 1

Author Comment

by:ris
ID: 6356105
I had to work with it a little bit but the problem was not with your code, it was with mine.  If you EnumWindows before the window to find has fully been created, the window to find isn't sent to the EnumWindowsProc function.  So I have to sleep for a second before looking for the window, and then it works.

Thanks for your help!
0
 
LVL 86

Expert Comment

by:jkr
ID: 6357374
>>So I have to sleep for a second before looking for the
>>window

You might want to use 'WaitForInputIdle()' rather than just 'Sleep()'ing, that's more reliable...
0
 
LVL 1

Author Comment

by:ris
ID: 6359640
Oh cool - I was actually wondering if there was an API call like that, but didn't know what to look for.  Thanks!
0

Featured Post

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

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

14 Experts available now in Live!

Get 1:1 Help Now