Windows 调用GetProcAddress时获取错误127

Windows 调用GetProcAddress时获取错误127,windows,winapi,hook,Windows,Winapi,Hook,我正在为WH_GETMESSAGE编写一个全局钩子。但是当从dll调用GetProcAddress函数时,我得到了错误代码127,即error_PROC_NOT_FOUND。找不到GetMsgProc。知道为什么吗 另外,我对这种编程还不熟悉,所以对于任何出乎意料的错误,我深表歉意 DLL文件: #include "windows.h" #include <stdio.h> BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason

我正在为WH_GETMESSAGE编写一个全局钩子。但是当从dll调用GetProcAddress函数时,我得到了错误代码127,即error_PROC_NOT_FOUND。找不到GetMsgProc。知道为什么吗

另外,我对这种编程还不熟悉,所以对于任何出乎意料的错误,我深表歉意

DLL文件:

#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);
}
#包括“windows.h”
#包括
BOOL APIENTRY DllMain(句柄模块、DWORD ul_原因、LPVOID LPREFERED)
{
返回TRUE;
}
__declspec(dllexport)LRESULT回调GetMsgProc(int-nCode、WPARAM-WPARAM、LPARAM-LPARAM)
{
MessageBox(空,文本(“我在”),文本(“在DLL中”),MB_OK);
返回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;
}
#包括
#包括
#包括
typedef LRESULT(回调*LPGetMsgProc)(int-nCode、WPARAM-WPARAM、LPARAM-LPARAM);
int main()
{
HMODULE hDll=LoadLibrary(“../../dllTouchInputHook/x64/Debug/dllTouchInputHook.dll”);
LPGetMsgProc proc=(LPGetMsgProc)GetProcAddress(hDll,“GetMsgProc”);
if(proc==NULL){
printf(“错误代码为%d”,GetLastError());
}
HHOOK hMsgHook=setWindowshookx(WH_GETMESSAGE,proc,hDll,0);
味精;
while(GetMessage(&msg,NULL,0,0)>0){
翻译信息(&msg);
发送消息(&msg);
}
unhookwindowshookx(hMsgHook);
返回0;
}

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

首先,您不应该以这种方式实现钩子。您应该将调用移动到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;
    }
}
#包括“windows.h”
#包括
HINSTANCE hThisDLL;
HHOOK-hMsgHook;
BOOL APIENTY DllMain(HINSTANCE hinstDLL、DWORD FDFREASON、LPVOID lpvReserved)
{
hThisDLL=hinstDLL;
返回TRUE;
}
LRESULT回调GetMsgProc(int-nCode、WPARAM-WPARAM、LPARAM-LPARAM)
{
MessageBox(空,文本(“我在”),文本(“在DLL中”),MB_OK);
返回CallNextHookEx(NULL、nCode、wParam、lParam);
}
__declspec(dllexport)BOOL WINAPI InstallHook()
{
如果(!hMsgHook)
hMsgHook=SetWindowsHookEx(WH_GETMESSAGE,&GetMsgProc,hThisDll,0);
返回(hMsgHook!=NULL);
}
__declspec(dllexport)VOID WINAPI卸载钩子()
{
if(hMsgHook)
{
unhookwindowshookx(hMsgHook);
hMsgHook=NULL;
}
}

#包括
#包括
#包括
typedef BOOL(WINAPI*LPInstallHook)();
typedef VOID(WINAPI*LPUninstallHook)();
int main()
{
HMODULE hDll=LoadLibrary(“../../dllTouchInputHook/x64/Debug/dllTouchInputHook.dll”);
如果(!hDll)
{
printf(“错误代码为%d”,GetLastError());
返回-1;
}
LPInstallHook installProc=(LPInstallHook)GetProcAddress(hDll,“InstallHook”);//或“\u InstallHook”
LPUninstallHook uninstallProc=(installProc)?(LPUninstallHook)GetProcAddress(hDll,“UninstallHook”):NULL;//或“\u UninstallHook”
如果(!(installProc&&uninstallProc))
{
printf(“错误代码为%d”,GetLastError());
免费图书馆(hDll);
返回-1;
}
如果(!installProc())
{
printf(“错误代码为%d”,GetLastError());
免费图书馆(hDll);
返回-1;
}
味精;
while(GetMessage(&msg,NULL,0,0)>0){
翻译信息(&msg);
发送消息(&msg);
} 
卸载proc();
免费图书馆(hDll);
返回0;
}

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

首先,您不应该以这种方式实现钩子。您应该将调用移动到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;
    }
}
#包括“windows.h”
#包括
HINSTANCE hThisDLL;
HHOOK-hMsgHook;
BOOL APIENTY DllMain(HINSTANCE hinstDLL、DWORD FDFREASON、LPVOID lpvReserved)
{
hThisDLL=hinstDLL;
返回TRUE;
}
LRESULT回调GetMsgProc(int-nCode、WPARAM-WPARAM、LPARAM-LPARAM)
{
MessageBox(空,文本(“我在”),文本(“在DLL中”),MB_OK);
返回CallNextHookEx(NULL、nCode、wParam、lParam);
}
__declspec(dllexport)BOOL WINAPI InstallHook()
{
如果(!hMsgHook)
hMsgHook=SetWindowsHookEx(WH_GETMESSAGE,&GetMsgProc,hThisDll,0);
返回(hMsgHook!=NULL);
}
__declspec(dllexport)VOID WINAPI卸载钩子()
{
if(hMsgHook)
{
unhookwindowshookx(hMsgHook);
hMsgHook=NULL;
}
}

#包括
#包括
#包括
typedef BOOL(WINAPI*LPInstallHook)();
typedef VOID(WINAPI*LPUninstallHook)();
int main()
{
HMODULE hDll=LoadLibrary(“../../dllTouchInputHook/x64/Debug/dllTouchInputHook.dll”);
如果(!hDll)
{
printf(“错误代码为%d”,GetLastError());
返回-1;
}
LPInstallHook installProc=(LPInstallHook)GetProcAddress(hDll,“InstallHook”);//或“\u InstallHook”
LPUninstallHook uninstallProc=(installProc)?(LPUninstallHook)GetProcAddress(hDll,“UninstallHook”):NULL;//或“\u UninstallHook”
如果(!(installProc&&uninstallProc))
{
printf(“错误代码为%d”,GetLastError());
免费图书馆(hDll);
返回-1;
}
如果(!installProc())
{
printf(“错误代码为%d”,GetLastError());
免费图书馆(hDll);
返回-1;
}
味精;