mfc工程中添加com接口支持
在普通的mfc工程中常常需要添加com接口支持.最典型的應用就是使用
com中的連接點機制時,需要在客戶端實現事件接受器.簡單說來,就是要實現在com服務器端聲明的事件接口.這個接口由server端暴露給client,然后由client實現之,并在適當的時候由server端觸發事件達到通知client的目的.這個過程就是連接點的回調.
??? 支持IDispatch的接口,就是通常所說的雙接口.支持IUnknown的接口通過vtbl來調用接口方法.vtbl是c++中類的內存結構形式,因而在vb,asp及各種script中只能通過組件暴露的IDispatch接口來調用接口方法.
??? 以實現連接點為例,我們分別用上面所說的兩種形式(客戶端sink組件實現IDispatch接口以及不實現IDispatch接口)來說說在mfc工程中如何添加com接口支持.
1.sink組件不支持IDispatch
(1)Server:
??? 在server端實現組件對事件回調的支持的步驟就不累述.這里說說關鍵地方在于,IDL文件需要這樣修改幾處:??
library CONNECTIONCOMLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
? uuid(AFE854B0-246F-4B66-B26F-A1060225C71C),
? helpstring("_IxxxEvents Interface")
]
// Old block - take this out
// dispinterface _IxxxEvents
// {
// properties:
// methods:
// [id(1), helpstring("method ExecutionOver")]
// HRESULT ExecutionOver(intResult);
// };
//To this one -put this in
interface _Ixxxvents : IUnknown
? {
? [id(1), helpstring("method ExecutionOver")] HRESULT
????????? ExecutionOver(intResult);
? };
? [
??? uuid(630B3CD3-DDB1-43CE-AD2F-4F57DC54D5D0),
??? helpstring("xxx Class")
? ]
? coclass xxx
? {
? [default] interface Ixxx;
? //[default, source] dispinterface _IxxxEvents; take this line
? //out and put the line below in
? [default, source] interface _IxxxEvents ;
? };
};
目的是取消對IDispatch接口的支持.
??? 在適當的地方添加Fire_xxx()即可觸發client實現的事件函數.
(2)Client:
??? 在client端添加一個新類,繼承自_IxxxEvent,然后實現其成員函數:InterfaceQuery(),AddRef(),Release(),還有當然就是在server端定義的事件觸發成員函數.
2.sink組件支持雙接口.
mfc和atl中不一樣.示例如下:
用ClassWizaed添加一個Automation類
.h中
class CProxyEvents : public CCmdTarget
{
?DECLARE_DYNCREATE(CProxyEvents)
?CProxyEvents();?????????? // protected constructor used by dynamic creation
// Attributes
public:
?void OnOption(VARIANT *Option);?
?void OnData(VARIANT *Data);
?void OnError(int ErrorCode);
?void OnConnectStatus(int ConnectStatus);
?void OnErrorMessage(BSTR ErrorMessage);
?void OnNews(BSTR News);
?void OnKeepAlive(BSTR AckResponse);
?HWND hwnd;?
// Operations
public:
?virtual ~CProxyEvents();
// Overrides
?// ClassWizard generated virtual function overrides
?//{{AFX_VIRTUAL(CProxyEvents)
?public:
?virtual void OnFinalRelease();
?//}}AFX_VIRTUAL
// Implementation
protected:
?// Generated message map functions
?//{{AFX_MSG(CProxyEvents)
??// NOTE - the ClassWizard will add and remove member functions here.
?//}}AFX_MSG
?DECLARE_MESSAGE_MAP()
?// Generated OLE dispatch map functions
?//{{AFX_DISPATCH(CProxyEvents)
??// NOTE - the ClassWizard will add and remove member functions here.
?//}}AFX_DISPATCH
?DECLARE_DISPATCH_MAP()
?DECLARE_INTERFACE_MAP()
};
.cpp中
BEGIN_MESSAGE_MAP(CProxyEvents, CCmdTarget)
?//{{AFX_MSG_MAP(CProxyEvents)
??// NOTE - the ClassWizard will add and remove mapping macros here.
?//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CProxyEvents, CCmdTarget)
?DISP_FUNCTION_ID(CProxyEvents, "Option",8,OnOption, VT_EMPTY, VTS_PVARIANT)
?DISP_FUNCTION_ID(CProxyEvents, "Data",2,OnData, VT_EMPTY, VTS_PVARIANT)?
//??[id(3), helpstring("method Error")] void Error(int ErrorCode);
?DISP_FUNCTION_ID(CProxyEvents, "Error",3,OnError, VT_EMPTY, VTS_I4)?
//??[id(4), helpstring("method ConnectStatus")] void ConnectStatus(int ConnectStatus);
?DISP_FUNCTION_ID(CProxyEvents, "ConnectStatus",4,OnConnectStatus, VT_EMPTY, VTS_I4)?
//??[id(5), helpstring("method ErrorMessage")] void ErrorMessage(BSTR ErrorMessage);
?DISP_FUNCTION_ID(CProxyEvents, "ErrorMessage",5,OnErrorMessage, VT_EMPTY, VTS_BSTR)?
//??[id(6), helpstring("method News")] void News(BSTR News);
?DISP_FUNCTION_ID(CProxyEvents, "News",6,OnNews, VT_EMPTY, VTS_BSTR)?
//??[id(7), helpstring("method KeepAlive")] void KeepAlive(BSTR AckResponse);
?DISP_FUNCTION_ID(CProxyEvents, "KeepAlive",7,OnKeepAlive, VT_EMPTY, VTS_BSTR)?
END_DISPATCH_MAP()
// Note: we add support for IID_IProxyEvents to support typesafe binding
//? from VBA.? This IID must match the GUID that is attached to the
//? dispinterface in the .ODL file.
// {1F2E2D8A-7CE3-4BCA-A360-C66C46A86626}
BEGIN_INTERFACE_MAP(CProxyEvents, CCmdTarget)
?INTERFACE_PART(CProxyEvents, DIID__IServerProxyEvents, Dispatch)
END_INTERFACE_MAP()
代碼中用
??? LPUNKNOWN unknown=pProxyEvents->GetIDispatch(FALSE);
??? m_dwCookie=0;
??? int ret=AfxConnectionAdvise(m_ServerProxy, DIID__IServerProxyEvents, unknown, FALSE, &m_dwCookie);建立連接.
一般地,使一個mfc類支持IDispatch接口(添加自動化支持)可以通過以下步驟:
1.在CMyDialog的類聲明中(即.h)加入
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
兩個宏,再在源文件(.cpp)中添加
BEGIN_INTERFACE_MAP( CMyDialog, CDialog )
??? INTERFACE_PART( CMyDialog, IID_IDispatch, Dispatch )
END_INTERFACE_MAP()
BEGIN_DISPATCH_MAP( CMyDialog, CDialog )
??? //這里添加屬性或方法
END_DISPATCH_MAP()
并在構造函數調用EnableAutomation();即可.在BEGIN_DISPATCH_MAP和END_DISPATCH_MAP宏之間加入想加的DISP_FUNCTION之類的宏以建立映射.如果覺得手動添加DISP_FUNCTION之類的宏麻煩,可如下修改即可使用ClassWizard來對這個類添加方法和屬性了
在.cpp中
BEGIN_DISPATCH_MAP( CMyDialog, CDialog )
??? //{{AFX_DISPATCH_MAP( CMyDialog )
??? //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
在.h中,在類聲明中的任何一個地方加入
??? //{{AFX_DISPATCH( CMyDialog )
??? //}}AFX_DISPATCH
如上后就可通過ClassWizard來添加方法和屬性了.
總結
以上是生活随笔為你收集整理的mfc工程中添加com接口支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ANSI, UNICODE与UTF8相互
- 下一篇: 事件接收器的实现