Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Standalone EXE - How to Manage Multiple Installed Versions of Inventor

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
mikahb4KRMZ
441 Views, 3 Replies

Standalone EXE - How to Manage Multiple Installed Versions of Inventor

I'm writing a standalone app for us to use internally that needs to interact with Inventor.  I'm writing in C# using .NET Framework 4.7.2.  My machine has Inventor 2019 (which we're using in Production) and Inventor 2022 (which we're evaluating for an upgrade) installed.

 

Broadly, the challenge seems to be that when I use COM to get a handle to Inventor, I need it to be Inventor 2019 (which is the interop DLL I'm using) but the system wants to give me Inventor 2022.

 

This eventually comes up whether I'm trying to attach to a running instance, or whether I'm trying to instantiate my own instance of Inventor - namely any time I try to use the `Inventor.Application` progid.  I suspect it may be because the registry key

 

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Inventor\Current Version

 

is pointing to Inventor 2022.  So, I could brute-force change that, but that's not a very flexible solution to force on any user who might run my app.

 

As an example, this call will fail (because it finds no instances) if I run it with Inventor 2019 running:

 

appRet = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;

 

 

I do not have lots of COM programming experience, maybe I'm misunderstanding how to go about this.  I can get a list of running processes, read the EXE path for the desired version of Inventor, and spot that Process running.  But, I can't seem to find a way to get a valid COM object representing that process.

 

Sorry if this isn't clear, my brain is mush.  Any help or guidance very much appreciated.

3 REPLIES 3
Message 2 of 4

You can use something like this approach. 

 

using System;
using System.Diagnostics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int version = GetInventorProcesses();
            Console.WriteLine(version);

            int requiredVersion = 23;
            string inventorExe = @"C:\Program Files\Autodesk\Inventor 2019\Bin\Inventor.exe";

            if (version == -1)
            {
                Console.WriteLine("Close all Inventor applications");
            }
            else if (version == 0)
            {
                Process.Start(inventorExe);
                Console.WriteLine("Starting Inventor");
            }
            else if (version != requiredVersion)
            {
                Console.WriteLine("Invalid version found");
                Console.WriteLine("Close all Inventor applications");
            }
            else
            {
                dynamic inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
                Console.WriteLine(inventorApp.Caption);
            }

            Console.ReadKey();
        }

        /// <summary>
        /// Checks all Inventor processes
        /// </summary>
        /// <returns>0 - Inventor process not found<br/>-1 - Multiple processes found<br/>Othervise Inventor build version</returns>
        static int GetInventorProcesses()
        {
            int result = 0;
            var inventorProcesses = System.Diagnostics.Process.GetProcessesByName("Inventor");
            foreach (Process process in inventorProcesses)
            {
                Console.WriteLine(process.ProcessName);
                Console.WriteLine(process.MainModule.FileName);
                Console.WriteLine(process.MainModule.FileVersionInfo.FileVersion);
                Console.WriteLine(new string('-', 30));

                int version = process.MainModule.FileVersionInfo.FileMajorPart;
                if (result == 0)
                    result = version;

                if (result != version)
                    result = -1;
            }

            return result;
        }
    }
}

 

Message 3 of 4

Thanks for the detailed reply @Michael.Navara!  Unfortunately, I'm getting the same behavior.  That is, when running my app, with Inventor 2019 open and running, this line has an exception:

dynamic inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");

Here's the exception:

mikahb4KRMZ_0-1642180892601.png

This happens whether I manually start Inventor or let the app kick off an instance.  Still stuck in the same spot.

 

Message 4 of 4
mikahb4KRMZ
in reply to: mikahb4KRMZ

Holy crap, found it!!  @Michael.Navara and I were on the right and same track...

 

The trick - as mentioned in this Stack Overflow answer - is that if Inventor is running WITHOUT Admin privileges and the IDE is running WITH Admin privileges, this error can get thrown.

 

I started Inventor 2019 as Admin, re-ran without changing code, and it works like a champ.  So, have to do some permissions testing but now it's connecting right up to the COM object.  I bet I can even get away from Dynamic and back to explicit cast to Inventor.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report