Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows MFC ATL WTL
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(&oWind) );
Posted 18-Apr-12 10:48am
mrbll183
Edited 19-Apr-12 12:47pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
mrbll at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
mrbll at 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
 
Apparently the atl library and mfc don't play together? I thought they could coexist.
Albert Holguin at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid