C++ name mangling

suggest change

C++ compilers encode additional information in the names of exported functions, such as argument types, to make overloads with different arguments possible. This process is called name mangling. This causes problems with importing functions in C# (and interop with other languages in general), as the name of int add(int a, int b) function is no longer add, it can be ?add@@YAHHH@Z, _add@8 or anything else, depending on the compiler and the calling convention.

There are several ways to deal with name mangling:

extern "C" __declspec(dllexport) int __stdcall add(int a, int b)
[DllImport("myDLL.dll")]

The exported name is still decorated (_add@8 for this stdcall function). The C# interop marshaler recognizes that extern "C" stdcall decoration, so DllImport can still bind add.

EXPORTS
    add
int __stdcall add(int a, int b)
[DllImport("myDLL.dll")]

The function name will be pure add in this case.

__declspec(dllexport) int __stdcall add(int a, int b)
[DllImport("myDLL.dll", EntryPoint = "?add@@YGHHH@Z")]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents