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

38,543

Solution 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

Solution 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".

Solution 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)

Solution 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

Share:
38,543
Slapout
Author by

Slapout

Available on the App Store Apple, the Apple logo and iPad are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc.

Updated on December 20, 2020

Comments

  • Slapout
    Slapout over 3 years

    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?