Solved

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

Posted on 1998-07-08
6
393 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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