GetProcAddress cannot find my functions

前端 未结 2 1061
慢半拍i
慢半拍i 2020-12-19 14:02

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:

#         


        
相关标签:
2条回答
  • 2020-12-19 14:31

    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.

    0 讨论(0)
  • 2020-12-19 14:35

    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();
    
    0 讨论(0)
提交回复
热议问题