COM Programming

From D Wiki
Jump to: navigation, search

COM interfaces are all derived from

Many Windows API interfaces are in terms of COM (Common Object Model) objects (also called OLE or ActiveX objects). A COM object is an object who's first field is a pointer to a vtbl[], and the first 3 entries in that vtbl[] are for QueryInterface(), AddRef(), and Release().

For understanding COM, Kraig Brockshmidt's Inside OLE is an indispensible resource.


To create objects in D that can be consumed by COM applications the following should be used.

COM objects are analogous to D interfaces. Any COM object can be expressed as a D interface, and every D object with an interface X can be exposed as a COM object X. This means that D is compatible with COM objects implemented in other languages.

While not strictly necessary, the Phobos library provides an Object useful as a super class for all D COM objects, called ComObject. ComObject provides a default implementation for QueryInterface(), AddRef(), and Release().

Windows COM objects use the Windows calling convention, which is not the default for D, so COM functions need to have the attribute extern (Windows).

So, to write a COM object:

class MyCOMobject : ComObject
    extern (Windows):

The sample code includes an example COM client program and server DLL.


To use COM objects within D programs the following can be used.

void main()
    auto hr = CoInitialize(null);
    auto CLSID_DOMDocument60 =  GUID(0x88d96a05,0xf192,0x11d4,[0xa6,0x5f,0x00,0x40,0x96,0x32,0x51,0xe5]);
    auto iid = IID_IUnknown;
    void* pUnk;
    hr = CoCreateInstance(&CLSID_DOMDocument60, null, CLSCTX_ALL, &iid, &pUnk);
    if (FAILED(hr))
            throw new Exception(...);