how does Windows (re)assign HWND values?

Lets say I use the Windows CE API call - EnumWindows, to get a list of the top level windows.  I will then have a list of HWND values for the open top level windows.

Now lets say I store that list of HWND values.

I realize that a window could close, but will the OS reuse those values for new windows?  If some time later I use one of the stored HWND values, for how long will the value refer to the window it referenced when I obtained the value?
plevintampabayAsked:
Who is Participating?
 
Mikeh926Commented:
jkr is correct that you shouldn't rely on handles remaining the same. However, if you are sure that the window will not be closed, then the handle will remain valid.

In practice CE tries not to reuse handles. If a window is closed and then reopened, it will almost certainly have a different handle value because CE allocates them on an incremental basis. The actual mechanism that CE uses is not documented and could change from one release of CE to another.

In theory if enough windows were closed and opened, a handle could be reused but that would probably take quite a while, so you might get away with testing your handle value with IsWindow() to find out if the window has been closed. Even then, if it's not your window and it could be closed at any point, there is no guarentee that it won'd be closed between you calling IsWindow() and doing whatever you are going to do with it.

Regards,
Mike.

0
 
jkrCommented:
It is not safe to rely on handle values for the reasons you have mentioned. On the other hand, there are over 4 billion possible handle values, so that it should take a while until they're reused. Anyway, the prince of calling 'EnumWindows()' is small compared to the risk of relying on unsafe assumptions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.