首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎样在DELPHI里响应COM接口的事件?该如何解决

2012-03-07 
怎样在DELPHI里响应COM接口的事件?//*****************************************************************

怎样在DELPHI里响应COM接口的事件?
//   *********************************************************************//
    _IApplicationEvents   =   dispinterface
        [ '{3C31BF76-2837-4BFA-A19E-34485A213F29} ']
        procedure   ReceiveCurConf(var   Conf:   OleVariant);   dispid   1;
        procedure   StatusChange(Status:   Integer;   const   bstrDetail:   WideString);   dispid   2;
    end;

IApplication   =   interface(IDispatch)
        [ '{98AA9DD3-0715-4170-8A96-04F06C8BAD49} ']
        procedure   Connect(shType:   Smallint;   const   bstrServerIP:   WideString;   const   bstrUser:   WideString;  
                                            const   bstrPass:   WideString);   safecall;
        procedure   DisConnect;   safecall;
        function   IsConnect:   Smallint;   safecall;
        function   GetSessions:   IDispatch;   safecall;
        procedure   Exit;   safecall;
        procedure   ShowServer;   safecall;
        function   GetCurrentAccount:   WideString;   safecall;
        function   GetCurrentHost:   WideString;   safecall;
    end;

//   *********************************************************************//
//   DispIntf:     IApplicationDisp
//   Flags:           (4416)   Dual   OleAutomation   Dispatchable
//   GUID:             {98AA9DD3-0715-4170-8A96-04F06C8BAD49}
//   *********************************************************************//
    IApplicationDisp   =   dispinterface
        [ '{98AA9DD3-0715-4170-8A96-04F06C8BAD49} ']
        procedure   Connect(shType:   Smallint;   const   bstrServerIP:   WideString;   const   bstrUser:   WideString;  
                                            const   bstrPass:   WideString);   dispid   1;
        procedure   DisConnect;   dispid   2;
        function   IsConnect:   Smallint;   dispid   3;
        function   GetSessions:   IDispatch;   dispid   4;
        procedure   Exit;   dispid   5;
        procedure   ShowServer;   dispid   6;
        function   GetCurrentAccount:   WideString;   dispid   7;
        function   GetCurrentHost:   WideString;   dispid   8;
    end;

procedure   ReceiveCurConf       StatusChange
这两个咋调用,处理?

[解决办法]
http://www.swissdelphicenter.ch/torry/showcode.php?id=2058
[解决办法]


mark
[解决办法]
_IApplicationEvents
你是需要自己实现这个接口的对象.
然后 '挂 '到那个IApplication的COM组件对象上去..

'挂 '的过程,COM技术中,被称为 '连接点 '技术.

简单过程是:
1,查询取得组件对象的IConnectionPointContainer接口指针.
2,通过IConnectionPointContainer接口指针查找连接点(FindConnectionPoint)
3,把自己实现的_IApplicationEvents对象(可以称它事件响应对象),通过2取到的连接点接口
Advise上去.
这样,你的事件响应对象就 '挂 '上去了.

要不再响应事件,调用连接点接口的Unadvise



[解决办法]
也给我发一份吧: yuyxz@126.com
[解决办法]
楼主也太懒了吧,jiangsheng(蒋晟.Net[MVP]) 贴的链接看都不看.
那不是非常好的例子吗?


[解决办法]
那例子不是才怪.

COM技术是相对较艰深的技术,用DELPHI使用它,已经相对C++用它有相当大的便捷.
但该学的还是要学.


[解决办法]
呵,那代码还没看明白?

那代码里面:
TIEEvents = class(TComponent, IUnknown, IDispatch)
这儿定义的TIEEvents 就是你要学着定义的事件响应对象.

-------------------------------------------------------
看TIEEvents类,
要实现的接口就两个IUnknown,IDispatch

// Protected declaratios for IUnknown
function QueryInterface(const IID: TGUID; out Obj): HResult; override;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;

// Protected declaratios for IDispatch
function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID:
Integer; DispIDs: Pointer): HResult; virtual; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; virtual; stdcall;
function GetTypeInfoCount(out Count: Integer): HResult; virtual; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; virtual; stdcall;
// Protected declarations


-----------------------------------------------------
_IApplicationEvents = dispinterface
[ '{3C31BF76-2837-4BFA-A19E-34485A213F29} ']
procedure ReceiveCurConf(var Conf: OleVariant); dispid 1;
procedure StatusChange(Status: Integer; const bstrDetail: WideString); dispid 2;
end;

注意看每个方法后的dispid, 这表示dispinterface调用时,是通过传dispid值=1来代码调用了第一个方法.

于是,你在实现你自己的事件响应对象时,就象学着
TIEEvents.Invoke方法定义里面那样去判断dispid值而作相应处理.


------------------------------------------------------

热点排行