Solved

Getting the main window of a thread (VC++)

Posted on 1998-07-08
6
392 Views
Last Modified: 2013-12-03
I've launched a process using CreateProcess (so I have the associated PROCESS_INFORMATION and STARTUPINFO stuff).  How do I get the process's main window?

thanks  
0
Comment
Question by:SeanPS
[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
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1409653
There is no one API to do it (because a process might not have a main window or have more than one).

Instead, you need to enumerate the top level windows using EnumWindows() and check which one corresponds with you process by comparing its GetWindowThreadProcessId() with your process ID.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1409654
A minor correction.  GetWindowThreadProcessId() returns the thread ID and sets the second argument to the process ID.  The PROCESS_INFORMATION structure will contain both.

Something like (modulo bugs and missing parts):

    HWND global_hwnd = NULL;

    int WINAPI WinMain(...)
    {
        PROCESS_INFORMATION pi;
        CreateProcess(..., &pi);
        WaitForInputIdle(pi.hProcess, INFINITE);
        EnumWindows(EnumWindowsProc, (LPARAM)pi.dwProcessId);

        if (global_hwnd != NULL)
        {
            // Do whatever...
        }

        // ...
    }

    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
        DWORD pid;
        GetWindowThreadProcessId(hwnd, &pid);
        if (pid == (DWORD)lParam)
        {
            global_hwnd = hwnd; // Found the window
            return FALSE;
        }
        else return TRUE;
    }

0
 

Author Comment

by:SeanPS
ID: 1409655
Adjusted points to 100
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 11

Expert Comment

by:alexo
ID: 1409656
Have fun!
0
 

Author Comment

by:SeanPS
ID: 1409657
based on your original ideas, it seems that EnumThreadWindows() is a slightly better alternative.  i was hoping that i could easily create a CWinThread object from the stuff i have, but that doesn't seem to be the case.

thanks for your help
sean
0
 
LVL 11

Expert Comment

by:alexo
ID: 1409658
>> it seems that EnumThreadWindows() is a slightly better alternative.
It is, provided that only one thread in the process is creating windows.

>> i was hoping that i could easily create a CWinThread object from the stuff i have
Check the MFC sources.  You'll see that CWinThread is a complicated beast.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…

749 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