--- title: "com::ptr Class | 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: - "com::ptr" - "msclr::com::ptr" - "msclr.com.ptr" - "com.ptr" dev_langs: - "C++" helpviewer_keywords: - "ptr class" ms.assetid: 0144d0e4-919c-45f9-a3f8-fbc9edba32bf caps.latest.revision: 10 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" --- # com::ptr Class A wrapper for a COM object that can be used as a member of a CLR class. The wrapper also automates lifetime management of the COM object, releasing all owned references on the object when its destructor is called. Analogous to [CComPtr Class](../atl/reference/ccomptr-class.md). ## Syntax ``` template ref class ptr; ``` #### Parameters `_interface_type` COM interface. ## Remarks A `com::ptr` can also be used as a local function variable to simplify various COM tasks and to automate lifetime management. A `com::ptr` cannot be used directly as a function parameter; use a [Tracking Reference Operator](../windows/tracking-reference-operator-cpp-component-extensions.md) or a [Handle to Object Operator (^)](../windows/handle-to-object-operator-hat-cpp-component-extensions.md) instead. A `com::ptr` cannot be directly returned from a function; use a handle instead. ## Example This example implements a CLR class that uses a `com::ptr` to wrap its private member `IXMLDOMDocument` object. Calling the public methods of the class results in calls to the contained `IXMLDOMDocument` object. The sample creates an instance of an XML document, fills it with some simple XML, and does a simplified walk of the nodes in the parsed document tree to print the XML to the console. ``` // comptr.cpp // compile with: /clr /link msxml2.lib #include #include #import 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); } void LoadXml(String^ xml) { pin_ptr pinnedXml = PtrToStringChars(xml); BSTR bstr = NULL; try { // load some XML into the document bstr = ::SysAllocString(pinnedXml); if (NULL == bstr) { throw gcnew OutOfMemoryException; } VARIANT_BOOL bIsSuccessful = false; // use operator -> to call IXMODOMDocument member function Marshal::ThrowExceptionForHR(m_ptrDoc->loadXML(bstr, &bIsSuccessful)); } finally { ::SysFreeString(bstr); } } // simplified function to write just the first xml node to the console void WriteXml() { IXMLDOMNode* pNode = NULL; try { // the first child of the document is the first real xml node Marshal::ThrowExceptionForHR(m_ptrDoc->get_firstChild(&pNode)); if (NULL != pNode) { WriteNode(pNode); } } finally { if (NULL != pNode) { pNode->Release(); } } } // note that the destructor will call the com::ptr destructor // and automatically release the reference to the COM object private: // simplified function that only writes the node void WriteNode(IXMLDOMNode* pNode) { BSTR bstr = NULL; try { // write out the name and text properties Marshal::ThrowExceptionForHR(pNode->get_nodeName(&bstr)); String^ strName = gcnew String(bstr); Console::Write("<{0}>", strName); ::SysFreeString(bstr); bstr = NULL; Marshal::ThrowExceptionForHR(pNode->get_text(&bstr)); Console::Write(gcnew String(bstr)); ::SysFreeString(bstr); bstr = NULL; Console::WriteLine("", strName); } finally { ::SysFreeString(bstr); } } com::ptr m_ptrDoc; }; // use the ref class to handle an XML DOM Document object int main() { try { // create the class from a progid string XmlDocument doc("Msxml2.DOMDocument.3.0"); // stream some xml into the document doc.LoadXml("persnickety"); // write the document to the console doc.WriteXml(); } catch (Exception^ e) { Console::WriteLine(e); } } ``` ```Output persnickety ``` ## Requirements **Header file** \ **Namespace** msclr::com ## See Also [C++ Support Library](../dotnet/cpp-support-library.md) [ptr Members](../dotnet/ptr-members.md)