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.