Exported function names are mangled
The other day I was trying to access an exported function from a C++/CLI dll, a.k.a dll interface. I created a C++ CLR class library project from Visual Studio 2010, and declared a export function as follows.
__declspec(dllexport) int GetInteger(void);
and when I tried to access the function from outside using the GetProcAdderss windows API, it return a null pointer, means it was not able to find the function from the list of exported functions in the dll. I was wondering why and want to see the list of exported functions in the compiled dll. Depoendency walker seemst to be a good tool to see a list of exported functions in a dll. When I loaded the dll in dependency walker, it showed the following function
This is because the project is compiled as c++ and so it by default mangles all the function names. . To verify if this is the actual cause of the failure, I used the mangled name in the call to GetProcAddress and it resolved successfully. So then it was a matter of how to tell the compiler to not mangle the names.
- I tried setting the compiler option /TC (Project properties –> C/C++ –> Advanced –> Compile As) which means compile the project as C code. This works but the entire project would be compiled as C code, but that is not what I want, as I have a lot of C++ and CLI classes out there in it.
- decorate the function export declaration prefixing extern “C” like as follows
extern "C" __declspec(dllexport) int GetInteger(void);
This seems to just add the export definition of the function in C style, without name mangling.
Hope this helps.