Partager via


ptr::Detach

Abandonne la propriété de l'objet COM, qui retourne un pointeur à l'objet.

_interface_type * Detach();

Valeur de retour

Le pointeur à l'objet COM.

Si aucun objet n'est détenu, la valeur NULL est retournée.

Exceptions

En interne, QueryInterface est appelé dans l'objet COM possédé et toute erreur HRESULT est convertie en une exception par ThrowExceptionForHR.

Notes

Detach ajoute d'abord une référence à l'objet COM de la part de l'appelant et libère toutes les références détenues par com::ptr.l'appelant doit finalement libérer l'objet retourné pour le détruire.

Exemple

Cet exemple implémente une classe CLR qui utilise com::ptr pour encapsuler son objet d' IXMLDOMDocument de membre privé.Les appels d'une fonction membre Detach d' DetachDocument d'abandonner la propriété de l'objet COM et retourner un pointeur vers l'appelant.

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

Configuration requise

fichier d'en-tête<msclr \ COM \ ptr.h>

Msclr::com deEspace de noms

Voir aussi

Référence

ptr::Release

ptr::Attach

Autres ressources

membres PTR