Why did Microsoft choose stdcall as their API convention?

Deadly 提交于 2019-12-07 05:00:44

问题


Is there a good reason?

Are their internal functions (not exported) also stdcall convention?


回答1:


It was an adaptation to the pascal calling convention for 32-bit code. Pascal was the calling convention for 16-bit operating systems like OS/2 and Windows 3. Why pascal was chosen is a bit of a guess, even I was a small pup back then, but it is slightly more efficient. Which mattered back when 640 KB was all you had to work with.

Most Win32 functions aren't true stdcall as it also prescribes how the exported function is decorated before presented to the linker. Like void Mumble(int arg) becomes _Mumble@4. The number after the @ describes the activation frame size. But most Win32 functions are exported without any decoration. Probably to give the programmer a fighting chance to make GetProcAddress() work. I think the decoration was intended to help the linker detect mismatches between the declared API function signature and the actual one. Having a mismatch in the number of passed arguments is an automatic kaboom since the callee will pop more or less arguments off the stack then were passed. Hard to diagnose too. A weakness of stdcall, the cdecl convention doesn't have this problem.

Internal calling is a mixed bag between stdcall, cdecl and thiscall. Can't say I've ever detected a pattern, although single-stepping Windows code isn't something I enjoy doing.




回答2:


Code compiled with stdcall is significantly smaller than code compiled with cdecl (the alternative). At the time the decision was made, smaller code was faster code.



来源:https://stackoverflow.com/questions/3555678/why-did-microsoft-choose-stdcall-as-their-api-convention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!