从 VC++ 通过 COM 实例化 C# 对象的类未注册错误 [英] Class not registered error for Instantiation of C# object via COM from VC++

查看:20
本文介绍了从 VC++ 通过 COM 实例化 C# 对象的类未注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 VC++ 项目中,我试图创建一个 C# 项目中包含的 C# 类的实例(通过 COM).

In a VC++ project, I am trying to create an instance (via COM) of a C# class contained within a C# project.

事实

  • C# 和 C++ 项目均使用 .NET 4.0 编译
  • 正在使用 regasm/codebase "CSharpProjectName.dll"" 注册 C# .dll,Windows 命令提示符报告类型注册成功."
  • 在 c++ 项目中,我尝试在 C# 项目中创建一个类的实例,但得到的 HRESULT 为 0x80040154 - 类未注册
  • Both the C# and C++ projects are compiled using .NET 4.0
  • The C# .dll is being registered using regasm /codebase "CSharpProjectName.dll"", and windows command prompt reports, "Types registered successfully."
  • In the c++ project, I attempt to create an instance of a class in the C# project, but I get an HRESULT of 0x80040154 - class not registered

这是我尝试从 C# .dll 创建 .NET 对象实例的示例.我试图实例化的具体类称为 Employee,为了简单地提出我的问题,它实现了 IPerson 接口:

Here is an example of my attempt to create an instance of the .NET object from the C# .dll. The concrete class I am trying to instantiate is called Employee, which for the sake of presenting my question simply, implements the IPerson interface:

    CSharpProjectName::IPersonPtr pPersonPtr;
    HRESULT hr = pPersonPtr.CreateInstance(CSharpProjectName::CLSID_Employee);

即使我使用regasm/codebase"注册了 c# .dll 并确认注册表中存在该键,为什么我会收到类未注册"错误?

Why am I getting a "class not registered" error even though I registered the c# .dll using "regasm /codebase" and confirmed existence of the key in the registry?

任何帮助将不胜感激.谢谢!

Any help would greatly be appreciated. Thanks!

推荐答案

我过去遇到过这个问题,这是由于两个进程都不是 32 位或 64 位.如果您运行的是 32 位操作系统,您现在可以停止阅读,因为我所说的不适用.

I've had that problem in the past and it was due to both processes not being 32 or 64 bit. If you are running a 32-bit OS, you can stop reading now because what I say doesn't apply.

使用 regedit 尝试在注册表中查找您的 ProgId 和 CLSID.如果您的 C++ 项目是 32 位的,请确保您的 C# 类已注册到 32 位配置单元--HKEY_CLASSES_ROOTWow6432Node.如果您的 C++ 项目是 64 位,请确保您的 C# 类已注册到 64 位配置单元--HKEY_CLASSES_ROOT.

Use regedit to try and find your ProgIds and CLSIDs in the registry. If your C++ project is 32-bit, make sure that your C# classes were registered to the 32-bit hive--HKEY_CLASSES_ROOTWow6432Node. If your C++ project is 64-bit, make sure that your C# classes were registered to the 64-bit hive--HKEY_CLASSES_ROOT.

如果需要注册到64位hive,可能需要调用c:windowsmicrosoft.netframework64下的RegAsm.exe版本...

If you need to register to the 64-bit hive, you may need to call the version of RegAsm.exe under c:windowsmicrosoft.netframework64...

出现问题的另一种可能性是您可能需要运行 .NET 4.0 版本的 regasm.exe.如果您只是在命令行中键入regasm",它将为您提供正在运行的 regasm 版本.您可能需要键入 regasm 的 .NET 4.0 版本的完整路径 - 位于 c:windowsmicrosoft.netframeworkv4.0.3019 egasm.exe.

The other possibility for things to go wrong is that you might need to run the .NET 4.0 version of regasm.exe. If you just type "regasm" in the command line, it will give you the version of regasm you are running. You might need to type the full path of .NET 4.0 version of regasm--found at c:windowsmicrosoft.netframeworkv4.0.3019 egasm.exe.

这篇关于从 VC++ 通过 COM 实例化 C# 对象的类未注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆