Is it possible to use interface method as event handlers in Delphi 2007? The simple versions don't work:
type
TMyEvent = procedure of object;
IMyInterface = interface
procedure Handler;
end;
TMyClass = class(TInterfacedObject, IMyInterface)
public
procedure Handler;
end;
var
ev: TMyEvent;
obj: TMyClass;
intf: IMyInterface;
begin
obj := TMyClass.Create;
intf := obj;
ev := obj.Handler; // compiles
ev := intf.Handler; // <== Error E2010 (incompatible types)
end.
Adding @
or Addr
changes the error to E2036 (variable required).
Update: This
procedure IntRefToMethPtr(const IntRef; var MethPtr; MethNo: Integer);
type
TVtable = array[0..999] of Pointer;
PVtable = ^TVtable;
PPVtable = ^PVtable;
begin
//QI=0, AddRef=1, Release=2, etc
TMethod(MethPtr).Code := PPVtable(IntRef)^^[MethNo];
TMethod(MethPtr).Data := Pointer(IntRef);
end;
var
ev: TMyEvent;
intf: IMyInterface;
begin
intf := TMyClass.Create;
IntRefToMethPtr(intf, ev, 3);
ev;
end.
works. However I'm not too fond of the magic 3 in there.