ComObjActive() [AHK_L 53+]

Retrieves a running object that has been registered with OLE.

ComObject := ComObjActive(CLSID)

Creates an object representing a typed value to be passed as a parameter or return value.

ParamObj := ComObject(VarType, Value , Flags)

Deprecated: The usages shown below are deprecated and may be altered or unavailable in a future release.

Creates an object which may be used in place of an optional parameter's default value when calling a method of a COM object. [v1.1.12+]: This function is obsolete. Instead, simply write two consecutive commas, as in Obj.Method(1,,3)

ParamObj := ComObjMissing()

Wraps or unwraps a raw IDispatch pointer in a usable object and automatically calls AddRef.

ComObject := ComObjEnwrap(DispPtr)
DispPtr := ComObjUnwrap(ComObject)

To write more future-proof code, use the following instead:

ComObject := ComObject(9, DispPtr, 1), ObjAddRef(DispPtr)
DispPtr := ComObjValue(ComObject), ObjAddRef(DispPtr)



CLSID or human-readable Prog ID of the COM object to retrieve.


COM object usable with object syntax.


An integer indicating the type of value. See ComObjType() for a list of types.


The value to wrap. Currently only integer and pointer values are supported.


Flags affecting the behaviour of this function and the wrapper object; see below.


Raw IDispatch pointer.


Flag Effect

Default behaviour. AddRef is called automatically for IUnknown and IDispatch pointers, so the caller should use ObjRelease() to release their copy of the pointer if appropriate.

As the default behaviour may be changing in a future release, it is recommended to always set Flags to 1 when wrapping an interface pointer, and call ObjAddRef() if needed.

1 Take ownership of an IUnknown, IDispatch or SAFEARRAY pointer. AddRef is not called. If the wrapper object contains a SAFEARRAY (excluding VT_BYREF), SafeArrayDestroy is called automatically when the wrapper object is freed.

ByRef [v1.1.17+]

If a wrapper object's VarType includes the VT_BYREF (0x4000) flag, empty brackets [] can be used to read or write the referenced value.

When creating a reference, Value must be the memory address of a variable or buffer with sufficient capacity to store a value of the given type. For example, the following can be used to create a variable which a VBScript function can write into:

VarSetCapacity(var, 24, 0)
vref := ComObject(0x400C, &var)  ; 0x400C is a combination of VT_BYREF and VT_VARIANT.

vref[] := "in value"
sc.Run("Example", vref)  ; sc should be initialized as in the example below.
MsgBox % vref[]

Note that although any previous value is freed when a new value is assigned by vref[] or the COM method, the final value is not freed automatically. Freeing the value requires knowing which type it is. Because it is VT_VARIANT in this case, it can be freed by calling VariantClear with DllCall or by using a simpler method: assign an integer, such as vref[] := 0.

General Remarks

In current versions, any function-call beginning with "ComObj" that does not match one of the other COM functions actually calls ComObjActive. For example, ComObjEnwrap(DispPtr) and ComObjActive(DispPtr) are both equivalent to ComObject(DispPtr) (VarType 9 is implied). However, this behaviour will be unavailable in a future release, so it is best to use only ComObject() and ComObjActive() as shown on this page.

If ComObjActive cannot retrieve an active object, it may throw an exception, exit the script or return an empty string, depending on the current ComObjError() setting and other factors.

When this function is used to wrap or retrieve an IDispatch or IUnknown interface pointer, the default behaviour is to increment the COM object's reference count. Therefore, the interface pointer must be manually released when it is no longer needed. When the wrapper object is freed, the reference it contains is automatically released.

Known limitation: Each time a COM object is wrapped, a new wrapper object is created. Comparisons and assignments such as if (obj1 == obj2) and array[obj1] := value treat the two wrapper objects as unique, even though they contain the same COM object.

ComObjCreate(), ComObjGet(), ComObjConnect(), ComObjError(), ComObjFlags(), ObjAddRef() / ObjRelease(), ComObjQuery(), GetActiveObject (MSDN)


ComObjUnwrap: See ComObjConnect().

Passes a VARIANT ByRef to a COM function.

; Preamble - ScriptControl requires a 32-bit version of AutoHotkey.
code =
Sub Example(Var)
    MsgBox Var
    Var = "out value!"
End Sub
sc := ComObjCreate("ScriptControl"), sc.Language := "VBScript", sc.AddCode(code)

; Example: Pass a VARIANT ByRef to a COM function.
var := ComVar()
var[] := "in value"  ; Use [] to assign a value.
sc.Run("Example", var.ref)  ; Pass the VT_BYREF ComObject (.ref).
MsgBox % var[]  ; Use [] to retrieve a value.

; The same thing again, but more direct:
VarSetCapacity(variant_buf, 24, 0)  ; Make a buffer big enough for a VARIANT.
var := ComObject(0x400C, &variant_buf)  ; Make a reference to a VARIANT.
var[] := "in value"
sc.Run("Example", var)  ; Pass the VT_BYREF ComObject itself, no [] or .ref.
MsgBox % var[]
; If a VARIANT contains a string or object, it must be explicitly freed
; by calling VariantClear or assigning a pure numeric value:
var[] := 0

; ComVar: Creates an object which can be used to pass a value ByRef.
;   ComVar[] retrieves the value.
;   ComVar[] := Val sets the value.
;   ComVar.ref retrieves a ByRef object for passing to a COM function.
ComVar(Type := 0xC)
    static base := { __Get: Func("ComVarGet"), __Set: Func("ComVarSet")
        , __Delete: Func("ComVarDel") } ; For base, see Custom Objects.
    ; Create a new object based on base.
    cv := {base: base}
    ; Allocate memory for a VARIANT to hold our value. VARIANT is used even
    ; when Type != VT_VARIANT so that VariantClear can be used by __delete.
    cv.SetCapacity("buf", 24), ptr := cv.GetAddress("buf")
    NumPut(0, NumPut(0, ptr+0, "int64"), "int64")
    if (Type != 0xC) { ; Not VT_VARIANT.
        NumPut(Type, ptr+0, "ushort") ; Set the variant type for __delete.
        ptr += 8 ; Point to the actual value.
    ; Create an object which can be used to pass the variable ByRef.
    cv.ref := ComObject(0x4000|Type, ptr)
    return cv

ComVarGet(cv, p*) { ; Called when script accesses an unknown field.
    if p.MaxIndex() = "" ; No name/parameters, i.e. cv[]
        return cv.ref[]

ComVarSet(cv, v, p*) { ; Called when script sets an unknown field.
    if p.MaxIndex() = "" ; No name/parameters, i.e. cv[]:=v
        return cv.ref[] := v

ComVarDel(cv) { ; Called when the object is being freed.
    ; Depending on type, this may be needed to free the value, if set.
    DllCall("oleaut32\VariantClear", "ptr", cv.GetAddress("buf"))