Solved

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

Posted on 1998-07-08
6
384 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 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

19 Experts available now in Live!

Get 1:1 Help Now