12 Replies Latest reply on Aug 29, 2014 8:23 PM by Sanya Shmidt

    How to catch if SolidWorks was closed

    Sanya Shmidt

      Hello guys!

       

      What would be the correct way to catch if user closed SolidWorks application.

       

      I use the following code when starting my app.

      ......

      If SeeIfSWRunning() <> True Then Me.Close()

      ......

       

       

      ' see if SolidWorks is running or not

          Public Function SeeIfSWRunning() As Boolean

              Dim p() As Process

              p = Process.GetProcessesByName("SLDWORKS")

              If p.Count > 0 Then

                  'MessageBox.Show("Running")

                  Return True

              Else

                  MessageBox.Show("SolidWorks is not running on this computer. Please start it prior to use this app.", TPCMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                  swApp = Nothing

                  Return False

              End If

       

      Lets say user accidentally closed application in the middle of design and then clicked button on my app to open the template for instance...

       

          ' opens a template

          Private Sub btn_OpenTemplate_Click(sender As Object, e As EventArgs) Handles btn_OpenTemplate.Click

              If SeeIfSWRunning() <> True Then Exit Sub

              Part = swApp.NewDocument(Template, 0, 0, 0)

          End Sub

       

      Thats where I`m getting error....

       

      An unhandled exception of type 'System.Runtime.InteropServices.COMException

       

      I noticed that there are a few process by SW that starts with SLDWORKS

       

      - this is when SW is open

      SW_processes.PNG

      - this is when SW is closed

      sw_processes1.PNG

       

      Is there a another way to check this?

       

      Thank you,

      Alex.

        • Re: How to catch if SolidWorks was closed
          Adam Hoffman

          Are you using Visual Studio and writing this as a stand alone exe?

            • Re: How to catch if SolidWorks was closed
              Sanya Shmidt

              yes. Using VB 2012

                • Re: How to catch if SolidWorks was closed
                  Adam Hoffman

                  This is what I use:

                   

                   

                   

                  swApp = System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application")

                   

                   

                  If swApp Is Nothing Then


                     MessageBox.Show("Error Connecting To SolidWorks!", _

                   

                                     "SolidWorks Connection Error", _

                   

                                     MessageBoxButtons.OK, MessageBoxIcon.Error)

                   

                     Application.Exit()

                   

                     End

                   

                  End If

                   

                   

                  Then periodically throughout the program, I use the above if statement to double-check the swApp variable.

                  If the possibility of more than one instance of SolidWorks to be open is possible, then I would go with Ivana's route.

                   

                   

                    • Re: How to catch if SolidWorks was closed
                      Sanya Shmidt

                      Adam,

                       

                      for some reason shutting down SW is not killing the process sldworks_fs thats where the issue is i believe.

                       

                      follow the link to see the video.... Not sure what I`m doing wrong. It does same thing if I use your code above.

                       

                      https://dl.dropboxusercontent.com/u/23443066/SW2014_issues.wmv

                        • Re: How to catch if SolidWorks was closed
                          Adam Hoffman

                          Ok, after watching the video I think I have a better understanding of what's happening. You are closing SolidWorks and not just the part document before clicking the "Open Template" button for the second time. But if you click the button before SolidWorks has had a chance to finish the shut-down procedure, then your app is trying to tell a no longer valid process to do something which can result in the errors that you are seeing. Try closing just the document itself and not SolidWorks before clicking the "Open Template" button a second time.

                           

                          Close Part.PNG

                           

                          Or you could possibly add a "Close Part" button to the app. The sldworks_fs.exe is an app that runs in the background if SolidWorks is installed on your machine whether SolidWorks is opened or not. Not sure what it does exactly though. But I believe it is not what is causing your issue.

                          • Re: How to catch if SolidWorks was closed
                            Ivana Kolin

                            if p.count > 0 then

                             

                            remove true    <---- remove this row

                             

                            else

                             

                            and if even that is not enough then you can try this :

                             

                             

                             

                                Private Shared Function getSwApp(silent As Boolean) As SldWorks

                                    ' check how many solidworks instances are running :

                                    Dim myProcess As Process = Nothing

                                    Dim iCountSw As Integer = 0

                                    For Each process2 As Process In Process.GetProcessesByName("SLDWORKS")

                                        ' check if MainWindowHandle is created, otherwise SolidWorks is not ready yet

                                        ' and the GetObject in getSldWorksObject will fail

                                        If process2.MainWindowHandle <> IntPtr.Zero Then

                                            myProcess = process2

                                            iCountSw += 1

                                        End If

                                    Next

                             

                             

                                    If iCountSw > 1 Then

                                        If silent Then

                                            Return Nothing

                                        Else

                                            Throw New ApplicationException("SolidWorks has been started multiple times.")

                                        End If

                                    ElseIf iCountSw = 1 Then

                                        ' this is ok

                                    Else

                                        If silent Then

                                            Return Nothing

                                        Else

                                            Throw New ApplicationException("SolidWorks is not started.")

                                        End If

                                    End If

                             

                             

                                    If myProcess IsNot Nothing AndAlso myProcess.HasExited Then

                                        MsgBox(myProcess.ExitTime)

                                        Throw New ApplicationException("SolidWorks is exiting.")

                                    End If

                                    ' get application :

                                    Dim swApp As SldWorks = getSldWorksObject()

                                    If swApp Is Nothing Then

                                        If silent Then

                                            Return Nothing

                                        Else

                                            Throw New ApplicationException("An instance of SolidWorks could not be retrieved.")

                                        End If

                                    End If

                                    Try

                                        If swApp.Visible Then ' generates error during solidworks exit

                                        End If

                                    Catch ex As Exception

                                        If silent Then

                                            Return Nothing

                                        Else

                                            Throw

                                        End If

                                    End Try

                                    Return swApp

                                End Function

                             

                             

                                Private Shared Function getSldWorksObject() As SldWorks

                                    Try

                                        Return GetObject(, "SldWorks.Application") ' CType(Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), sldworks.SldWorks)

                                    Catch ex As Exception

                                        Return Nothing

                                    End Try

                                End Function

                    • Re: How to catch if SolidWorks was closed
                      Ivana Kolin

                      we are using this

                      Private Shared Function getSwApp(silent As Boolean) As sldworks.SldWorks

                              ' check how many solidworks instances are running :

                              Dim iCountSw As Integer = 0

                              For Each process2 As Diagnostics.Process In Diagnostics.Process.GetProcessesByName("SLDWORKS")

                                  ' check if MainWindowHandle is created, otherwise SolidWorks is not ready yet

                                  ' and the GetObject in getSldWorksObject will fail

                                  If process2.MainWindowHandle <> IntPtr.Zero Then

                                      iCountSw += 1

                                  End If

                              Next

                              If iCountSw > 1 Then

                                  If silent Then

                                      Return Nothing

                                  Else

                                      Throw New ApplicationException("SolidWorks has been started multiple times.")

                                  End If

                              ElseIf iCountSw = 1 Then

                                  ' this is ok

                              Else

                                  If silent Then

                                      Return Nothing

                                  Else

                                      Throw New ApplicationException("SolidWorks is not started.")

                                  End If

                              End If

                              ' get application :

                              Dim swApp As sldworks.SldWorks = getSldWorksObject()

                              If swApp Is Nothing Then

                                  If silent Then

                                      Return Nothing

                                  Else

                                      Throw New ApplicationException("An instance of SolidWorks could not be retrieved.")

                                  End If

                              End If

                              Return swApp

                          End Function

                       

                       

                          Private Shared Function getSldWorksObject() As sldworks.SldWorks

                              Try

                                  Return GetObject(, "SldWorks.Application") ' CType(Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), sldworks.SldWorks)

                              Catch ex As Exception

                                  Return Nothing

                              End Try

                          End Function