Visual C++ Developer Center > Visual C++ Forums > Visual C++ Language > IRunningObjectTable::GetObject return a error
Ask a questionAsk a question
 

AnswerIRunningObjectTable::GetObject return a error

  • Monday, July 07, 2008 4:09 AMliixixi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

Answers

  • Tuesday, July 08, 2008 3:15 AMliixixi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The problem has reseloved, it should use ROTFLAGS_REGISTRATIONKEEPSALIVE to register the object to ROT.
    • Marked As Answer byYan-Fei Wei Friday, July 11, 2008 2:45 AM
    •  

All Replies

  • Monday, July 07, 2008 6:13 PMBrian MuthMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What is the value of HRESULT returned by GetObject()?

    Is the COM Server a DLL, a DCOM executable or a DCOM service?
  • Tuesday, July 08, 2008 3:15 AMliixixi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The problem has reseloved, it should use ROTFLAGS_REGISTRATIONKEEPSALIVE to register the object to ROT.
    • Marked As Answer byYan-Fei Wei Friday, July 11, 2008 2:45 AM
    •  
  • Tuesday, July 08, 2008 5:06 AMBrian MuthMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Out of curiosity, did the HRESULT value returned by GetObject() help you deduce the problem?