TObjectDispatchInvoke (Delphi)
Contents
Description
This example illustrates the usage of TObjectDispatch. A class that acts as an Automation controller is defined—TMyClass—with a method that takes no arguments and returns an integer—MyMethod. An IDispatch instance is used to invoke MyMethod. Note that the invocation is done with the help of a dispid (dispatch identifier) obtained with the GetIDsOfNames method.
Code
program TObjectDispatch;
{$APPTYPE CONSOLE}
uses
SysUtils,
ObjComAuto,
ActiveX,
Variants;
{$METHODINFO ON}
type
TMyClass = class
public
function MyMethod(): Integer;
end;
function TMyClass.MyMethod(): Integer;
begin
Writeln('Inside MyMethod');
Result := 10;
end;
var
Result: HRESULT;
MyInstance: TMyClass;
Dispatcher: IDispatch;
DispathIdentifier: Integer;
MethodName: string;
MethodResult: OleVariant;
Param: array of OleVariant;
begin
MyInstance := TMyClass.Create;
Dispatcher := ObjComAuto.TObjectDispatch.Create(MyInstance, False);
MethodName := 'MyMethod';
Result := Dispatcher.GetIDsOfNames(GUID_NULL, @MethodName, 1,
SysLocale.DefaultLCID, @DispathIdentifier);
Result := Dispatcher.Invoke(DispathIdentifier, GUID_NULL,
SysLocale.DefaultLCID, DISPATCH_METHOD, Param, @MethodResult, nil, nil);
if Result = S_OK then
begin
Writeln('Invoke return value is S_OK');
Writeln('MethodName return value is ' + IntToStr(MethodResult));
end
else
begin
Writeln('Invoke return value is ' + IntToStr(Result));
end;
end.
Console Output
Inside MyMethod Invoke return value is S_OK MyMethod return value through Invoke is 10
Uses
- System.Win.ObjComAuto.TObjectDispatch.GetIDsOfNames ( fr | de | ja )
- System.Win.ObjComAuto.TObjectDispatch.Invoke ( fr | de | ja )
- System.IDispatch ( fr | de | ja )