C++ interface vs D and com

Adam Sansier via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 12 19:49:54 PDT 2016


On Wednesday, 13 July 2016 at 02:34:14 UTC, Mike Parker wrote:
> On Tuesday, 12 July 2016 at 23:55:55 UTC, Adam Sansier wrote:
>
>>
>> Ok, Another hack:
>>
>> 		iInterface x;
>> 		void** y = cast(void**)&x;
>> 		*y = malloc(iInterface.sizeof);
>> 		
>> 		x.__vptr = cast(immutable(void*)*)(*ptr);
>> 		x.func();
>>
>> works.
>>
>> x is the object of type iInterface. It has no object 
>> associated with it, basically create one using malloc and set 
>> it's vtable.
>>
>> this avoids the need to create the class.
>
> What happens when you declare an interface that extends from 
> IUnknown (and not extern(C++)), then cast the pointer returned 
> from the COM API? It should just work without needing to muck 
> around with the vtable.

That was what I tried first, It didn't work. I don't know what 
the problem though. I either get an access violation or the 
functions don't do anything.

I think it's more complex because without extern(C++) the vtable 
is in a different place than expected(it's offset by 1), so 
simple casting does not work.

"A COM interface differs from a regular interface in that there 
is no object.Interface entry in vtbl[0]; the entries vtbl[0..$] 
are all the virtual function pointers, in the order that they 
were declared. This matches the COM object layout used by Windows.

A C++ interface differs from a regular interface in that it 
matches the layout of a C++ class using single inheritance on the 
target machine. "



More information about the Digitalmars-d-learn mailing list