描述:
我写的ActiveX控件中,有一个ATL类,这个类中有一个成员是非ATL类的对象,我想在这个非ATL类中连接上事件。请问,这样能实现吗?怎么实现呢?
解决方案1:
class CYourMFCClass : public CWinThread //(CWinThread derives from CCmdTarget)
{
public:
// member data and member functions for CPrintEditObj go here
// Interface Maps
protected:
DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(MyCPContainer, IConnectionPointContainer)
STDMETHOD_(....)();
END_INTERFACE_PART(MyCPContainer)
......................
};
按照上面的方法把四个接口都实现了。
MFC中使用嵌套类来实现COM的
客户程序和server连接时调用IConnectionPoint::Advise, Advise的第一个参数是客户的IUnknown接口,当你想调用客户的事件时, IUnknown->QueryInterface找到sink接口,然后调用sink接口的函数
(注:客户需要实现一个接口,供server程序回调用)
1. 这个非atl类实现IConnectionPointContainer, IEnumConnectionPoints,IConnectionPoint, IEnumConnections 就可以了呀。
2. 用户那边得和你的server组件连上
A client can use the IConnectionPointContainer interface:
To obtain access to an enumerator sub-object with the IEnumConnectionPoints interface. The IEnumConnectionPoints interface can then be used to enumerate connection points for each outgoing IID.
To obtain access to connection point sub-objects with the IConnectionPoint interface for each outgoing IID. Through the IConnectionPoint interface, a client starts or terminates an advisory loop with the connectable object and the client's own sink. The client can also use the IConnectionPoint interface to obtain an enumerator object with the IEnumConnections interface to enumerate the connections that it knows about.
这个非ATL类实现ISomeEventSink就可以了,完全没问题,
atl有什么特别的,不就是c++写的嘛,我觉得用sdk写COM一样