Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Connect to Inventor from COM

oransen
Collaborator

Connect to Inventor from COM

oransen
Collaborator
Collaborator

I have been using this function for many years...

 

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"Shall I run Inventor?") ;
       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") ;
       false;
   }

   return true ;
}

 

...but sometimes it fails and runs a second instance of Inventor.

 

Can anyone tell me why the random failure might be occurring?

0 Likes
Reply
584 Views
3 Replies
Replies (3)

AlexKorzun
Autodesk
Autodesk

Possibly you've missed the "return"?

AlexKorzun_0-1627600044860.png

 

Thank you,




Alex Korzun
Inventor-Revit Interop / Inventor-Fusion Interop / Inventor ETO
Autodesk, Inc.

oransen
Collaborator
Collaborator

Hell's Bells! My mistake, but don't you love C++ ? I'll try that but I don't think that is my problem....

0 Likes

oransen
Collaborator
Collaborator

Your comment was correct, but it did not solve the problem... hey ho hum!

0 Likes