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.

For a complete D solution with sample code and some wrapping to make working with COM easier, see Adam D Ruppe's COM helper repository. Note that the Phobos Windows headers are now more up to date than when the README for his repository was authored, and it is possible that it may not be necessary to use the version from Andrej Mitrovic that he suggests. Also note that for accessing a COM object from scripting languages, it may be necessary to create a type library using MIDL, which should then be registered with Windows by standard means.


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(...);