windows 如何使用 SetWindowsHookEx 和 WH_KEYBOARD 挂钩外部进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/820804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hook external process with SetWindowsHookEx and WH_KEYBOARD
提问by s5804
I am trying to hook for example Notepad without sucess. Making a global hook seems to work fine.
我试图挂钩例如记事本但没有成功。制作一个全局钩子似乎工作正常。
Testing on XP SP2.
在 XP SP2 上测试。
Edit: Amended code works now.
编辑:修改后的代码现在有效。
MyDLL code
我的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;
}
My program
我的程序
#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;
}
回答by RichieHindle
Three problems:
三个问题:
You're using the process ID when you should be using the thread ID.
当您应该使用线程 ID 时,您正在使用进程 ID。
Your HHOOK needs to go into shared memory:
你的 HHOOK 需要进入共享内存:
#pragma data_seg(".shared")
HHOOK hhk = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
You need to pass your HHOOK
to CallNextHookEx
:
你需要将你的传递HHOOK
给CallNextHookEx
:
return CallNextHookEx( hhk, code, wParam, lParam);