Welcome
Collapse Getting StartedGetting Started
Overview
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
Arrays
Attributes Imported from ACIS SAT Files
Bitmasks
Block Definitions and Block Instances
Bodies in Body Folders
BOOL and VARIANT_BOOL
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
Manipulators
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
SQLite
STL Container Classes and Smart Pointers
Suspend Automatic Rebuilds
swdisp.cpp and swdisp.h Files
System Options and Document Properties
Tessellation and Edges
Tracking IDs
Expand Type LibrariesType Libraries
VBA and SolidWorks x64
Units
Unmanaged C+ and C++/CLI Code Differences
Examples and Projects
Expand SolidWorks API HelpSolidWorks API Help
Expand FeatureWorks API HelpFeatureWorks API Help
Expand SolidWorks Document Manager API HelpSolidWorks Document Manager API Help
Expand eDrawings API HelpeDrawings API Help
Expand SolidWorks Routing API HelpSolidWorks Routing API Help
Expand SolidWorks Simulation API HelpSolidWorks Simulation API Help
Expand SolidWorks Utilities API HelpSolidWorks Utilities API Help
Expand SolidWorks Workgroup PDM API HelpSolidWorks Workgroup PDM API Help
Hide Table of Contents

Smart Pointers

Microsoft's Active Template Library (ATL) contains helper classes that simplify the use of commonly used COM interfaces, components, and structures, such as BSTRs, interface pointers, and variants.

The ATL CComPtr helper class provides a smart pointer wrapper around COM interface pointers. In this context, a smart pointer is a pointer that properly releases its reference counts toward COM interfaces when it goes out of scope.

To instantiate the interface types that you will be accessing, you use CComPtr with the interface type. For example:

CComPtr<ISldWorks> swApp

CComPtr<IModelDoc2> swModel

CComPtr<ISelectionMgr> swSelMgr

You can assign an interface pointer to your smart pointer in any of the following ways:

  • Call the smart pointer's CoCreateInstance member function.

  • Pass the interface's raw interface pointer to CoCreateInstance.

  • Call the smart pointer's Attach member function to assign a previously created interface pointer to the smart pointer. This does not cause the smart pointer to call the smart pointer's AddRef function through the new interface pointer.

  • Simple assignment through the smart pointer's assignment operator causes the smart pointer to call AddRef through the new interface pointer.

Calling the smart pointer's CoCreateInstance member function is the simplest way to create a new interface pointer. For example, to attach to the currently running instance of the sldworks.exe COM server on your local computer, use:

hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);

Like most other smart pointers, CComPtr overrides C++ operators to make the smart pointers act like normal C++ pointers.

To set a smart pointer to the value of a pointer returned from a function, you can use the & operator, which works just like a non-smart pointer. If you need to decrement a reference count, use the smart pointer's Release member function.

To use ATL helper classes in your program, include atlbase.h in stdafx.h. For an example of using smart pointers, see Access Assembly Example (C++ COM).

For more information about using smart pointers with container classes, see STL Container Classes and Smart Pointers.

For more information about CComPtr and smart pointers, see MSDN.

Source: Williams, Mickey and Bennett, David. Visual C++ Unleashed. Sams Publishing, 2000.

 

 



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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Smart Pointers
*Comment:  
*   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:

x

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
x

Web Help Content Version: API Help (English only) 2012 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.