为什么 CoCreateInstance 无法创建我的组件的实例?

Why CoCreateInstance can't create an instance of my component?

本文关键字:组件 实例 我的 创建 CoCreateInstance 为什么      更新时间:2023-10-16

我通过Dale Rogerson的《Inside COM》这本书学习COM。我尝试在注册表中注册我的组件,然后通过此信息在我的客户端的代码中创建我的组件的实例。但是我看到::FormatMessage函数这样写:类未注册。因此,::CoCreateInstance不能创建我的组件的实例。

我REG-file

:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTCLSID{68584B56-9224-4DCC-AD35-1070CC9B8FDE}]
@="bush_component_01"
[HKEY_CLASSES_ROOTCLSID{68584B56-9224-4DCC-AD35-1070CC9B8FDE}InprocServer32]
@="D:\projects\com_sandbox_solution_01\Debug\bush_component_01.dll"
[HKEY_CLASSES_ROOTCLSID{EB4BFC91-6A6E-43D1-B4CD-7A5DF24DB8D8}]
@="IID_IX"
[HKEY_CLASSES_ROOTCLSID{EB4BFC91-6A6E-43D1-B4CD-7A5DF24DB8D8}InprocServer32]
@="D:\projects\com_sandbox_solution_01\Debug\bush_component_01.dll"

我的客户端代码:

...
// GUID of my DLL (it registered in registry (look at my REG-code above))
// {68584B56-9224-4DCC-AD35-1070CC9B8FDE}
static const CLSID CLSID_component_01 =
{ 0x68584b56, 0x9224, 0x4dcc, { 0xad, 0x35, 0x10, 0x70, 0xcc, 0x9b, 0x8f, 0xde } };
// GUID of my some interface (it registered in registry (look at my REG-code above))
// {EB4BFC91-6A6E-43D1-B4CD-7A5DF24DB8D8}
static const IID IID_IX =
{ 0xeb4bfc91, 0x6a6e, 0x43d1, { 0xb4, 0xcd, 0x7a, 0x5d, 0xf2, 0x4d, 0xb8, 0xd8 } };
...
// Code of my client:
 ::CoInitialize(nullptr);
  IUnknown* comp = nullptr;
  HRESULT hcri = ::CoCreateInstance(CLSID_component_01, nullptr, 
    CLSCTX_INPROC_SERVER, IID_IX, (void**)&comp);
  if (FAILED(hcri)){
    void* msg = nullptr;
    ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      nullptr, hcri, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0,
      nullptr);
    trace("***** TRACE *****");
    trace((LPTSTR)msg);
    trace("****************");
    keep_window_open();
    return 1;
  }
// But ::FormatMessage function writes this: Class not registered

为什么会发生?

在64位Windows中,32位应用程序应该使用Wow6432Node注册子项(就64位regedit而言)。

遗憾的是,32位的书没有涵盖这些