GetProcAddress() 失败,错误 127

Moo*_*oon 5 c++ dll loadlibrary getprocaddress

这是我的 DLL 代码:

#include <Windows.h>
#include <iostream>

int sysLol(char *arg);

int sysLol(char *arg)
{
   std::cout<<arg<<"\n";
   return 1;
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序代码:

#include <Windows.h>
#include <iostream>
#include <TlHelp32.h>
#include <stdlib.h>

typedef int (WINAPI* Lol)(char* argv);
struct PARAMETERS
{
    DWORD Lol;
};

int main()
{
    PARAMETERS testData;
    HMODULE e = LoadLibrary(L"LIB.dll"); //This executes without problem
    if (!e) std::cout<<"LOADLIBRARY: "<<GetLastError()<<"\n";
    else std::cout<<"LOADLIBRARY: "<<e<<"\n";
    testData.Lol = (DWORD)GetProcAddress(e,"sysLol"); //Error 127?
    if (!testData.Lol) std::cout<<testData.Lol<<" "<<GetLastError()<<"\n";
    else std::cout<<"MESSAGEBOX: "<<testData.Lol<<"\n";
    std::cin.ignore();
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

所以,我的 LIB.dll 使用 成功加载LoadLibrary(),但GetProcAddress()以 127 失败。这似乎是因为它没有找到我的函数名称,但我不明白为什么会失败。

非常感谢您的帮助!:)~P

egu*_*gur 5

由于该标记是 C++,因此您需要C为该函数声明一个名称:

extern "C" int sysLol(char *arg);
Run Code Online (Sandbox Code Playgroud)

您可以使用Dependency Walker看到编译器为您的 C++ 函数提供的实际名称。

成功后,将函数转换为 GetProcAddress 返回的指针,指向实际的函数类型:

typedef int (*sysLol_t)(char *arg);
sysLol_t pFunc = GetProcAddress(e,"sysLol");
Run Code Online (Sandbox Code Playgroud)