GetProcAddress找不到我的功能 [英] GetProcAddress cannot find my functions

查看:228
本文介绍了GetProcAddress找不到我的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个名为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:

  #includestdafx.h
#includeD3D_API.h
#include< iostream>

D3D_API_API void render()
{
std :: cout<<< 方法叫。 <<的std :: ENDL;
}

以下是尝试使用该功能的应用程序:

  #includestdafx.h
#include< windows.h>
#include< iostream>

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

if(myDLL == NULL){
std :: cerr<<< 加载D3D_API.dll失败! <<的std :: ENDL;
}

typedef void(WI​​NAPI * render_t)();

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

if(render == NULL){
std :: cerr < .dll中没有找到render()! <<的std :: ENDL;
}
return 0;
}

我的目标是制作一个支持D3D和OpenGL的3D引擎,自己的.DLL使用统一的API。

解决方案

您导出的函数被视为一个C ++函数(因为* .cpp文件扩展名),因此C ++ 名称调整用于装饰导出功能名称。如果您使用Microsoft的 Dependency Walker 工具检查您创建的DLL,那么您将看到这些函数的全称。 p>

您可以在导入代码中使用装饰名称,也可以强制编译器以C样式导出函数,即以当前导入代码所期望的未装饰形式导出。



您可以通过将 externC添加到您的函数签名中来告知编译器。这样的东西:

  externCD3D_API_API void 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".

解决方案

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.

这篇关于GetProcAddress找不到我的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆