Results 1 to 9 of 9

Thread: One question about hooks.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    4

    Question One question about hooks.

    I have been beating my head against my monitor for days now trying to understand how to use windows hooks. After all my researching I’ve come to just one question that no web page or chat thread can seem to answer, and that is… Cross process HookProcs must be in a dll, does this mean that they can reside in an ActiveX Dll? …or must they be in a C++ compiled dll?

  2. #2
    Member
    Join Date
    Jul 1999
    Posts
    42
    Some code to experiment with. It will add user defined menu entries to the default system menu (left of the form caption). The messages are captured with a custom hoopup. The methode use the VB AddressOf statment. You can only use AddressOff in a BAS module in the same project (See VB Help on AddressOff). You can try to create a ActiveX.DLL with a class that exports a long that represents the address of a function in a BAS module from the same ActiveX.DLL

    ' Module code
    Option Explicit

    ' User defined menu identifiers
    Public Const IDM_MENU_1 As Long = 1010
    Public Const IDM_MENU_2 As Long = 1011
    Public Const IDM_MENU_3 As Long = 1012

    ' WIN 32 constants from USER32.DLL
    Public Const WM_SYSCOMMAND = &H112
    Public Const GWL_WNDPROC = (-4)
    Public Const MF_SEPARATOR = &H800&
    Public Const MF_STRING = &H0&

    ' API function declarations frim USER23.DLL
    Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Any) As Long

    ' Storage locator for the deault window proc. that will be replaced
    ' by Sub WindowProc(...) also in this BAS module
    Public DefaultWindowProc As Long

    ' Begin the process
    ' 1) Load form
    ' 2) Form_Load event will add new menu entries to the default
    ' system menu and replace the default window proc with Sub WindowPoc(...)
    ' 3) Try the syste menu
    ' 4) Form_Unload will restore the default window proc

    Sub Main()
    Form1.Show 1
    End Sub

    ' New custom window prodc that will handle the user defined system menu entries IDM_MENU_1/2/3
    Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, lParam As Long) As Long
    If iMsg = WM_SYSCOMMAND Then
    Select Case wParam
    Case IDM_MENU_1: MsgBox "Menu 1": Exit Function
    Case IDM_MENU_2: MsgBox "Menu 2": Exit Function
    Case IDM_MENU_3: MsgBox "Menu 3": Exit Function
    End Select
    End If
    '
    WindowProc = CallWindowProc(DefaultWindowProc, hwnd, iMsg, wParam, lParam)
    End Function



    ' Form code for subclassing your window
    Option Explicit

    ' Append new menu item to the default system menu
    ' The control boc property of the form must be True
    ' The new window proc. loaded in the Form_Load event
    ' will handle the new menu messages IDM_MENU_1/2/3
    Public Sub AddAboutmenu(ByVal hwnd As Long)
    Dim hSysMenu As Long

    hSysMenu = GetSystemMenu(hwnd, 0&)
    Call AppendMenu(hSysMenu, MF_SEPARATOR, 0&, 0&)
    Call AppendMenu(hSysMenu, MF_STRING, IDM_MENU_1, "Menu 1")
    Call AppendMenu(hSysMenu, MF_STRING, IDM_MENU_2, "Menu 2")
    Call AppendMenu(hSysMenu, MF_STRING, IDM_MENU_3, "Menu 3")
    End Sub

    Private Sub Form_Load()
    ' Replace the deffault window proc. with a custom VB window proc.
    ' This custom window proc. must be in a BAS module
    DefaultWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    AddAboutmenu hwnd
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    Call SetWindowLong(hwnd, GWL_WNDPROC, DefaultWindowProc)
    End Sub




  3. #3
    Member
    Join Date
    Jul 1999
    Posts
    42

    Thumbs up

    Yes, you can use a WindowProc function from a ActiveX Dll to customize the default WindowProc of a from in a regular EXE.


  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    4

    Re: One question about hooks.

    I think you misunderstood my question… I want to use hooks on different processes. The code you gave me wouldn’t work on a window that’s in a different process.

  5. #5
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    They must be in a C++ DLL.
    See Applemans Book, he sells a C++ DLL to hook out of process.

    YOu also might check some of the other web sites for a freebe.
    None come to mind at this writing -- planetsourcecode maybe?

  6. #6
    Megatron
    Guest
    What type of hook are you trying to install?

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    4
    I’m just curious… I want to be able to hook everything and see how it works and what its doing. I want to be able to install all of the hooks on a system or on a particular thread. I just want to be able to tinker around with it.

    ...so the only way to do this is to learn C++ huh? Does anyone know any good sites for C++ ...maybe a particularly good example on how to write C++ Dlls? ...or good books like Dan Appleman's API book?

  8. #8
    Megatron
    Guest
    Not exactly...The only thing you need to do in C++ is write your procedure.
    Code:
    LRESULT CALLBACK KeyboardProc(int,WPARAM,LPARAM);
    HHOOK hHook;
    
    LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if( wParam == B )
        {
             ofstream file("MyLog.txt");
             file << "B was pressed";
             file.close();
         }
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }
    Then you can either do the following in VB, or C. (This loads the DLL).
    Code:
    HMODULE hMod = LoadLibrary("C:\\MyLib.dll");
    if(!hMod) return -1
    HOOKPROC lpfnHkprc = GetProcAddress(hMod, "_KeyboardProc@12");
    HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD, lpfnHkprc, hMod, 0);
    
    /* If you're not using C, then you need to put the following code in the Unload() event, otherwise, just insert the result of your WinMain() code here */
    
    UnHookWindowsHookEx(hHook);
    FreeLibrary(hMod);

  9. #9
    Megatron
    Guest
    See this link for another example of integrating C++ DLLs in VB.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width