我必须使用DLL中的一个简单函数;我能够加载库,但GetProcAddress返回NULL。我想我明白了名字错误,但也许我做错了。谢谢(代码如下,asap我将添加所需的其他信息):
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;
...
答案 0 :(得分:6)
是的,MessageBox()调用成功而没有错误。在它之前移动GetLastError()调用。
错误是可预测的,它找不到“mouseProc”。该名称将在DLL中被破坏,最有可能是“_mouseProc @ 12”。在DLL上使用dumpbin.exe / exports以确保。
Fwiw:你可以通过不动态加载DLL而只是链接其导入库来减轻这段代码的痛苦。将DLL注入其他进程的事实并不意味着您必须将其注入其中。您只需要模块句柄,就可以调用SetWindowsHookEx()。从DllMain()入口点或使用GetModuleHandle()获取它。