Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmangling C++ DLL Function Names [duplicate]

Tags:

c++

dll

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.

like image 216
Jim Fell Avatar asked Jan 25 '11 15:01

Jim Fell


2 Answers

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");
like image 142
Chris Becke Avatar answered Oct 19 '22 12:10

Chris Becke


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

like image 3
Simon Richter Avatar answered Oct 19 '22 13:10

Simon Richter