Collapse Getting StartedGetting Started
Expand Types of SolidWorks API ApplicationsTypes of SolidWorks API Applications
Expand SolidWorks API Object Model and Class HierarchySolidWorks API Object Model and Class Hierarchy
Collapse Programming with the SolidWorks APIProgramming with the SolidWorks API
Expand Add-insAdd-ins
API Dependent on SolidWorks Being Visible
Attributes Imported from ACIS SAT Files
Block Definitions and Block Instances
Bodies in Body Folders
Bounding Box and Note Text
COM vs. Dispatch
CommandManager and CommandGroups
Components, Configurations, and Suppression States
Compound Note
ComVisibleAttribute in VSTA macros
Controls, Visual Basic 6.0, and Windows XP
Expand Double Arrays and Integer PairsDouble Arrays and Integer Pairs
Drawing Views and Model Entities
Early and Late Binding
Expand EventsEvents
Features of Components
Helper Functions
Hole Wizard Features and Objects
IDispatch Object Arrays as Input in .NET
Implementation Guidelines
Import and Export File Options
In-process Methods
Instantiate ActiveX Controls as Tabs
Interface Pointers
Keystrokes and Accelerator Keys
Library Features and Objects
Lightweight Components
Line Attributes for View::GetPolyLinesAndCurves
Line Weights
Long vs. Integer
Expand Macro FeaturesMacro Features
Mass Properties
Expand Multibody PartsMultibody Parts
Option Explicit Statement
Partition Rollback and API Handles
Persistent Reference IDs
Presentation Transforms
Expand PropertyManager PagesPropertyManager Pages
.NET Interop Assemblies
Quick Tips and Bubble ToolTips
Return Values
Expand SafeArraysSafeArrays
Selection Criteria
Selection Lists
Selections that Define Features
Setup Project to Distribute SolidWorks Add-in
Smart Pointers
SolidWorks Objects
Sorting Tables
STL Container Classes and Smart Pointers
Suspend Automatic Rebuilds
System Options and Document Properties
Tessellation and Edges
Tracking IDs
Expand Type LibrariesType Libraries
Unmanaged C++ and C++/CLI Code
VBA and SolidWorks x64
Examples and Projects
Expand SolidWorks API HelpSolidWorks API Help
Expand FeatureWorks API HelpFeatureWorks API Help
Expand SolidWorks Costing API HelpSolidWorks Costing API Help
Expand SolidWorks Document Manager API HelpSolidWorks Document Manager API Help
Expand SolidWorks Routing API HelpSolidWorks Routing API Help
Expand SolidWorks Simulation API HelpSolidWorks Simulation API Help
Expand SolidWorks Sustainability API HelpSolidWorks Sustainability API Help
Expand SolidWorks Toolbox API HelpSolidWorks Toolbox API Help
Expand SolidWorks Utilities API HelpSolidWorks Utilities API Help
Expand SolidWorks Workgroup PDM API HelpSolidWorks Workgroup PDM API Help
Expand eDrawings API HelpeDrawings API Help
Hide Table of Contents

Interface Pointers

Interface pointers can be an area of confusion with unmanaged C++ programming. Each SolidWorks API method that returns an interface pointer automatically increments the reference count on the interface pointer by 1.

  • COM implementations. You can call a SolidWorks API that returns an interface pointer. You can then use this pointer as you like, but you are responsible for releasing it.

This unmanaged C++ COM example demonstrates how to handle interface pointers. 



// Get the IModelDoc pointer

HRESULT res = UserApp->getSWApp()->get_IActiveDoc( &m_ModelDoc );  


if( m_ModelDoc == NULL )




// Get the IPartDoc pointer

res = m_ModelDoc->QueryInterface(IID_IPartDoc, (LPVOID *)&m_PartDoc);

ASSERT( res == S_OK );



// Use the interface pointers within your code and then release them when done



m_ModelDoc->Release(); // Release the IModelDoc2 pointer

m_PartDoc->Release(); // Release the IPartDoc pointer




NOTE: LPINTERFACE is a typedef and is pointer to the named interface. e.g., LPPARTDOC points to the PartDoc interface, LPMODELDOC2 is a pointer to the ModelDoc2 interface, etc.

You can also use smart pointers with COM implementations. CComPtr is an ATL smart pointer. IInterfacePtr (e.g., IModelDoc2Ptr) is also a smart pointer that does many of the same things as CComPtr, but it is not part of ATL. It is a typedef that resolves to a _com_ptr_t  (defined in comip.h) using  _COM_SMARTPTR_TYPEDEF. For more information about using smart pointers with container classes, see STL Container Classes and Smart Pointers.

For an example of using smart pointers, see Access Assembly Example (C++ COM). For an example of IInterfacePtr, see Get Spline Points Example (C++ COM).

  • Dispatch implementations. The release of the interface pointer is hidden in the destructor of the Dispatch objects (for example, IModelDoc2, IFace2, and so on). This implies that attaching an interface pointer to more than one of these Dispatch objects will cause a release to be performed by each of the objects as they go out of scope. This will cause a problem because the reference count was only incremented once when the interface pointer was returned to you.

To avoid this problem, you must manually increment the reference count ( pdisp->AddRef(); ) if you are attaching the interface pointer to more than one object.

This C++ Dispatch example demonstrates how to handle the reference count on interface pointers. 



// Get interface pointer to the active document

modDisp = UserApp->getSWApp()->GetActiveDoc(); 

// Reference count on modDisp automatically incremented by 1


if( modDisp == NULL )



// Attach to the IModelDoc2 object

IModelDoc2 m_ModelDoc( modDisp ); 


// Attach to the IPartDoc object

IPartDoc m_PartDoc( modDisp ); 

// Manually increment the reference count on modDisp because

// you will use modDisp a second time




// Use objects within your code



// Variables go out of scope and destructor called for IModelDoc2 and IPartDoc,

// which will decrement the reference count on modDisp by two


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Interface Pointers
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2013 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.