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.
User contributions licensed under CC BY-SA 3.0