8

Is there a good reason?

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

pnuts
  • 58,317
  • 11
  • 87
  • 139
Benjamin
  • 10,085
  • 19
  • 80
  • 130

2 Answers2

11

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 5
    the lack of symbol decoration is cause the winapi binaries use `.def` files for exporting(which strips decoration and/or uses an ordianl), this makes importing via GetProcAddress simple, as there is no symbol decoration – Necrolis Aug 24 '10 at 12:04
  • 2
    I do enjoy single-stepping Windows code sometimes, and I can say that most of the time stdcall is used internally as well. – Paul Sep 20 '20 at 20:09
4

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.

Larry Osterman
  • 16,086
  • 32
  • 60
  • Actually I preferred Hans Passant's answer to mine, but whatever. – Larry Osterman Aug 25 '10 at 03:38
  • Because you are a Microsoftee(for long time), I chose your answer. – Benjamin Aug 25 '10 at 21:20
  • 2
    Do you have any evidence for that claim? Intuitively I think it should be a wash between stdcall and cdecl and googling doesn't come up with any size comparisons between the two calling conventions. – Björn Lindqvist Aug 31 '15 at 10:35
  • 1
    @BjörnLindqvist For the size; a single `retn` in the callee vs adjusting esp after every `call`. Larry blogged about this but I can't find it right now... – Anders Jul 20 '22 at 14:25