stdcall and cdecl

后端 未结 9 910
旧时难觅i
旧时难觅i 2020-11-27 09:17

There are (among others) two types of calling conventions - stdcall and cdecl. I have few questions on them:

  1. When a cdecl fun
9条回答
  •  时光说笑
    2020-11-27 09:57

    I noticed a posting that say that it does not matter if you call a __stdcall from a __cdecl or visa versa. It does.

    The reason: with __cdecl the arguments that are passed to the called functions are removed form the stack by the calling function, in __stdcall, the arguments are removed from the stack by the called function. If you call a __cdecl function with a __stdcall, the stack is not cleaned up at all, so eventually when the __cdecl uses a stacked based reference for arguments or return address will use the old data at the current stack pointer. If you call a __stdcall function from a __cdecl, the __stdcall function cleans up the arguments on the stack, and then the __cdecl function does it again, possibly removing the calling functions return information.

    The Microsoft convention for C tries to circumvent this by mangling the names. A __cdecl function is prefixed with an underscore. A __stdcall function prefixes with an underscore and suffixed with an at sign “@” and the number of bytes to be removed. Eg __cdecl f(x) is linked as _f, __stdcall f(int x) is linked as _f@4 where sizeof(int) is 4 bytes)

    If you manage to get past the linker, enjoy the debugging mess.

提交回复
热议问题