13 Replies Latest reply on Oct 30, 2014 1:33 PM by Edgar Quintana

    Attaching to running SolidWorks Session?

    Brian Titus

      Programming with VBA for a long time I've used the following to either attach/start a SolidWorks session:


                sw = GetObject(, "SldWorks.Application")


      With Visual Studio 2010, I am using the following command:


                sw = New SldWorks()


      In both of these cases, this is SUPPOSED to attach to an existing SolidWorks session IF one is already running.  ON some machines this works as expected, but in many cases it does not find the running session and fires up a NEW SolidWorks.  This results in two (2) sessions running.


      Any ideas on how to prevent/fix this?

      I found some vague references on MSDN about how it might not be registered in the Running Object Table (ROT)?


      Repair/Re-install of SolidWorks do not help.

        • Re: Attaching to running SolidWorks Session?
          Daniel Andersson

          I might be in deep waters now. But I  think SW = New Sldworks() creates a new object of Sldworks. To me it is not clear that it would connect to a current session.


          Have you tried (vb.net)?

          Dim SW As Object = GetObject (, "Sldworks.Application")



            • Re: Attaching to running SolidWorks Session?
              Brian Titus

              Well..According to the SolidWorks API Help:


              Use CreateObject, GetObject, New, or similar functions to obtain the ISldWorks object from a Dispatch application (Visual Basic or C++ Dispatch). Standalone .exe C++ COM applications can use CoCreateInstance.


              Here's MY code in VB2010:


              Public Function ConnectToSolidWorks() As SldWorks

                      Dim sw As SldWorks = Nothing



              ***This next line THROWS an EXCEPTION ****

                          sw = GetObject(, "SldWorks.Application")

                          If sw IsNot Nothing Then

                              swAttach = True

                          End If

                      Catch ex As Exception

                          writelogfile("ERROR with GETOBJECT")

                      End Try


                      If sw Is Nothing Then

                          sw = New SldWorks()

                          If sw IsNot Nothing Then

                              If sw.Visible = False Then sw.Visible = True

                              writelogfile("Spawned new SolidWorks process")

                          End If

                      End If


                      Return sw


                  End Function


              My error log shows this:

              10:42:30 : ERROR with GETOBJECT

              10:42:30 : Spawned new SolidWorks process


              On another machine the log shows NO ERRORS.

            • Re: Attaching to running SolidWorks Session?
              Jeetendra Prasad




              Just want to add that using "New SldWorks()" will work for specific version of solidworks for which the code was developed. Means if you developed your code with SW2013 Interop dll and then if you use your program / DLL in your deployment system with SW2014, then "New SldWorks()" will return null.



              -- Jeetendra Prasad.

                • Re: Attaching to running SolidWorks Session?
                  Brian Titus

                  This code was developed using the 2014 interop libraries ( and is ONLY run using SolidWorks 2014

                    • Re: Attaching to running SolidWorks Session?
                      Adam Hoffman

                      This is what I use in VB.Net when writing stand alone apps:


                      Imports System

                      Imports System.IO

                      Imports System.Runtime.InteropServices

                      Imports System.Runtime.InteropServices.Marshal


                      Imports SldWorks

                      Imports SwConst

                      Imports SwCommands

                      Imports EdmLib


                      'Verify SolidWorks Connection
                      Private Function SWVerify() As SldWorks.SldWorks

                              Dim SolidWorksApp As SldWorks.SldWorks = Nothing

                                  SolidWorksApp = System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application")
                              Catch SWConnectError As Exception
                              End Try

                              Return SolidWorksApp

                      End Function


                      Then I use the function like this:


                      Dim swApp as SldWorks.SldWorks = SWVerify()


                      If swApp Is Nothing Then

                         swApp = New SldWorks.SldWorks

                         swApp.Visible = True

                      End If


                      It has always seemed to work for me. If you are writing this as a add-in then you want to assign the swApp variable when your add-in is first loaded by SolidWorks itself. You don't want to use the SWVerify method in the middle of your program when using an add-in because this could force you to connect to a second SolidWorks process running in the background. Pretty much hosing up everything you are trying to do with your add-in. This is pulled straight from the vb add-in template:


                      Imports System
                      Imports System.Collections
                      Imports System.Reflection
                      Imports System.Runtime.InteropServices

                      Imports SolidWorks.Interop.sldworks
                      Imports SolidWorks.Interop.swcommands
                      Imports SolidWorks.Interop.swconst
                      Imports SolidWorks.Interop.swpublished
                      Imports SolidWorksTools
                      Imports SolidWorksTools.File

                      Imports System.Collections.Generic
                      Imports System.Diagnostics


                      Dim WithEvents iSwApp As SolidWorks.Interop.sldworks.SldWorks
                      Public Shared mySwApp As SolidWorks.Interop.sldworks.SldWorks
                      Dim addinID As Integer


                      ReadOnly Property SwApp() As SldWorks
                                  Return iSwApp
                              End Get
                      End Property


                      Function ConnectToSW(ByVal ThisSW As Object, ByVal Cookie As Integer) As Boolean Implements SolidWorks.Interop.swpublished.SwAddin.ConnectToSW
                              iSwApp = ThisSW

                              mySwApp = iSwApp

                              addinID = Cookie

                              ' Setup callbacks
                              iSwApp.SetAddinCallbackInfo(0, Me, addinID)


                              ConnectToSW = True
                      End Function


                      Then the mySwApp variable I can use in my other classes and modules since the initial iSwApp is read only. But this all depends on if you are writing this as a stand alone app or an add-in. Hope it helps.