Hi,
When I use RunningObjectTable, there is a puzzling error. Why I can't get a right pointer by IRunningObjectTable::GetObject.
Here is the test code:
int main(int argc, char* argv[])
{
CoInitialize(NULL);
{
CComPtr<IHello> ptr;
ptr.CoCreateInstance(CLSID_Hello);
ptr->Hello(); // I can InternalAddRef() in this function.
} // So, when ptr is auto released, the object in COM server end will not be desstructed.
{
HRESULT hr = S_OK;
IRunningObjectTable* pROT = NULL;
const WCHAR szDelim[2] = L"!";
IMoniker* pmkService = NULL;
CComBSTR bstrMkItemName("12345678");
hr = CreateItemMoniker( szDelim, bstrMkItemName, &pmkService );
if( FAILED( hr ) )
{
return hr;
}
hr = GetRunningObjectTable( 0, &pROT );
if( FAILED( hr ) )
{
return hr;
}
IUnknown* pUnk;
pROT->GetObject(pmkService, &pUnk); // after this line, why pUnk = NULL?
IHello* iptr;
pUnk->QueryInterface(&iptr);
iptr->Hello();
}
return 0;
}
The code in COM server end:
STDMETHODIMP CHello::Hello()
{
MessageBox(NULL, "Hello", "", MB_OK);
HRESULT hr = S_OK;
IRunningObjectTable* pROT = NULL;
const WCHAR szDelim[2] = L"!";
IMoniker* pmkService = NULL;
CComBSTR bstrMkItemName("12345678");
hr = CreateItemMoniker( szDelim, bstrMkItemName, &pmkService );
if( FAILED( hr ) )
{
return hr;
}
hr = GetRunningObjectTable( 0, &pROT );
if( FAILED( hr ) )
{
return hr;
}
hr = pROT->Register(0, this, pmkService, &m_dwRegister);
this->InternalAddRef();
return S_OK;
}
thanks.
liixixi