GetProcAddress 找不到我的函数

发布于 2024-08-28 07:53:39 字数 1119 浏览 4 评论 0原文

我制作了一个带有名为“render()”的函数的 DLL,我想将其动态加载到我的应用程序中,但 GetProcAddress 找不到它。这是 DLL .h:

#ifdef D3D_API_EXPORTS
#define D3D_API_API __declspec(dllexport)
#else
#define D3D_API_API __declspec(dllimport)
#endif

D3D_API_API void render();

这是 DLL .cpp:

#include "stdafx.h"
#include "D3D_API.h"
#include <iostream>

D3D_API_API void render()
{
    std::cout << "method called." << std::endl;
}

这是尝试使用该函数的应用程序:

#include "stdafx.h"
#include <windows.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE myDLL = LoadLibrary( L"D3D_API.dll" );

    if (myDLL == NULL) {
        std::cerr << "Loading of D3D_API.dll failed!" << std::endl;
    }

    typedef void (WINAPI *render_t)();

    render_t render = (render_t)GetProcAddress( myDLL, "render" );

    if (render == NULL) {
        std::cerr << "render() not found in .dll!" << std::endl;
    }
    return 0;
}

我的目标是制作一个使用统一 API 通过自己的 .DLL 支持 D3D 和 OpenGL 的 3D 引擎。我在记事本中查看了.dll,其中有一个字符串“render”。

I made a DLL with a function named "render()" and I want to load it dynamically to my application, but GetProcAddress cannot find it. Here's the DLL .h:

#ifdef D3D_API_EXPORTS
#define D3D_API_API __declspec(dllexport)
#else
#define D3D_API_API __declspec(dllimport)
#endif

D3D_API_API void render();

And here's DLL .cpp:

#include "stdafx.h"
#include "D3D_API.h"
#include <iostream>

D3D_API_API void render()
{
    std::cout << "method called." << std::endl;
}

Here's the application that tries to use that function:

#include "stdafx.h"
#include <windows.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE myDLL = LoadLibrary( L"D3D_API.dll" );

    if (myDLL == NULL) {
        std::cerr << "Loading of D3D_API.dll failed!" << std::endl;
    }

    typedef void (WINAPI *render_t)();

    render_t render = (render_t)GetProcAddress( myDLL, "render" );

    if (render == NULL) {
        std::cerr << "render() not found in .dll!" << std::endl;
    }
    return 0;
}

My goal is to make a 3D engine that supports both D3D and OpenGL through their own .DLLs using a unified API. I looked at the .dll in notepad and there was a string "render".

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

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

发布评论

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

评论(2

-黛色若梦 2024-09-04 07:53:39

您导出的函数被视为 C++ 函数(因为 *.cpp 文件扩展名),因此 C++ 名称修改< /a> 用于修饰导出的函数名称。如果您使用 Microsoft 的 Dependency Walker 工具来检查您创建的 DLL,您将看到函数的全名。

您可以在导入代码中使用该修饰名称,也可以强制编译器以 C 风格导出函数,即以当前导入代码所需的未修饰形式导出。

您可以通过在函数签名中添加 extern "C" 来告诉编译器这样做。像这样的事情:

extern "C" D3D_API_API void render();

现在你的导入代码应该像expexted一样工作。

The function you export is treated as a C++ function (because of *.cpp file extension) and so C++ name mangling is used to decorate the exported function name. If you use the Dependency Walker tool from Microsoft to inspect your created DLL you will see the functions full name.

You can either use that decorated name in your import code or force the compiler to export your function in C style, that is, in its undecorated form that your current import code expects.

You can tell the compiler to do so by adding extern "C" to your functions signature. Something like this:

extern "C" D3D_API_API void render();

Now your import code should work as expexted.

韬韬不绝 2024-09-04 07:53:39

正如对答案的评论所说:

使用 'extern "C"' 将删除任何 C++ 名称修改,但仍然保留
C 名称修改。为了导出普通名称,您应该查看
使用 .DEF 文件。看
blogs.msdn.microsoft.com/oldnewthing/20120525-00/? p=7533

您需要向您的项目添加一个扩展名为 .DEF 的新文件,其内容类似于:

LIBRARY      "MyRenderLib"

EXPORTS
render

然后在您的 DLL 标头中您不使用 __declspec(dllexport),而仅使用 extern "C"

extern "C" void render();

As the comment to the answer says:

using 'extern "C"' will remove any C++ name mangling, but still leaves
C name mangling. In order to export the plain names you should look at
using a .DEF file. See
blogs.msdn.microsoft.com/oldnewthing/20120525-00/?p=7533

You need to add a new file with .DEF extension to your project, with similar to this contents:

LIBRARY      "MyRenderLib"

EXPORTS
render

Then in your DLL header you don't use __declspec(dllexport), but only extern "C"

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