windows - 调用 GetProcAddress 时出现错误 127

标签 windows winapi hook

我正在为 WH_GETMESSAGE 编写一个全局钩子(Hook)。但是当从 dll 调用 GetProcAddress 函数时,我收到错误代码 127,即 ERROR_PROC_NOT_FOUND。它无法找到 GetMsgProc。知道为什么吗?

另外,我是这种编程的新手,对于任何意外的错误,我深表歉意。

动态链接库文件:

#include "windows.h"
#include <stdio.h>

BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    return TRUE;
}

__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MessageBox(NULL, TEXT("I am in"),TEXT("In a DLL"), MB_OK);

    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

程序加载DLL文件:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

typedef LRESULT(CALLBACK *LPGetMsgProc)(int nCode, WPARAM wParam, LPARAM lParam);

int main()
{
    HMODULE hDll = LoadLibrary(_T("../../dllTouchInputHook/x64/Debug/dllTouchInputHook.dll"));
    LPGetMsgProc proc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc");
    if (proc == NULL) {
        printf("The error code is %d", GetLastError());
    }

    HHOOK hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, proc, hDll, 0);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnhookWindowsHookEx(hMsgHook);

    return 0;
}

最佳答案

未找到该函数,因为它没有像您期望的那样导出为 "GetMsgProc"。它实际上更像是 "_GetMsgProc@12"(32 位)或 "_GetMsgProc@20"(64 位)。如果您希望它导出为 "GetMsgProc",那么您需要在编译 DLL 时使用 .DEF 文件。

您一开始就不应该以这种方式实现 Hook 。您应该将调用移动到 DLL 本身内部的 SetWindowsHookEx(),然后导出一个函数来调用它,例如:

#include "windows.h"
#include <stdio.h>

HINSTANCE hThisDLL;
HHOOK hMsgHook;

BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    hThisDLL = hinstDLL;
    return TRUE;
}

LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    MessageBox(NULL, TEXT("I am in"), TEXT("In a DLL"), MB_OK);
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

__declspec(dllexport) BOOL WINAPI InstallHook()
{
    if (!hMsgHook)
        hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, &GetMsgProc, hThisDll, 0);
    return (hMsgHook != NULL);
}

__declspec(dllexport) VOID WINAPI UninstallHook()
{
    if (hMsgHook)
    {
        UnhookWindowsHookEx(hMsgHook);
        hMsgHook = NULL;
    }
}

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

typedef BOOL (WINAPI *LPInstallHook)();
typedef VOID (WINAPI *LPUninstallHook)();

int main()
{
    HMODULE hDll = LoadLibrary(_T("../../dllTouchInputHook/x64/Debug/dllTouchInputHook.dll"));
    if (!hDll)
    {
        printf("The error code is %d", GetLastError());
        return -1;
    }

    LPInstallHook installProc = (LPInstallHook) GetProcAddress(hDll, "InstallHook"); // or "_InstallHook"
    LPUninstallHook uninstallProc = (installProc) ? (LPUninstallHook) GetProcAddress(hDll, "UninstallHook") : NULL; // or "_UninstallHook"

    if (!(installProc && uninstallProc))
    {
        printf("The error code is %d", GetLastError());
        FreeLibrary(hDll);
        return -1;
    }

    if (!installProc())
    {
        printf("The error code is %d", GetLastError());
        FreeLibrary(hDll);
        return -1;
    }

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } 

    uninstallProc();

    FreeLibrary(hDll);

    return 0;
}

关于windows - 调用 GetProcAddress 时出现错误 127,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777304/

相关文章:

c++ - 如何在获取事件后延迟 1s 复制文件?

c++ - SetWindowsHookEx、KeyboardProc 和非静态成员

python - 通过cmd启动识别正在运行的python程序

c - 在 Aero 窗口中禁用垂直同步

c++ - SetWindowshookEx 有时在 dll 注入(inject)后不起作用

winapi - 获取硬盘所有逻辑驱动器号并收集根目录的有效方法

c++ - 当窗口设置为事件时获取通知

linux-kernel - 如何在 PPC Linux 上运行时 Hook 系统调用表?

python - 无法使用 pip 安装 Scipy

windows - Windows中缺少Hadoop安装