我试图捕捉并重新翻译键盘和鼠标事件。所以我在 dll 中使用 SetWindowsHookEx 和函数。第一次 Ш 有一个大错误 - GetProcAddress 无法获取我的函数。这个答案对我有很大帮助GetProcAddress 返回 NULL
现在我可以从 dll 调用函数,我可以从钩子中看到效果:我的键盘在程序运行时不起作用。但是没有调用钩子。
主文件
#include <Windows.h>
#include <WinUser.h>
#include <iostream>
#include <fstream>
using namespace std;
HHOOK hhk;
int main(){
HINSTANCE hinstLib = LoadLibrary(TEXT("hookdll.dll"));
typedef void (*Install)();
typedef void (*Uninstall)();
typedef LRESULT (_stdcall *HookProcedure)(int, WPARAM , LPARAM );
Install install = (Install) GetProcAddress(hinstLib, "install");
Uninstall uninstall = (Uninstall) GetProcAddress(hinstLib, "uninstall");
HookProcedure hookProc = (HookProcedure) GetProcAddress(hinstLib, "_hookProcedure@12");//omg
cout << GetLastError()<< "\n";
install();
hhk = SetWindowsHookEx(WH_KEYBOARD, hookProc, hinstLib, NULL);
cout << GetLastError()<< "\n";
for(int i = 0; i < 50; i++){
Sleep(200);
cout << i << "\n";
}
UnhookWindowsHookEx(hhk);
uninstall();
return 0;
}
钩子dll.cpp
#include <Windows.h>
#include <iostream>
#include <fstream>
HINSTANCE hinst;
HHOOK hhk;
std::ofstream myfile;
extern "C" __declspec(dllexport)
LRESULT CALLBACK hookProcedure(int code, WPARAM w, LPARAM l)
{
MessageBox(NULL, (LPCWSTR)L"HEY HEY", (LPCWSTR)L"Test", MB_OK);
//never saw this message
return CallNextHookEx(NULL, code, w, l);
}
extern "C" __declspec(dllexport) void install() {
myfile.open("log.txt",std::ios_base::app);
myfile << "\ninstall " << GetLastError();
}
extern "C" __declspec(dllexport) void uninstall() {
myfile << "\nuninstall " << GetLastError();
myfile.close();
}
extern "C" __declspec(dllexport)
BOOL WINAPI DllMain( __in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved) {
hinst = hinstDLL;
return TRUE;
}