Search code examples
windowswinapivisual-c++name-decoration

Why extern "C" still cannot remove name mangling in the following case


extern "C"
{
__declspec(dllexport) LRESULT CALLBACK MTest
}

Using depends , I found there is still name mangling even using extern "C".


Solution

  • The only way to get truly undecorated names with __declspec(dllexport) is to export them with the __cdecl calling convention. CALLBACK becomes __stdcall, which decorates the "C" form of the name with a leading _ and trailing @bytes.

    Otherwise you can use a .DEF file, which is a pain. Another MSVC specific way is to embed a /EXPORT directive into the object file (or pass it as a explicit linker setting)

    #pragma comment(linker, "/EXPORT:ExportSymbol=DecoratedName");
    

    For some reason the = part of the directive is not listed in the help