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; } }
Solved! Go to Solution.
Solved by adam.nagy. Go to Solution.
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.
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.
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.
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.
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.
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
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.
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.
[B] While running as Admin User:
Process monitor shows unsuccessfull registry events as below.
Same problem here with AutoCAD. GetActiveObject fails when running as administrator.
Probably same issue as the one mentioned here and same explanation for it too.
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.