实现IDispatch接口不就行了吗?
声明大概是这样
function ComObj.AddEvent(const pEvent: IDispatch):HResult; stdcall;
pEvent如果是自定义接口,按接口声明实现它的函数就是了,IDispatch实现如:
TComEventObject = class(TObject, IDispatch)
protected
//IInterface
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
//IDispatch
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
end;
function TComEventObject.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TComEventObject._AddRef: Integer;
begin
Result := 1;//不使用接口的引用计数机制,自己控制释放
end;
function TComEventObject._Release: Integer;
begin
Result := 1;//不使用接口的引用计数机制,自己控制释放
end;
function TComEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
Result := E_NOTIMPL;
end;
function TComEventObject.GetTypeInfo(Index, LocaleID: Integer;
out TypeInfo): HResult;
begin
Pointer(TypeInfo) := nil;
Result := E_NOTIMPL;
end;
function TComEventObject.GetTypeInfoCount(out Count: Integer): HResult;
begin
Count := 0;
Result := E_NOTIMPL;
end;
function TComEventObject.Invoke(DispID: Integer; const IID: TGUID;
LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
ArgErr: Pointer): HResult;
var
param0 : Variant;
begin
Result := S_OK;
//事件回调, DispID = 事件ID
case DispID of
1 :
begin //Params为参数结构体
param0 := Variant(TDispParams(Params).rgvarg^[0]);//多个参数从0开始
end;
else
Result := DISP_E_MEMBERNOTFOUND;
end;
end;