Accessing COM Objects

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 14 23:09:33 PDT 2016


On Monday, 13 June 2016 at 17:38:41 UTC, Incognito wrote:

> Cool. Oleview gives me the idl files. How to convert the idl 
> files to d or possibly c?

There are ready tools idl2d:
https://github.com/dlang/visuald/tree/master/c2d

and tlb2idl:
https://github.com/dlang/visuald/tree/master/tools

I've used this idl2d and it works pretty well (although not 
perfect, sometimes manual editing still required).

Example of real-world DirectShow interfaces translated:
https://gist.github.com/thedeemon/46748f91afdbcf339f55da9b355a6b56

> Would I just use them in place of IUnknown once I have the 
> interface?

If you have the interface defined AND you know its IID, you can 
request it from CoCreateInstance and then use as ordinary D 
object.

You might want to look at this wrapper that takes most of COM 
machinery:
https://gist.github.com/thedeemon/3c2989b76004fafe9aa0

Then you just write almost as in C#, something like

   auto pGraph = ComPtr!IGraphBuilder(CLSID_FilterGraph, 
"pGraph").require;

   ComPtr!ICaptureGraphBuilder2 pBuilder = 
ComPtr!ICaptureGraphBuilder2(CLSID_CaptureGraphBuilder2).require;
   pBuilder.SetFiltergraph(pGraph);
   ...
   auto CLSID_NullRenderer = 
Guid!("C1F400A4-3F08-11D3-9F0B-006008039E37"); //qedit.dll
   auto pNullRendr = ComPtr!IBaseFilter(CLSID_NullRenderer, 
"nulrend");
   pGraph.AddFilter(pNullRendr, "Null Renderer"w.ptr);
   ...
   auto imf = ComPtr!IMediaFilter(pGraph);
   imf.SetSyncSource(null);

All the CreateInstance, QueryInterface, AddRef/Release etc. is 
taken care of. And even HRESULT return codes are automatically 
checked.


More information about the Digitalmars-d-learn mailing list