3

I am designing an API that has to be binary-compatible between at least mingw and msvc++. So far I have restricted myself to using function which take and return primitive data types or pointers to POD-structs with uniform members (i.e. the members are all of the same type, which should reduce the risk of incompatible padding).

It would be convenient at some points to return structs by value though, so that the callee does not need to keep a temporary copy. So the question is: Is it safe to pass structs by value to/from stdcall functions, when the callee was compiled by a different compiler than the caller? Does this still hold for less recent versions of msvc and mingw? I would be more confident that it is, but I found this topic discussing a problem in this exact situation with cdecl calling convention, which was apparently only solved in mingw 4.6.

Medo42
  • 3,821
  • 1
  • 21
  • 37
  • I actually tried it out now, and it will not compile. So, is it not possible at all to pass structs by value in that convention? That would clear up the question pretty quickly. – Medo42 May 30 '11 at 21:20
  • you should stick with passing pointers, as passing structs by value is generally a bad idea (unless they fit into a register, but even then...). – Necrolis May 31 '11 at 14:03

2 Answers2

2

Using struct just like is not good option. You need to use

#pragma pack

Refer http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Fcompiler%2Fref%2Frnpgpack.htm

http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx

And make sure mingw respect pragma instruction.

  • It seems that mingw does support pack, so setting #pragma pack(1) in my API header might help to keep non-uniform structs compatible (uniform structs are very probably not padded to begin with, since alignment of a type usually corresponds with its size). +1, but no accept because it does not actually answer my question. – Medo42 May 31 '11 at 20:45
0

I don't know mingw, but if it can call Win32 APIs then it can pass structs in a way that is compatible with stdcall - since many Win32 APIs are both stdcall and take structs.

Martyn

Martyn Lovell
  • 2,086
  • 11
  • 13