Results 1 to 10 of 10

Thread: hooking windows - please help! *resolved*

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2000
    Posts
    51

    hooking windows - please help! *resolved*

    I've tried to create a hooking-dll in VC++ for VB use.
    The dll should hook the MouseProc of every window of a certain thread. For testing purpose the dll is writing the hwnd of the Mousevent to the titlebar of my form every time the MouseProc fires. As long as I'm trying to hook VB-windows only, everything is fine. As soon as I'm trying to hook other windows (TaskBar e.g.), nothing works.

    Here is my VC++code:


    code:--------------------------------------------------------------------------------
    --- in the Hook.cpp ------
    #include <iostream>
    #include <windows.h>

    HHOOK hHook;
    HWND hWndSend;
    HINSTANCE hDll;

    void __stdcall StartHook(HWND hWndHook, HWND _hWndSend) {
    hWndSend = _hWndSend;
    hDll = LoadLibrary("Hook.dll");
    DWORD ThreadID = GetWindowThreadProcessId(hWndHook,NULL);
    HOOKPROC hProc = (HOOKPROC) GetProcAddress(hDll, "MouseProc");
    hHook = SetWindowsHookEx(WH_MOUSE, hProc, hDll, ThreadID);
    char str[100];
    sprintf(str, "%d", hHook);
    SetWindowText(hWndSend, str);
    }

    void __stdcall StopHook(void) {
    UnhookWindowsHookEx(hHook);
    FreeLibrary(hDll);
    }

    LRESULT __stdcall CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION)
    {
    MOUSEHOOKSTRUCT mhs;
    memcpy(&mhs, (void*)lParam, sizeof(MOUSEHOOKSTRUCT));
    char str[100];
    sprintf( str, "%d", mhs.hwnd);
    SetWindowText(hWndSend,str);
    }
    return CallNextHookEx(hHook, nCode, wParam, lParam);
    }


    --- in the Hook.def ------
    LIBRARY "hook.dll"
    EXPORTS
    StartHook
    StopHook
    MouseProc
    --------------------------------------------------------------------------------


    The vb-Project looks like this:

    code:--------------------------------------------------------------------------------
    Private Declare Sub StartHook Lib "Hook.dll" (ByVal hWndHook As Long, ByVal hWndSend As Long)
    Private Declare Sub StopHook Lib "Hook.dll" ()
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Sub Command1_Click()
    Dim hWndHook As Long
    hWndHook = FindWindow("Shell_TrayWnd", vbNullString)
    StartHook hWndHook, Me.hWnd
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    StopHook
    End Sub
    --------------------------------------------------------------------------------


    This doesn't work at all. When I'm putting hWndHook to Me.hwnd, it works perfectly.

    Any ideas?

    Thanks, Quixx.
    Last edited by Quix; May 29th, 2002 at 12:47 PM.

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