Returning structs from COM

evilrat via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 19 04:23:46 PST 2016


On Monday, 19 December 2016 at 09:49:13 UTC, kinke wrote:
> On Saturday, 3 December 2016 at 09:51:00 UTC, John C wrote:
>> Some DirectX methods return structs by value, but when I try 
>> calling them I either get garbage or an access violation.
>>
>> Usually COM methods return structs by pointer as a parameter, 
>> but these are returning the struct as the actual return value, 
>> as in this definition:
>>
>>   extern(Windows):
>>   struct D2D1_SIZE_F { float width, height; }
>>
>>   interface ID2D1Bitmap : ID2D1Image {
>>     D2D1_SIZE_F GetSize();
>>   }
>>
>> If I rewrite GetSize to return by pointer as a parameter, it 
>> appears to work and I get the correct width and height without 
>> an AV being thrown. And I can add a helper method that returns 
>> by value:
>>
>>   interface ID2D1Bitmap : ID2D1Image {
>>     void GetSize(D2D1_SIZE_F* size);
>>
>>     final D2D1_SIZE_F GetSize() {
>>       D2D1_SIZE_F size;
>>       GetSize(&size);
>>       return size;
>>     }
>>   }
>>

yes it works indeed O_o

also align(8) for struct seems to work in x86 but has some issues 
with ESP(reserved 4 bytes for pointer, still has 4 bytes for 
second float?), crashes without align at later point.

x64 still has messed up 'this'(both dmd and ldc, not tested with 
gdc),
but with passing parameter instead of return it really keeps 
stack where it leaves it and even 'this' are in place so 
everything is seems to work.



More information about the Digitalmars-d-learn mailing list