How to communicate between two COM objects using Running Object Table (ROT)?

£可爱£侵袭症+ 提交于 2019-12-03 14:04:53

Try using CreateItemMoniker instead of CreatePointerMoniker - it allows you to specify a name for your object in ROT.

You should be able to register your object like this:

DWORD RegisterInROT(LPCWSTR szObjName, IUnknown* pObj)
{
  DWORD dwCookie = 0;
  CComPtr<IRunningObjectTable> pROT;
  if (GetRunningObjectTable(0, &pROT) == S_OK)
  {
    CComPtr<IMoniker> pMoniker;
    if (CreateItemMoniker(NULL, szObjName, &pMoniker) == S_OK)
        if (pROT->Register(0, pObj, pMoniker, &dwCookie) == S_OK)
           return dwCookie;
  }
  return 0;
}

If you don't want your object to be auto-killed when there are no more references to it, you could specify ROTFLAGS_REGISTRATIONKEEPSALIVE instead of 0 (check in in MSDN). The function returns cookie you can use to explicitly remove your object from ROT later like this:

void RevokeFromROT(DWORD dwCookie)
{
  CComPtr<IRunningObjectTable> pROT;
  if (GetRunningObjectTable(0, &pROT) == S_OK)
       pROT->Revoke(dwCookie);
}

You can get the object from ROT like this (you should use the same name you used to register the object of course =)

void GetObjectFromROT(LPCWSTR szObjName, IUnknown** pObj)
{
  CComPtr<IRunningObjectTable> pROT;
  if (GetRunningObjectTable(0, &pROT) == S_OK)
  {
    CComPtr<IMoniker> pMoniker;
    if (CreateItemMoniker(NULL, szObjName, &pMoniker) == S_OK)
        pROT->GetObject(pMoniker, pObj);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!