PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/commands/ComObjActive.htm

https://github.com/Lexikos/AutoHotkey_L-Docs
HTML | 176 lines | 144 code | 32 blank | 0 comment | 0 complexity | 54459a69cd6f3e35a64bf4ae0157cbca MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>ComObjActive() - Syntax &amp; Usage | AutoHotkey</title>
  5. <meta name="description" content="The ComObjActive function retrieves a running object that has been registered with OLE." />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <link href="../static/theme.css" rel="stylesheet" type="text/css" />
  9. <script src="../static/content.js" type="text/javascript"></script>
  10. </head>
  11. <body>
  12. <h1>ComObjActive() <span class="ver">[AHK_L 53+]</span></h1>
  13. <p>Retrieves a running object that has been registered with OLE.</p>
  14. <pre class="Syntax">ComObject := <span class="func">ComObjActive</span>(CLSID)</pre>
  15. <p id="param">Creates an object representing a typed value to be passed as a parameter or return value.</p>
  16. <pre class="Syntax">ParamObj := <span class="func">ComObject</span>(VarType, Value <span class="optional">, Flags</span>)</pre>
  17. <hr>
  18. <p class="warning"><strong>Deprecated:</strong> The usages shown below are deprecated and may be altered or unavailable in a future release.</p>
  19. <p id="missing">Creates an object which may be used in place of an optional parameter's default value when calling a method of a COM object. <span class="ver">[v1.1.12+]</span>: This function is obsolete. Instead, simply write two consecutive commas, as in <code>Obj.Method(1,,3)</code></p>
  20. <pre class="Syntax">ParamObj := <span class="func">ComObjMissing</span>()</pre>
  21. <p id="enwrap">Wraps or unwraps a raw <a href="http://msdn.microsoft.com/en-us/library/dd318520.aspx">IDispatch</a> pointer in a usable object and automatically calls AddRef.</p>
  22. <pre class="Syntax">
  23. ComObject := <span class="func">ComObjEnwrap</span>(DispPtr)
  24. DispPtr := <span class="func">ComObjUnwrap</span>(ComObject)
  25. </pre>
  26. <p>To write more future-proof code, use the following instead:</p>
  27. <pre>ComObject := ComObject(9, DispPtr, 1), ObjAddRef(DispPtr)
  28. DispPtr := <a href="ComObjValue.htm">ComObjValue</a>(ComObject), ObjAddRef(DispPtr)</pre>
  29. <hr>
  30. <h2 id="Parameters">Parameters</h2>
  31. <dl>
  32. <dt>CLSID</dt>
  33. <dd><p>CLSID or human-readable Prog ID of the COM object to retrieve.</p></dd>
  34. <dt>ComObject</dt>
  35. <dd><p>COM object usable with <a href="../Objects.htm#Usage_Objects">object syntax</a>.</p></dd>
  36. <dt>VarType</dt>
  37. <dd><p>An integer indicating the type of value. See <a href="ComObjType.htm#vt">ComObjType()</a> for a list of types.</p></dd>
  38. <dt>Value</dt>
  39. <dd><p>The value to wrap. Currently only integer and pointer values are supported.</p></dd>
  40. <dt>Flags</dt>
  41. <dd><p>Flags affecting the behaviour of this function and the wrapper object; see below.</p></dd>
  42. <dt>DispPtr</dt>
  43. <dd><p>Raw IDispatch pointer.</p></dd>
  44. </dl>
  45. <h2 id="Flags">Flags</h2>
  46. <table class="info">
  47. <tr>
  48. <th class="center">Flag</th>
  49. <th>Effect</th>
  50. </tr>
  51. <tr>
  52. <td class="center">0</td>
  53. <td>
  54. <p>Default behaviour. <a href="http://msdn.microsoft.com/en-us/library/ms691379.aspx">AddRef</a> is called automatically for IUnknown and IDispatch pointers, so the caller should use <a href="ObjAddRef.htm">ObjRelease()</a> to release their copy of the pointer if appropriate.</p>
  55. <p>As the default behaviour may be changing in a future release, it is recommended to always set <em>Flags</em> to <code>1</code> when wrapping an interface pointer, and call <a href="ObjAddRef.htm">ObjAddRef()</a> if needed.</p>
  56. </td>
  57. </tr>
  58. <tr>
  59. <td class="center">1</td>
  60. <td>Take ownership of an IUnknown, IDispatch or SAFEARRAY pointer. AddRef is not called. If the wrapper object contains a SAFEARRAY (excluding VT_BYREF), <a href="https://msdn.microsoft.com/en-us/library/ms221702(v=vs.85).aspx">SafeArrayDestroy</a> is called automatically when the wrapper object is freed.</td>
  61. </tr>
  62. </table>
  63. <h2 id="ByRef">ByRef <span class="ver">[v1.1.17+]</span></h2>
  64. <p>If a wrapper object's <a href="ComObjType.htm"><em>VarType</em></a> includes the VT_BYREF (0x4000) flag, empty brackets <code>[]</code> can be used to read or write the referenced value.</p>
  65. <p>When creating a reference, <em>Value</em> 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:</p>
  66. <pre>VarSetCapacity(var, 24, 0)
  67. vref := ComObject(0x400C, &amp;var) <em>; 0x400C is a combination of VT_BYREF and VT_VARIANT.</em>
  68. vref[] := "in value"
  69. sc.Run("Example", vref) <em>; sc should be initialized as in the <a href="#ByRefEx">example below</a>.</em>
  70. MsgBox % vref[]</pre>
  71. <p>Note that although any previous value is freed when a new value is assigned by <code>vref[]</code> 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 <a href="https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-variantclear">VariantClear</a> with <a href="DllCall.htm">DllCall</a> or by using a simpler method: assign an integer, such as <code>vref[] := 0</code>.</p>
  72. <h2 id="Remarks">General Remarks</h2>
  73. <p>In current versions, any function-call beginning with "ComObj" that does not match one of the other COM functions actually calls ComObjActive. For example, <code>ComObjEnwrap(DispPtr)</code> and <code>ComObjActive(DispPtr)</code> are both equivalent to <code>ComObject(DispPtr)</code> (<em>VarType</em> 9 is implied). However, this behaviour will be unavailable in a future release, so it is best to use only <code>ComObject()</code> and <code>ComObjActive()</code> as shown on this page.</p>
  74. <p>If ComObjActive cannot retrieve an active object, it may throw an exception, exit the script or return an empty string, depending on the current <a href="ComObjError.htm">ComObjError()</a> setting and <a href="ComObjError.htm#factors">other factors</a>.</p>
  75. <p>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 <a href="ObjAddRef.htm">manually released</a> when it is no longer needed. When the wrapper object is freed, the reference it contains is automatically released.</p>
  76. <p><b>Known limitation:</b> Each time a COM object is wrapped, a new wrapper object is created. Comparisons and assignments such as <code>if (obj1 == obj2)</code> and <code>array[obj1] := value</code> treat the two wrapper objects as unique, even though they contain the same COM object.</p>
  77. <h2 id="Related">Related</h2>
  78. <p><a href="ComObjCreate.htm">ComObjCreate()</a>, <a href="ComObjGet.htm">ComObjGet()</a>, <a href="ComObjConnect.htm">ComObjConnect()</a>, <a href="ComObjError.htm">ComObjError()</a>, <a href="ComObjFlags.htm">ComObjFlags()</a>, <a href="ObjAddRef.htm">ObjAddRef() / ObjRelease()</a>, <a href="ComObjQuery.htm">ComObjQuery()</a>, <a href="http://msdn.microsoft.com/en-us/library/ms221467.aspx">GetActiveObject (MSDN)</a></p>
  79. <h2 id="Examples">Examples</h2>
  80. <p>ComObjUnwrap: See <a href="ComObjConnect.htm#Examples">ComObjConnect()</a>.</p>
  81. <div class="ex" id="ByRefEx">
  82. <p><a class="ex_number" href="#ByRefEx"></a> Passes a VARIANT ByRef to a COM function.</p>
  83. <pre>
  84. <em>; Preamble - ScriptControl requires a 32-bit version of AutoHotkey.</em>
  85. code =
  86. (
  87. Sub Example(Var)
  88. MsgBox Var
  89. Var = "out value!"
  90. End Sub
  91. )
  92. sc := <a href="ComObjCreate.htm">ComObjCreate</a>("ScriptControl"), sc.Language := "VBScript", sc.AddCode(code)
  93. <em>; Example: Pass a VARIANT ByRef to a COM function.</em>
  94. var := ComVar()
  95. var[] := "in value" <em>; Use [] to assign a value.</em>
  96. sc.Run("Example", var.ref) <em>; Pass the VT_BYREF ComObject (.ref).</em>
  97. MsgBox % var[] <em>; Use [] to retrieve a value.</em>
  98. <em>; The same thing again, but more direct:</em>
  99. VarSetCapacity(variant_buf, 24, 0) <em>; Make a buffer big enough for a VARIANT.</em>
  100. var := ComObject(0x400C, &amp;variant_buf) <em>; Make a reference to a VARIANT.</em>
  101. var[] := "in value"
  102. sc.Run("Example", var) <em>; Pass the VT_BYREF ComObject itself, no [] or .ref.</em>
  103. MsgBox % var[]
  104. <em>; If a VARIANT contains a string or object, it must be explicitly freed
  105. ; by calling VariantClear or assigning a pure numeric value:</em>
  106. var[] := 0
  107. <em>; ComVar: Creates an object which can be used to pass a value ByRef.
  108. ; ComVar[] retrieves the value.
  109. ; ComVar[] := Val sets the value.
  110. ; ComVar.ref retrieves a ByRef object for passing to a COM function.</em>
  111. ComVar(Type := 0xC)
  112. {
  113. static base := { __Get: Func("ComVarGet"), __Set: Func("ComVarSet")
  114. , __Delete: Func("ComVarDel") } <em>; For base, see Custom Objects.
  115. ; Create a new object based on base.</em>
  116. cv := {base: base}
  117. <em>; Allocate memory for a VARIANT to hold our value. VARIANT is used even
  118. ; when Type != VT_VARIANT so that VariantClear can be used by __delete.</em>
  119. cv.SetCapacity("buf", 24), ptr := cv.GetAddress("buf")
  120. NumPut(0, NumPut(0, ptr+0, "int64"), "int64")
  121. if (Type != 0xC) { <em>; Not VT_VARIANT.</em>
  122. NumPut(Type, ptr+0, "ushort") <em>; Set the variant type for __delete.</em>
  123. ptr += 8 <em>; Point to the actual value.</em>
  124. }
  125. <em>; Create an object which can be used to pass the variable ByRef.</em>
  126. cv.ref := ComObject(0x4000|Type, ptr)
  127. return cv
  128. }
  129. ComVarGet(cv, p*) { <em>; Called when script accesses an unknown field.</em>
  130. if p.MaxIndex() = "" <em>; No name/parameters, i.e. cv[]</em>
  131. return cv.ref[]
  132. }
  133. ComVarSet(cv, v, p*) { <em>; Called when script sets an unknown field.</em>
  134. if p.MaxIndex() = "" <em>; No name/parameters, i.e. cv[]:=v</em>
  135. return cv.ref[] := v
  136. }
  137. ComVarDel(cv) { <em>; Called when the object is being freed.
  138. ; Depending on type, this may be needed to free the value, if set.</em>
  139. DllCall("oleaut32\VariantClear", "ptr", cv.GetAddress("buf"))
  140. }
  141. </pre>
  142. </div>
  143. </body>
  144. </html>