Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C++
Article

A COM Smart Pointer

Rate me:
Please Sign up or sign in to vote.
3.18/5 (19 votes)
22 Feb 20062 min read 195.2K   1.7K   38   18
A wrapper class to any COM interface pointer.

Introduction

CComPtr wraps any interface pointer and will call AddRef() and Release() properly. That is, you don't need to worry about controlling the lifetime of your interface pointer.

Details

  1. ATL does provide a Smart Pointer class named CComQIPtr. But it uses some ATL related funtion so that it can be used only in an ATL environment.
  2. VC does provide _com_ptr_t & _bstr_ptr_t keywords to provide some kind of a wrapper. But it depends on the MS VC compiler, without which you can benefit.
  3. My CComPtr provides compiler unrelated features to wrap any interface pointer except IUnknown. Later I will explain why this limitation comes.

Illustration

Note: INTERFACE and piid are passed into the class CComPtr through template parameters.

CComPtr has four constructors to meet different requirement.

  • CComPtr()

Simply constructor, do nothing.

  • CComPtr(INTERFACE* lPtr)

Construct a new CComPtr object with an existing interface pointer. After that this new CComPtr object can be used as interface pointer itself.

  • CComPtr(IUnknown* pIUnknown, IID iid)

Construct a new CComPtr object with an IUnknown pointer and an IID, which represents the interface you are interested in. Internally constructor will call pIUnknown->QueryInterface to fetch interface pointer according to the IID.

  • CComPtr(const CComPtr<INTERFACE, piid>& RefComPtr)

This contsructor takes another existing CComPtr object as parameter. After that, clone the old CComPtr object.

CComPtr has one destructor, which will release interface pointer.

~CComPtr(){ if (m_Ptr) { m_Ptr->Release(); m_Ptr = NULL; } }

CComPtr has five operators.

  • (a) Operator INTERFACE* : Returns interface pointer wrapped by class CComPtr.

Example:

CComPtr<IWavePciStream> m_wavePciStreamPtr; 
IWavePciStream* pWavePciStream = (IWavePciStream*)m_wavePciStreamPtr;
  • (b) Operator *: Used to fetch interface object wrapped by class CComPtr.

Example:

CComPtr<IWavePciStream> m_wavePciStreamPtr; 
IWavePciStream wavePciStream = *m_wavePciStreamPtr;
  • (c) Operator &: Used to fetch the pointer to interface pointer wrapped by class CComPtr.

Example:

CComPtr<IWavePciStream> m_wavePciStreamPtr; 
IWavePciStream** ppWavePciStream = &m_wavePciStreamPtr;
  • (d) Operator -> : Used to make CComPtr object act as a TRUE interface pointer.

Example

CComPtr<IWavePciStream> m_wavePciStreamPtr; 
m_wavePciStreamPtr->AnyPciStreamMethods();
  • (e) Operator = : Used to transfer an exsiting interface pointer information to a new CComPtr object. This operator has three different parameter list.
    1. In parameter is INTERFACE*
    2. In parameter is another CComPtr object.
    3. In parameter is an interface pointer to IUnknown, which will be used to query other interface pointer defined by piid.

CComPtr has another three methods.

  • Attach : Used to attach an existing interface pointer to a CComPtr object.
  • Detach : Used to detach interface pointer from a CComPtr object.
  • Release : Used to release wrapped interface pointer explicitly.

CComPtr is implemented in pure C++. That is, it does not depend on any specific C/C++ compiler and can be compiled under any ANSI C/C++ compliant compiler. I have tested in VC++ 6.0. I do believe it works in GNU C/C++ and MAC CodeWarrior.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Program Manager Microsoft
China China
Graduated from Shanghai Tongji university in 1997 with bachelor degree in computer science, Wenbin got his first job as a system engineer at Bell Alcatel Mobile Shanghai office responsible for telcom-class mobile system development and deployment. Since then, Wenbin has in turn worked for Intel and Microsoft both inside and outside China, first as senior engineer, later project manager and then senior product manager.

With 15-year experience working with the world top IT companies, Wenbin has developed solid skill in C/C++, C#, Java, software engineering, agile development, etc, and multiple talents in product management, public presentation and speech, etc. He has always been an active member at PMI (Project Management Institution) and a regular lecture at Intel Developer Forum, Microsoft TechED conference as well as many other world-class industrial conferences. His wide-ranged industrial practice and high-level personal maturity have made him one of the best in public speech and professional training.

Over the years, Wenbin has cultivated his very own style in public speech, which is considered informative, engaging and refined. Since last year, Wenbin has also taken new adventure in project and product management consulting business and has proven high capacity through his work with many local emergent IT firms. Wenbin’s specialty in management consulting is on project management methodologies and processes, project management tools (e.g. MS Project), and team recruitment, build, and motivation.

In addition, Wenbin has received many professional qualifications including MCSE (Microsoft Certified System Engineer), MCSD (Microsoft Certified System Developer), MCDBA (Microsoft Certified Database Administrator), SCJP 2 (Sun Certified Java Programmer 2), and PMP (PMI Certified Project Management Professional). On top of that, Wenbin has got several on-duty inventions and one of them was successfully patented by United States Patent and Trademark Office.

Comments and Discussions

 
GeneralMy vote of 5 Pin
johnsonpm26-Apr-11 21:02
johnsonpm26-Apr-11 21:02 
NewsReconsider your stuff [modified] PinPopular
cnstrnd18-Oct-06 15:19
cnstrnd18-Oct-06 15:19 
GeneralComet Pin
Jim Crafton22-Feb-06 8:47
Jim Crafton22-Feb-06 8:47 
GeneralRe: Comet - looks good! Pin
Skeeter22-Feb-06 11:10
Skeeter22-Feb-06 11:10 
GeneralRe: Comet - looks good! Pin
Jim Crafton22-Feb-06 13:42
Jim Crafton22-Feb-06 13:42 
Not really sure. I've started to use it for some stuff in my projects, and on the forums, there's a link to a newer version, but I don't why the development seems to have stopped. But even in it's current state it's quite useful.


¡El diablo está en mis pantalones! ¡Mire, mire!

Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

SELECT * FROM User WHERE Clue > 0
0 rows returned

Save an Orange - Use the VCF!
GeneralThis is buggy and in efficient as hell Pin
goldsam28-Oct-05 7:46
goldsam28-Oct-05 7:46 
GeneralRe: This is buggy and in efficient as hell Pin
Skeeter21-Feb-06 11:08
Skeeter21-Feb-06 11:08 
GeneralDCOM Pin
javn4-Aug-03 14:01
javn4-Aug-03 14:01 
GeneralSome bugs here Pin
Tim Smith17-Mar-02 4:52
Tim Smith17-Mar-02 4:52 
GeneralRe: Some bugs here Pin
Nish Nishant17-Mar-02 13:31
sitebuilderNish Nishant17-Mar-02 13:31 
Questionis this new? Pin
1-Mar-02 4:55
suss1-Mar-02 4:55 
AnswerRe: is this new? Pin
Tim Smith1-Mar-02 5:05
Tim Smith1-Mar-02 5:05 
GeneralATL version Pin
21-Oct-01 23:13
suss21-Oct-01 23:13 
GeneralRe: ATL version Pin
5-Nov-01 10:16
suss5-Nov-01 10:16 
GeneralYou're just coping the info in MSDN Pin
16-Oct-01 3:59
suss16-Oct-01 3:59 
GeneralRe: You're just coping the info in MSDN Pin
Andrew Peace16-Oct-01 11:25
Andrew Peace16-Oct-01 11:25 
GeneralRe: You're just coping the info in MSDN Pin
Skeeter16-Oct-01 15:01
Skeeter16-Oct-01 15:01 
GeneralRe: You're just coping the info in MSDN Pin
22-Oct-01 0:21
suss22-Oct-01 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.