Re: Create COM object from a specific DLL instance
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:elEAw0ojIHA.4076@TK2MSFTNGP05.phx.gbl...
ohad.ads@gmail.com wrote:
I have an ATL COM DLL (let's call it dll.dll), which exposes an ATL
simple object as a CoClass (called MyCls).
dll.dll is being loaded by a.exe and b.exe.
I need that dll.dll instance that is being loaded by a.exe, to get or
create an instance of MyCls from dll.dll loaded by b.exe, since it has
to perform an operation on b.exe
Trying to cocreateinstance in dll.dll will always return an instance
handled by the current dll scope, i.e., a.exe.
How can I do that?
b.exe could create an instance of the object and register it in the
running object table (ROT, see IRunningObjectTable) under some agreed-upon
moniker (item monikers are handy for this). a.exe will then pick it up
from the ROT.
A few months ago someone on this NG was kind enough to post some code for
doing this. It was a big help! Here is my adaptation of that code:
// Server: Register this object in ROT
BOOL CInstance::RotRegister(const CString& strDocName)
{
HRESULT hr = S_FAIL;
CComPtr<IRunningObjectTable> pTable;
if(SUCCEEDED(GetRunningObjectTable(0, &pTable)))
{
CComPtr<IMoniker> pMoniker;
hr = CreateItemMoniker(NULL, CComBSTR(strDocName), &pMoniker);
if (SUCCEEDED(hr))
{ hr = pTable->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE,
(IUnknown*)this, pMoniker, &m_dwRegister);
}
}
return SUCCEEDED(hr);
}
// Client: Get pointer to desired COM object instance
bool CDoc::GetInterfacePtr(CComBSTR& bsItemName, IUnknown** pUnknown )
{
bool bFound = false;
CComPtr<IMoniker> spMoniker;
HRESULT hr = CreateItemMoniker(NULL, bsItemName, &spMoniker);
if (SUCCEEDED(hr))
{ CComPtr<IRunningObjectTable> spTable;
if(SUCCEEDED(GetRunningObjectTable(0, &spTable)))
{ hr = spTable->GetObject( spMoniker, pUnknown );
if (SUCCEEDED(hr))
{ // Return object pointer
bFound = true;
}
}
}
return bFound;
}
--
Scott McPhillips [VC++ MVP]