Find main window title from process ID?

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?
LVL 1
risAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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
 
jkrCommented:
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
 
risAuthor Commented:
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
 
jkrCommented:
>>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
 
risAuthor Commented:
Oh cool - I was actually wondering if there was an API call like that, but didn't know what to look for.  Thanks!
0
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.

All Courses

From novice to tech pro — start learning today.