How to check whether the window handle is valid?

24 08 2008


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.


Actions

Information

2 responses

17 09 2008
SuperKoko

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.

24 09 2008
Jijo.Raj

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.

Leave a comment