ComObjQuery

Queries a COM object for an interface or service.

InterfaceComObj := ComObjQuery(ComObj, SID, IID)
InterfaceComObj := ComObjQuery(ComObj, IID)

Parameters

ComObj

Type: Object or Integer

A COM wrapper object, an interface pointer, or an object with a Ptr property which returns an interface pointer. See ComValue for details.

IID

Type: String

An interface identifier (GUID) in the form "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".

SID

Type: String

A service identifier in the same form as IID.

Return Value

Type: Object

This function returns a COM wrapper object of type dependent on the IID parameter.

IIDClassVariant TypeDescription
IID_IDispatch ComObject VT_DISPATCH (9) Allows the script to call properties and methods of the object using normal object syntax.
Any other IID ComValue VT_UNKNOWN (13) Provides only a Ptr property, which allows the object to be passed to DllCall or ComCall.

Error Handling

An exception is thrown on failure, such as if the interface is not supported.

Remarks

In its two-parameter mode, this function is equivalent to IUnknown::QueryInterface. When SID and IID are both specified, it internally queries for the IServiceProvider interface, then calls IServiceProvider::QueryService.

ComCall can be used to call native interface methods.

ComCall, ComObject, ComObjGet, ComObjActive

Examples

Determines the class name of an object.

obj := ComObject("Scripting.Dictionary")

MsgBox "Interface name: " ComObjType(obj, "name")

IID_IProvideClassInfo := "{B196B283-BAB4-101A-B69C-00AA00341D07}"

; Request the object's IProvideClassInfo interface.
try
    pci := ComObjQuery(obj, IID_IProvideClassInfo)
catch
{
    MsgBox "IProvideClassInfo interface not supported."
    return
}

; Call GetClassInfo to retrieve a pointer to the ITypeInfo interface.
ComCall(3, pci, "ptr*", &ti := 0)

; Wrap ti to ensure automatic cleanup.
ti := ComValue(13, ti)

; Call GetDocumentation to get the object's full type name.
ComCall(12, ti, "int", -1, "ptr*", &pname := 0, "ptr", 0, "ptr", 0, "ptr", 0)

; Convert the BSTR pointer to a usable string.
name := StrGet(pname, "UTF-16")

; Clean up.
DllCall("oleaut32\SysFreeString", "ptr", pname)
pci := ti := ""

; Display the type name!
MsgBox "Class name: " name

Automates an existing Internet Explorer window.

sURL := "https://www.autohotkey.com/boards/"
if WebBrowser := GetWebBrowser()
    WebBrowser.Navigate(sURL)

GetWebBrowser()
{
    ; Get a raw pointer to the document object of the top-most IE window.
    static msg := DllCall("RegisterWindowMessage", "Str", "WM_HTML_GETOBJECT")
    lResult := SendMessage(msg, 0, 0, "Internet Explorer_Server1", "ahk_class IEFrame")
    if !lResult
        return  ; IE not found.
    static IID_IHTMLDocument2 := GUID("{332C4425-26CB-11D0-B483-00C04FD90119}")
    static VT_UNKNOWN := 13
    DllCall("oleacc\ObjectFromLresult", "Ptr", lResult
        , "Ptr", IID_IHTMLDocument2, "Ptr", 0
        , "Ptr*", pdoc := ComValue(VT_UNKNOWN, 0))
    
    ; Query for the WebBrowserApp service. In this particular case,
    ; the SID and IID are the same, but it isn't always this way.
    static IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"
    static SID_SWebBrowserApp := IID_IWebBrowserApp
    pweb := ComObjQuery(pdoc, SID_SWebBrowserApp, IID_IWebBrowserApp)
    
    ; Return the WebBrowser object as IDispatch for usability.
    ; This works only because IWebBrowserApp is derived from IDispatch.
    ; pweb will release its ptr automatically, so AddRef to counter that.
    ObjAddRef(pweb.ptr)
    static VT_DISPATCH := 9
    return ComValue(VT_DISPATCH, pweb.ptr)
}

GUID(sGUID) ; Converts a string to a binary GUID and returns it in a Buffer.
{
    GUID := Buffer(16, 0)
    if DllCall("ole32\CLSIDFromString", "WStr", sGUID, "Ptr", GUID) < 0
        throw ValueError("Invalid parameter #1", -1, sGUID)
    return GUID
}