When using PInvoke, why use __stdcall?

c++, calling-convention, function-declaration, pinvoke, stdcall

Solution

There is only one calling convention on x64 and so it does not matter which calling convention you specify. It is always ignored on x64.

On x86 it is important to make sure calling conventions match on both sides of the interface. So if you ever anticipate running your code on x86 it would be prudent to get that right now.

Problem

I have been using PInvoke to let my C# application call C++ functions I wrote. Now, I keep hearing everywhere and beyond that I need to define those externally accessible functions with the `__stdcall` convention. My question is: Why? So far, I have accidentally ignored this advice, and everything works like a charm. When I add `__stdcall` to my functions, everything keeps working in the same way (or at least it seems so). This article says that `__stdcall` is used for Win32 bit functions, but I am compiling against the x64 platform. Does that mean that I shouldn't be using `__stdcall` after all or does it mean that I am missing out on something else? And please, use simple English when responding. ;-) Lines like this (quoted from the article I linked): The callee cleans the stack, so the compiler makes vararg functions __cdecl. make my brain feel like there's tumbleweed blowing through it.

Original source

Related problems