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?
To prevent the C++ compiler from mangling the name of a function, you can apply the extern "C" linkage specifier to the declaration or declarations, as shown in the following example: extern "C" { int f1(int); int f2(int); int f3(int); };
The name mangling process helps to access the class variables from outside the class. The class variables can be accessed by adding _classname to it. The name mangling is closest to private not exactly private. The above class variable is accessed by adding the _classname to it.
The exports table contains the name of every function that the DLL exports to other executables. These functions are the entry points into the DLL; only the functions in the exports table can be accessed by other executables. Any other functions in the DLL are private to the DLL.
__declspec(dllexport) adds the export directive to the object file so you do not need to use a . def file. This convenience is most apparent when trying to export decorated C++ function names.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With