Is it safe to return a struct from an stdcall dll function?

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 (ie 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.


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.


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

链接地址: http://www.djcxy.com/p/41892.html

上一篇: 在mingw中使用msvc lib

下一篇: 从stdcall dll函数返回结构是否安全?