Keith Rice | Technical Director | CADSharp.com | keith@cadsharp.com
This guide is provided as-is. By using it, you accept any responsibility for any consequences, intended or unintended, that result.
Introduction
This guide will show you how to create an stand-alone from scratch in VB.NET or C#. These are both .NET languages and the only difference between the two is syntax; they both actually compile down to the same language at run-time. Nevertheless, if your only experience with programming is using VBA macros, VB.NET is recommended.
Prerequisites
- SOLIDWORKS 2012 or later is installed
- Visual Studio is installed. A free edition, called Visual Studio Community, is available here.
Instructions
-
Open Visual Studio and create a new console application
- The steps below work for a Windows Form Application as well, except the code that connects to SolidWorks will need to be executed during a control event or during the form constructor.
-
Create a new folder in your solution directory called "libs" (arbitrary) and copy "SolidWorks.Interop.sldworks.dll" and any other DLLs you want (e.g., swconst, swcommands) there
-
Add references in your project to that directory. Now you don't have to worry about broken references.
-
Add the following to the top of the class or type SldWorks below, click on it, and choose the binding option.
VB.NET
Imports SolidWorks.Interop.sldworksC#
using SolidWorks.Interop.sldworks; -
Add this code to the sub-procedure / function called Main(). If you want to specify what version of SolidWorks you want to run, do not simply create "SldWorks.Application" but rather "SldWorks.Application.X", where X is the desired version year (e.g., 2016) minus 1992. Therefore, "24" is the major version number of SolidWorks 2016.
VB.NET
Dim app As SldWorks = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application.24")) If Not app Is Nothing Then app.Visible = True app.SendMsgToUser("Hello world!") End IfC#
SldWorks app = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application.24")); if (app != null) { app.Visible = true; app.SendMsgToUser("Hello world!"); } -
Build the project. The EXE can be found in \bin\.
Frequently Asked Questions
Q: How do I pass arguments via the command line?
Example: Running the following via the command line will result in the stand-alone creating a message box in SOLIDWORKS containing the string argument:
<path to exe> "I passed this argument!"
To accomplish this, use the following code in conjunction with the code presented earlier, for example after SolidWorks is made visible (if you decide to make it visible):
VB.NET
Dim args() As String = System.Environment.GetCommandLineArgs()
If args IsNot Nothing And args.Length > 1 Then
swApp.SendMsgToUser(args(1))
Else
swApp.SendMsgToUser("No arguments.")
End If
When using System.Environment.GetCommandLineArgs, the first value in the returned array is always the path to the executable.
C#
swApp.Visible = true;
if (args.Length != 0)
{
swApp.SendMsgToUser(args[0]);
}
else
{
swApp.SendMsgToUser("No arguments.");
}
Q: How do I simulate passing command line arguments into my stand-alone during debugging?
Two options:
- Create a BAT file that use to execute the stand-alone. This example assumes that your program is taking a single string argument.
START <path to executable> "test"
- In Visual Studio, go the project properties-->Debug tab-->Command line arguments.
Q: How do I return a value with my stand-alone?
Check out this answer on StackOverflow.
Q: How do I attach my stand-alone to a current SLDWORKS.EXE process instead of starting a new one?
Import System.Runtime.InteropServices and use this code:
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 Function GetSolidWorks(Optional version As Integer = -1) As SldWorks
Dim app As SldWorks = Nothing
Dim myGuid As Guid = Nothing
If version = -1 Then
myGuid = Type.GetTypeFromProgID("SldWorks.Application").GUID
Else
myGuid = Type.GetTypeFromProgID("SldWorks.Application." + version.ToString()).GUID
End If
GetActiveObject(myGuid, IntPtr.Zero, app)
If app Is Nothing Then Return Nothing Else Return app
End Function
Q: Why does creating a SOLIDWORKS instance using GetObject not load certain addins?
For example, if you create SOLIDWORKS using this line:
Dim app As SldWorks = GetObject(, "SldWorks.Application.24")
This is known bug (SPR 335683) that remains unfixed as SOLIDWORKS 2025.
You might be able to get all addins to appear with this code. (Modify the SOLIDWORKS start path as necessary.)
Sub main()
Dim t As DateTime = Now
Dim swApp As SldWorks = Nothing
Dim startInfo As New ProcessStartInfo("C:\Program Files\SolidWorks Corp\SW2013\SolidWorks\SLDWORKS.exe")
startInfo.WindowStyle = ProcessWindowStyle.Normal
startInfo.Arguments = "-m"
Process.Start(startInfo)
Do While swApp Is Nothing
Try
swApp = CType(GetObject(, "sldworks.application"), SldWorks)
Catch ex As Exception
If Math.Abs(Now.Subtract(t).Seconds) > 90 Then
Throw New ApplicationException("SolidWorks will not start" )
End If
Thread.Sleep(3000)
End Try
Loop
End Sub
Q: What are other known ways to create an instance of SOLIDWORKS?
The following approaches will return the pointer of the newly created version of SOLIDWORKS. To return pointers for multiple instances of SOLIDWORKS, see the next question.
- Using the "new" keyword
SldWorks app = new SldWorks();
This will open the version of SolidWorks that accords with the version of SolidWorks.Interop.sldworks.dll being referenced. If that version is not installed then you will get the following exception:
System.Runtime.InteropServices.COMException
HResult=0x80040154
Message=Retrieving the COM class factory for component with CLSID {13551C8B-F74D-493E-B550-489532B7A905} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
- Using Interaction.CreateObject().
Dim App As SldWorks = CreateObject("SldWorks.Application.25")
This appears to behave the same as Activator.CreateInstance().
Q: How can I create multiple instances of SOLIDWORKS that can be controlled independently via my program?
You will need to use the Running Object Table to accomplish this. We have functions that accomplish this and return separate pointers for each SOLIDWORKS instance in CADSharpTools.