gpt4 book ai didi

c - GetProcAddress 返回 NULL

转载 作者:行者123 更新时间:2023-12-01 09:59:08 27 4
gpt4 key购买 nike

我必须使用 DLL 中的一个简单函数;我能够加载库,但 GetProcAddress 返回 NULL。我想我了解名称修改,但也许我做错了什么。谢谢(代码如下,我会尽快添加所需的其他信息):

mydll.h

#ifdef MYDLL_EXPORTS
#define MYDLL_API extern "C" __declspec(dllexport)
#else
#define MYDLL_API extern "C" __declspec(dllimport)
#endif

MYDLL_API void testFunction(void);
MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam);

mydll.cpp

#include "stdafx.h"
#include "mydll.h"

// This is an example of an exported function.
MYDLL_API void testFunction(void)
{
MessageBox(NULL, (LPCWSTR)L"Test", (LPCWSTR)L"Test", MB_OK);
}

MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
if(wParam != NULL && wParam == MK_RBUTTON)
{
MessageBox(NULL, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
}
}

// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

来自 main.cpp 的代码

...
case WM_CREATE:
{
// creates state for window
stateClassPointer = new stateClass();
// saves states pointer in a space reserved for user data
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) stateClassPointer);

// now it will load DLL and set up hook procedure for mouse events

// declares local variables
HOOKPROC hkprcMouseProc;
HINSTANCE hinstDLL;
HHOOK hhookMouseProc;
//FARPROC WINAPI test;
// loads DLL
if((hinstDLL = LoadLibrary(TEXT("C:\\Users\\Francesco\\Dropbox\\poli\\bi\\not\\pds\\sp\\wk5\\lsp5\\Debug\\mydll.dll"))) == NULL)
{
MessageBox(hWnd, (LPCWSTR)L"Error loading DLL", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR);
break;
}
// saves DLL handle in the state class
stateClassPointer->setHInstance(hinstDLL);
// sets up hook procedure for mouse events
if((hkprcMouseProc = (HOOKPROC)GetProcAddress(hinstDLL, "mouseProc")) == NULL)
{
MessageBox(hWnd, (LPCWSTR)L"Error setting windows hook: GetProcAddress", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR);
break;
}
if((hhookMouseProc = SetWindowsHookEx(WH_MOUSE, hkprcMouseProc, hinstDLL, 0)) == NULL)
{
MessageBox(hWnd, (LPCWSTR)L"Error setting windows hook: SetWindowsHookEx", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR);
break;
}
// saves hook handle in the state class
stateClassPointer->setHHook(hhookMouseProc);
/*test = GetProcAddress(hinstDLL, "testFunction");
test();*/
}
break;
...

最佳答案

是的,MessageBox() 调用成功且没有错误。将 GetLastError() 调用移到它之前。

错误是可以预测的,它找不到“mouseProc”。该名称将在 DLL 中被破坏,很可能是“_mouseProc@12”。确保在您的 DLL 上使用 dumpbin.exe/exports。

Fwiw:您可以通过不动态加载 DLL 而只是链接其导入库来减轻这段代码的痛苦。 DLL 将被注入(inject)到其他进程的事实并不意味着您必须将它注入(inject)到您的进程中。您只需要模块句柄,这样您就可以调用 SetWindowsHookEx()。从 DllMain() 入口点或使用 GetModuleHandle() 获取它。

关于c - GetProcAddress 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7044450/

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