SetWindowsHookEx not injecting DLL

SetWindowsHookEx not injecting DLL

本文关键字:DLL injecting not SetWindowsHookEx      更新时间:2023-10-16

我所要做的就是把我的DLL注入到其他一些程序中(在进程创建时),并让它执行DllMain函数。不幸的是,无论我尝试什么代码,它都不能工作。

例如,我有这个代码:http://pastebin.com/P4NzLb3X基本上它只是使用SetWindowsHookEx来安装一个键盘钩子。然而,与进程黑客检查显示,DLL实际上从未被注入到任何进程中。(

我已经找了一整天的解决方案。我怎么解决这个问题?

我在这两个链接的帮助下解决了这个问题:

http://www.gamedev.net/topic/568401-problem-with-loading-dll--setwindowshookex/

64位Windows的全局Keyhook

有三件事必须解决:

  • 为DLL添加一个.def文件,并使用那里的导出,因为Visual Studio显然喜欢用extern "C" __declspec(dllexport) int制作奇怪的名称混淆-这修复了32位进程上的DLL加载
  • 添加CALLBACK属性到函数(extern "C" int CALLBACK meconnect(...)) -这修复了在上面修复后发生在32位进程上的崩溃。
  • 在主机进程(调用SetWindowsHookEx函数的进程)中添加一个消息循环,如下所示:

    MSG msg;
    while(1) {
        // Keep pumping...
        PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        Sleep(10);
        SHORT v = GetKeyState(VK_RETURN);
        bool pressed = (v & 0x8000) != 0;
        if(pressed) 
            break;
    }