|
-
May 23rd, 2002, 07:14 PM
#1
Thread Starter
Member
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.
-
May 24th, 2002, 03:13 AM
#2
Thread Starter
Member
-
May 27th, 2002, 01:16 AM
#3
Thread Starter
Member
-
Jul 9th, 2002, 09:07 PM
#4
PowerPoster
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|