?
Solved

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

Posted on 1998-07-08
6
Medium Priority
?
394 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 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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