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:
The vb-Project looks like this: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
This doesn't work at all. When I'm putting hWndHook to Me.hwnd, it works perfectly.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
Any ideas?
Thanks, Quixx.




Reply With Quote