GetProcAddress返回NULL

gc5*_*gc5 2 c dll null hook getprocaddress

我必须使用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);
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

来自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;
...
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 6

是的,MessageBox()调用成功,没有错误.在它之前移动GetLastError()调用.

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

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

  • 您可以添加链接器的定义(.DEF)文件,将名称更改为您喜欢的名称. (2认同)
  • 这种修改解决了32位代码中的问题.其中有许多不同的调用约定,__ cdecl,__ stdcall和__fastcall是更常见的约定.获得不匹配会导致*非常难以诊断运行时错误.通过修饰这样的功能,链接器可以检测到这种不匹配.您使用__stdcall,@ xx后缀表示激活堆栈帧的大小.三个参数,每个4个字节.不要修复它,而是使用我的答案的最后一段中的技术. (2认同)