Link to home
Start Free TrialLog in
Avatar of Gichamba
Gichamba

asked on

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

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?

Gichamba

ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

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.
Avatar of Gichamba

ASKER

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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/createwindowex.asp

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!