1

我已经声明了 C++/CLI 类如下

namespace testcominterface {

  [ComVisible(true)]
  [Guid("FFCA805F-8DAB-4AF8-A7B7-B488136E8177")]
  public interface class ITestInterface
  {
      public :
          void TestMethod();
  };


  [ComVisible(true)]
  [Guid("E65F4772-54B5-4105-83E5-DCED24ABC815")]
  [ClassInterface(ClassInterfaceType::AutoDual)]
  [ComDefaultInterface(ITestInterface::typeid)]       
  public ref class testCoClass : ITestInterface
  {
      public:
          virtual void TestMethod()
                {

                    Console::WriteLine("testCoClass::TestMethod : Test method");

                }
  };
 }

我想通过本机 C++ COM 创建“testCoClass”(通过#import TLB 文件并使用 CoCreateInstance)我总是收到错误“未注册的类”。如果我使用“Regasm.exe”注册程序集,它工作正常,但我不想注册程序集。

我已按照此博客文章http://blogs.msdn.com/b/cheller/archive/2006/08/24/how-to-embed-a-manifest-in-an-assembly-let-me中的步骤进行操作-count-the-ways.aspx将清单嵌入到程序集中,但它不起作用。(请注意,此方法始终适用于 C# 程序集,但这是 C++/CLI 程序集。

我很感激任何建议。

4

1 回答 1

2

需要注册 COM 服务器,以便 COM 可以在客户端程序请求时找到 DLL。从技术上讲,可以通过为客户端程序提供一个带有无 reg-free COM 条目的清单来避免这种情况,这<clrClass>对于使用托管代码编写的 COM 服务器是必需的。关键是这个清单需要嵌入到客户端,而不是服务器。在你的 COM 服务器正常工作之前不要去那里。

一个标准错误是忘记在 Regasm.exe 中使用 /codebase 选项。没有它,程序集需要强命名并放入 GAC。这不是你想在你的开发机器上做的事情。另一个常见错误是使用了错误版本的 Regasm.exe。您需要注意 64 位机器上的位数。如果您使用 VS2010 并使用 GAC,请选择正确的,.NET 4 为 GAC 提供了不同的位置。

你应该改进你使用的属性。一个合适的 COM 服务器只暴露接口并隐藏实现。在接口声明上使用 [InterfaceType(ComInterfaceType::InterfaceIsDual)],在类上使用 [ClassInterface(ClassInterfaceType::None)]。您现在也不再需要 [ComDefaultInterface] 并且对 mscorlib.tlb 的类型库依赖将消失。

If you still have trouble then SysInternals' ProcMon utility can show you exactly where in the registry the client looked for your server and compare it against the actual registry locations that your server uses.

于 2012-04-01T14:57:53.287 回答