Condividi tramite


ptr::QueryInterface

Eseguire una query all'oggetto COM di proprietà per un'interfaccia e associa il risultato a un altro com::ptr.

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

Parametri

  • other
    com::ptr che ottiene l'interfaccia.

Eccezioni

Internamente, QueryInterface viene chiamato sull'oggetto COM di proprietà e qualsiasi errore HRESULT viene convertito in eccezione da ThrowExceptionForHR.

Note

Utilizzare questo metodo per creare un wrapper COM per un'interfaccia differente dell'oggetto COM di proprietà del wrapper corrente. Questo metodo chiama QueryInterface tramite l'oggetto COM di proprietà per richiedere un puntatore a un'interfaccia specifica dell'oggetto COM e di connette il puntatore a interfaccia restituito a com::ptrpassato.

Esempio

In questo esempio viene implementata una classe CLR che utilizza com::ptr per eseguire il wrapping del relativo oggetto di IXMLDOMDocument il membro privato. La funzione membro di WriteTopLevelNode utilizza QueryInterface per riempire com::ptr locale di IXMLDOMNode quindi passare com::ptr (da tracking reference) a una funzione membro privata che scrive il nome e le proprietà Text del nodo nella console.

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

Requisiti

Header file<msclr\com\ptr.h>

msclr::com diSpazio dei nomi

Vedere anche

Riferimenti

ptr::GetInterface

Altre risorse

Membri ptr