First of all, let me congratulate you on a very well-asked question. It was an admiration, this time, to get all the code needed to reproduce the problem.
The problem is related to the slightly different ABIs used by gcc and Microsoft tools for function return values. For return values ββthat can fit into registers, for example, int return values, there is no difference. But since your structure is too large to fit in one register, and in this situation there are differences between the APIs.
For large return values, the caller passes a hidden function pointer. This hidden pointer is pushed onto the stack by the caller. The function writes the return value to the memory address indicated by this hidden pointer. The difference in ABI is who removes the hidden pointer from the stack. Microsoft tools use ABI, which requires the caller to invoke a hidden pointer, but by default gcc ABI asks the caller to do so.
Now that gcc is almost infinitely configurable, there is a switch that allows you to control the ABI. And you can use gcc the same rules as Microsoft tools. This requires the callee_pop_aggregate_return attribute.
Change your code to C as follows:
__declspec(dllexport) SampleStruct __cdecl SampleMethod(void) __attribute__((callee_pop_aggregate_return(0)));
David heffernan
source share