Aracılığıyla paylaş


com::ptr Sınıfı

CLR sınıfının üyesi olarak kullanılabilecek bir COM nesnesi için sarmalayıcı. Sarmalayıcı ayrıca COM nesnesinin yaşam boyu yönetimini otomatikleştirir ve yok edicisi çağrıldığında nesne üzerinde sahip olunan tüm başvuruları serbest bırakmaktadır. CComPtr sınıfına benzer.

Sözdizimi

template<class _interface_type>
ref class ptr;

Parametreler

_interface_type
COM arabirimi.

Açıklamalar

A com::ptr , çeşitli COM görevlerini basitleştirmek ve yaşam süresi yönetimini otomatikleştirmek için yerel işlev değişkeni olarak da kullanılabilir.

bir com::ptr doğrudan işlev parametresi olarak kullanılamaz; bunun yerine bir İzleme başvuru işleci veya Nesneye tanıtıcı işleci (^) kullanın.

bir com::ptr işlevden doğrudan döndürülemez; bunun yerine bir tanıtıcı kullanın.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. sınıfının ortak yöntemlerini çağırmak, içerilen nesneye yapılan çağrılarla sonuçlanır IXMLDOMDocument . Örnek, XML belgesinin bir örneğini oluşturur, basit bir XML ile doldurur ve XML'yi konsola yazdırmak için ayrıştırılmış belge ağacındaki düğümlerin basitleştirilmiş bir adımını yapar.

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

Üyeler

Ortak oluşturucular

Veri Akışı Adı Açıklama
ptr::ptr com::ptr COM nesnesini sarmak için bir oluşturur.
ptr::~ptr bir com::ptröğesini yok eder.

Genel yöntemler

Veri Akışı Adı Açıklama
ptr::Attach com nesnesini öğesine com::ptrekler.
ptr::CreateInstance içinde com::ptrbir COM nesnesinin örneğini oluşturur.
ptr::Detach NESNEye bir işaretçi döndürerek COM nesnesinin sahipliğini verir.
ptr::GetInterface içinde com::ptrbir COM nesnesinin örneğini oluşturur.
ptr::QueryInterface Bir arabirim için sahip olunan COM nesnesini sorgular ve sonucu başka bir com::ptröğesine ekler.
ptr::Release COM nesnesinde sahip olunan tüm başvuruları serbest bırakır.

Genel işleçler

Veri Akışı Adı Açıklama
ptr::operator-> Sahip olunan COM nesnesinde yöntemleri çağırmak için kullanılan üye erişim işleci.
ptr::operator= com nesnesini öğesine com::ptrekler.
ptr::operator bool Koşullu ifadede kullanmak com::ptr için işleç.
ptr::operator! Sahip olunan COM nesnesinin geçersiz olup olmadığını belirlemek için işleç.

Gereksinimler

Üst bilgi dosyası<msclr\com\ptr.h>

Ad alanı msclr::com

ptr::ptr

Sahip olunan COM nesnesine bir işaretçi döndürür.

ptr();
ptr(
   _interface_type * p
);

Parametreler

P
COM arabirim işaretçisi.

Açıklamalar

Bağımsız değişken içermeyen oluşturucu, temel alınan nesne tanıtıcısına atar nullptr . 'a com::ptr yapılacak sonraki çağrılar iç nesneyi doğrular ve bir nesne oluşturulana veya eklenene kadar sessizce başarısız olur.

Tek bağımsız değişken oluşturucu COM nesnesine bir başvuru ekler, ancak çağıranın başvuruyu serbest bırakmaz, bu nedenle çağıranın denetimi gerçekten bırakmak için COM nesnesinde çağrısı Release yapması gerekir. com::ptr'nin yıkıcısı çağrıldığında, başvuruları COM nesnesinde otomatik olarak serbest bırakır.

NULL Bu oluşturucuya geçiş, bağımsız değişken olmayan sürümü çağırmakla aynıdır.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. Oluşturucunun her iki sürümünün de kullanımını gösterir.

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

ptr::~ptr

bir com::ptröğesini yok eder.

~ptr();

Açıklamalar

yok olduğunda com::ptr , sahip olduğu tüm başvuruları COM nesnesine yayınlar. COM nesnesine başka başvuru yapılmadığını varsayarsak, COM nesnesi silinir ve belleği serbest bırakılır.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. işlevinde main , iki XmlDocument nesnenin yıkıcıları bloğun try kapsamı dışına çıktığında çağrılır ve temel yıkıcı com::ptr çağrılır ve COM nesnesine sahip olan tüm başvurular serbest kalır.

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

ptr::Attach

com nesnesini öğesine com::ptrekler.

void Attach(
   _interface_type * _right
);

Parametreler

_Sağ
Eklenecek COM arabirim işaretçisi.

Özel durumlar

com::ptr zaten bir COM nesnesine başvuru sahibiyse, Attach oluştururInvalidOperationException.

Açıklamalar

çağrısı COM nesnesine Attach başvurur, ancak çağıranın başvuruyu serbest bırakmaz.

NULL'a geçişAttach, hiçbir işlem yapılmamasıyla sonuçlanıyor.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. Üye ReplaceDocument işlevi önce daha önce sahip olunan herhangi bir nesneyi çağırır Release ve ardından yeni bir belge nesnesi eklemek için çağırır Attach .

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

   // replace currently held COM object with another one
   void ReplaceDocument(IXMLDOMDocument* pDoc) {
      // release current document object
      m_ptrDoc.Release();
      // attach the new document object
      m_ptrDoc.Attach(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;
};

// unmanaged function that creates a raw XML DOM Document object
IXMLDOMDocument* CreateDocument() {
   IXMLDOMDocument* pDoc = NULL;
   Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,
      CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pDoc));
   return pDoc;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      // get another document object from unmanaged function and
      // store it in place of the one held by our ref class
      pDoc = CreateDocument();
      doc.ReplaceDocument(pDoc);
      // no further need for raw object reference
      pDoc->Release();
      pDoc = NULL;
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }
   }
}

ptr::CreateInstance

içinde com::ptrbir COM nesnesinin örneğini oluşturur.

void CreateInstance(
   System::String ^ progid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   System::String ^ progid,
   LPUNKNOWN pouter
);
void CreateInstance(
   System::String ^ progid
);
void CreateInstance(
   const wchar_t * progid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   const wchar_t * progid,
   LPUNKNOWN pouter
);
void CreateInstance(
   const wchar_t * progid
);
void CreateInstance(
   REFCLSID rclsid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   REFCLSID rclsid,
   LPUNKNOWN pouter
);
void CreateInstance(
   REFCLSID rclsid
);

Parametreler

progid
Bir ProgID dize.

pouter
Toplama nesnesinin IUnknown arabiriminin (denetlenen IUnknown) işaretçisi. Belirtilmezse pouter kullanılır NULL .

cls_context
Yeni oluşturulan nesneyi yöneten kodun çalıştırılacağı bağlam. Değerler numaralandırmadan CLSCTX alınır. Belirtilmezse cls_context , CLSCTX_ALL değeri kullanılır.

rclsid
CLSID nesnesini oluşturmak için kullanılacak veri ve kodla ilişkilendirildi.

Özel durumlar

com::ptr zaten bir COM nesnesine başvuru sahibiyse, CreateInstance oluştururInvalidOperationException.

Bu işlev, herhangi bir hatayı HRESULT uygun bir özel duruma dönüştürmek için öğesini çağırır CoCreateInstance ve kullanırThrowExceptionForHR.

Açıklamalar

CreateInstance , progID veya CLSID'den tanımlanan belirtilen nesnenin yeni bir örneğini oluşturmak için kullanır CoCreateInstance . Yeni com::ptr oluşturulan nesneye başvurur ve yok etme sonrasında sahip olunan tüm başvuruları otomatik olarak serbest bırakır.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. Sınıf oluşturucuları, belge nesnesini progID'den veya CLSID artı CLSCTX'ten oluşturmak için iki farklı biçimi CreateInstance kullanır.

// comptr_createinstance.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);
   }
   XmlDocument(REFCLSID clsid, DWORD clsctx) {
      m_ptrDoc.CreateInstance(clsid, NULL, clsctx);
   }

   // 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() {
   try {
      // create the class from a progid string
      XmlDocument doc1("Msxml2.DOMDocument.3.0");

      // or from a clsid with specific CLSCTX
      XmlDocument doc2(CLSID_DOMDocument30, CLSCTX_INPROC_SERVER);
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
}

ptr::Detach

NESNEye bir işaretçi döndürerek COM nesnesinin sahipliğini verir.

_interface_type * Detach();

Dönüş değeri

COM nesnesinin işaretçisi.

Sahip olunan bir nesne yoksa NULL döndürülür.

Özel durumlar

Dahili olarak, QueryInterface sahip olunan COM nesnesinde çağrılır ve tüm hatalar HRESULT tarafından ThrowExceptionForHRözel duruma dönüştürülür.

Açıklamalar

Detach önce çağıranın adına COM nesnesine bir başvuru ekler ve ardından sahip com::ptrolduğu tüm başvuruları serbest bırakır. Çağıranın nihai olarak döndürülen nesneyi yok etmek için serbest bırakması gerekir.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. DetachDocument Üye işlevi, COM nesnesinin sahipliğini vermek ve çağırana bir işaretçi döndürmek için çağırırDetach.

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

   // detach the COM object and return it
   // this releases the internal reference to the object
   IXMLDOMDocument* DetachDocument() {
      return m_ptrDoc.Detach();
   }

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

// unmanaged function that loads XML into a raw XML DOM Document object
HRESULT LoadXml(IXMLDOMDocument* pDoc, BSTR bstrXml) {
   HRESULT hr = S_OK;
   VARIANT_BOOL bSuccess;
   hr = pDoc->loadXML(bstrXml, &bSuccess);
   if (S_OK == hr && !bSuccess) {
      hr = E_FAIL;
   }
   return hr;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;
   BSTR bstrXml = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      bstrXml = ::SysAllocString(L"<word>persnickety</word>");
      if (NULL == bstrXml) {
         throw gcnew OutOfMemoryException("bstrXml");
      }
      // detach the document object from the ref class
      pDoc = doc.DetachDocument();
      // use unmanaged function and raw object to load xml
      Marshal::ThrowExceptionForHR(LoadXml(pDoc, bstrXml));
      // release document object as the ref class no longer owns it
      pDoc->Release();
      pDoc = NULL;
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }

   }
}

ptr::GetInterface

Sahip olunan COM nesnesine bir işaretçi döndürür.

_interface_type * GetInterface();

Dönüş değeri

Sahip olunan COM nesnesinin işaretçisi.

Özel durumlar

Dahili olarak, QueryInterface sahip olunan COM nesnesinde çağrılır ve tüm hatalar HRESULT tarafından ThrowExceptionForHRözel duruma dönüştürülür.

Açıklamalar

, com::ptr çağıranın adına COM nesnesine bir başvuru ekler ve ayrıca COM nesnesinde kendi başvurusunu korur. Çağıranın sonuç olarak döndürülen nesnede başvuruyu serbest bırakması gerekir, aksi takdirde hiçbir zaman yok edilmeyecektir.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. GetDocument Üye işlevi, COM nesnesine bir işaretçi döndürmek için kullanırGetInterface.

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

   // add a reference to and return the COM object
   // but keep an internal reference to the object
   IXMLDOMDocument* GetDocument() {
      return m_ptrDoc.GetInterface();
   }

   // simplified function that only writes the first node
   void WriteDocument() {
      IXMLDOMNode* pNode = NULL;
      BSTR bstr = NULL;

      try {
         // use operator -> to call XML Doc member
         Marshal::ThrowExceptionForHR(m_ptrDoc->get_firstChild(&pNode));
         if (NULL != pNode) {
            // write out the xml
            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 {
         if (NULL != pNode) {
            pNode->Release();
         }
         ::SysFreeString(bstr);
      }
   }

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

// unmanaged function that loads XML into a raw XML DOM Document object
HRESULT LoadXml(IXMLDOMDocument* pDoc, BSTR bstrXml) {
   HRESULT hr = S_OK;
   VARIANT_BOOL bSuccess;
   hr = pDoc->loadXML(bstrXml, &bSuccess);
   if (S_OK == hr && !bSuccess) {
      hr = E_FAIL;
   }
   return hr;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;
   BSTR bstrXml = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      bstrXml = ::SysAllocString(L"<word>persnickety</word>");
      if (NULL == bstrXml) {
         throw gcnew OutOfMemoryException("bstrXml");
      }
      // detach the document object from the ref class
      pDoc = doc.GetDocument();
      // use unmanaged function and raw object to load xml
      Marshal::ThrowExceptionForHR(LoadXml(pDoc, bstrXml));
      // release reference to document object (but ref class still references it)
      pDoc->Release();
      pDoc = NULL;

      // call another function on the ref class
      doc.WriteDocument();
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }

   }
}
<word>persnickety</word>

ptr::QueryInterface

Bir arabirim için sahip olunan COM nesnesini sorgular ve sonucu başka bir com::ptröğesine ekler.

template<class _other_type>
void QueryInterface(
   ptr<_other_type> % other
);

Parametreler

Diğer
com::ptr Arabirimi alacak olan.

Özel durumlar

Dahili olarak, QueryInterface sahip olunan COM nesnesinde çağrılır ve tüm hatalar HRESULT tarafından ThrowExceptionForHRözel duruma dönüştürülür.

Açıklamalar

Geçerli sarmalayıcının sahip olduğu COM nesnesinin farklı bir arabirimi için BIR COM sarmalayıcısı oluşturmak için bu yöntemi kullanın. Bu yöntem, COM nesnesinin belirli bir arabirimine işaretçi istemek için sahip olunan COM nesnesi üzerinden çağrır QueryInterface ve döndürülen arabirim işaretçisini geçirilen com::ptröğesine ekler.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. WriteTopLevelNode Üye işlevi, yerel com::ptr bir öğeyi ile IXMLDOMNode doldurmak için kullanır QueryInterface ve ardından (başvuruyu com::ptr izleyerek) düğümün adını ve metin özelliklerini konsola yazan özel üye işlevine geçirir.

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

   // write the top level node to the console
   void WriteTopLevelNode() {
      com::ptr<IXMLDOMNode> ptrNode;

      // query for the top level node interface
      m_ptrDoc.QueryInterface(ptrNode);
      WriteNode(ptrNode);
   }

   // 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(com::ptr<IXMLDOMNode> % node) {
      BSTR bstr = NULL;

      try {
         // write out the name and text properties
         Marshal::ThrowExceptionForHR(node->get_nodeName(&bstr));
         String^ strName = gcnew String(bstr);
         Console::Write("<{0}>", strName);
         ::SysFreeString(bstr);
         bstr = NULL;

         Marshal::ThrowExceptionForHR(node->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.WriteTopLevelNode();
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
}
<#document>persnickety</#document>

ptr::Release

COM nesnesinde sahip olunan tüm başvuruları serbest bırakır.

void Release();

Açıklamalar

Bu işlevin çağrılması, COM nesnesinde sahip olunan tüm başvuruları serbest bırakır ve iç tanıtıcıyı COM nesnesine nullptrayarlar. COM nesnesinde başka başvuru yoksa yok edilir.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. Üye işlevi, ReplaceDocument yeni belgeyi eklemeden önce önceki herhangi bir belge nesnesini serbest bırakmak için kullanır Release .

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

   // replace currently held COM object with another one
   void ReplaceDocument(IXMLDOMDocument* pDoc) {
      // release current document object
      m_ptrDoc.Release();
      // attach the new document object
      m_ptrDoc.Attach(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;
};

// unmanaged function that creates a raw XML DOM Document object
IXMLDOMDocument* CreateDocument() {
   IXMLDOMDocument* pDoc = NULL;
   Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,
      CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pDoc));
   return pDoc;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      // get another document object from unmanaged function and
      // store it in place of the one held by our ref class
      pDoc = CreateDocument();
      doc.ReplaceDocument(pDoc);
      // no further need for raw object reference
      pDoc->Release();
      pDoc = NULL;
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }
   }
}

ptr::operator->

Sahip olunan COM nesnesinde yöntemleri çağırmak için kullanılan üye erişim işleci.

_detail::smart_com_ptr<_interface_type> operator->();

Dönüş değeri

smart_com_ptr COM nesnesine A.

Özel durumlar

Dahili olarak, QueryInterface sahip olunan COM nesnesinde çağrılır ve tüm hatalar HRESULT tarafından ThrowExceptionForHRözel duruma dönüştürülür.

Açıklamalar

Bu işleç, sahip olunan COM nesnesinin yöntemlerini çağırmanıza olanak tanır. Kendi ve Releaseöğesini AddRef otomatik olarak işleyen geçici smart_com_ptr bir döndürür.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. işlevi, WriteDocument belge nesnesinin üyesini get_firstChild çağırmak için kullanıroperator->.

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

   // add a reference to and return the COM object
   // but keep an internal reference to the object
   IXMLDOMDocument* GetDocument() {
      return m_ptrDoc.GetInterface();
   }

   // simplified function that only writes the first node
   void WriteDocument() {
      IXMLDOMNode* pNode = NULL;
      BSTR bstr = NULL;

      try {
         // use operator -> to call XML Doc member
         Marshal::ThrowExceptionForHR(m_ptrDoc->get_firstChild(&pNode));
         if (NULL != pNode) {
            // write out the xml
            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 {
         if (NULL != pNode) {
            pNode->Release();
         }
         ::SysFreeString(bstr);
      }
   }

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

// unmanaged function that loads XML into a raw XML DOM Document object
HRESULT LoadXml(IXMLDOMDocument* pDoc, BSTR bstrXml) {
   HRESULT hr = S_OK;
   VARIANT_BOOL bSuccess;
   hr = pDoc->loadXML(bstrXml, &bSuccess);
   if (S_OK == hr && !bSuccess) {
      hr = E_FAIL;
   }
   return hr;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;
   BSTR bstrXml = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      bstrXml = ::SysAllocString(L"<word>persnickety</word>");
      if (NULL == bstrXml) {
         throw gcnew OutOfMemoryException("bstrXml");
      }
      // detach the document object from the ref class
      pDoc = doc.GetDocument();
      // use unmanaged function and raw object to load xml
      Marshal::ThrowExceptionForHR(LoadXml(pDoc, bstrXml));
      // release reference to document object (but ref class still references it)
      pDoc->Release();
      pDoc = NULL;

      // call another function on the ref class
      doc.WriteDocument();
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }

   }
}
<word>persnickety</word>

ptr::operator=

com nesnesini öğesine com::ptrekler.

ptr<_interface_type> % operator=(
   _interface_type * _right
);

Parametreler

_Sağ
Eklenecek COM arabirim işaretçisi.

Dönüş değeri

üzerinde com::ptrbir izleme başvurusu.

Özel durumlar

com::ptr zaten bir COM nesnesine başvuru sahibiyse, operator= oluştururInvalidOperationException.

Açıklamalar

BIR COM nesnesinin atanması com::ptr COM nesnesine başvurur, ancak çağıranın buna olan başvuruyu serbest bırakmaz.

Bu işleç ile aynı etkiye Attachsahiptir.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. ReplaceDocument Üye işlevi önce daha önce sahip olunan herhangi bir nesneyi çağırır Release ve ardından yeni bir belge nesnesi eklemek için kullanıroperator=.

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

   // replace currently held COM object with another one
   void ReplaceDocument(IXMLDOMDocument* pDoc) {
      // release current document object
      m_ptrDoc.Release();
      // attach the new document object
      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;
};

// unmanaged function that creates a raw XML DOM Document object
IXMLDOMDocument* CreateDocument() {
   IXMLDOMDocument* pDoc = NULL;
   Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,
      CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pDoc));
   return pDoc;
}

// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      // get another document object from unmanaged function and
      // store it in place of the one held by the ref class
      pDoc = CreateDocument();
      doc.ReplaceDocument(pDoc);
      // no further need for raw object reference
      pDoc->Release();
      pDoc = NULL;
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }
   }
}

ptr::operator bool

Koşullu ifadede kullanmak com::ptr için işleç.

operator bool();

Dönüş değeri

true sahip olunan COM nesnesi geçerliyse; false yoksa.

Açıklamalar

Sahip olunan COM nesnesi değilse geçerlidir nullptr.

Bu işleç, tam sayı türüne _detail_class::_safe_bool dönüştürülemediğinden daha bool güvenli olan öğesine dönüştürür.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. CreateInstance Üye işlevi, yeni belge nesnesini oluşturduktan sonra geçerli olup olmadığını belirlemek için kullanır operator bool ve geçerliyse konsola yazar.

// comptr_op_bool.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:
   void CreateInstance(String^ progid) {
      if (!m_ptrDoc) {
         m_ptrDoc.CreateInstance(progid);
         if (m_ptrDoc) { // uses operator bool
            Console::WriteLine("DOM Document created.");
         }
      }
   }

   // 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() {
   try {
      XmlDocument doc;
      // create the instance from a progid string
      doc.CreateInstance("Msxml2.DOMDocument.3.0");
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
}
DOM Document created.

ptr::operator!

Sahip olunan COM nesnesinin geçersiz olup olmadığını belirlemek için işleç.

bool operator!();

Dönüş değeri

true sahip olunan COM nesnesi geçersizse; false yoksa.

Açıklamalar

Sahip olunan COM nesnesi değilse geçerlidir nullptr.

Örnek

Bu örnek, özel üye IXMLDOMDocument nesnesini sarmak için kullanan bir com::ptr CLR sınıfı uygular. CreateInstance Üye işlevi, belge nesnesinin zaten sahip olup olmadığını belirlemek için kullanır operator! ve yalnızca nesne geçersizse yeni bir örnek oluşturur.

// comptr_op_not.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:
   void CreateInstance(String^ progid) {
      if (!m_ptrDoc) {
         m_ptrDoc.CreateInstance(progid);
         if (m_ptrDoc) {
            Console::WriteLine("DOM Document created.");
         }
      }
   }

   // 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() {
   try {
      XmlDocument doc;
      // create the instance from a progid string
      doc.CreateInstance("Msxml2.DOMDocument.3.0");
   }
   catch (Exception^ e) {
      Console::WriteLine(e);
   }
}
DOM Document created.