Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未调用dll钩子函数_C++_Winapi_Dll_Hook - Fatal编程技术网

C++ 未调用dll钩子函数

C++ 未调用dll钩子函数,c++,winapi,dll,hook,C++,Winapi,Dll,Hook,我试图捕捉并重新传输键盘和鼠标事件。所以我在dll中使用SetWindowsHookEx和函数。第一次出现一个大错误-GetProcAddress无法获取我的函数。这个答案对我帮助很大 现在我可以从dll中调用函数,我可以看到钩子的效果:当程序运行时,我的键盘不能工作。但胡克没有打电话 main.cpp #include <Windows.h> #include <WinUser.h> #include <iostream> #include <fstr

我试图捕捉并重新传输键盘和鼠标事件。所以我在dll中使用SetWindowsHookEx和函数。第一次出现一个大错误-GetProcAddress无法获取我的函数。这个答案对我帮助很大

现在我可以从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;
}
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
HHOOK-hhk;
int main(){
HINSTANCE hinstLib=LoadLibrary(文本(“hookdll.dll”);
typedef void(*安装)();
typedef void(*卸载)();
typedef LRESULT(_stdcall*HookProcedure)(int、WPARAM、LPARAM);
安装=(安装)GetProcAddress(hinstLib,“安装”);
卸载=(卸载)GetProcAddress(hinstLib,“卸载”);
HookProcedure hookProc=(HookProcedure)GetProcAddress(hinstLib)_hookProcedure@12“”;//天哪

cout键盘挂钩允许您截取
WM_KEYDOWN
WM_keydup
窗口消息。除非存在活动的windows消息队列,否则控制台应用程序不会接收这些消息。如果您希望在控制台应用程序中查看和处理这些消息,请创建一个窗口,并将键盘焦点交给它。请确保确实接收消息
在拥有窗口的线程中使用
GetMessage
DispatchMessage
WinAPI调用泵送


如果您不想创建窗口,可以使用
WH\u KEYBOARD\u LL
截取发送到线程活动消息队列的键盘相关消息。您仍然需要使用
GetMessage
从队列中读取消息,否则它最终将开始删除消息。

谢谢您的回答。让我检查一下我是否理解其用途我认为我的过程是全局的,可以捕捉每一个事件。如果Im不对,我可能需要winapi中的另一个函数?我还尝试使用
SetWindowsHookEx(WH\u KEYBOARD\u LL,…)
-结果与此类似。谢谢!其他阅读此问题的人:我通过关键字发现的下一个问题似乎很奇怪,我不相信调用dll和链接过程以及链接到挂钩的过程也应该进行无限循环。请记住,如果您安装了全局挂钩,您的dll将被注入任何钩子可以应用于。