Skip to content

Instantly share code, notes, and snippets.

@bstaint

bstaint/COM.ahk Secret

Created December 19, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstaint/85de29d37efaa3669e222e436120d503 to your computer and use it in GitHub Desktop.
Save bstaint/85de29d37efaa3669e222e436120d503 to your computer and use it in GitHub Desktop.
GetDllObject(dllPath, clsID, IID="{00000000-0000-0000-C000-000000000046}")
{
GUID(sbinClassId, clsID)
GUID(sbinIId, IID)
objPtr := COM_CreateInstanceFromDll(dllPath, sbinClassId, sbinIId)
obj := ComObject(9, objPtr, 1), ObjAddRef(objPtr)
Return obj
}
; 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment