8

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

I have a DLL that is written in C++. The exported function names need to be unmangled. For example, int MyFunc( int Param1, int Param2 ); needs to appear to an outside application trying to call the library function simply as MyFunc. However, when I look at it using Dependency Walker, it looks like _MyFunc@8. This is how I have it declared in C++:

extern "C" __declspec(dllexport) int WINAPI MyFunc( int Param1, int Param2 );

I thought the extern "C" would do the trick. How do I get rid of the mangling? Thanks.

Community
  • 1
  • 1
Jim Fell
  • 13,750
  • 36
  • 127
  • 202

5 Answers5

8

Ways to get rid of mangling: (assuming MSVC is the build environment)

Export via a .DEF file.

Export as extern "C" ensuring that the __cdecl calling convention is used. __stdcall prepends the _ and postpends the @ on dll exported functions even when extern "C" is used.

extern "C" __declspec(dllexport) int __cdecl MyFunc(int Param1, int Param2);

Export using a #pragma directive. You need to pass the fully mangled name on the other side of this. __FUNCDNAME__ is a useful directive to put in a macro in a function to list its decorated name,

#pragma comment(linker, "/EXPORT:MyFunc=_MyFunc@8");
Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • Does `extern "C"` ensure that the `__cdecl` calling convention is used, or are those two separate things? – Jim Fell Jan 25 '11 at 15:30
  • extern "C" merely ensures that the symbol is compatible with C code. __cdecl and __stdcall are separate directives that can be applied to fully decorated c++ functions – Chris Becke Jan 25 '11 at 15:32
  • 1
    Should the pragma go immediately before the function declaration? – Jim Fell Jan 25 '11 at 15:35
  • The pragma solution did the trick. Not the cleanest way to do it, as it leaves the mangled artifacts in the DLL. However, for my particular situation this is simple and effective. – Jim Fell Jan 25 '11 at 15:45
3

The leading underscore and @8 suffix are not from C++ name mangling, but rather denote stdcall calling convention, as is normal for dllexport.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64
2

This is probably because you did not put extern "C" declspec on the function definition, you only put it on the declaration.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
0

On the header (.h), define it as:

extern "C" 
{
  __declspec(dllexport) int __stdcall MyFunc( int Param1, int Param2 );
}

Then, on the implementation (.cpp) of the function:

extern "C"
{
  __declspec(dllexport) int __stdcall MyFunc( int Param1, int Param2 )
  {
     // ... code ...
  }
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

On Gnu systems there is c++filt

doron
  • 27,972
  • 12
  • 65
  • 103
  • And on Windows it's `undname`. But the question's about stopping the mangling not decoding it. – Rup Jan 25 '11 at 18:37