Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble un-hooking 2

Status
Not open for further replies.

tsh73

Programmer
Apr 27, 2002
304
RU
Hello all, hope you can clear this puzzle for me.
On the main form in load event I have
Code:
    Dim Lng_Menu&
    Lng_Menu = GetSystemMenu(hwnd, False)
    AppendMenuA Lng_Menu, MF_SEPARATOR, 0, vbNullString
    AppendMenuA Lng_Menu, MF_CALLBACKS, &HF, "About"
    If Not Hooked Then
        lpOldProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf Handler)
        Hooked = True
    End If
Ok, this works fine, it adds a line in systemmenu and calls given proc (Handler(...))

But then I close the program it quits VB IDE as well.
Ok, I guess I should restore previous WNDPROC, do I?
Here interesting things starts.
If I put the code for restoring WNDPROC say to keyPress window, like this
Code:
Dim Lng_Menu As Long, res As Long
    If KeyAscii = Asc("q") Then
        'reset handler to prev
        If Hooked Then
            Lng_Menu = GetSystemMenu(Me.hwnd, False)
            res = SetWindowLong(hwnd, GWL_WNDPROC, lpOldProc)
            Hooked = False
Close #1
        End If
and press this key - then I exit OK and go to VB IDE.

BUT! If I put that very code (except If KeyAscii = ... ) in form's Query Unload - it bombs out of VB IDE.
I checked that I do not try to do it twice (Hooked global var), I even added logging to file to check if I have multyple calls of load / query unload - no, it called only once... I checked returned val of SetWindowLong (res variable), it's not 0. Actually I believe it's same value - be it called from query Unload or KeyPress.

So far I just commented out piece that adds line to menu.
But it really makes me wonder what I doing wrong.
Any ideas?
 
Have you tried compile it and test on exe rather than in VBE?
 
is "hwnd" a varible that you are setting the value of the menu to in some other code? I try to get the handle at each point that I need it, instead of assigning it to a variable. I might be mistaken but I seem to remember reading somewhere that handles can change and it's not recommended to assign a handle to a variable.
 
This is a highly confusing statement mentioned in the VB documentation for hWnd property. hWnd property never changes throughout the life of a window or control. I don't understand what MS mean by the following statement.

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

In my opinion unhooking your window is not necessary, although it may be considered a good programming practice. If you do not end your program using an End statement, or in some other abrupt way, unhooking the window is not necessary.

I just created a subclassed window and tried terminating the program both ways - with and without unhooking, and it went fine both ways.
 
<hWnd property never changes throughout the life of a window or control

Well, yes, but not throughout the life of an application, right? So, if you close a form and then reopen it, for example, its hWnd property changes. I think that's what Microsoft is getting at, and explaining poorly: Since the value of the hWnd property can change during the life of an application (since the item can get destroyed and reinstantiated), be careful when you store its value in a variable, because the reference could be obsolete if the object to which it refers gets destroyed and reinstantiated.

Bob
 
Glad to know I wasn't hallucinating and I did see that written down. Whether it's true or not.

I've got a reference book "VB programmers guide to the Win32 API by Dan Appleman" that says using SetWindowLong on GWL_WNDPROC is "dangerous and should not be done by a VB application". Hmmm...

Robert
 
>a reference book

the reference book, I think you'll find (although I suspect my copy might be a little older and a little more out of date than yours ...)

>dangerous and should not be done by a VB application

But Dan isn't always right
 
>So, if you close a form and then reopen it, for example

Quite so - and that's just an example within VB. There are also a number of (rare) API techniques that require destroying a window and then recreating it which result in a changed hWnd
 
>But Dan isn't always right

Just most of the time. :)

strongm, what sort of rare API techniques would involve this? My gut feeling is that it would most often have something to do with repainting.
 
Hypetia, you are right about my case.
It was loose END after
-------------------------------
For Each frm In Forms
Unload frm
Next
-------------------------------
... I was sure it wasn't matter as all is unloaded.
Anyway I removed it and all works fine.
Have a star.
 
>what sort of rare API techniques

Superclassing, for example
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top