70

How to get main window handle from process id?

I want to bring this window to the front.

It works well in "Process Explorer".

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
  • 7
    If you have two Firefox windows open, which one is the "main" window? They're equals. Process Explorer seems to choose whichever one most recently had the focus. – Rob Kennedy Dec 11 '09 at 17:43
  • 5
    Windows does not maintain the concept of a *"main window"*. There are *top-level* windows, *child* windows, and *owned* windows. Any process can have zero or more top-level windows. Unless you provide a succinct specification what determines the *"main window"*, this question cannot be answered. – IInspectable Feb 13 '16 at 22:13

7 Answers7

77

I checked how .NET determines the main window.

My finding showed that it also uses EnumWindows().

This code should do it similarly to the .NET way:

struct handle_data {
    unsigned long process_id;
    HWND window_handle;
};

HWND find_main_window(unsigned long process_id)
{
    handle_data data;
    data.process_id = process_id;
    data.window_handle = 0;
    EnumWindows(enum_windows_callback, (LPARAM)&data);
    return data.window_handle;
}

BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam)
{
    handle_data& data = *(handle_data*)lParam;
    unsigned long process_id = 0;
    GetWindowThreadProcessId(handle, &process_id);
    if (data.process_id != process_id || !is_main_window(handle))
        return TRUE;
    data.window_handle = handle;
    return FALSE;   
}

BOOL is_main_window(HWND handle)
{   
    return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
Ben
  • 25
  • 4
Hiale
  • 924
  • 1
  • 9
  • 10
  • Could you explain the logic behind is_main_window? I don't see the difference (in my tests) against using `if (data.process_id != process_id || !IsWindowVisible(handle))`. Also, this approach needs tweaking to support process ids that have multiple main windows (such as a web browser). – Class Skeleton Jul 23 '15 at 15:12
  • 4
    @CamelCase `GetWindow(handle, GW_OWNER) == 0` checks that the window is not an [owned window](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v=vs.85).aspx#owned_windows) (e.g. a dialog box or something). `IsWindowVisible(handle)` checks to see that the window is visible and not hidden (quite a few applications with no GUI still have a window that is hidden, or even ones with a hidden GUI like configuration apps that run in the tray). So the window is considered the "main window" if it is visible and has no owner, which is a good-enough description of most "main windows". – Jason C Oct 29 '15 at 15:41
  • This is a really good explanation of parent and owner that might clear a lot of this up for a lot of people, to help with adding or removing from the logic in this code: https://blogs.msdn.microsoft.com/oldnewthing/20100315-00/?p=14613 in a nutshell a window with no parent could still have an owner and not be a top level window. – Beeeaaar Apr 26 '17 at 00:05
  • This works for some applications, but it can break down for more complicated things. Some applications (e.g. MSO) have lots of windows that are "main" according to this definition and that cannot be safely closed first. I wound that checking the window classname is a good bet to get the "main" window for most applications. Also, beware of race conditions. A user might close the application while you're looping and break your code – sudo rm -rf slash Aug 06 '18 at 09:59
43

I don't believe Windows (as opposed to .NET) provides a direct way to get that.

The only way I know of is to enumerate all the top level windows with EnumWindows() and then find what process each belongs to GetWindowThreadProcessID(). This sounds indirect and inefficient, but it's not as bad as you might expect -- in a typical case, you might have a dozen top level windows to walk through...

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 6
    How do I know that the main window? – Alexey Malistov Dec 11 '09 at 15:49
  • 7
    @Alexey:From MSDN: "The EnumWindows function does not enumerate child windows." – Jerry Coffin Dec 11 '09 at 15:53
  • 1
    For reference, this is how .NET retrieves the main window handle: [System.Diagnostics.MainWindowFinder.FindMainWindow](http://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/ProcessManager.cs,db7ac68b7cb40db1) and [EnumWindowsCallback](http://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/ProcessManager.cs,8e9441ce1dfeed16). So arguably, .NET doesn't provide a *"direct way"* either. – IInspectable Jul 21 '15 at 13:58
11

There's the possibility of a mis-understanding here. The WinForms framework in .Net automatically designates the first window created (e.g., Application.Run(new SomeForm())) as the MainWindow. The win32 API, however, doesn't recognize the idea of a "main window" per process. The message loop is entirely capable of handling as many "main" windows as system and process resources will let you create. So, your process doesn't have a "main window". The best you can do in the general case is use EnumWindows() to get all the non-child windows active on a given process and try to use some heuristics to figure out which one is the one you want. Luckily, most processes are only likely to have a single "main" window running most of the time, so you should get good results in most cases.

Dathan
  • 7,266
  • 3
  • 27
  • 46
  • 2
    Actually, .NET caches the window handle the first time the [System.Diagnostics.Process.MainWindowHandle](http://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/Process.cs,4a6008b8d55b354b) property is accessed. The window handle isn't stored up front. It is evaluated using the same algorithm outlined by Jerry Coffin in [his answer](http://stackoverflow.com/a/1888944/1889329). – IInspectable Jul 21 '15 at 13:41
  • Yes, you're right. I don't know where I got that claim - probably just assuming from experience, even though the enumeration order of windows is unlikely to be guaranteed. – Dathan Jul 21 '15 at 20:18
11

This is my solution using pure Win32/C++ based on the top answer. The idea is to wrap everything required into one function without the need for external callback functions or structures:

#include <utility>

HWND FindTopWindow(DWORD pid)
{
    std::pair<HWND, DWORD> params = { 0, pid };

    // Enumerate the windows using a lambda to process each window
    BOOL bResult = EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL 
    {
        auto pParams = (std::pair<HWND, DWORD>*)(lParam);

        DWORD processId;
        if (GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second)
        {
            // Stop enumerating
            SetLastError(-1);
            pParams->first = hwnd;
            return FALSE;
        }

        // Continue enumerating
        return TRUE;
    }, (LPARAM)&params);

    if (!bResult && GetLastError() == -1 && params.first)
    {
        return params.first;
    }

    return 0;
}
Der_Meister
  • 4,771
  • 2
  • 46
  • 53
Benj
  • 31,668
  • 17
  • 78
  • 127
  • 3
    You may want to double check that the Window has no owner to avoid exiting the loop too early if the app has two windows: with `if (GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second && GetWindow(hwnd, GW_OWNER) == 0)` – Pep Feb 18 '17 at 17:37
2

Though it may be unrelated to your question, take a look at GetGUIThreadInfo Function.

AntonK
  • 1,210
  • 1
  • 16
  • 22
1

As an extension to Hiale's solution, you could provide a different or modified version that supports processes that have multiple main windows.

First, amend the structure to allow storing of multiple handles:

struct handle_data {
    unsigned long process_id;
    std::vector<HWND> handles;
};

Second, amend the callback function:

BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam)
{
    handle_data& data = *(handle_data*)lParam;
    unsigned long process_id = 0;
    GetWindowThreadProcessId(handle, &process_id);
    if (data.process_id != process_id || !is_main_window(handle)) {
        return TRUE;
    }
    // change these 2 lines to allow storing of handle and loop again
    data.handles.push_back(handle);
    return TRUE;   
 }

Finally, amend the returns on the main function:

std::vector<HWD> find_main_window(unsigned long process_id)
{
    handle_data data;
    data.process_id = process_id;
    EnumWindows(enum_windows_callback, (LPARAM)&data);
    return data.handles;
}
Class Skeleton
  • 2,913
  • 6
  • 31
  • 51
  • [When you transfer control across stack frames, all the frames in between need to be in on the joke](https://devblogs.microsoft.com/oldnewthing/20120910-00/?p=6653). – IInspectable Sep 21 '21 at 20:38
-1

Just to make sure you are not confusing the tid (thread id) and the pid (process id):

DWORD pid;
DWORD tid = GetWindowThreadProcessId( this->m_hWnd, &pid);
Oliver Zendel
  • 2,695
  • 34
  • 29