Help with COM_CreateInstanceFromDll

Get help with using AutoHotkey and its commands and hotkeys
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Help with COM_CreateInstanceFromDll

12 Dec 2015, 02:41

Code: Select all

sDll := "HtmlToImage.dll"
sClsId := "{C5984F55-8CF2-4338-B8F3-B904ABEFCBDE}"

GUID(sbinClassId, sClsId)
GUID(sbinIId, "{00000000-0000-0000-C000-000000000046}") ; IId_IUnknown
pcm := COM_CreateInstanceFromDll(sDll, sbinClassId, sbinIId)
MsgBox, % pcm

obj := ComObject(9, pcm, 1), ObjAddRef(pcm)
obj.ConvertFromCode("<span style='background:#9CF6C0'>test</span>", "output.png", 180)

; https://autohotkey.com/board/topic/20376-invoking-directly-contextmenu-of-files-and-folders/page-3#entry256628
COM_CreateInstanceFromDll(sDll, ByRef sbinClassId, ByRef sbinIId)
{
	static IID_IClassFactory := "{00000001-0000-0000-C000-000000000046}"

	if (!GUID(sbinIID_IClassFactory, IID_IClassFactory))
		return	

	hDll := DllCall("ole32\CoLoadLibrary", "Str", sDll, "Int", 1, "UInt")

	If (ErrorLevel <> 0) or (hDll = 0)
		Return

	iErr := DllCall(sDll . "\DllGetClassObject"
					,"Str" , sbinClassId
					,"Str" , sbinIID_IClassFactory
					,"UInt*", pIFactory
					,"Int")
	If iErr
		Return
	
	iObjPtr := COM_IClassFactory_CreateInstance(pIFactory, 0, sbinIId)
	
	Release (pIFactory)
	
	Return iObjPtr
}


COM_IClassFactory_CreateInstance(ppvIClassFactory, pUnkOuter, ByRef riid)
{
	iErr := DllCall(VTable(ppvIClassFactory, 3), "UInt", ppvIClassFactory
					, "UInt",  pUnkOuter
					, "Str",   riid
					, "Uint*", ppvObject
					, "Int")
	
	If iErr
		Return
	
	Return ppvObject
}

GUID(ByRef GUID, sGUID) ; Converts a string to a binary GUID and returns its address.
{
    VarSetCapacity(GUID, 16, 0)
    return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "ptr", &GUID) >= 0 ? &GUID : ""
}

vtable(ptr, n) {
    ; NumGet(ptr+0) returns the address of the object's virtual function
    ; table (vtable for short). The remainder of the expression retrieves
    ; the address of the nth function's address from the vtable.
    return NumGet(NumGet(ptr+0), n*A_PtrSize)
}
The COM_CreateInstanceFromDll successfully returns an integer address pcm. But how can I convert the address pcm to a COM object? Thanks.
(File download: HtmlToImage.7z)
HotKeyIt
Posts: 2158
Joined: 29 Sep 2013, 18:35
Contact:

Re: Help with COM_CreateInstanceFromDll

13 Dec 2015, 13:38

You have to register the dll first regsvr32 HtmlToImage.dll.
But even after registering your code does not work.
It works fine in AHK_H with ComObjDll (source code)

Code: Select all

hti:=ComObjDll(LoadLibrary("HtmlToImage.dll"),"{C5984F55-8CF2-4338-B8F3-B904ABEFCBDE}")
hti.ConvertFromURL("http:\\www.autohotkey.com","output.jpg",180)
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

Re: Help with COM_CreateInstanceFromDll

13 Dec 2015, 18:46

Hi, I've tested the latest AHK_H v1/v2, in two different computers, all failed. It said the library isn't registered.

Many articles titled with "COM without registering" are using the CreateInstance technology. "without registering" means no need regsvr32 xxx.dll isn't it?
HotKeyIt
Posts: 2158
Joined: 29 Sep 2013, 18:35
Contact:

Re: Help with COM_CreateInstanceFromDll

13 Dec 2015, 22:55

Yes but not for this dll, its implementation seems to be different and part of it requires registration. Probably it is due to Events interface but I am not sure :(

Return to “Ask For Help”

Who is online

Users browsing this forum: Dom_Christo, Google [Bot], guyzyl, mikeyww and 33 guests