Hooking PeekMessage crash
Posted: Tue Jun 22, 2004 3:25 am
I'm using madCodeHook to write a system-wide keyhook by hooking PeekMessage (GetMessage will be implemented later). My DLL code looks like this:
As soon as I inject it, it bring down every process, and the system reboots (XP). If I comment out the two HookAPI lines, everything is fine... so it seems the problem is in the DLL.
What am I doing wrong?
PS. I'm not using SetWindowsHookEx because I want to be able to inject/uninject and delete the DLL at any time. I suppose I could write a keyhook with SetWindowsHookEx, then use madRemote to execute a function in each remote process to force them to unload the DLL. Dunno if this would work though.
Code: Select all
library KeyHandler;
uses Windows, Messages, madCodeHook;
type
TParam = record
WParam: WPARAM;
LParam: LPARAM;
end;
var PeekMessageCallNext: function (lpMsg: TMsg; hWnd: HWND; wMsgFilterMin, wMsgFilterMax, wRemoveMsg: UINT): BOOL; stdcall;
function PeekMessageCallBack(lpMsg: TMsg; hWnd: HWND; wMsgFilterMin, wMsgFilterMax, wRemoveMsg: UINT): BOOL; stdcall;
var
Param: TParam;
begin
case lpMsg.message of
WM_KEYFIRST..WM_KEYLAST: begin
Param.WParam := lpMsg.wParam;
Param.LParam := lpMsg.lParam;
SendIpcMessage('KYHNDLR', @Param, SizeOf(Param));
end;
end;
Result := PeekMessageCallNext(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
end;
begin
HookAPI(user32, 'PeekMessageA', @PeekMessageCallBack, @PeekMessageCallNext);
HookAPI(user32, 'PeekMessageW', @PeekMessageCallBack, @PeekMessageCallNext);
end.
What am I doing wrong?
PS. I'm not using SetWindowsHookEx because I want to be able to inject/uninject and delete the DLL at any time. I suppose I could write a keyhook with SetWindowsHookEx, then use madRemote to execute a function in each remote process to force them to unload the DLL. Dunno if this would work though.