See update below before reading!
I have a class the needs to get an instance of a running app to work with the API of that app. It seems to run just fine when running in debug mode. When I run Unit Tests it does not seem to be able to get access to the app.
I get the exception:
System.Runtime.InteropServices.COMException: Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
First, let me say that I understand that if my test relies on an external app then it is technically not a "unit test". In this case using the unit testing framework to test the code is still very useful. The only other way I know is to just run and use breakpoints to test values or just see if I get the desired result. This is how I grew my skills and honestly, it sucked. I missed many bugs this way.
My code in the class that is the problem when testing:
if(solidWorksApp == null)
{
string progId = "SldWorks.Application";
solidWorksApp = System.Runtime.InteropServices.Marshal.GetActiveObject(progId) as ISldWorks;
}
I start the app in the unit test using the following:
[TestInitialize]
public void TestInitialize()
{
var progId = "SldWorks.Application";
var progType = System.Type.GetTypeFromProgID(progId);
app = System.Activator.CreateInstance(progType) as ISldWorks;
app.Visible = true;
DocumentSpecification documentSpecification;
documentSpecification = (DocumentSpecification)app.GetOpenDocSpec(fullFilePath);
documentSpecification.DocumentType = (int)swDocumentTypes_e.swDocPART;
var result = app.OpenDoc7(documentSpecification);
if (result == null)
{
throw new Exception("Couldn't load SOlidworks test file.");
}
}
Why would I not be able to get access to the app when it is started by a unit test? Is there a workaround?
Update: I tried starting the app normally and then getting a pointer to the app in my test using Marshal.GetActiveObject. It still didn't work. I then tried starting the app manually as administrator and Marshal.GetActiveObject. I was then able to get the app. It seems to be a permissions thing.
User contributions licensed under CC BY-SA 3.0