Help with COM_CreateInstanceFromDll
Posted: 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)
}
(File download: HtmlToImage.7z)