开发者

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜