how does Windows (re)assign HWND values?

Posted on 2006-05-24
Last Modified: 2013-12-03
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?
Question by:plevintampabay
    LVL 86

    Expert Comment

    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.
    LVL 3

    Accepted Solution

    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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
    In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now