The application retrieve window handles, using Enum* routines.
It happens that in the while the application manage the handle (get class name, window statistics...) of an enumerated/created window, the handle is no more valid. The code managing window handles are protected using a try/catch block, but the window handle is stored and the successively used for managing the represented window.
How to handle the window handle lifetime? It is possible to detect the handle invalidity?
I'd like to avoid try/catch blocks every time the application uses the window handles.
I already have the actual solution... but I didn't know about this until now!
Thank you all for having clarified about the window handle lifetime, but there is actually a method for being detected about window handle lifetime: CbtProc.
In the case the hook is installed system wide, it's possible to notify specific applications (it depends all on real implementation of the CBT hook) about a window destroy, which indicates that the a specific handle won't be valid after the notification.
From the documentation:
HCBT_DESTROYWND Specifies the handle to the window about to be destroyed.
Of course the access of the handles using WINAPI routines must be synchronized with the notification system, which doesn't seem to give good feasibility (CBT hook actually blocks window destroy because it is synchronized with the application logic).