How to find a window handle of the current process.

Have a library function which may or may not need to create a window.
If it does create a window, and the user closes the min window, MFC gets into a weird state. It does not exit as it normally would, even after closing the library function subwindow.
Debugger shows its stuck in a message loop, even though all windows are closed.

Now if I make the library function "owned" by the main window, it closes just fine, as it gets a WM_DESTROY message when the main window is closed.

But the library function may get called in any C++ context, and does not necessarily open a window. It may even get called from applications hat have no other windows open.

And since he library function can be called from any application, of course it does not know the names or classes of windows that application might be using.

So, if I can simply find any top level window owned by the current application process, i can make that window the owner of the library subwindow, and it closes fine (tested that already). Just need the hook for correct closing the process. But I can find a general way of finding windows in the same process.

FindWindow and FindWindowEX both just return any old processes windows.
No option I can find to ask for one of the running applications windows.

Most other GetWindow sorts of functions appear to require a Window Handle,
so circular problem, have to have a window handle to find a window handle.

This seems like such a simple thing to do, find my applications open windows.
What am I missing?
You should be able to use FindWindow()/FindWindowEx() in conjunction with GetWindowThreadProcessId() (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx ) to obtain the PID that owns the window. If the PID of the window is the same PID obtained via GetCurrentProcessId() (http://msdn.microsoft.com/en-us/library/windows/desktop/ms683180(v=vs.85).aspx ) then you have found a window that belongs to your process.
Well, FindWindow just finds a random desktop window.
It has a parameter to let you see the next window in the "z" order, but since that is subject to change, there is no way of 100% knowing you have visited every window. You might get back to the same window you started with because it was surfaced or buried, and skip any number of windows because the order changed.

If you could pass a process id to findwindow, that would do it.

Apparently enumwindows will do something like that, but asking what does everyone have seems remarkably heavyweight way to ask what have I got?

thanks for taking the time to answer.
Solution from another forum, clunky but tested, works.

static BOOL CALLBACK MyEnumThreadWndProc( HWND hwnd, LPARAM param ){
HWND *oW = reinterpret_cast<HWND*> (param);
(*oW) = hwnd;
return false;
}

...
HWND oWind=0;
EnumThreadWindows(GetCurrentThreadId(), &MyEnumThreadWndProc, reinterpret_cast<LPARAM>(&oWind) );
Last edited on
Topic archived. No new replies allowed.