Search code examples
delphicomdelphi-2010idl

Defining property of type function/procedure pointer in Delphi 2010 RIDL editor


I am wondering if it is possible to define property of type function/procedure pointer in Delphi 2010 RIDL editor, for an interface definition, such that when I create an instance of its implementor using its CoCreator, I may then assign a function/procedure in my source that uses that interface definition, to that property. Actually I want to know how to fill "???" on the following.

TLB file:

IComIntf = interface(IDispatch)
...
  function Get_OnDoSomething : ??? safecall;
  procedure Set_OnDoSomething(const New_Event : ???); safecall;
...
  property OnDoSomething : ???;
...
implementation

uses ComObj;

class function CoComIntf.Create: IComInt;
...
begin
  Result := CreateComObject(CLASS_ComIntf) as IComIntf;
end;

implementation file, ComIntfUnit.pas:

type
  TOnDoSomething = function (Info: OleVariant): HResult of object;

TComIntf = class(TAutoObject, IComIntf)
private
  fOnDoSomething : TDoSomething;
...
public
  property OnDoSomething: TOnDoSomething read fOnDoSomething write fOnDoSomething;
...

Client form:

uses ComIntfUnit;

type
  TForm1 = class(TForm)
  private
  { Private declarations }
    fCom : IComIntf;
    function DoSomething(Info: OleVariant): HResult;
  public
  { Public declarations }
...
  end;
...

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  fCom := CoComIntf.Create;
  fCom.OnDoSomething := DoSomething;
...

Thank you in advance.


Solution

  • Raw function pointers are not COM compatible types. You could certainly use a void pointer if you wished but that wouldn't really be in the spirit of COM.

    What you are expected to do here is to pass another interface. Then the callee can call a function on that interface and have the caller's code be executed.