如何在MFC程序中实现ATL COM接口,并支持事件?
入题.
如果有源代码万分感谢
问题点数:0、回复次数:2Top
1 楼newcore(to be or not to be, it's a question.)回复于 2005-06-01 22:24:32 得分 0
我收藏的一篇帖子,mfc实现客户端事件接口,希望有帮助.
--------------------------------------------------------------------------------
I'm having trouble with connection points, ATL, and VB5. Is there an example? (06/97)
How do I handle events from a ConnectPointContainer in ATL? (06/97)
--------------------------------------------------------------------------------
I'm having trouble with connection points, ATL, and VB5. Is there an example?
Yes there is. Kevin Moule (krmoule@UNDERGRAD.MATH.UWATERLOO.CA) has created a simple in-process ATL-based component that uses connection points to fire events to Visual Basic 5.0 (via VB5's WithEvents keyword). Here's a link to Kevin's page where you can download the source.
Chapter 7 of The Active Template Library: A Developer's Guide has several connection point examples.
Back to Top
--------------------------------------------------------------------------------
How do I handle events from a ConnectPointContainer in ATL?
Here's an answer provided by Dietrich Schmidt (Dietrich.Schmidt@bigfoot.com)
I coudn't find an ATL example for this. So I tried by myself and was successful. Here are the steps required: 1. create an ATL Server component which implements a connection point:
1a. Define interface in IDL:
interface IMyEvent : IDispatch
{
[id(1), helpstring("method Update")] HRESULT Update(long UpdateId);
};
1b. Make your main ATL class implement a connection point:
public IConnectionPointImpl<CMyClass, &IID_IMyEvent, CComDynamicUnkArray>,
public IConnectionPointContainerImpl<CMyClass>
...
BEGIN_CONNECTION_POINT_MAP(CMyClass)
CONNECTION_POINT_ENTRY(IID_IMyEvent)
END_CONNECTION_POINT_MAP()
1c. Send events over the connection point inside of CMyClass:
// fire event over IMyEvent
IConnectionPointImpl<CMyClass, &IID_IMyEvent, CComDynamicUnkArray>* p = this;
Lock();
HRESULT hr = S_OK;
IUnknown** pp = p->m_vec.begin();
while (pp < p->m_vec.end() && hr == S_OK)
{
if (*pp != NULL)
{
IMyEvent* pIMyEvent = (IMyEvent*)*pp;
hr = pIMyEvent->Update(nId);
}
pp++;
}
Unlock();
2. Implement a client in C++, which implements IMyEvent and connects to the connection Point. First, it must retrieve an interface pointer to IMyClass, for example with CoCreateInstance. Then, you connect to the Connection Point:
2a. Add the ATL headers and the global _module to you client.
CComModule _Module; // ATL global Modul object
2b. implement IMyEvent (you also need the definitions of the used IID/GUID) class CMyEvent :
public IDispatchImpl<IMyEvent, &IID_IMyEvent, &LIBID_MYEXAMPLELib>,
public CComObjectRoot
{
public:
CMyEvent() {}
BEGIN_COM_MAP(CMyEvent)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IMyEvent)
END_COM_MAP()
// IMyEvent methods
STDMETHOD(Update)(long Id);
};
2c. connect to the connection point
// connect via connection point
CComObject<CMyEvent>::CreateInstance(&m_MyEvent);
ASSERT(m_MyEvent);
// give this pointer to the server
HRESULT hr = AtlAdvise( m_pMyClass, m_MyEvent->GetUnknown(),
IID_IMyClass, &dwAdviseHandle);
ASSERT(SUCCEEDED(hr));
That's it. Now you get events.
Top
2 楼jarineon(都市霓虹灯)回复于 2005-06-02 01:02:38 得分 0
我的意思是在现有的MFC工程中,添加支持事件的COM接口,怎么做?
新增一个ATL Class,编译后不产生TLB文件,客户端连接的时候也出错.Top
相关问题
- MFC写ActiveX,怎么支持多个接口?
- ATL 没选择支持MFC, COleDateTime用不了, 怎样处理DATE?
- 高分请教一个支持MFC的ATL Projects 问题
- 写一个支持MFC的ATL COM 但是编译时出错!!:(
- ATL中如何添加对mfc的支持
- 在MFC扩展DLL里使用ADO为何会出现连接错误提示:不支持此接口?
- 請問,在 ATL(不支持MFC) 中如何連接如下SQL語句 , 謝謝
- ATL编程不支持MFC时使用_bstr_t时出现的问题,给高分!!!!
- 如何在ATL建的exe中加入对mfc的支持,比如CString?
- 在ATL和MFC混合编程中,如何在MFC继承的类中调用ATL接口中声明的方法。




