Solved

Find main window title from process ID?

Posted on 2001-08-03
5
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

729 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