Home Search Contact us About us
Title DCOM with smart pointers (Remote Launch)
Summary Use this method to instantiate a COM object on a Remote machine.
Contributor John McTainsh
Published 8-Sep-2000
Last updated 2-Aug-2001
Page rating   80% for 7 votes Useless Brilliant

Description.

Use this method to use a COM object on another machine. Note this COM object must be built as out of process server. 

Where do the names come from?

The name of the items in this example can be found in the tlh file and class factory as follows;

Name here Purpose How to find
TrickyCom.exe Name of the file containing the object. May be a DLL or EXE. Path and name of file.
TRICKYCOMLib This is the Library name space The namespace is located in .tlh and begins with namespace and ends with ...Lib {
ICtrlFlasherPtr Smart pointer name Look in .tlh and add Ptr to the end of the first parameter in _COM_SMARTPTR_TYPEDEF(ICtrlFlasher,
CtrlFlasher The coclass name Look in .tlh and use the name defines in struct /* coclass */ CtrlFlasher;
Increment() and Count The methods and properties Look in .tlh for the // Property data and // Wrapper methods for error-handling sections

Solution.

In StdAfx.h add.

#define _WIN32_DCOM                             //Just after VC_EXTRALEAN
. . . . .
//Reference Com object to create *.tlh and *.tli 
//note named_guids is used to get the CLSID_xxx and IID_Ixxx.
#import "..\Debug\TrickyCom.exe" named_guids raw_interfaces_only
//The namespace is located in *.tlh and ends in ...Lib {
using namespace TRICKYCOMLib;

In Gizzmo.cpp in BOOL CGizzmoApp::InitInstance(); or any simular start-up function that is called only once.

    //Initialise the application for use with COM objects
    HRESULT hr = CoInitialize( NULL );
    if( FAILED( hr ) )
    {
        AfxMessageBox( "CoInitialize failed\n");
        return FALSE;
    }

    //TODO Call CoUninitialize(); in the Destructor or other location
    //when all COM activity is completed.

In the header of the class place the object reference variable. UseItHere.h

    ICtrlFlashPtr m_pFlash;                 //Smart pointer

In class contructor initialise the variable. UseItHere.cpp Constructor.

    m_pFlash = NULL;

In class destructor release the object. UseItHere.cpp Destructor.

    m_pFlash.Release();
    m_pFlash = NULL;

In an initialisation routine such as InitDialog instantiate the object. UseItHere.cpp note the remote machine name in this example is "Cheung_Anson". This machine name could be an IP address such as "147.132.229.107".

    try
    {
        HRESULT hr;

        //
        //Launch on a REMOTE server
        //
        COSERVERINFO csi = { 0, L"Cheung_Anson", NULL, 0 };
        MULTI_QI qi = {&__uuidof( ICtrlFlash ), NULL, S_OK };

        hr = CoCreateInstanceEx( __uuidof(CtrlFlash), NULL,
                                CLSCTX_REMOTE_SERVER, &csi, 1, &qi );
        if( FAILED(hr) )
            _com_issue_error(hr);

        ICtrlFlash* pCd = static_cast<ICtrlFlash*>(qi.pItf);

        m_pFlash.Attach(pCd);
    }
    catch( _com_error e )
    {
        TRACE( _T("COM - Error %08x : %s\n"), e.Error(), e.ErrorMessage() );
    }

Place this code where you want to access the object. ie In a button handler.

    try
    {
        TRACE( _T("Inital count = %d\n"), pFlash->nBeerCount );
        pFlash->MoreBeer();
    }
    catch( _com_error e )
    {
        //Catch and display errors
        TRACE( _T("Com Error %x, %s\n"), e.Error, e.ErrorMessage() );
    }
Comments Date
Home Search Contact us About us