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

3.3k views Asked by At

I have two COM objects written in C++ and ATL. There are in one library and I know their IIDs and CLIDs.

I can't find an example of doing this simple communication between two simple COM objects. How to create IMoniker and how to add it to ROT? And then, how to retrieve pointer of this object,in other COM in different process/thread?

Does anyone can provide a small example?

EDIT: More info:

I'm writing an add-on for IE. There are two COM object completely unrelated that IE load for different purpose. One is BHO (Browser Helper Obect), other is Asynchronous Pluggable Protocol (APP) I found I can communicate through ROT here.

1

There are 1 answers

0
Nikolay On

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);
  }
}