ptr::CreateInstance

创建com::ptr的 COM 对象的实例。

void CreateInstance(
   System::String ^ progid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   System::String ^ progid,
   LPUNKNOWN pouter
);
void CreateInstance(
   System::String ^ progid
);
void CreateInstance(
   const wchar_t * progid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   const wchar_t * progid,
   LPUNKNOWN pouter
);
void CreateInstance(
   const wchar_t * progid
);
void CreateInstance(
   REFCLSID rclsid,
   LPUNKNOWN pouter,
   DWORD cls_context
);
void CreateInstance(
   REFCLSID rclsid,
   LPUNKNOWN pouter
);
void CreateInstance(
   REFCLSID rclsid
);

参数

  • progid
    一个 ProgID 字符串。

  • pouter
    聚合到对象的控件接口 (由 IUnknown) 的指针。 如果 pouter 未指定, NULL 被使用。

  • cls_context
    代码管理新创建的对象中运行的上下文。 值从 CLSCTX 枚举中采用。 如果 cls_context 未定义,值 CLSCTX_ALL 被使用。

  • rclsid
    CLSID与用于创建对象的数据和代码关联。

异常

如果 com::ptr 已经拥有对 COM 对象的一个引用,则 CreateInstance 将抛出 InvalidOperationException

此函数使用 ThrowExceptionForHR 调用 CoCreateInstance 和转换任何错误 HRESULT 设置为适当的异常。

备注

使用 CoCreateInstance 指定CreateInstance 创建对象的新实例,标识从 CLSID 或 ProgID。 com::ptr 引用新创建的对象中,并且自动释放任何拥有的引用时使用。

示例

本示例实现使用 com::ptr 包装其私有成员 IXMLDOMDocument 对象的 CLR 类。 创建文档对象的 CreateInstance 类构造函数使用两种不同形式从 ProgID 或从一 CLSCTX CLSID 以及。

// comptr_createinstance.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);   
   }
   XmlDocument(REFCLSID clsid, DWORD clsctx) {
      m_ptrDoc.CreateInstance(clsid, NULL, clsctx);
   }

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

// use the ref class to handle an XML DOM Document object
int main() {
   try {
      // create the class from a progid string
      XmlDocument doc1("Msxml2.DOMDocument.3.0");

      // or from a clsid with specific CLSCTX
      XmlDocument doc2(CLSID_DOMDocument30, CLSCTX_INPROC_SERVER);
   }
   catch (Exception^ e) {
      Console::WriteLine(e);   
   }
}

要求

Header file <msclr\com\ptr.h>

Namespace msclr::com

请参见

其他资源

ptr 成员