インターフェイスの所有される COM オブジェクトをクエリ、別の com::ptrに結果をアタッチします。
template<class _other_type>
void QueryInterface(
ptr<_other_type> % other
);
パラメーター
- other
インターフェイスを取得 com::ptr 。
例外
内部的には、 QueryInterface は所有される COM オブジェクトに対してが呼び出されたときに、エラー HRESULTThrowExceptionForHRによって例外に変換されます。
解説
現在のラッパーによって所有される COM オブジェクトの異なるインターフェイスの COM ラッパーを作成するには、このメソッドを使用します。メソッド呼び出し QueryInterfacecom::ptr独自のへの COM オブジェクトにアタッチしますの特定のインターフェイス ポインターに返されたインターフェイス ポインターを要求する所有された COM オブジェクトを通じてこの。
使用例
この例では、プライベート メンバー IXMLDOMDocument オブジェクトをラップするために com::ptr を使用する CLR クラスを実装します。WriteTopLevelNode のメンバー関数は IXMLDOMNode のローカル com::ptr を塗りつぶすために QueryInterface を使用し、コンソールにノード名とテキスト プロパティを記述するプライベート メンバー関数に com::ptr (追跡の参照によって)を渡します。
// 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);
}
}
必要条件
ヘッダー ファイル <msclr\com\ptr.h>
名前空間 msclr::com