ConnectToInventor

Top  Previous  Next

This function establishes a connection from your program to the Inventor application and API:

 

bool ConnectToInventor ()

{

   CLSID InvAppClsid;

   HRESULT hRes = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);

   if (FAILED(hRes)) {

       pInvApp = nullptr ;

       ShowCOMError (hRes,L"ConnectToInventor, CLSIDFromProgID failed, admin vs normal user conflict?") ;

       return false;

   }

 

   // See if Inventor is already running...

   CComPtr<IUnknown>pInvAppUnk = nullptr ;

   hRes = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);

   if (FAILED (hRes)) {

       // Inventor is not already running, so try to start it...

       const int ikAnswer = YesNoBoxW (L"Inventor non sta girando, lo lancio?") ;

       if (ikAnswer == IDNO) {

           return false ;

       }

 

       hRes = CoCreateInstance (InvAppClsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IUnknown), (void **) &pInvAppUnk);

       if (FAILED (hRes)){

           pInvApp = nullptr ;

           ShowCOMError (hRes,L"ConnectToInventor,CoCreateInstance failed") ;

           return false;

       }

   }

 

   // Get the pointer to the Inventor application...

   hRes = pInvAppUnk->QueryInterface (__uuidof(Application), (void **)&pInvApp);

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"ConnectToInventor,QueryInterface failed") ;

       return false;

   }

 

   MakeInventorVisible () ;

 

   return true ;

}

 

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)