Accessing COM Objects

John via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 13 00:40:09 PDT 2016


On Monday, 13 June 2016 at 01:22:33 UTC, Incognito wrote:
> I've been reading over D's com and can't find anything useful. 
> It seems there are different ways:
>
> http://www.lunesu.com/uploads/ModernCOMProgramminginD.pdf
>
> which is of no help and requires an idl file, which I don't 
> have.
>
> Then theres this
>
> http://wiki.dlang.org/COM_Programming
>
> which is also of no help:
>
> import std.stdio;
>
> import std.stdio, core.sys.windows.com, 
> core.sys.windows.windows, std.exception, std.meta, std.traits;
> import std.utf, core.stdc.stdlib, core.sys.windows.objidl, 
> core.sys.windows.ole2;
> pragma(lib, "ole32.lib");
>
>
> GUID Guid(string str)()
> {
>     static assert(str.length==36, "Guid string must be 36 chars 
> long");
>     enum GUIDstring = "GUID(0x" ~ str[0..8] ~ ", 0x" ~ 
> str[9..13] ~ ", 0x" ~ str[14..18] ~
>         ", [0x" ~ str[19..21] ~ ", 0x" ~ str[21..23] ~ ", 0x" ~ 
> str[24..26] ~ ", 0x" ~ str[26..28]
>         ~ ", 0x" ~ str[28..30] ~ ", 0x" ~ str[30..32] ~ ", 0x" 
> ~ str[32..34] ~ ", 0x" ~ str[34..36] ~ "])";
>     return mixin(GUIDstring);
> }
>
> int main(string[] argv)
> {
>
> 	// Adobe Photoshop App 9.0 CLSID 
> {c09f153e-dff7-4eff-a570-af82c1a5a2a8}
> 	// Adobe Photoshop App 9.1 CLSID 
> {6DECC242-87EF-11cf-86B4-444553540000}
>
> 	auto CLSID_DOMDocument60 = 
> Guid!("6DECC242-87EF-11cf-86B4-444553540000");
> 	auto iid = IID_IUnknown;
>
> 	void* pUnk;
> 	auto hr = CoCreateInstance(&CLSID_DOMDocument60, null, 
> CLSCTX_ALL, &iid, &pUnk);
> 	if (FAILED(hr))
> 		throw new Exception("Error!");
>
>     writeln("Hello D-World!");
>     return 0;
> }
>
> Maybe my CLSID's are wrong. Got them from the registry. The 
> exception triggers each time. Even if it worked, I wouldn't 
> know how to use it.
>
> I can do this stuff in C# by simply dragging and dropping a dll 
> into the references and it works fine but is a bit slow. I was 
> hoping I could speed things up using D but it seems like COM 
> isn't really supported, despite what several references say.

COM is supported in D. The difference is that C# hides all the 
plumbing behind nice classes.

You're going to need Photoshop's COM interface to do anything 
with it. If you don't have a C header that you can translate into 
D, you could use a tool included in the Windows SDK called 
OleView. It will peek inside COM  libraries and give you the 
interface definitions.

As to why CoCreateInstance isn't working, make sure you're 
calling CoInitialize or CoInitializeEx beforehand. If it's still 
failing, use std.windows.syserror.sysErrorString(hr) to see if it 
gives you a reason. Otherwise, CLSCTX_ALL might be the culprit - 
try using CLSCTX_SERVER instead.


More information about the Digitalmars-d-learn mailing list