Difference between revisions of "COM Programming"
(→Importing) |
(→Importing) |
||
Line 31: | Line 31: | ||
==Importing== | ==Importing== | ||
+ | |||
+ | To use COM objects within D programs the following can be used. | ||
<syntaxhighlight lang="D"> | <syntaxhighlight lang="D"> |
Revision as of 16:17, 14 May 2016
COM interfaces are all derived from std.c.windows.com.IUnknown.
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.
Exporting
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:
import std.c.windows.com;
class MyCOMobject : ComObject
{
extern (Windows):
...
}
The sample code includes an example COM client program and server DLL.
Importing
To use COM objects within D programs the following can be used.
import core.sys.windows.com;
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(...);
...
}