I am developing an external VB.NET add-on that needs to establish connection with Solidworks upon launch. Here is the code I'm using:
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, for whatever reason, the TryCast fails to assign anything to swApp, unless my application is run with Administrator privileges. Weird thing is, some people I sent my application to reported that they managed to get a connection without having to run it as Administrator, but most of them said it failed to connect. I do not want to have a run-as-administrator as a requirement to use my add-on.
I have tried, in the past, other methods of connecting to Solidworks (GetObject, CreateObject, Activator.CreateInstance, etc.), but they were plagued with problems, like creating invisible background instances of Solidworks, unreliable connections, etc. TryCast seems to be almost perfect, but for some reason, it doesn't work for everyone without Administrator privileges.
What is causing this problem, and how can I fix this?