Originale-mail to me for new edition

 

Accessing Automation objects (Windows only)

 

Use variants to access Automation objects. When a variant references an Automation object, you can call the object’s methods and read or write to its properties through the variant. To do this, you must include ComObj in the uses clause of one of your units or your program or library.

Automation object method calls are bound at runtime and require no previous method declarations. The validity of these calls is not checked at compile time.

The following example illustrates Automation method calls. The CreateOleObject function (defined in ComObj) returns an IDispatch reference to an Automation object and is assignment-compatible with the variant Word.

var

  Word: Variant;

begin

  Word := CreateOleObject('Word.Basic');

  Word.FileNew('Normal');

  Word.Insert('This is the first line'#13);

  Word.Insert('This is the second line'#13);

  Word.FileSaveAs('c:\temp\test.txt', 3);

end;

You can pass interface-type parameters to Automation methods.

Variant arrays with an element type of varByte are the preferred method of passing binary data between Automation controllers and servers. Such arrays are subject to no translation of their data, and can be efficiently accessed using the VarArrayLock and VarArrayUnlock routines.

 

Automation object method-call syntax

The syntax of an Automation object method call or property access is similar to that of a normal method call or property access. Automation method calls, however, can use both positional and named parameters. (But some Automation servers do not support named parameters.)

A positional parameter is simply an expression. A named parameter consists of a parameter identifier, followed by the := symbol, followed by an expression. Positional parameters must precede any named parameters in a method call. Named parameters can be specified in any order.

Some Automation servers allow you to omit parameters from a method call, accepting their default values. For example,

Word.FileSaveAs('test.doc');

Word.FileSaveAs('test.doc', 6);

Word.FileSaveAs('test.doc',,,'secret');

Word.FileSaveAs('test.doc', Password := 'secret');

Word.FileSaveAs(Password := 'secret', Name := 'test.doc');

Automation method call parameters can be of integer, real, string, Boolean, and variant types. A parameter is passed by reference if the parameter expression consists only of a variable reference, and if the variable reference is of type Byte, Smallint, Integer, Single, Double, Currency, TDateTime, AnsiString, WordBool, or Variant. If the expression is not of one of these types, or if it is not just a variable, the parameter is passed by value. Passing a parameter by reference to a method that expects a value parameter causes COM to fetch the value from the reference parameter. Passing a parameter by value to a method that expects a reference parameter causes an error.

 

Topic groups

 

See also

Automation objects (Windows only)

Value and variable parameters

Variant types: Overview

 

 

译文

 

访问自动化对象(仅适用于Windows

 

访问自动化自动化对象需要使用变体。当一个变体引用一个自动化对象时,可以通过该变体调用对象的方法以及读或写其属性。要这样做,则必须在单元、程序或库的uses子句中包括ComObj单元。

自动化对象方法调用在运行时被加载而不需要任何既有的方法声明。编译时不检查这些调用的有效性。

下面的例子说明了自动化对象的调用。CreateOldObject函数(在ComObj单元中定义)返回对自动化对象的一个IDispatch引用,而且该引用对变体Word赋值兼容。

var

  Word: Variant;

begin

  Word := CreateOleObject('Word.Basic');

  Word.FileNew('Normal');

  Word.Insert('This is the first line'#13);

  Word.Insert('This is the second line'#13);

  Word.FileSaveAs('c:\temp\test.txt', 3);

end;

可以向自动化方法传递接口类型的参数。

在自动化控制器和服务器之间传递二进制数据,只有一个元素且元素类型为varByte的变体数组是首选的方法。这样的数组不受其数据传输的限制,而且可以通过用VarArrayLockVarArrayUnlock例程被有效访问。

 

自动化对象方法调用语法

自动化对象方法调用或属性访问的语法与一般的方法调用或属性访问相似。然而,自动化方法调用既可以使用位置(positional)参数,也可以使用命名(named)参数。(某些自动化服务器不支持命名参数。)

简单的位置参数如表达式。命名参数由参数标识符、跟随其后的赋值符号(:=)、再随其后的表达式组成。在方法调用中,位置参数必须再命名参数之前。命名参数可以指定为任何顺序。

一些自动化服务器允许在方法调用中忽略参数而接受缺省值。例如,

Word.FileSaveAs('test.doc');

Word.FileSaveAs('test.doc', 6);

Word.FileSaveAs('test.doc',,,'secret');

Word.FileSaveAs('test.doc', Password := 'secret');

Word.FileSaveAs(Password := 'secret', Name := 'test.doc');

自动化方法调用参数可以是整数、实数、串、布尔以及变体类型。如果参数表达式仅由变量引用组成,并且变量引用是ByteSmallintIntegerSingleDoubleCurrencyTDateTimeAnsiStringWordBoolVariant等类型之一,那么参数可以通过该引用传递。如果表达式不是这些类型之一,或者如果只是一个变量,那么参数通过值被传递。通过变量引用传递参数到期待值参数的方法,将使COM对象从引用参数中获取值。通过值传递参数到一个期待引用参数的方法,将导致错误。

 

主题组

 

相关主题

自动化对象(仅适用于Windows

值参数和变量参数

变体类型:概述