← LibrarySolidWorks API: Add-in Architecture and the Integration ModelEngineering · ComputersLesson 1/10← PrevNext →
GuidePublished 4 Aug 20268 min readBy Kevin JoginSolidWorks APICAD automationCOM interopadd-in development

KEVOS® Knowledge Library · SolidWorks API Add-in Development

Add-in Architecture and the SolidWorks Integration Model

What actually makes an add-in an add-in — the interface contract, the COM registration it depends on, and the load-to-release lifecycle that every well-behaved SolidWorks® extension must honour.

01Executive summary

A SolidWorks add-in is not defined by what it looks like to the user. It is defined by a contract. A managed class earns the name add-in when three conditions hold simultaneously: it implements the ISwAddin interface, it correctly handles the connect and disconnect calls that interface demands, and it is registered with COM so the operating system can hand it back to SolidWorks on request. Everything that fails any one of those three tests is, by definition, a stand-alone program — regardless of whether it drives SolidWorks, ships as a DLL, or looks integrated.

That definition matters commercially as well as technically. It determines whether your code runs inside the SolidWorks process or beside it, whether it can receive notifications, which API members are reachable, and how much installer engineering you will be signing up for. This page sets out the architecture before any code is written, because the decision is expensive to reverse.

3conditions that define an add-in
2interface members you must implement
1process shared with SolidWorks
2registry hives touched at registration

02The three-part definition

The interface contract is deliberately small. ISwAddin asks for two members and nothing else: one invoked when SolidWorks loads your assembly into memory, and one invoked when it unloads it. All of the apparent complexity of add-in development — menus, pages, notifications, installers — is built on top of that pair.

InterfaceISwAddin
bool ConnectToSW(object ThisSW, int Cookie)
bool DisconnectFromSW()
ThisSW
The live SolidWorks application object for the session that loaded you. Functionally identical to the application pointer used in a macro or stand-alone client; cast it to the interface type (ISldWorks) and hold it as a field.
Cookie
The session-unique identifier SolidWorks assigns to your add-in. It is the routing key for every callback and for acquiring the Command Manager. Store it; you cannot obtain it again later.
Return value
true tells SolidWorks the transition succeeded. Returning false from the connect call signals a failed load and should be reserved for genuine initialisation failure.

The second condition is COM registration. A managed class library is invisible to SolidWorks until the assembly is registered for COM interop and the class is marked visible with a stable GUID. The third condition is discovery: an entry under the SolidWorks add-ins registry key, named for that same GUID, so the application knows a candidate exists and can ask COM to produce it. Miss any one and you have a correctly compiled assembly that never loads.

Definition in practice

If a colleague describes their DLL as an add-in because it appears in a toolbar, ask whether it implements ISwAddin. Toolbars can be created from a stand-alone client too. The interface, the connect and disconnect handling, and COM registration are the whole test.

03Load-to-release lifecycle

Understanding the order of events removes most of the mystery from add-in debugging. The sequence below runs on every SolidWorks session in which your add-in is enabled.

  1. Build timeAssembly compiled and registered

    The class library is compiled to a DLL. Registration for COM interop publishes the class under its GUID and runs your registration hook, which writes the SolidWorks discovery keys.

  2. Session startSolidWorks enumerates candidates

    SolidWorks reads its add-ins registry key, collects the GUIDs it finds and consults the per-user start-up key to decide which of them to load immediately rather than on demand.

  3. ActivationCOM instantiates the class

    The operating system resolves the GUID to your assembly, the runtime loads it into the SolidWorks process, and an instance of your add-in class is created.

  4. ConnectConnectToSW is called

    You receive the application object and the cookie. This is where you register callback routing, acquire the Command Manager, build menus and toolbars, and attach application-level event handlers.

  5. RuntimeCallbacks and notifications flow

    Menu clicks, page control events and document notifications are dispatched into your instance for as long as the add-in stays loaded.

  6. DisconnectDisconnectFromSW is called

    Triggered by the user unloading the add-in or by SolidWorks closing. Remove command groups, detach every event handler, close any open pages and release interop references.

  7. ReleaseReferences dropped and memory reclaimed

    Null every field holding a SolidWorks object and request collection so the runtime releases the underlying COM references promptly rather than at an arbitrary later point.

Teardown is not optional

Command groups that are not removed on disconnect stay in the SolidWorks menus and toolbars. When the user clicks one, the callback target no longer exists and the failure surfaces inside SolidWorks rather than inside your add-in. Treat the disconnect path as the mirror image of the connect path and build them together.

04The in-process model and what it buys you

An add-in is loaded by SolidWorks into the SolidWorks process and runs on the same thread. That single fact produces most of the practical differences between add-ins and stand-alone clients.

Consequence

Call overhead falls away

Method calls do not cross a process boundary, so marshalling cost is materially lower. On calculation-heavy work over an active model — traversals, repeated property reads, geometry queries — the difference compounds.

Consequence

The full API surface opens

A small set of members are only meaningful in-process, including preview and preview-bitmap operations. If your requirement touches them, the architecture decision is already made for you.

Consequence

Callbacks become available

Only a registered add-in can receive callbacks and notifications. A stand-alone client can poll, but it cannot be told that a document changed, a selection was made or a file was saved.

Consequence

Your faults become SolidWorks faults

An unhandled exception in your code executes inside the host process. Defensive error handling is not a nicety; it is the difference between a warning dialog and a lost modelling session.

Design rule

Because you share the host's process and thread, treat every callback body as if it were running inside SolidWorks itself — which it is. Keep them short, wrap anything that can throw, and push long-running work behind an explicit user action rather than an event.

05The object graph you will be working in

Almost every add-in resolves the same small set of root objects during connect, then reaches the rest of the model through them.

C# — minimal add-in skeletonIllustrative
[Guid("00000000-0000-0000-0000-000000000000"), ComVisible(true)]
[SwAddin(Title = "KEVOS Example",
         Description = "Demonstration add-in",
         LoadAtStartup = true)]
public class KevosAddin : ISwAddin
{
    private ISldWorks _sw;
    private int _cookie;

    public bool ConnectToSW(object ThisSW, int Cookie)
    {
        _sw = (ISldWorks)ThisSW;
        _cookie = Cookie;

        // Route callbacks back to this instance before anything
        // that can raise one is created.
        _sw.SetAddinCallbackInfo(0, this, _cookie);

        BuildCommands();
        AttachApplicationEvents();
        return true;
    }

    public bool DisconnectFromSW()
    {
        DetachApplicationEvents();
        RemoveCommands();

        _sw = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        return true;
    }
}

07Releasing interop references

Interop objects hold references to unmanaged COM objects. Leaving them to be collected whenever the runtime feels like it produces the classic symptom set: SolidWorks refuses to close cleanly, an orphaned process lingers, or documents stay locked after the add-in unloads.

  1. Null the fields

    Set every field holding a SolidWorks type — application, model, component, page and control references — to null in the teardown path.

  2. Detach first

    Remove event handlers before nulling the object that raises them, otherwise the handler keeps the source alive.

  3. Force collection

    Request a collection so the release happens at a moment you choose rather than at an arbitrary later point in the host process.

  4. Verify on exit

    Close SolidWorks with the add-in loaded and confirm the process actually terminates. This single test catches most reference leaks.

08Quick reference

Add-in test
Implements ISwAddin + handles connect/disconnect + registered with COM. All three, or it is a stand-alone.
Interface members
ConnectToSW(object, int) and DisconnectFromSW(), both returning bool.
Assembly type
Class library (DLL). A stand-alone client is an executable.
Process model
In-process; same process and thread as the host application.
Discovery
Registry entry named for the class GUID under the SolidWorks add-ins key, plus a per-user start-up flag.
Callback routing
SetAddinCallbackInfo(0, this, Cookie), called during connect.
Teardown obligations
Detach events, remove command groups, close pages, null references, collect.

09Where this leads

Continue in this pathway

Continue learning

NEXT LESSON →SolidWorks API: Development Environment and Project ConfigurationGuide · ComputersSolidWorks API: COM Registration and Add-in DiscoveryGuide · ComputersSolidWorks API: Command Manager, Menus and ToolbarsGuide · ComputersSolidWorks API: Property Manager Pages and the Control ModelGuide · Computers