Wrong vtable for COM interfaces that don't inherit IUnknown

evilrat evilrat666 at gmail.com
Tue Jul 16 01:38:49 UTC 2019


On Monday, 15 July 2019 at 22:01:25 UTC, KytoDragon wrote:
> I am currently trying to write a XAudio2 backend and have come 
> across the problem, that some of the interfaces for XAudio2's 
> COM objects seem to be missing the first entry in their vtable. 
> After reading the iterface article in the spec 
> (https://dlang.org/spec/interface.html#com-interfaces) it seems 
> that only interfaces inheriting from 
> core.stdc.windows.com.IUnknown (or any interface named 
> "IUnknown") get the COM interface layout instead of the D 
> layout.
>
> What can I do to get the COM layout on interfaces that don't 
> inherit IUnknown?
> Examples: IXAudio2Voice or any of the IXAudio2*Callback 
> interfaces. I have already declared everything extern(Windows), 
> but that does not fix it.

As you can see it is by design. IUnknown has different vtbl[] 
comparing to regular D classes/interfaces. If it works with 
declaring your own empty IUnknown use it, also you can try 
extern(C++) which does similar thing to vtable, it might even 
work!

Another option is to craft your own "vtable" with structs, but 
I'm not going into details about that, you can always google how 
to do it in C and adapt.

And finally you can swap vtable for object instances at runtime, 
this however is the most complicated and error prone solution, I 
for example doesn't fully understand D rules for vtable 
constructing, esp. confusing is that interface entry notice, so 
anything non trivial (such as class inheriting from class 
w/multiple interfaces) is beyond me. Expect heavy AV storm.


Also from what I see MS done this intentionally, means they 
either no longer loves COM or there was some other good reason.


Oh and let me put this here...
Have you seen this? It even has very simple xaudio example.
https://code.dlang.org/packages/directx-d


More information about the Digitalmars-d-learn mailing list