Skip to content

Latest commit

 

History

History
133 lines (113 loc) · 3.55 KB

File metadata and controls

133 lines (113 loc) · 3.55 KB
title ptr::ptr | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic reference
f1_keywords
ptr::ptr
ptr.ptr
msclr.com.ptr.ptr
msclr::com::ptr::ptr
dev_langs
C++
helpviewer_keywords
ptr::ptr
ms.assetid 4f5883b4-7c0a-46c6-aa9f-4e49eed463eb
caps.latest.revision 8
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

ptr::ptr

Constructs a com::ptr to wrap a COM object.

Syntax

ptr();  
ptr(  
   _interface_type * p  
);  

Parameters

P
A COM interface pointer.

Remarks

The no-argument constructor assigns nullptr to the underlying object handle. Subsequent calls to the com::ptr will validate the internal object and silently fail until an object is actually created or attached.

The one-argument constructor adds a reference to the COM object but does not release the caller's reference, so the caller must call Release on the COM object to truly give up control. When the com::ptr's destructor is called it will automatically release its references on the COM object.

Passing NULL to this constructor is the same as calling the no-argument version.

Example

This example implements a CLR class that uses a com::ptr to wrap its private member IXMLDOMDocument object. It demonstrates usage of both versions of the constructor.

// comptr_ptr.cpp  
// compile with: /clr /link msxml2.lib  
#include <msxml2.h>  
#include <msclr\com\ptr.h>  
  
#import <msxml3.dll> raw_interfaces_only  
  
using namespace System;  
using namespace System::Runtime::InteropServices;  
using namespace msclr;  
  
// a ref class that uses a com::ptr to contain an   
// IXMLDOMDocument object  
ref class XmlDocument {  
public:  
   // construct the internal com::ptr with a null interface  
   // and use CreateInstance to fill it  
   XmlDocument(String^ progid) {  
      m_ptrDoc.CreateInstance(progid);     
   }  
  
   // construct the internal com::ptr with a COM object  
   XmlDocument(IXMLDOMDocument* pDoc) : m_ptrDoc(pDoc) {}  
  
   // note that the destructor will call the com::ptr destructor  
   // and automatically release the reference to the COM object  
  
private:  
   com::ptr<IXMLDOMDocument> m_ptrDoc;  
};  
  
// use the ref class to handle an XML DOM Document object  
int main() {  
   IXMLDOMDocument* pDoc = NULL;  
  
   try {  
      // create an XML DOM document object  
      Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,   
         CLSCTX_ALL, IID_IXMLDOMDocument, (void**)&pDoc));  
      // construct the ref class with the COM object  
      XmlDocument doc1(pDoc);  
  
      // or create the class from a progid string  
      XmlDocument doc2("Msxml2.DOMDocument.3.0");  
   }  
   // doc1 and doc2 destructors are called when they go out of scope  
   // and the internal com::ptr releases its reference to the COM object  
   catch (Exception^ e) {  
      Console::WriteLine(e);     
   }  
   finally {  
      if (NULL != pDoc) {  
         pDoc->Release();        
      }  
   }  
}  

Requirements

Header file <msclr\com\ptr.h>

Namespace msclr::com

See Also

ptr Members
ptr::CreateInstance
ptr::~ptr