We help IT Professionals succeed at work.

how do you know when a window's hWnd Changes ?

Gichamba asked
Last Modified: 2007-12-19
Hello all,
For some security reasons, my clients have contracted me to write  a vb application that runs a windows shell (replacing explorer). As you would expect of such an application, its heavily dependent on the API. In brief it hooks into the the windows messaging system via a small win32 dll i had to write in C. So far everything is working perfectly and the application is in testing phase. I have been well relieved that everything is going according to plan until i stumbled on the following in the MSDN and i quote:

"The Microsoft Windows operating environment identifies each form and control in an application by assigning it a handle, or hWnd. The hWnd property is used with Windows API calls. Many Windows operating environment functions require the hWnd of the active window as an argument.

Note:   Because the value of this property can change while a program is running, never store the hWnd value in a variable." end of quote.

To me this is unthinkable. The program does exactly what MSDN tell me not to do: it stores the hWnd of all the open top-level and unowned  windows in the system into variables so that i can manipulate them from my application. The "Constantness" of other applications' windows is a big factor.

Now can someone tell me what is constant in a window(other applications' windows). What property of a window is guaranteed to remain constant throughout a window's life. If the hWnd of a window can change when it is running is there a way my application can know when this is happening. An answer in any programming language will be acceptable. I dont want to keep monitoring the system for any loaded windows as this will certainly cause irritating flickers.

And another question, what worries me most: Why does my application appear to be running perfectly?


Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)

I understand that in such important issue you need to be sure that my opinion is 100% correct. I suggest you to open another question in the Windows Programming area to listen other experts. Having confirmation from number of EE stars, you will fill better.


Thanks AlexFM for your comment. Can you point me to any documentation that will shed a little more light on the "Constantness" of the hWnd prefferably a microsoft site?

It is really difficult. This is so obvious, that I don't think some documentation explicitly mentions this. Window is created using CreateWindowEx API:


It returns HWND which is used in all calls to this window. This MSDN topic doesn't write that window handle may changed (possibly because you are first who thought about this). This is window handle definition from MSDN Library Glossary:

window handle
In the Win32 API, a 32-bit value (assigned by Windows) that uniquely identifies a window. An application uses this handle to direct the actions of functions to the window. A window handle has the HWND data type; an application must use this type when declaring a variable that holds a window handle.

You can find this in MSDN library, just type w in the Index.

Notice that in Win32 programming (without using of MFC and VB) keeping of window handle in the variable is the only way to talk with this window. This is done always; VB control which returns hWnd property keeps this value as it's own class member.


I do agree with you AlexFM especially with the Win32 part you have just quoted, the only problem i have is: why did someone at microsoft find it neccesary to write the part i quoted above?

I think it is unsuccessful and misleading attempt to encourage good programming style. If you have the control on the form and want to get it's hWnd, it is always better to get it each time from this control instead of keeping it as variable.


Thank you AlexFM, I think that must be it since the quote above came from the VB documentation part, I am now convinced that the reason why this can change is you can unload a window then load it again during your application's life, and in such a case you are not guaranteed that the hWnd will be the same.
I think the key phrase in the quote is "value of this property can change while a program is running" and not "When a window is open".
Thank you Alex for convincing me iam not going down the wrong path!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.