Sdílet prostřednictvím


ptr::ptr

Konstrukce com::ptr k obtékání objektu COM.

ptr();
ptr(
   _interface_type * p
);

Parametry

  • P
    Ukazatel rozhraní COM.

Poznámky

Konstruktor bez argumentů přiřadí nullptr k podkladové popisovač objektu.Následné volání com::ptr bude ověření vnitřního objektu a bezobslužně nezdaří, dokud objekt skutečně vytvořena nebo připojena.

Přidá odkaz na objekt COM jeden argument konstruktoru ale neuvolní volajícího odkaz, tak musí volající Release na objekt COM skutečně povede ovládacího prvku.Když com::ptr's zavolán destruktor se automaticky uvolní své odkazy na objekt COM.

Předávání NULL pro tento konstruktor je stejné jako volání verze bez argumentů.

Příklad

Tento příklad implementuje třídu CLR, která používá com::ptr jeho soukromý člen zalomení IXMLDOMDocument objektu.Ukazuje použití obou verzí konstruktoru.

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

Požadavky

Soubor hlaviček<msclr\com\ptr.h>

Obor názvů msclr::com

Viz také

Referenční dokumentace

ptr::CreateInstance

ptr::~ptr

Další zdroje

ptr – členové