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: 

Marshal.GetActiveObject("Inventor.Application") fails with CO_E_CLASSSTRING

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
JohanLarsson
10196 Views, 11 Replies

Marshal.GetActiveObject("Inventor.Application") fails with CO_E_CLASSSTRING

I have the following helper class. It have worked fine for a long time but suddenly it does not work in one solution.

It still works in another solution, I have restarted the computer and is now fresh out of ideas of how to fix this.

Any suggestions to why I see this problem and how to solve it?

 

Error message: Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

 

 public static class InventorUtilities
    {
        public static Inventor.Application GetInventorApplication(bool startNewIfNotFound=false)
        {
            if (_inventorApplication != null)
                return _inventorApplication;
            try
            {
                _inventorApplication = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
            }
            catch (COMException ex)
            {
                if (startNewIfNotFound)
                {
                    _inventorApplication = StartInventorApplication();
                }
                throw new COMException("Inventor application not found", ex);
            }

            return _inventorApplication;
        }

        private static Inventor.Application _inventorApplication;
        public static Application InventorApplication { get { return GetInventorApplication(); } }

        public static Inventor.Application StartInventorApplication()
        {
            try
            {
                // Start Inventor.
                System.Type oType = System.Type.GetTypeFromProgID("Inventor.Application");
                _inventorApplication = (Inventor.Application)System.Activator.CreateInstance(oType);

                // Make Inventor visible.
                _inventorApplication.Visible = true;
            }
            catch (COMException ex)
            {
                throw new COMException("Failed to start Inventor", ex);
            }
            return _inventorApplication;
        }

        public static MeasureTools MeasureTools { get { return InventorApplication.MeasureTools; } }

        public static TransientGeometry TransientGeometry { get { return InventorApplication.TransientGeometry; } }

        public static TransientObjects TransientObjects { get { return InventorApplication.TransientObjects; } }

        public static UnitsOfMeasure UnitsOfMeasure { get { return InventorApplication.UnitsOfMeasure; } }

        public static T GetActiveDocument<T>()
        {
            return (T)InventorApplication.ActiveDocument;
        }

    }

 

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
11 REPLIES 11
Message 2 of 12
JohanLarsson
in reply to: JohanLarsson

To clearify, the method GetInventorApplication() fails on row 

_inventorApplication = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");

 

The code works when called from one soultion but fails when used in another.

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Message 3 of 12
JohanLarsson
in reply to: JohanLarsson

Now the code works in both solutions, I have a suspicion that it had something to do with code for registering an Inventor add-in.

The solution that failed had a project with code for registering an add-in. I unloaded that project and restarted Visual studio among other things and now it works. The only thing I am sure of is that the code that failed is untouched.

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Message 4 of 12

Hi,

 

The only issue with GetActiveObject I have ever heard was: at the machine of one customer, get object failed if the program [requestedExecutionLevel level="requireAdministrator" ] while Inventor running with different ExecutionLevel. 

Message 5 of 12

I was running Visual Studo as admin but still no dice. I have not had this problem in a while now and I dont know how to recreate it.

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Message 6 of 12

When you recreate this issue next time, please try to use Process Monitor to watch if there is any failure for loading some modules. Probably it may be helpful to know something.

Message 7 of 12

I just found the same thing. If I ran Inventor 2013 or Visual Studio 2012 as Administrator then I got the COMException:

 

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

 

If I ran both without selecting Run As Adminstrator then I could call Marshal.GetActiveObject("Inventor.Application") without any issues.

 

In the end I made sure my taskbar shortcuts weren't set to Run As Administrator for Inventor 2013 and Visual Studio 2012. I'm running Windows 8 64 bit with Any CPU projects.

 

I was just using this sample for my tests:

http://modthemachine.typepad.com/files/inventor_training_module_01_samples.zip

Message 8 of 12

Hi adam.davidson,

 

Sorry I have not more comments than what I have shared for this problem. This looks a specific issue on specific scenario. I'd appreciate you could use  Process Monitor to watch if there is any failure for loading some modules, and provide with us. It may be helpful to diagnose what happened.

Message 9 of 12
Vaal_it
in reply to: xiaodong_liang

I am facing same issue while attempting to connect/create Inventor instance.

 

Here is the code:

        /// <summary>
        /// Connects to a running instance of Inventor or
        /// Creates a new instance and connects to it.
        /// </summary>
        /// <returns>Instance of Inventor app</returns>
        private Inventor.Application Connect()
        {
            string InventorProgID = "Inventor.Application";
            Inventor.Application inventorApp = null;

            // Try to get an active instance of Inventor
            try
            {
                inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject(InventorProgID) as Inventor.Application;
                //This throws Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
            }
            catch
            {
                //(MK_E_UNAVAILABLE)) is expected when Inventor is not running in interactive mode.
            }

            // If Inventor is not running, create a new Inventor session
            if (null == inventorApp)
            {

                Type inventorAppType = Type.GetTypeFromProgID(InventorProgID); //This returns null, when process is Run As Administrator.
                Debug.Assert(inventorAppType != null, "inventorAppType is null.");

                if (null != inventorAppType)
                {
                    inventorApp = Activator.CreateInstance(inventorAppType) as Inventor.Application;
                    Debug.Assert(inventorApp != null, "inventorApp is null.");
                }
            }

            return inventorApp;
        }

 

This is what we observed in using process monitor.

 

[A] While running as Non-Admin User:

Process monitor shows successfull registry events as below.

 

ProcessMonitorInventorConnection-NonAdminMode.jpg

 

 

[B] While running as Admin User:

Process monitor shows unsuccessfull registry events as below.

 

ProcessMonitorInventorConnection-AdminMode.jpg

 

 

Message 10 of 12
FRFR1426
in reply to: adam.davidson

Same problem here with AutoCAD. GetActiveObject fails when running as administrator.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 11 of 12
adam.nagy
in reply to: FRFR1426

Probably same issue as the one mentioned here and same explanation for it too.

http://forums.autodesk.com/t5/inventor-customization/not-able-to-create-inventor-instance-when-visua...



Adam Nagy
Autodesk Platform Services
Message 12 of 12
11210497
in reply to: JohanLarsson

public static class Marshal is a Member of System.Runtime.InteropServices

you just have to add:

using System.Runtime.InteropServices; in that .cs file

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

Post to forums