Announcement

Collapse
No announcement yet.

Why the Hell this Hook Is not Global?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why the Hell this Hook Is not Global?

    I have made this code
    (it is just test code)
    Code:
    #Compile Dll
    #Dim All
    
    #Include "WIN32API.INC"
    
    Global hHook As Long
    Global gWH As Long
    Global lpPrevWndProc As Long
    
    Sub StartHook Alias "StartHook"(ByVal Hwnd As Long)Export
    Dim hInst As Long
    Dim Thrd As Long
    Dim Address As Long
    
    hInst = GetWindowLong(Hwnd, %GWL_HINSTANCE)
    Thrd = GetCurrentThreadId()
    Address = CodePtr(HookProc)
    hHook = SetWindowsHookEx(%WH_CBT, Address, hInst, 0)
    
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    Sub StopHook Alias "StopHook"()Export
      UnhookWindowsHookEx hHook
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Function HookProc(ByVal lMsg As Long, ByVal wparam As Long, ByVal lparam As Long) As Long
    Dim hw As Long
    Dim Address As Long
    Dim hMenu As Long
    Dim X As Long
    Dim S As Asciiz * 20
    Dim rt As Long
    
    If lMsg < 0 Then
        HookProc = CallNextHookEx(hHook, lMsg, wparam, lparam)
        Exit Function
    End If '»If lMsg < 0 Then
    
    If lMsg = %HCBT_ACTIVATE Then
        'MsgBox "Activate Window"
        gWH = wparam
        hMenu = GetSystemMenu(gWH, %False)
        S = "&Our Function"
        X = AppendMenu(hMenu, %MF_STRING, %SC_NEWMENU, S)
        rt = GetLastError
        Address = CodePtr(WindowProc)
        lpPrevWndProc = SetWindowLong(gWH, %GWL_WNDPROC, Address)
    End If
    
    HookProc = 0
    
    End Function
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      'We need to trap the WM_SYSCOMMAND message to
      'determine when the user has clicked on our
      'new menu item. The message is stored in uMsg.
      'Our new menu item is identified as SC_NEWMENU
      Dim xx As Long
     If uMsg = %WM_SYSCOMMAND Then
        Select Case wParam And &HFFFF&
        Case %SC_NEWMENU
           xx = SetWindowPos(gWH, %HWND_TOPMOST, 0, 0, 0, 0, %FLAGS)
        End Select
    End If
    
    'Always call the original handler when done
    WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
    
    End Function
    Everything is fine inside my apps (i call StartHook within Vb6)
    But it doesn't seem to trap when other apps windows are activate
    nore it add's My User-Made Menu...
    Can Anybody Help?
    I've read in MSDN that Hook placed in Standard DLL
    Were System-Wide!

    -------------
    Daniel
    Last edited by Gary Beene; 12 Jul 2014, 07:35 PM. Reason: Code: tags

  • #2
    My understanding is that a separate instance of the hook DLL is created for each application/process it hooks into. Therefore you'll need to implement interprocess communication to pass data between the instances of your DLL. GLOBAL variables are only available within the same process-address-space, and not between instances of a DLL.

    BTW, this is the reason that hooks are supposed to reside in DLL's in the first place!


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Daniel, I think you also need to make that hook function exported.


      ------------------

      Comment

      Working...
      X