Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 627
Slapout Avatar asked Sep 23 '09 16:09

Slapout


People also ask

How do you stop the name mangling?

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); };

What is the name mangling and how does it work?

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.

What is exported function in DLL?

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.

What does __ Declspec Dllexport do?

__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.


1 Answers

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

like image 64
Dewfy Avatar answered Oct 10 '22 19:10

Dewfy