ole.html

来自「perl教程」· HTML 代码 · 共 944 行 · 第 1/5 页

HTML
944 行
字号
<dd>
<pre>
        <span class="variable">$Object</span><span class="operator">-&gt;</span><span class="variable">Invoke</span><span class="operator">(</span><span class="string">'SetProperty'</span><span class="operator">,</span> <span class="variable">@Args</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_spinmessageloop">Win32::OLE-&gt;SpinMessageLoop</a></strong>

<dd>
<p>This class method retrieves all pending messages from the message queue
and dispatches them to their respective window procedures.  Calling this
method is only necessary when not using the COINIT_MULTITHREADED model.
All OLE method calls and property accesses automatically process the
message queue.</p>
</dd>
</li>
<dt><strong><a name="item_uninitialize">Win32::OLE-&gt;Uninitialize</a></strong>

<dd>
<p>The <a href="#item_uninitialize"><code>Uninitialize()</code></a> class method uninitializes the OLE subsystem.  It
also destroys the hidden top level window created by OLE for single
threaded apartments.  All OLE objects will become invalid after this call!
It is possible to call the <a href="#item_initialize"><code>Initialize()</code></a> class method again with a different
apartment model after shutting down OLE with Uninitialize().</p>
</dd>
</li>
<dt><strong><a name="item_withevents">Win32::OLE-&gt;WithEvents(OBJECT[, HANDLER[, INTERFACE]])</a></strong>

<dd>
<p>This class method enables and disables the firing of events by the
specified OBJECT.  If no HANDLER is specified, then events are
disconnected.  For some objects Win32::OLE is not able to
automatically determine the correct event interface.  In this case the
INTERFACE argument must contain either the COCLASS name of the OBJECT
or the name of the event DISPATCH interface.  Please read the <em>Events</em>
section below for detailed explanation of the Win32::OLE event
support.</p>
</dd>
</li>
</dl>
<p>Whenever Perl does not find a method name in the Win32::OLE package it
is automatically used as the name of an OLE method and this method call
is dispatched to the OLE server.</p>
<p>There is one special hack built into the module: If a method or property
name could not be resolved with the OLE object, then the default method
of the object is called with the method name as its first parameter.  So</p>
<pre>
        <span class="keyword">my</span> <span class="variable">$Sheet</span> <span class="operator">=</span> <span class="variable">$Worksheets</span><span class="operator">-&gt;</span><span class="variable">Table1</span><span class="operator">;</span>
        <span class="keyword">or</span>
        <span class="keyword">my</span> <span class="variable">$Sheet</span> <span class="operator">=</span> <span class="variable">$Worksheets</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">Table1</span><span class="operator">}</span><span class="operator">;</span>
</pre>
<p>is resolved as</p>
<pre>
        <span class="keyword">my</span> <span class="variable">$Sheet</span> <span class="operator">=</span> <span class="variable">$Worksheet</span><span class="operator">-&gt;</span><span class="variable">Item</span><span class="operator">(</span><span class="string">'Table1'</span><span class="operator">);</span>
</pre>
<p>provided that the $Worksheets object doesnot have a <code>Table1</code> method
or property.  This hack has been introduced to call the default method
of collections which did not name the method in their type library.  The
recommended way to call the &quot;unnamed&quot; default method is:</p>
<pre>
        <span class="keyword">my</span> <span class="variable">$Sheet</span> <span class="operator">=</span> <span class="variable">$Worksheets</span><span class="operator">-&gt;</span><span class="variable">Invoke</span><span class="operator">(</span><span class="string">''</span><span class="operator">,</span> <span class="string">'Table1'</span><span class="operator">);</span>
</pre>
<p>This special hack is disabled under <code>use strict 'subs';</code>.</p>
<p>
</p>
<h2><a name="object_methods_and_properties">Object methods and properties</a></h2>
<p>The object returned by the <a href="#item_new"><code>new()</code></a> method can be used to invoke
methods or retrieve properties in the same fashion as described
in the documentation for the particular OLE class (eg. Microsoft
Excel documentation describes the object hierarchy along with the
properties and methods exposed for OLE access).</p>
<p>Optional parameters on method calls can be omitted by using <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>
as a placeholder.  A better way is to use named arguments, as the
order of optional parameters may change in later versions of the OLE
server application.  Named parameters can be specified in a reference
to a hash as the last parameter to a method call.</p>
<p>Properties can be retrieved or set using hash syntax, while methods
can be invoked with the usual perl method call syntax.  The <a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys</code></a>
and <a href="../../lib/Pod/perlfunc.html#item_each"><code>each</code></a> functions can be used to enumerate an object's properties.
Beware that a property is not always writable or even readable (sometimes
raising exceptions when read while being undefined).</p>
<p>If a method or property returns an embedded OLE object, method
and property access can be chained as shown in the examples below.</p>
<p>
</p>
<h2><a name="functions">Functions</a></h2>
<p>The following functions are not exported by default.</p>
<dl>
<dt><strong><a name="item_hresult"><code>HRESULT(ERROR)</code></a></strong>

<dd>
<p>The <a href="#item_hresult"><code>HRESULT()</code></a> function converts an unsigned number into a signed HRESULT
error value as used by OLE internally.  This is necessary because Perl
treats all hexadecimal constants as unsigned.  To check if the last OLE
function returned &quot;Member not found&quot; (0x80020003) you can write:</p>
</dd>
<dd>
<pre>
        <span class="keyword">if</span> <span class="operator">(</span><span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">LastError</span> <span class="operator">==</span> <span class="variable">HRESULT</span><span class="operator">(</span><span class="number">0x80020003</span><span class="operator">))</span> <span class="operator">{</span>
            <span class="comment"># your error recovery here</span>
        <span class="operator">}</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_in"><code>in(COLLECTION)</code></a></strong>

<dd>
<p>If COLLECTION is an OLE collection object then <code>in $COLLECTION</code>
returns a list of all members of the collection.  This is a shortcut
for <code>Win32::OLE::Enum-</code>All($COLLECTION)&gt;.  It is most commonly used in
a <code>foreach</code> loop:</p>
</dd>
<dd>
<pre>
        <span class="keyword">foreach</span> <span class="keyword">my</span> <span class="variable">$value</span> <span class="operator">(</span><span class="variable">in</span> <span class="variable">$collection</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="comment"># do something with $value here</span>
        <span class="operator">}</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_valof"><code>valof(OBJECT)</code></a></strong>

<dd>
<p>Normal assignment of Perl OLE objects creates just another reference
to the OLE object.  The <a href="#item_valof"><code>valof()</code></a> function explictly dereferences the
object (through the default method) and returns the value of the object.</p>
</dd>
<dd>
<pre>
        <span class="keyword">my</span> <span class="variable">$RefOf</span> <span class="operator">=</span> <span class="variable">$Object</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$ValOf</span> <span class="operator">=</span> <span class="variable">valof</span> <span class="variable">$Object</span><span class="operator">;</span>
        <span class="variable">$Object</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">Value</span><span class="operator">}</span> <span class="operator">=</span> <span class="variable">$NewValue</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Now $ValOf still contains the old value wheras $RefOf would
resolve to the $NewValue because it is still a reference to
$Object.</p>
</dd>
<dd>
<p>The <a href="#item_valof"><code>valof()</code></a> function can also be used to convert Win32::OLE::Variant
objects to Perl values.</p>
</dd>
</li>
<dt><strong><a name="item_with">with(OBJECT, PROPERTYNAME =&gt; VALUE, ...)</a></strong>

<dd>
<p>This function provides a concise way to set the values of multiple
properties of an object.  It iterates over its arguments doing
<code>$OBJECT-</code>{PROPERTYNAME} = $VALUE&gt; on each trailing pair.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="overloading">Overloading</a></h2>
<p>The Win32::OLE objects can be overloaded to automatically convert to
their values whenever they are used in a bool, numeric or string
context.  This is not enabled by default.  You have to request it
through the OVERLOAD pseudoexport:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Win32::OLE</span> <span class="string">qw(in valof with OVERLOAD)</span><span class="operator">;</span>
</pre>
<p>You can still get the original string representation of an object
(<code>Win32::OLE=0xDEADBEEF</code>), e.g. for debugging, by using the
<code>overload::StrVal()</code> method:</p>
<pre>
        <span class="keyword">print</span> <span class="variable">overload::StrVal</span><span class="operator">(</span><span class="variable">$object</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>Please note that <code>OVERLOAD</code> is a global setting.  If any module enables
Win32::OLE overloading then it's active everywhere.</p>
<p>
</p>
<h2><a name="events">Events</a></h2>
<p>The Win32::OLE module now contains <strong>ALPHA</strong> level event support.  This
support is only available when Perl is running in a single threaded
apartment.  This can most easily be assured by using the <code>EVENTS</code>
pseudo-import:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Win32::OLE</span> <span class="string">qw(EVENTS)</span><span class="operator">;</span>
</pre>
<p>which implicitly does something like:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Win32::OLE</span><span class="operator">;</span>
        <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">Initialize</span><span class="operator">(</span><span class="variable">Win32::OLE::COINIT_OLEINITIALIZE</span><span class="operator">);</span>
</pre>
<p>The current interface to OLE events should be considered experimental
and is subject to change.  It works as expected for normal OLE
applications, but OLE control events often don't seem to work yet.</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?