C++ DLL Export: Decorated/Mangled names - c++

Created basic C++ DLL and exported names using Module Definition file (MyDLL.def).
After compilation I check the exported function names using dumpbin.exe
I expect to see:
SomeFunction
but I see this instead:
SomeFunction = SomeFunction###23mangledstuff#####
Why?
The exported function appears undecorated (especially compared to not using the Module Def file), but what's up with the other stuff?
If I use dumpbin.exe against a DLL from any commercial application, you get the clean:
SomeFunction
and nothing else...
I also tried removing the Module Definition and exporting the names using the "C" style of export, namely:
extern "C" void __declspec(dllexport) SomeFunction();
(Simply using "extern "C" did not create an exported function)
However, this still creates the same output, namely:
SomeFunction = SomeFunction###23mangledstuff#####
I also tried the #define dllexport __declspec(dllexport) option and created a LIB with no problem. However, I don't want to have to provide a LIB file to people using the DLL in their C# application.
It's a plain vanilla C++ DLL (unmanaged code), compiled with C++ nothing but a simple header and code. Without Module Def I get mangled exported functions (I can create a static library and use the LIB no problem. I'm trying to avoid that). If I use extern "C" __declspec(dllexport) OR a Module Definition I get what appears to be an undecorated function name... the only problem is that it is followed by an "=" and what looks like a decorated version of the function. I want to get rid of the stuff after the "=" - or at least understand why it is there.
As it stands, I'm pretty certain that I can call the function from C# using a P/Invoke... I just want to avoid that junk at the end of the "=".
I'm open to suggestions on how to change the project/compiler settings, but I just used the standard Visual Studio DLL template - nothing special.

Instead of using .def file just insert pragma comment like this
#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction###23mangledstuff#####")
Edit: Or even easier: Inside the body of the function use
#pragma comment(linker, "/EXPORT:" __FUNCTION__"=" __FUNCDNAME__)
. . . if you have troubles finding the decorated function name. This last pragma can be further reduced with a simple macro definition.

You can get what you want by turning off debug info generation. Project + Properties, Linker, Debugging, Generate Debug Info = No.
Naturally, you only want to do this for the Release build. Where the option is already set that way.

You have to declare the functions as extern "C" if you don't want their names to be mangled.

From experience, be careful if you use __stdcall in your function signature. With __stdcall, the name will remain mangled to some extent (you will find out quickly enough). Apparently, there are two levels of mangling, one the extern "C" deals with at the C++ level, but it does not deal with another level of name mangling caused by __stdcall. The extra mangling is apparently relevant to overloading -- but I am not certain of that.

Even without the mangling, the 32-bit and 64-bit builds name exports differently, even with extern "C". Check it out with DEPENDS.EXE.
This can mean BIG trouble to any client that does a LoadLibrary+GetProcAdress to access your function.
So, on top of all the others use a Module Definition File as follows:
LIBRARY MYDLL
EXPORTS
myFunction=myFunction
Yeap, it's a bit of a pain to maintain, but then how many exported functions do you write a day?
Moreover, I usually change the macros like shown below, since my DLLs export functions not C++ classes and I want them to be callable by most programming environments:
#ifdef WTS_EXPORTS
#define WTS_API(ReturnType) extern "C" __declspec(dllexport) ReturnType WINAPI
#else
#define WTS_API(ReturnType) extern "C" __declspec(dllimport) ReturnType WINAPI
#endif
WTS_API(int) fnWTS(void);
The last line used to confuse VisualAssistX a couple of years ago, I don't know if it properly digests it now :-)

Sorry for replying to an old thread, but what has been marked as the answer did not work for me.
As a number of people have pointed out, the extern "C" decoration is important. Changing the "Project / Properties / Linker / Debugging / Generate debug info" setting made absolutely no difference to the mangled names being generated for me in either Debug or Release build mode.
Setup: VS2005 compiling a Visual C++ Class Library project. I was checking the compiled .dll output with Microsoft's Dependency Walker tool.
Here is an example recipe that worked for me...
In project.h:
#define DllExport extern "C" __declspec( dllexport )
DllExport bool API_Init();
DllExport bool API_Shutdown();
In project.cpp:
#include "project.h"
bool API_Init()
{
return true;
}
bool API_Shutdown()
{
return true;
}
Then being called from C# managed code, class.cs:
using System.Runtime.Interopservices;
namespace Foo
{
public class Project
{
[DllImport("project.dll")]
public static extern bool API_Init();
[DllImport("project.dll")]
public static extern bool API_Shutdown();
}
}
Doing the above prevented the mangled names in both Debug and Release mode, regardless of the Generate debug info setting. Good luck.

I know how many times I've tried forcing function names using code and #pragma's.
And I always end with exactly same thing, using Module-Definition File (*.def) at the end.
And here is the reason:
//---------------------------------------------------------------------------------------------------
// Test cases built using VC2010 - Win32 - Debug / Release << doesn't matter
//---------------------------------------------------------------------------------------------------
// SET: Project > Properties > Linker > Debugging > Generate Debug Info = Yes (/DEBUG)
// || (or, also doesn't matter)
// SET: Project > Properties > Linker > Debugging > Generate Debug Info = No + delete PDB file!
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback
extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> _SetCallback#4
__declspec(dllexport) void SetCallback(LPCALLBACK function);
> ?SetCallback##YAXP6AXHPADPAX#Z#Z
__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> ?SetCallback##YGXP6GXHPADPAX#Z#Z
//---------------------------------------------------------------------------------------------------
// this also big is nonsense cause as soon you change your calling convention or add / remove
// extern "C" code won't link anymore.
// doesn't work on other cases
#pragma comment(linker, "/EXPORT:SetCallback")
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
// doesn't work on other cases
#pragma comment(linker, "/EXPORT:SetCallback=SetCallback")
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=_SetCallback#4")
extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=?SetCallback##YAXP6AXHPADPAX#Z#Z")
__declspec(dllexport) void SetCallback(LPCALLBACK function);
// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=?SetCallback##YGXP6GXHPADPAX#Z#Z")
__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
//---------------------------------------------------------------------------------------------------
// So far only repetable case is using Module-Definition File (*.def) in all possible cases:
EXPORTS
SetCallback
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback
extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> SetCallback
__declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback
__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> SetCallback
// And by far this is most acceptable as it will reproduce exactly same exported function name
// using most common compilers. Header is dictating calling convention so not much trouble for
// other sw/ppl trying to build Interop or similar.
I wonder why no one did this, it took me only 10 mins to test all cases.

the SomeFunction###23mangledstuff##### is mangled to give the types and class of the C++ function. The simple exports are functions that are callable from C i.e. are written in C or else are declared extern "C' in C++ code. If is you want a simple interface you have to make the functions you export be use just C types and make them non member functions in the global namespace.

Basically, when you use functions in C++, parts of their names now include their signature and suchlike, in order to facilitate language features like overloading.
If you write a DLL using __declspec(dllexport), then it should also produce a lib. Link to that lib, and you will automatically be linked and the functions registered by the CRT at start-up time (if you remembered to change all your imports to exports). You don't need to know about name mangling if you use this system.

In case it wasn't clear from the hundreds of lines of waffle on the subject of mangled exports. Here's my 2c worth :)
After creating a project called Win32Project2 using VS 2012 and choosing export all symbols in the wizard. You should have 2 files called Win32Project2.cpp and Win32project2.h
Both of those will reference an example exportable variable and an example exported function.
In Win32Project2.h you will have the following:
#ifdef WIN32PROJECT2_EXPORTS
#define WIN32PROJECT2_API __declspec(dllexport)
#else
#define WIN32PROJECT2_API __declspec(dllimport)
#endif
extern WIN32PROJECT2_API int nWin32Project2;
WIN32PROJECT2_API int fnWin32Project2(void);
To unmangle CHANGE the last two lines to extern "C" declarations to:
extern "C" WIN32PROJECT2_API int nWin32Project2;
extern "C" WIN32PROJECT2_API int fnWin32Project2(void);
In Win32Project2.cpp you will also have the following default definitions:
// This is an example of an exported variable
WIN32PROJECT2_API int nWin32Project2=0;
// This is an example of an exported function.
WIN32PROJECT2_API int fnWin32Project2(void)
{
return 42;
}
To unmangle CHANGE THESE TO:
// This is an example of an exported variable
extern "C" WIN32PROJECT2_API int nWin32Project2=0;
// This is an example of an exported function.
extern "C" WIN32PROJECT2_API int fnWin32Project2(void)
{
return 42;
}
Essentially you must use the extern "C" prefix in front of declarations in order to force the linker to produce unmangled C like names.
If you prefer to use mangled names for that bit of extra obfuscation (in case the mangling info is useful to someone somehow) use "dumpbin /exports Win32Project2.dll" from a VC command line to lookup the actual reference names. It will have the form "?fnWind32Project2#[param bytes]#[other info] . There are also other DLL viewing tools around if running a VC command shell doesn't float your boat.
Exactly why MS doesn't default to this convention is a mystery. The actual mangling information means something (like parameter size in bytes and more) which might be useful for validation and debugging but is otherwise guff.
To import the DLL function above into C# project (in this case a basic C# windows application with a form on it containing the button "button1") here's some sample code:
using System.Runtime.InteropServices;
namespace AudioRecApp
{
public partial class Form1 : Form
{
[ DllImport("c:\\Projects\test\Debug\Win32Projects2.dll")]
public static extern int fnWin32Project2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int value;
value = fnWin32Project2();
}
}
}

Related

Failing to GetProcAddress

I'm having trouble with loading a DLL in my assignment project.
Here's the header file:
I have omitted code that works and is irrelevant to the problem. Basically, hinstLib is not NULL but when the line Filter = (FILTPTR) GetProcAddress(hinstLib, "Filter"); is executed, Filter has no value. To me it seems like it is saying that the DLL has been found but it cannot find the function "Filter" inside the DLL and I have no idea why, albeit I could be wrong. I still haven't got my head around how some of this works.
Here is the DLL:
Any ideas anyone? All help is greatly appreciated!
James
Your specifiers are wrong.
A good, concise way do to this is to use same header to in DLL and APP, defining the export-import interface., which uses macro like this:
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif
And declarations:
extern "C" MY_DLL_API int Filter(int* data, int count, const WCHAR* parameterString);
Library's .cpp file would use this header and would define MY_DLL_EXPORTS.
If I understand your code right, you made it so that linker tries to export same function from both modules? ALso, function's prototype should be C-compatible to be actually extern "C"
when the line Filter = (FILTPTR) GetProcAddress(hinstLib, "Filter"); is executed, Filter has no value. To me it seems like it is saying that the DLL has been found but it cannot find the function "Filter" inside the DLL and I have no idea why
The function is likely being exported with a decorated name. You are not specifying a calling convention, so the default is usually __cdecl, which prefixes the function name with an underscore, thus it would be exported as "_Filter" instead. But this is compiler-specific behavior, so double-check your DLL's EXPORTS table with a PE viewer/dumper to see the actual name being exported. You may need to add a .def file to your project to ensure the function is exported as "Filter" as desired.

Extern points to 0x00000000

I am using Microsoft Visual C++ 2010 Express, on Windows XP.
I have one application that produces a DLL, a header file in this application also declares a pointer to a BUFFER as an extern.
To register this DLL with the system, I drag it onto the regsvr32.exe icon in the system32 folder.
I have another application that tests the use of this DLL, by initialising and calling functions from it. This application accesses the same BUFFER as the DLL, by using this extern declaration.
At first, when I used the Visual Studio debugger to debug the test application, I could see the contents of the extern BUFFER from the loaded DLL. However, after repeatedly debugging this application, now the BUFFER does not display its memory address, just "0x0000000", so I can't view the data.
Does anyone know why this might be? I can't understand why it used to work, but now doesn't. I haven't changed any aspect of this part of the source code, at all.
Is it OK to access the BUFFER in the DLL by using an extern pointer like this, or is there
a better way?
Thanks for your help.
In C++ extern means, that variable is declared in another *.cpp (translation unit).
Example:
myfile1.cpp:
int globalVariable = 0;
myfile2.cpp:
extern int globalVariable; //same variable, because of extern
If you need to export from dll, you must use dllexport (in library) and dllimport (in library consumer) for functions and variables, e.g.:
mylibrary.cpp:
__declspec(dllexport) int myGlobalExportingVariable = 0;
myprogram.cpp:
__declspec(dllimport) int myGlobalExportingVariable;
Of course, in real world, you would probably use something like this:
mylibrary.hpp:
#ifdef MYLIBRARY
#define MYLIBRARY_ITEM __declspec(dllexport)
#else
#define MYLIBRARY_ITEM __declspec(dllimport)
#endif
MYLIBRARY_ITEM void func1();
MYLIBRARY_ITEM int variable0;
MYLIBRARY_ITEM float func2();
//...
And you #include this header in both mylibrary.cpp and myprogram.cpp; don't forget to define MYLIBRARY macro in your project settings (C++ -> Preprocessor -> Preprocessor definitions).
By the way: as pointed in comment by PiotrLegnica, registering your dll library with regsvr32.exe is pointless unless you use technology COM (component object model).
When you declare a variable with extern then it’s mean only declare it but not define it( no memory allocation for that variable) because its define it other place.
You declare the variable in your cpp file with dll codes. In header make it as extern with dllexport/import and now use it.

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

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?
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
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".
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)
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

Specify ordinals of C++ exported functions in a DLL

I am writing a DLL with mixed C/C++ code. I want to specify the ordinals of the functions I'm exporting. So I created a .DEF file that looks like this
LIBRARY LEONMATH
EXPORTS
sca_alloc #1
vec_alloc #2
mat_alloc #3
sca_free #4
vec_free #5
mat_free #6
...
I would like to specify the ordinals of my C++ functions and class methods too. I have tried using the Dependency Walker to add the mangled names of my functions to the .DEF file:
??0CScalar##QAE#XZ #25
??0CScalar##QAE#O#Z #26
??0CScalar##QAE#ABV0##Z #27
??1CScalar##QAE#XZ #28
But this has failed. Any ideas why this could be happening?
EDIT: kauppi made a good observation, so I'm adding more information to the question.
Platform: Windows (and I'm not interested in portability)
Compiler: Microsoft's C++ compiler (I'm using VS2005)
Why I want to do this?: Using the ordinals has the advantage of letting me call exported C++ functions from C code.
Well, I don't have experience with ordinals (which look like some ugly, compiler-specific thing), but I can help you with making C++/C code compatible.
Suppose, in C++, that your header file looks like this:
class MyClass
{
void foo(int);
int bar(int);
double bar(double);
void baz(MyClass);
};
You can make it C-compatible by doing the following:
#ifdef __cplusplus
#define EXTERN_C extern "C"
// Class definition here; unchanged
#else
#define EXTERN_C
typedef struct MyClass MyClass;
#endif
EXTERN_C void MyClass_foo (MyClass*, int);
EXTERN_C int MyClass_bar_int (MyClass*, int);
EXTERN_C double MyClass_bar_double (MyClass*, double);
EXTERN_C void MyClass_baz (MyClass*, MyClass*);
In the C++ source file, you just define the various extern "C" functions to pass to the desired member functions, like this (this is only one; the rest work similarly)
extern "C" void MyClass_foo (MyClass* obj, int i)
{
obj->foo(i);
}
The code will then have a C interface, without having to change the C++ code at all (except for declarations in the header; but those could also be moved to another file "myclass_c.h" or the like). All the functions declared/defined extern "C" won't be mangled, so you can do other operations on them easily. You will also probably want functions to construct/destroy instances of MyClass (you can, of course, use new/delete for this).
You said "Using the ordinals has the advantage of letting me call exported C++ functions from C code." , I am sorry to say that this is incorrect.
C++ class member functions have special calling convention which requires an invisible this value passed in an implementation-specific register/parameter. And also you need a class instance to pass, which you can not accomplish in C.
The only 2 uses of this that I know, are faster dynamic linking of the DLL and smaller Import Table. Just inspect mfc70.dll in system32 directory with the dependancy walker.

Overloaded functions in C++ DLL def file

I'm writing a C/C++ DLL and want to export certain functions which I've done before using a .def file like this
LIBRARY "MyLib"
EXPORTS
Foo
Bar
with the code defined as this, for example:
int Foo(int a);
void Bar(int foo);
However, what if I want to declare an overloaded method of Foo() like:
int Foo(int a, int b);
As the def file only has the function name and not the full prototype I can't see how it would handle the overloaded functions. Do you just use the one entry and then specify which overloaded version you want when passing in the properly prototyped function pointer to LoadLibrary() ?
Edit: To be clear, this is on Windows using Visual Studio 2005
Edit: Marked the non-def (__declspec) method as the answer...I know this doesn't actually solve the problem using def files as I wanted, but it seems that there is likely no (official) solution using def files. Will leave the question open, however, in case someone knows something we don't have overloaded functions and def files.
Function overloading is a C++ feature that relies on name mangling (the cryptic function names in the linker error messages).
By writing the mangled names into the def file, I can get my test project to link and run:
LIBRARY "TestDLL"
EXPORTS
?Foo##YAXH#Z
?Foo##YAXHH#Z
seems to work for
void Foo( int x );
void Foo( int x, int y );
So copy the C++ function names from the error message and write them into your def file. However, the real question is: Why do you want to use a def file and not go with __declspec(dllexport) ?
The mangled names are non-portable, I tested with VC++ 2008.
In the code itself, mark the functions you want to export using __declspec(dllexport). For example:
#define DllExport __declspec(dllexport)
int DllExport Foo( int a ) {
// implementation
}
int DllExport Foo( int a, int b ) {
// implementation
}
If you do this, you do not need to list the functions in the .def file.
Alternatively, you may be able to use a default parameter value, like:
int Foo( int a, int b = -1 )
This assumes that there exists a value for b that you can use to indicate that it is unused. If -1 is a legal value for b, or if there isn't or shouldn't be a default, this won't work.
Edit (Adam Haile): Corrected to use __declspec as __dllspec was not correct so I could mark this as the official answer...it was close enough.
Edit (Graeme): Oops - thanks for correcting my typo!
I had a similar issue so I wanted to post on this as well.
Usually using
extern "C" __declspec(dllexport) void Foo();
to export a function name is fine.
It will usually export the name
unmangled without the need for a
.def file. There are, however, some
exceptions like __stdcall functions
and overloaded function names.
If you declare a function to use the
__stdcall convention (as is done for many API functions) then
extern "C" __declspec(dllexport) void __stdcall Foo();
will export a mangled name like
_Foo#4. In this case you may need to explicitly map the exported name
to an internal mangled name.
A. How to export an unmangled name. In a .def file add
----
EXPORTS
; Explicit exports can go here
Foo
-----
This will try to find a "best match" for an internal function Foo and export it. In the case above where there is only
one foo this will create the mapping
Foo = _Foo#4
as can be see via dumpbin /EXPORTS
If you have overloaded a function name then you may need to explicitly say which function you want in the .def file
by specifying a mangled name using the entryname[=internalname] syntax. e.g.
----
EXPORTS
; Explicit exports can go here
Foo=_Foo#4
-----
B. An alternative to .def files is that you can export names "in place" using a #pragma.
#pragma comment(linker, "/export:Foo=_Foo#4")
C. A third alternative is to declare just one version of Foo as extern "C" to be exported unmangled. See here for details.
There is no official way of doing what you want, because the dll interface is a C api.
The compiler itself uses mangled names as a workaround, so you should use name mangling when you don't want to change too much in your code.
There isn't a language or version agnostic way of exporting an overloaded function since the mangling convention can change with each release of the compiler.
This is one reason why most WinXX functions have funny names like *Ex or *2.
Systax for EXPORTS definition is:
entryname[=internalname] [#ordinal [NONAME]] [PRIVATE] [DATA]
entryname is the function or variable name that you want to export. This is required. If the name you export is different from the name in the DLL, specify the export's name in the DLL with internalname.
For example, if your DLL exports a function, func1() and you want it to be used as func2(), you would specify:
EXPORTS
func2=func1
Just see the mangled names (using Dependency walker) and specify your own functions name.
Source: http://msdn.microsoft.com/en-us/library/hyx1zcd3(v=vs.71).aspx
Edit: This works for dynamic DLLs, where we need to use GetProcAddress() to explicitly fetch a functions in Dll.