ptr::QueryInterface
Vlastnictví objektu COM pro rozhraní vyhledá a výsledek se připojuje na jiný com::ptr.
template<class _other_type>
void QueryInterface(
ptr<_other_type> % other
);
Parametry
- other
com::ptr , Získá rozhraní.
Výjimky
Interně QueryInterface o vlastnictví objektu COM a případné chyby, se nazývá HRESULT je převeden na výjimku podle ThrowExceptionForHR.
Poznámky
Tuto metodu použijte, chcete-li vytvořit COM wrapper pro jiné rozhraní objektu COM, které jsou ve vlastnictví aktuální obálky.Tato metoda volá QueryInterface prostřednictvím vlastnictví objektu COM požádat o odkaz na konkrétní rozhraní modelu COM objekt a ukazatel vrácené rozhraní k předaný com::ptr.
Příklad
Tento příklad implementuje třídu CLR, která používá com::ptr jeho soukromý člen zalomení IXMLDOMDocument objektu.WriteTopLevelNode Členské funkce používá QueryInterface vyplní místní com::ptr se IXMLDOMNode a potom předá com::ptr (sledováním odkazu) privátní členskou funkci, která zapíše do konzoly na uzel vlastnosti jméno a text.
// 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);
}
}
Požadavky
Soubor hlaviček<msclr\com\ptr.h>
Obor názvů msclr::com