Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a library function (that does not know anything about the application its linked to) that sometimes opens a window.

If the application's main window is closed while the library window is open, the process gets into a weird state that wont exit, even after the library function widow is closed.

If the library function is "owned" by the main window, everything closed fine. However, in general the library function has no idea what windows are open.

The standard windows functions appear to either search all windows for all processes, or they require a window handle to find another window handle.

How do I find ANY top level window of the process my function is running in?
=======================================================================
Solution, 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) );
Posted
Updated 19-Apr-12 12:47pm
v2

You can use the system call EnumWindows to find all of your top-level windows. After your application has closed all its own windows, no other window should remain. If EnumWindows detects anything, then it's probably the additional window that your function has created.

If you know the window class or window name of that window, you could do a further verification that you have found the right window.

If you have a multi-threaded application with multiple UI threads you can use EnumThreadWindows to enumerate all top-level windows belonging to a particular thread.
 
Share this answer
 
Comments
mrbll 19-Apr-12 13:29pm    
thanks. EnumThreadWindows looks clunky to use, but that is the first answer I got that appears to be workable and not search every window on the computer.
You should be able to call AfxGetMainWnd() from the exported function and get the top-level window from the thread calling the exported function (may return NULL if there's no window associated with the thread).

See documentation:
http://msdn.microsoft.com/en-US/library/waas15s1%28v=vs.80%29.aspx[^]

Or... you can use AfxGetApp(), then call CWinThread::GetMainWnd() (rememeber CWinApp is derived from CWinThread).
Reference:
http://msdn.microsoft.com/en-US/library/5k9f064x%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-US/library/sazyysab%28v=VS.80%29.aspx[^]
 
Share this answer
 
Comments
mrbll 19-Apr-12 13:26pm    
Sounded perfect, but when I included afxwin.h, its unhappy.
1>c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxv_w32.h(16) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>

Apparently the atl library and mfc don't play together? I thought they could coexist.
Albert Holguin 19-Apr-12 15:11pm    
They should be able to coexist fine, compilation order may cause issues though. Does your project already use MFC? ...control which libraries you're using through the project settings so that the compilation order will be correct, don't #include the files.
mrbll 19-Apr-12 15:49pm    
Thanks. I tried altering the project to add "use mfc "(making my windows with WTL.) It did not make the afx call visible. Tried putting afxwin.h in stdafx.h at the place the comment says visual studio will add it, but it got the same compile error. Whats the trick to make visual studio do the right includes for afxwin.h?
mrbll 19-Apr-12 15:48pm    
Thanks. I tried altering the project to add "use mfc "(making my windows with WTL.) It did not make the afx call visible. Tried putting afxwin.h in stdafx.h at the place the comment says visual studio will add it, but it got the same compile error. Whats the trick to make visual studio do the right includes for afxwin.h?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900