How do I stop name-mangling of my DLL's exported function?

帅比萌擦擦* 提交于 2019-12-17 04:30:44

问题


I'm trying to create a DLL that exports a function called "GetName". I'd like other code to be able to call this function without having to know the mangled function name.

My header file looks like this:

#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif

EXPORT TCHAR * CALLBACK GetName();

My code looks like this:

#include <windows.h>
#include "PluginOne.h"

int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
     return TRUE ;
}

EXPORT TCHAR * CALLBACK GetName()
{
    return TEXT("Test Name");
}

When I build, the DLL still exports the function with the name: "_GetName@0".

What am I doing wrong?


回答1:


Small correction - for success resolving name by clinet

extern "C"

must be as on export side as on import.

extern "C" will reduce name of proc to: "_GetName".

More over you can force any name with help of section EXPORTS in .def file




回答2:


This is normal for a DLL export with a __stdcall convention. The @N indicates the number of bytes that the function takes in its arguments -- in your case, zero.

Note that the MSDN page on Exporting from a DLL specifically says to "use the __stdcall calling convention" when using "the keyword __declspec(dllexport) in the function's definition".




回答3:


the right answer is the following:

extern "C" int MyFunc(int param);

and

int MyFunc(int param);

is two declarations which uses different internal naming, first - is in C-style, second - in the C++ style.

internal naming required for build tools to determine which arguments function receives, what type returns etc, since C++ is more complicated (oop's, overloaded, virtual functions etc) - it uses more complicated naming. calling convention also affects both c and c++ namings.

both this styles of naming is applied when using __declspec(dllexport) in the same manner.

if you want to omit name mangling of exported routine, add a module definition file to your project, type in it (in this case you not required to declspec dllexport):

LIBRARY mylib
EXPORTS
  MyFunc

this will omit explicit name decoration (samples below).

_MyFunc (c style, __cdecl)
_MyFunc@4 (c style, __stdcall)
?MyFunc@@YAHH@Z (c++ style, __cdecl)
?MyFunc@@YGHH@Z (c++ style, __stdcall)



回答4:


You can use the "-Wl,--kill-at" linker switch to disable name mangling.

For example, in Code::Blocks, in the custom linker settings, add: -Wl,--kill-at



来源:https://stackoverflow.com/questions/1467144/how-do-i-stop-name-mangling-of-my-dlls-exported-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!