Accessing COM Objects

Joerg Joergonson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 15 14:06:01 PDT 2016


On Wednesday, 15 June 2016 at 19:21:51 UTC, John wrote:
> On Wednesday, 15 June 2016 at 18:32:28 UTC, Joerg Joergonson 
> wrote:
>>>   import core.sys.windows.com, core.sys.windows.oaidl;
>>
>> Thanks. Should these not be added to the generated file?
>
> The problem is that other type libraries will probably require 
> other headers to be imported, and there's no way to work out 
> which, so I've left that up to the user for now.
>
>>
>> Also, could you add to it the following:
>>
>> const static GUID iid = 
>> Guid!("5DE90358-4D0B-4FA1-BA3E-C91BBA863F32");
>>
>> inside the interface (Replace the string with the correct 
>> guid)?
>>
>>
>>
>> This allows it to work with ComPtr which looks for the iid 
>> inside the interface, shouldn't hurt anything.
>
> I could add that as an option.
>
>>
>> In any case, I haven't got ComPtr to work so...
>>
>>
>> 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);
>> }
>>
>>
>> ....
>>
>> also tried CoCreateInstance and getting error 80040154
>>
>> Not sure if it works.
>>
>> ....
>>
>> Changed the GUID to another one found in the registry(not the 
>> one at the top of the generated file) and it works. Both load 
>> photoshop
>
> Oops. The one at the top of the file is the type library's ID, 
> not the class ID. I should just omit it if it causes confusion.
>
>>
>>
>> int main(string[] argv)
>> {
>>
>> 	//auto ps = ComPtr!_Application(CLSID_PS).require;
>> 		
>> 	//const auto CLSID_PS = 
>> Guid!("6DECC242-87EF-11cf-86B4-444553540000"); // PS 90.1 
>> fails because of interface issue
>> 	const auto CLSID_PS = 
>> Guid!("c09f153e-dff7-4eff-a570-af82c1a5a2a8");   // PS 90.0 
>> works.
>> 	
>>
>>
>> 			
>>     auto hr = CoInitialize(null);
>>     auto iid = IID__Application;
>>
>>
>>     _Application* pUnk;
>>
>>     hr = CoCreateInstance(&CLSID_PS, null, CLSCTX_ALL, &iid, 
>> cast(void**)&pUnk);
>>     if (FAILED(hr))
>>             throw new Exception("ASDF");
>>
>> }
>>
>> The photoshop.d file
>> http://www.filedropper.com/photoshop_1
>>
>>
>> So, I guess it works but how to access the methods? The 
>> photoshop file looks to have them listed but they are all 
>> commented out.
>
> They're commented out because Photoshop seems to have only 
> provided a late-binding interface and you have to call them by 
> name through IDispatch.Invoke. It's possible to wrap all that 
> in normal D methods, and I'm working on it, but it won't be 
> ready for a while.


Ok, I've tried things like uncommenting

	Document Open(BSTR Document, VARIANT As, VARIANT AsSmartObject);
	void Load(BSTR Document);

	/*[id(0x70537673)]*/ BSTR get_ScriptingVersion();
   /*[id(0x70464D4D)]*/ double get_FreeMemory();
   /*[id(0x76657273)]*/ BSTR get_Version();
and everything crashes with bad reference.



If I try ComPtr, same thing



	const auto CLSID_PS = 
Guid!("c09f153e-dff7-4eff-a570-af82c1a5a2a8");   // PS 90.0 works.
	
	

			
     auto hr = CoInitialize(null);
     auto iid = IID__Application;

  	auto ps = 
cast(_Application)(ComPtr!_Application(CLSID_PS).require);

     _Application pUnk;
	
     hr = CoCreateInstance(&CLSID_PS, null, CLSCTX_ALL, &iid, 
cast(void**)&pUnk);
     if (FAILED(hr))
             throw new Exception("ASDF");

	auto ptr = cast(wchar*)alloca(wchar.sizeof * 1000);
	
	auto fn = `ps.psd`;
	for(auto i = 0; i < fn.length; i++)
	{
		ptr[i] = fn[i];
	}

	writeln(ps.get_FreeMemory());
	
	pUnk.Load(ptr);



My thinking is that CoCreateinstance is suppose to give us a 
pointer to the interface so we can use it, if all this stuff is 
crashing does that mean the interface is invalid or not being 
assigned properly or is there far more to it than this?



(


More information about the Digitalmars-d-learn mailing list