I am writing a VB.NET add-on for Solidworks, in which I first need to connect to an already running instance of Solidworks (similarly to how such connections are made to Excel). I am using Marshal.GetActiveObject method:
Sub ConnectToSolidworks()
Const PROG_ID As String = "SldWorks.Application"
Dim progType = System.Type.GetTypeFromProgID(PROG_ID)
If progType Is Nothing Then MSG("Cannot detect a valid Solidworks installation.") : End : Application.Exit()
'connect to Solidworks
Try
swApp = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject(PROG_ID), SolidWorks.Interop.sldworks.ISldWorks)
Catch ex As Exception
printException(System.Reflection.MethodBase.GetCurrentMethod().Name, ex)
End Try
'check if connected
If swApp Is Nothing Then
MSG("Unable to connect to Solidworks.")
End
Application.Exit()
End If
End Sub
Unfortunately, there is a problem. If Solidworks is launched as an Administrator, and my application is not, or vice versa, Marshal.GetActiveObject fails with HRESULT: 0x800401E3 exception.
However, I need my application to work without administrative access, and regardless of whenever Solidworks was launched as administrator.
The reason why I'm not using Interaction.CreateObject, Interaction.GetObject or System.Activator.CreateInstance, is because these methods will launch Solidworks on their own if it is not running already, which I want to avoid at all costs.
With that in mind, how can I get Marshal.GetActiveObject to work when the elevation levels mismatch, or is there yet some another way to get this done?
EDIT: I tried Interaction.GetObject again, and it also throws an exception if elevation levels mismatch ("Cannot create ActiveX component").
User contributions licensed under CC BY-SA 3.0