All Resources
Code Snippet.NET2026-03-20

SolidWorks Connection Manager with Mock Fallback

Thread-safe singleton that manages the SolidWorks COM connection with registry-based detection and automatic mock mode fallback for development without SolidWorks.

csharp
using Microsoft.Win32;
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;

public class SolidWorksManager
{
    private static SolidWorksManager? _instance;
    private static readonly object _lock = new();
    private ISldWorks? _swApp;

    public bool IsInMockMode { get; private set; }

    public static SolidWorksManager Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    _instance ??= new SolidWorksManager();
                }
            }
            return _instance;
        }
    }

    private SolidWorksManager()
    {
        if (!IsSolidWorksInstalled())
        {
            IsInMockMode = true;
            return;
        }
        ConnectToSolidWorks();
    }

    private bool IsSolidWorksInstalled()
    {
        using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\SolidWorks");
        return key != null;
    }

    private void ConnectToSolidWorks()
    {
        try
        {
            // Try running instance first
            _swApp = (ISldWorks)Marshal.GetActiveObject("SldWorks.Application");
        }
        catch
        {
            var type = Type.GetTypeFromProgID("SldWorks.Application");
            if (type != null)
                _swApp = (ISldWorks?)Activator.CreateInstance(type);
        }
    }

    public T? ExecuteOperation<T>(Func<ISldWorks, T> operation)
    {
        if (IsInMockMode || _swApp == null) return default;
        try { return operation(_swApp); }
        catch { return default; }
    }
}

How It Works

The SolidWorksManager singleton ensures only one COM connection to SolidWorks exists across your entire application. It uses double-checked locking for thread safety — the _instance null check happens outside the lock for performance, with a second check inside to prevent race conditions.

Registry Detection

Before attempting a COM connection, the manager checks if SolidWorks is installed by looking for the HKLM\SOFTWARE\SolidWorks registry key. This avoids the slow 5-10 second timeout you'd get from a failed GetActiveObject call on a machine without SolidWorks.

COM Connection Strategy

Two approaches, tried in order:

  1. GetActiveObject — attaches to a running SolidWorks instance. Fast (instant), preferred when a user already has SolidWorks open.
  2. CreateInstance — launches a new SolidWorks process. Slow (10-30 seconds), used as fallback.

.NET 8 Note

Marshal.GetActiveObject was removed in .NET 5+. For .NET 8 projects, use P/Invoke:

[DllImport("oleaut32.dll")]
static extern int GetActiveObject(
    ref Guid rclsid, IntPtr pvReserved, out object ppunk);

Or use the COMHelper pattern that wraps CLSIDFromProgID + GetActiveObject.

Mock Mode

When SolidWorks is not installed, IsInMockMode is set to true. This enables:

  • UI development without SolidWorks — your WPF app launches normally, controls work, only SolidWorks operations are no-ops
  • Unit testing — test your business logic without requiring a SolidWorks license on the CI server
  • Demo mode — show the application to stakeholders who don't have SolidWorks installed

The ExecuteOperation Pattern

The generic ExecuteOperation<T> method wraps any SolidWorks API call with:

  • Null check on the ISldWorks instance
  • Mock mode bypass (returns default instead of crashing)
  • Exception swallowing for COM failures (SolidWorks COM calls can throw unexpectedly)

Usage:

var title = SolidWorksManager.Instance
    .ExecuteOperation(sw => sw.GetActiveDoc2()?.GetTitle());
C#.NETSingletonSolidWorks APIDesign Pattern