Udostępnij za pośrednictwem


com::ptr Class

Otoki dla obiektu COM, który może być używany jako członek klasy CLR.Otoka również automatyzuje zarządzanie okres istnienia obiektu COM, zwalniając własnością wszystkich odwołań do obiektu, wywołanie destruktora jej.Analogiczne do Klasa CComPtr.

template<class _interface_type>
ref class ptr;

Parametry

  • _interface_type
    Interfejs COM.

Uwagi

A com::ptr można również jako zmiennej lokalnej funkcji różnych zadań COM uprościć i zautomatyzować zarządzanie okres istnienia.

A com::ptr nie mogą być użyte bezpośrednio jako parametr funkcji; use a Operator odwołania śledzenia (C++ Component Extensions) or a Operator uchwytu do obiektu (^) (C++ Component Extensions) instead.

A com::ptr nie może być zwrócona bezpośrednio z funkcji; Zamiast tego użyj uchwytu.

Przykład

W tym przykładzie zaimplementowano klasy CLR, która korzysta z com::ptr do zawijania Członkowskie prywatnego IXMLDOMDocument obiektu.Wywołanie metody publiczne wyniki klasy w wywołaniach zamkniętego IXMLDOMDocument obiektu.Próbka tworzy instancję dokumentu XML, wypełni niektóre proste XML i jest uproszczone walk węzłów drzewa analizowane dokumentu do drukowania XML do konsoli.

// comptr.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);   
   }

   void LoadXml(String^ xml) {
      pin_ptr<const wchar_t> 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("</{0}>", strName);
      }
      finally {
         ::SysFreeString(bstr);
      }
   }

   com::ptr<IXMLDOMDocument> 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("<word>persnickety</word>");

      // write the document to the console
      doc.WriteXml();
   }
   catch (Exception^ e) {
      Console::WriteLine(e);   
   }
}
  

Wymagania

Plik nagłówkowy <msclr\com\ptr.h>

Obszar nazw msclr::com

Zobacz też

Inne zasoby

Obsługa języka C++ Library

PTR członków