Solved

Find main window title from process ID?

Posted on 2001-08-03
5
449 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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