Dispinterface Keyword
The
dispinterface
keyword is similar to
interface
for declaring an interface type, but it
declares a dispatch interface. Delphi uses a dispatch interface to
access a COM automation server through the Invoke
method of the IDispatch
interface. A class cannot
implement a dispatch interface, nor can a dispatch interface inherit
from another interface (dispatch or normal).
Declarations can specify a dispatch identifier with the
dispid
directive. All the dispatch identifiers
within a single dispinterface
must be unique. No
other method directives are allowed.
Property declarations do not include read
or
write
specifiers. By default a property has read
and write access, or you can declare the property with the
readonly
or writeonly
directive. An array property can have a default
directive. No other property directives are
allowed.
The purpose of a dispinterface
declaration is to
make a dispatch interface available at compile time, so you
don’t have to wait until runtime to learn you misspelled a
method name. The author of the COM server must provide the
dispinterface
declaration for the COM object. If
you have a type library, you can use Delphi’s type library
editor to generate the source code and
dispinterface
declaration.
A COM server can implement an ordinary interface, a dispatch
interface, or both. Check the type library to learn what kind of
interface it supports. An ordinary interface is faster than using a
dispatch interface, but if you must use a dispatch interface, it is
more convenient to use a dispinterface
than to use
a Variant
to access the COM
object.
The COM server must create an object that implements the
dispinterface
, using a class factory or something
similar, such as CreateComObject
(in the
ComObj
unit). Consult the documentation for the
COM server to learn exactly how to create the COM object.
// Below is the IStrings dispatch interface from stdvcl.pas.
// When you create an ActiveX control from a Delphi control,
// IStrings is the COM interface for a TStrings-type property,
// such as TMemo.Lines or TListView.Items.
type
IStringsDisp = dispinterface
['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']
property ControlDefault[Index: Integer]: OleVariant dispid 0;
default;
function Count: Integer; dispid 1;
property Item[Index: Integer]: OleVariant dispid 2;
procedure Remove(Index: Integer); dispid 3;
procedure Clear; dispid 4;
function Add(Item: OleVariant): Integer; dispid 5;
function _NewEnum: IUnknown; dispid -4;
end;