我试图钩取例如记事本的应用程序,但没有成功。全局钩子似乎可以正常工作。
在XP SP2上测试。
编辑:修改后的代码现在可以工作了。
我的DLL代码
在XP SP2上测试。
编辑:修改后的代码现在可以工作了。
我的DLL代码
#include <windows.h>
#include <iostream>
#include <stdio.h>
HINSTANCE hinst;
#pragma data_seg(".shared")
HHOOK hhk;
#pragma data_seg()
//#pragma comment(linker, "/SECTION:.shared,RWS") compiler error in VC++ 2008 express
LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam,LPARAM lParam) {
if (code < 0) {
return CallNextHookEx(0, code, wParam, lParam);
}
Beep(1000, 20);
return CallNextHookEx(hhk, code, wParam, lParam);
}
extern "C" __declspec(dllexport) void install(unsigned long threadID) {
hhk = SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc, hinst, threadID);
}
extern "C" __declspec(dllexport) void uninstall() {
UnhookWindowsHookEx(hhk);
}
BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved) {
hinst = hinstDLL;
return TRUE;
}
我的程序
#include <Windows.h>
unsigned long GetTargetThreadIdFromWindow(char *className, char *windowName)
{
HWND targetWnd;
HANDLE hProcess;
unsigned long processID = 0;
targetWnd = FindWindow(className, windowName);
return GetWindowThreadProcessId(targetWnd, &processID);
}
int _tmain(int argc, _TCHAR* argv[]) {
unsigned long threadID = GetTargetProcessIdFromWindow("Notepad", "Untitled - Notepad");
printf("TID: %i", threadID);
HINSTANCE hinst = LoadLibrary(_T("MyDLL.dll"));
if (hinst) {
typedef void (*Install)(unsigned long);
typedef void (*Uninstall)();
Install install = (Install) GetProcAddress(hinst, "install");
Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
install(threadID);
Sleep(20000);
uninstall();
}
return 0;
}
hhk
初始化为0
,否则它将无法正确共享。 - yoyoyoyosefCallNextHookEx
函数中的HHOOK
参数;有关更多信息,请参见https://dev59.com/o3VC5IYBdhLWcg3wsTRi。 - Frerich Raabe