4
votes

Based on my understanding, name mangling is used when there is function overloading, so that the functions with the same name can be distinguished.

But I have noticed that name mangling is also used with the WinMain() function (which is not overloaded). After name mangling it becomes _WinMain@16.

So why is name mangling used with the WinMain() function?


This is the code that I used:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{

    return 0;
}
1
If the compiler actually did mangle the WinMain symbol, it would be something completely different from what you see. What you see is the compiler having special treatment of the WinMain symbol. - Some programmer dude
I believe _WinMain@16 is some internal name used by the mingw/gcc compiler. What the 16 is supposed to mean, I have no idea. - Lundin
@Lundin the @ means stdcall calling convention, the 16 is the size of the parameter list (here: 4 times a 4 bytes argument). Functions using other calling conventions are decorated differently. - user2371524
@Olaf it does, kind of. Give the same name to several static variables in different scopes and see what will happen. - Lundin
@Olaf It isn't standardized. But obviously a compiler faced with 50 variables all called foo will have to use some manner of name mangling - highly implementation-defined. - Lundin

1 Answers

8
votes

This is not what's commonly called name mangling. Name mangling is for C++ to create distinguished symbol names from functions of the same name (and is unfortunately implementation defined). Here you have a simple "decorated name" by the conventions of win32 ABI. Windows uses them to distiguish different calling conventions available on that platform.

In this case, the single @ denotes the stdcall calling convention and the number following is the number of bytes in the argument list. The WINAPI macro expands to some compiler-specific attribute that selects stdcall as the calling convention.

Different ABIs use different name decorations. E.g. on Linux, your standard calling convention is cdecl and it doesn't use any decoration at all. But you could have decorations containing a symbol version (coincidentally after an @ sign)