user8315006
user8315006

Reputation:

How does the __cdecl calling convention returns a struct?

I want to call a function that returns a struct, this function has the __cdecl calling convention.

How does the __cdecl calling convention returns a struct?

Upvotes: 2

Views: 2051

Answers (1)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

That depends.

There is no uniform way.

Simple function returns, like ints are returned in registers, but none of the calling conventions, __cdecl, __stdcall, etc. describe how exactly a struct is returned.

  • Some return simple structs that fit in one (EAX, RAX) or two registers (EDX:EAX or RDX:RAX) in these mentioned registers.
  • Others return such structs by reference, i.e. the function result is compiled as a void function taking an extra pointer argument to such a struct.
  • Others apply a mix of these strategies.

I have had big problems with this (DLL functions that return structs). The fact that different compilers use different strategies to return structs, even on the same platform and using the same calling convention, has made me issue the warning that one should never return structs from, say, DLL functions.

More info:

Upvotes: 7

Related Questions