Gold mine of Visual C++ tricks!
How to check whether the window handle is valid?
![]()
Since communication by messages are so easy, windows used to communicate with each other by using messages. For instance, if simple data blocks are to be transfered – WM_COPYDATA can be used. For all those instances the window should be alive. But how to know whether the current window handle is valid or whether it points to a dead window?
![]()
You can call the api – IsWindow() by passing window handle. If the handle points to a live window, then the function returns true else false. Have a look at the code snippet.
// The handle to be tested. HWND hWindow = GetSafeHwnd(); // Check whether the window is still there. BOOL bWindowAlive = IsWindow( hWindow );
![]()
While digging for the api, i found an interesting info. What about window handle re-cycling? For instance you have a window handle and you’re going to check that window by calling IsWindow() function. But in between that the real window is closed and a new window is created. Whether the window handler will be allocated to the new window?
Well, the answer is here. Have a look at it. Its interesting. The oldnewthing.
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on August 24, 2008 at 7:09 pm, and is filed under Windows APIs. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Beware: This is only a sanity check. Do not rely on it to manage the lifetime of windows. IsWindow may return true on a dead window handle because of handle recycling, even if it’s less likely to happen on Windows XP than on Windows Me.
about 1 year ago
I’ve already specified about handle recycling. Did you go through the link I’ve provided? Its here – http://blogs.msdn.com/oldnewthing/archive/2007/07/17/3903614.aspx.