所有されている COM オブジェクトに対してインターフェイスをクエリし、その結果を別の com::ptr にアタッチします。
template<class _other_type>
void QueryInterface(
ptr<_other_type> % other
);
パラメータ
- other
インターフェイスを取得する com::ptr 。
例外
内部的には、所有されている COM オブジェクト上の QueryInterface が呼び出され、エラー HRESULT が ThrowExceptionForHR によって例外に変換されます。
解説
このメソッドを使用して、現在のラッパーによって所有されている COM オブジェクトの異なるインターフェイスの COM ラッパーを作成します。このメソッドは、所有されている COM オブジェクトを通じて QueryInterface を呼び出すことにより、COM オブジェクトの特定のインターフェイスへのポインタを要求すると共に、返却されたインターフェイス ポインタを渡された com::ptr にアタッチします。
使用例
この例では、プライベート メンバ IXMLDOMDocument オブジェクトをラップするために com::ptr を使用する CLR クラスを実装します。WriteTopLevelNode メンバ関数は、QueryInterface を使用することにより、ローカルの com::ptr に IXMLDOMNode を格納した後、(参照の追跡により) 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);
}
}
<#document>persnickety</#document>
必要条件
Header file <msclr\com\ptr.h>
Namespace msclr::com