Issue connecting to a running program using Marshal.GetActiveObject - Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

0

This is in a unit test. My code to start the app in the TestInitialize method:

        var progId = "SldWorks.Application";
        var progType = System.Type.GetTypeFromProgID(progId);
        app = System.Activator.CreateInstance(progType) as ISldWorks;

Solidworks starts just fine, I can see it start. When I test my method I am trying to get the currently running SolidWorks in the constructor I get the error on the last line:

        string progId = "SldWorks.Application";
        solidWorksApp = System.Runtime.InteropServices.Marshal.GetActiveObject(progId) as ISldWorks;

Searching for the error I see a lot of help that says this is because Microsoft Office can't start. Solidworks DOES use Office in some functions but SolidWorks starts just fine.

I tried

Thread.Sleep(5000);

After I start SolidWorks but that did not help. I thought perhaps Solidworks wasn't completely started when I am trying to get the running instance.

There is already an add in running inside SolidWorks that uses the same technique to use the running SolidWorks app. I disabled the add in to test to see if perhaps only one at a time could connect. That did not resolve the issue.

EDIT: This only happens when I try to run a unit test. IT does not happen when I use the method or constructor in normal use.

Edit 2 I am beginning to think maybe this is a permission issue. Some of the answers elsewhere about a similar issue talk about needing to run as Admin to get Excel. When I run my app in debug mode through Visual Studio everything works fine. When I run my unit test the Marshal.GetActiveObject() fails.

c#
reflection
asked on Stack Overflow Mar 27, 2020 by Eric Snyder • edited Apr 3, 2020 by Eric Snyder

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0