Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have already pointed out</p> <ul> <li>In general, an application can have zero or multiple top-level windows.</li> <li>If you're creating the window yourself you can just remember the HWND somewhere.</li> </ul> <p>But maybe your code is in a DLL, so you didn't actually create the top-level window yourself. So what to do?</p> <p>I would suggest the following:</p> <ul> <li>Use <code>EnumWindows</code> to enumerate all top-level windows.</li> <li>Use <code>GetWindowLongPtr</code> to get the HINSTANCE for each top-level window. Compare this against the HINSTANCE of the application, which you can get using <code>GetModuleHandle(NULL)</code>. If they're identical, you've found your main window.</li> </ul> <p>Edit: Here is some code. Turns out you also have to use <code>IsWindowVisible</code> because there seem to be quite a few invisible "helper" windows.</p> <pre><code>HWND hwndMain; BOOL CALLBACK EnumWindowProc(HWND hwnd, LPARAM lParam) { HINSTANCE hinst=(HINSTANCE)GetModuleHandle(NULL); if((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE)==hinst &amp;&amp; IsWindowVisible(hwnd)) { hwndMain=hwnd; return FALSE; } else return TRUE; } </code></pre> <p>Then in the place you want to find the window:</p> <pre><code>hwndMain=NULL; EnumWindows(EnumWindowProc, 0); </code></pre> <p>And after this, <code>hwndMain</code> should contain the handle of the window, or <code>NULL</code> if none exists.</p> <p>Using <code>EnumWindows</code> is a bit burdensome but is recommended over calling <code>GetWindow</code> in a loop because, as MSDN notes: "An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed."</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload