-
hooking windows - help!
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.
-
Hm...
that doesn't help.
FindWindow returns the handle I want, that's not the problem.
Even if I put a handle manually (copied from spyxx) it's not working. :confused:
It seems to be a problem if hWnd is in a thread created by a
different process than the calling program. On msdn you can
read, this would only work if the MouseProc is in a dll. For that
reason I've created that dll in C++. :mad:
I'm really new to C++, that's my first program.
I've passed hours searching the forums to figure out how to
create that hooking-dll. :( :(
Could anyone plz help?
thanks, Quix.
-
-
Yesterday, I tried doing the same thing. I created a dll using ATL which created a Keyboard hook and has its own application-defined procedure to handle all the messages. Whenever a key is pressed (system-wide), it shows a message box. So I tried using that dll in VB and it only works for the VB IDE window or my own project windows. It won't work anywhere else (eg Taskbar).
So now I am going to try doing the same thing in C++ and I'll see if that solves the problem or.
I also heard that you can subclass any window outside of your program's address space in VB. Maybe that is the problem.