让普通C++类中支持连接点(Windows系统)

来源:互联网 发布:淘宝怎样查看买家电话 编辑:程序博客网 时间:2024/05/22 13:45


容器不要Impl的,连接点可以使用Impl的,从如下继承

class YourClass:
public IYourBaseInterface,
public IConnectionPointContainer,
public IConnectionPointImpl< T >

查询连接点容器时要转换一下得到正确的vtbl
 STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppvObj)
 {
  if (iid == __uuidof(IConnectionPointContainer))
  {
   *ppvObj = (IConnectionPointContainer*)this;
  }
  else
   *ppvObj=this;
  return S_OK;
 }

重载IConnectionPointContainer::FindConnectionPoint
返回指针必须要转换正确,这里是关键

 STDMETHOD(FindConnectionPoint)(REFIID riid, IConnectionPoint** ppCP)
 {
  if (riid == __uuidof(IMiooServiceWindowEvent))
  {
   *ppCP = (IConnectionPoint*)(IConnectionPointImpl< T >*)this;
   return S_OK;
  }
 }
   
简单返回this不行的,若那样,当访问IConnectionPointContainer::FindConnectionPoint时就会访问YourClass的第二个函数了,很搞笑的  

原创粉丝点击