为什么我无法访问我的 DLL 函数

发布于 2024-09-06 00:38:46 字数 807 浏览 15 评论 0 原文

我正在尝试使用 LoadLibrary() 动态加载 DLL,它可以工作,但是我无法获取我尝试调用的 DLL 中的函数的地址。

DLL函数:(在CPP文件中)

_declspec(dllexport) void MyDllFunc()
{
    printf("Hello from DLL");
}

调用代码:

typedef void (*MyDllFuncPtr)();

int _tmain(int argc, _TCHAR* argv[])
{

    HINSTANCE LoadMe;
    LPCWSTR str = L"C:\\Users\\Tony\\Documents\\Visual Studio 2008\\Projects\\DLL Loading\\Release\\MyDll.dll";

    LoadMe = LoadLibrary(str);

    if(LoadMe != 0)
        printf("Successfully Loaded!\r\n");
    else
        printf("Loading Failed \r\n" );


    MyDllFuncPtr func;

    func = (MyDllFuncPtr)GetProcAddress(LoadMe, "MyDllFunc");

    if (func != NULL)
        func();

    FreeLibrary(LoadMe);

}

func返回NULL!!!

我做错了什么?

这是一个 Win32 控制台项目。

I'm trying to load a DLL dynamically using LoadLibrary(), which works, however I cannot then get the address of the function in the DLL that I'm trying to call.

DLL function: (in CPP file)

_declspec(dllexport) void MyDllFunc()
{
    printf("Hello from DLL");
}

Calling code:

typedef void (*MyDllFuncPtr)();

int _tmain(int argc, _TCHAR* argv[])
{

    HINSTANCE LoadMe;
    LPCWSTR str = L"C:\\Users\\Tony\\Documents\\Visual Studio 2008\\Projects\\DLL Loading\\Release\\MyDll.dll";

    LoadMe = LoadLibrary(str);

    if(LoadMe != 0)
        printf("Successfully Loaded!\r\n");
    else
        printf("Loading Failed \r\n" );


    MyDllFuncPtr func;

    func = (MyDllFuncPtr)GetProcAddress(LoadMe, "MyDllFunc");

    if (func != NULL)
        func();

    FreeLibrary(LoadMe);

}

func returns NULL!!!

What am I doing wrong?

This is a Win32 Console project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

知你几分 2024-09-13 00:38:46

extern "C" _declspec(dllexport) void MyDllFunc()

Write

extern "C" _declspec(dllexport) void MyDllFunc()
や三分注定 2024-09-13 00:38:46

你做错了。 __declspec(dllexport) 与 __declspec(dllimport) 配对。

#1: In the DLL, declare the function's prototype with __declspec(dllexport).
#2: In the .exe, declare the function's prototype with __declspec(dllimport).
#3: Compile the .dll. You should also get a .lib file.
#4: Link the .exe with the .lib, and compile.
#5: Success.

当您使用 __declspec(dllimport) 和 __declspec(dllexport) 时,您永远不需要接触 WinAPI 函数来加载 DLL。 dllimport/export 会为您完成这一切。此外,您不需要 extern C 任何内容。

You're doing it wrong. __declspec(dllexport) pairs with __declspec(dllimport).

#1: In the DLL, declare the function's prototype with __declspec(dllexport).
#2: In the .exe, declare the function's prototype with __declspec(dllimport).
#3: Compile the .dll. You should also get a .lib file.
#4: Link the .exe with the .lib, and compile.
#5: Success.

When you use __declspec(dllimport) and __declspec(dllexport), you never need to touch the WinAPI functions for loading a DLL. dllimport/export does it all for you. In addition, you don't need to extern C anything.

一场信仰旅途 2024-09-13 00:38:46

使用修饰 http://msdn.microsoft.com/en-us/library/a90k134d%28v=VS.80%29.aspx" rel="nofollow noreferrer">__declspec(dllexport),您可以使用减少装饰外部“C”,但是,它不会完全取消装饰符号,为此,您需要使用 def 文件 并将其导出为命名符号,否则您需要使用 GetProcAddress 使用损坏/修饰的符号名称,当使用 extern "C 导出时该名称很短“

Your exported functions name's are being decorated when using __declspec(dllexport), you can reduce the decoration using extern "C", however, it will not fully undecorated the symbol, to do that you need to use a def file and export it as a named symbol, else you need to use GetProcAddress using the mangled/decorated symbol name, which is short when exported using extern "C".

萌面超妹 2024-09-13 00:38:46

如果 DLL 构建为 C++ dll,则其函数名称将会更改。这是编译器相关的。我强烈建议将其设为 C dll(C 接口 + C++ 内核)。我现在没有例子,但你应该能够在网上找到一些东西。

If the DLL is built as a C++ dll, it's functions' names will change. This is compiler dependent. I'd strongly recommend to make it a C dll (C interface + C++ guts). I don't have an example on me right now, but you should be able to find something on the net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文