COM/OLE advanced questions

evilrat evilrat666 at gmail.com
Fri Nov 3 02:30:59 UTC 2017


You'd better read some more authorative source since my 
experience is very limited on that matter, but here is some quick 
notes

On Thursday, 2 November 2017 at 14:22:56 UTC, Guillaume Piolat 
wrote:
>
> Question 1. Is it mandatory to inherit from 
> core.sys.windows.unknwn.IUnknown, or just having an interface 
> named "IUnknown" validate it for being a COM interface?
>
>    If yes, then how am I supposed to use COM interfaces in 
> other OSes? core.sys.windows.unknwn.IUnknown is defined under 
> version(Windows).
>
>    I wonder what the exact compiler hook is.
>

You can't(or maybe you can but that is not a true COM). COM is 
Windows tech. It is backed up by OLE or whatever server it is.

My guess it will work fine when derive from any extern(Windows) 
interface(that's right, not class) that has base 3 
methods(AddRef, Release, QueryInterface)

It is also tied to a Windows registry, since to be able to create 
COM objects using CoCreateInstance its class factory has to be 
registered with class id (CLSID), then IIRC, for example in C# 
creating a COM object instance(using new) makes corresponding 
lookup by GUID to retrieve CLSID and CoCreateInstance calls that 
factory to make an object.
But you don't have to use COM mechanincs on your COM enabled 
classes inside your code.

Now there can be a problem, say for example you want to make a 
COM class in D for external usage, most likely you will have to 
make interface first, then derive a class for actual 
implementation, because class will add stuff to vtable and break 
order. (just a guess)

>
> Question 2. If this fails, may I emulate COM vtable layout with 
> extern(C++) class? I wonder what the exact differences are 
> anyway between extern(C++) and the special IUnknown.
>

I don't know the exact internals mechanics, but yes, not sure if 
extern(C++) will work though. COM can be used from C, and so COM 
methods is basically a C function that takes object as first 
argument, so on C++ side it is flavored to make a vtable and 
respective method calls(or vice versa, depending how the specific 
class implemented). (like I said, find some better read on this)

Also since it can be implemented in C, and that requires manually 
making COM vtable in there(IIRC with an array), you can just do 
the same in D.

>
> Question 3. It seems I can inherit both from A D object and a 
> COM interface. What will be the choosen layout?
>

like I said in Q2, if your goal is to make COM class accessible 
by other languages then make interface derived from IUnknown(or 
any other COM interface you like) and fill in the methods, but 
implement them in derived from that interface class. (again, just 
a guess, because of vtable shifting) and don't forget, on Windows 
you also must register a class factory (see 
IClassFactory.CreateInstance) to be able to create objects from 
other COM clients.



More information about the Digitalmars-d-learn mailing list