Solved

Find main window title from process ID?

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

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

867 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

24 Experts available now in Live!

Get 1:1 Help Now