[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 1998-07-08
6
Medium Priority
?
397 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
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 300 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

872 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