キーボードとマウスのイベントをキャッチして再変換しようとしています。そのため、SetWindowsHookEx と関数を dll で使用します。初めて大きなエラーが 1 つあります - GetProcAddress が関数を取得できません。その答えは私に大いに役立ちますGetProcAddress は NULL を返します
これで、dll から関数を呼び出すことができ、フックの効果を確認できます。プログラムの実行中にキーボードが機能しません。しかし、フックは呼び出されません。
main.cpp
#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;
}
hookdll.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;
}