gpt4 book ai didi

windows - 调用 GetProcAddress 时出现错误 127

转载 作者:可可西里 更新时间:2023-11-01 11:15:51 28 4
gpt4 key购买 nike

我正在为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com