6 Replies Latest reply on Oct 24, 2018 4:31 AM by Gertjan Van Dijk

    Connecting to Solidworks instance without creating a new one

    J. R.

      Hi,

       

      Making an addon for Solidworks (external executable, written in VB.NET), I always used to set swApp using = CreateObject("SldWorks.Application"). It worked, with a little issue of creating an invisible instance of Solidworks if it wasn't running when I ran my application. However, certain a person using my application with Solidworks Student Version has reported an error which indicates that my application cannot connect to his Solidworks instance. As I do not have access to a Student Version, I cannot check why, but I suspect the problem lies in this specific line. Can somebody advise me how should I set the swApp so it always connects to the running Solidworks instance, regardless of the version, and does not create a Solidworks ghost instance if my application was executed prior to running Solidworks?

       

      I have tried the following lines, which I found in the official Solidworks API page, as well as various forum threads:

      swApp = CreateObject("SldWorks.Application")  //didn't work with the Student Version, also creates a ghost instance of Solidworks;

      swApp = Application.SldWorks //gives a warning "'SldWorks' is not a member of 'Application'

      swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) //also creates a ghost instance of Solidworks;

      swApp = GetObject(, "SldWorks.Application") //gives a "Cannot create ActiveX component" error;

      swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks) '= Application.SldWorks //gives a "Operation unavailable" error.

       

      Can somebody point out the right way to do this, please?

          • Re: Connecting to Solidworks instance without creating a new one
            J. R.

            After checking out that article, I tried using swApp = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject(PROG_ID), SolidWorks.Interop.sldworks.ISldWorks), and it worked for me, but not for the person who tried running my addon on Solidworks Student Edition. He reported that after he installed Solidworks API SDK, my app version with swApp = CreateObject("SldWorks.Application") started working.
            Do you have a comment on this? Why wouldn't the Marshal method work, and CreateObject work after installing the API SDK?

            • Re: Connecting to Solidworks instance without creating a new one
              J. R.

              I figured it out. The CreateObject method (as well as the GetObject) creates a background (invisible) instance of Solidworks if it cannot detect a running Solidworks version. And the next time this method (or the Marshal method) is called, it tries to connect to that background instance, rather than the instance user has initiated, and therefore swApp cannot be set correctly. Terminating these background instances of Solidworks, and leaving just the visible one, fixes the problem, and allows the Marshal method (as well as every other method) to connect properly and assign the swApp variable. This means that the problem wasn't related to the Student Edition, or the installation of API SDK at all. Thank you for posting that link, I will use the Marshal method in my application from now on, so the problem never comes up again.

            • Re: Connecting to Solidworks instance without creating a new one
              Keith Rice

              Activator.CreateInstance() should work so there is probably something deeper going on here.

               

              You can also try this after you import System.Runtime.InteropServices:

               

              Private Declare Function GetActiveObject Lib "oleaut32.dll" (ByRef rclsid As Guid, ByVal pvReserved As IntPtr, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppunk As Object) As Integer
              
                  Public Shared Function GetSolidWorks() As SldWorks
                      Dim app As SldWorks = Nothing
                      Dim myGuid As Guid = myGuid = Type.GetTypeFromProgID("SldWorks.Application").GUID
                      GetActiveObject(myGuid, IntPtr.Zero, app)
                      Return app
                  End Function
              

               

              Keith

              SOLIDWORKS API Training and Services

                • Re: Connecting to Solidworks instance without creating a new one
                  J. R.

                  I figured it out. The CreateObject method (as well as the GetObject) creates a background (invisible) instance of Solidworks if it cannot detect a running Solidworks version. And the next time this method (or the Marshal method) is called, it tries to connect to that background instance, rather than the instance user has initiated, and therefore swApp cannot be set correctly. Terminating these background instances of Solidworks, and leaving just the visible one, fixes the problem, and allows the Marshal method (as well as every other method) to connect properly and assign the swApp variable. This means that the problem wasn't related to the Student Edition, or the installation of API SDK at all. Thank you for providing this method, I will write it down and use if I ever get a problem with the Marshal method.

                    • Re: Connecting to Solidworks instance without creating a new one
                      Gertjan Van Dijk

                      In order to prevent issues like this; i always first count the amount of active solidworks instances at the start of my standalones, and flat-out refuse to start the program unless there is exactly 1 (no more, no less) solidworks instance.

                       

                      This prevents the issue of starting a "ghost" instance, and it also prevents another issue, where the standalone might connect to the wrong instance.

                       

                      snippet (C#):

                       

                      using System;

                      using System.Diagnostics;

                       

                      static void ConnectToSW()

                      {

                      //we need exactly 1 running solidworks process; make sure.

                      Process[] runningProcesses;

                       

                      runningProcesses = Process.GetProcessesByName("SLDWORKS");

                       

                      switch (runningProcesses.Count())

                      {

                      case 0:

                      System.Windows.Forms.MessageBox.Show("No SolidWorks process found");

                      System.Environment.Exit(1);

                      break;

                      case 1:

                      _swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks;

                      //connect to a running solidworks, or start a new one (starting doesn't happen in this case)

                      return;

                      default:

                      // case > 2

                      System.Windows.Forms.MessageBox.Show("More than one SolidWorks process found; close the exesses and try again");

                      System.Environment.Exit(1);

                      break;

                      }

                      }