VC++ 中通过 COM 实例化 C# 对象的类未注册错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9711079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:30:58  来源:igfitidea点击:

Class not registered error for Instantiation of C# object via COM from VC++

c#.netvisual-c++comregasm

提问by BigSauce

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

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

Facts

事实

  • 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# 和 C++ 项目都是使用 .NET 4.0 编译的
  • 正在使用regasm /codebase "CSharpProjectName.dll""注册 C# .dll ,Windows 命令提示符报告“类型注册成功”。
  • 在 C++ 项目中,我尝试在 C# 项目中创建一个类的实例,但我得到0x80040154的 HRESULT - 类未注册

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:

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

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

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?

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

Any help would greatly be appreciated. Thanks!

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

采纳答案by Joseph Willcoxson

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.

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

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_ROOT\Wow6432Node. If your C++ project is 64-bit, make sure that your C# classes were registered to the 64-bit hive--HKEY_CLASSES_ROOT.

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

If you need to register to the 64-bit hive, you may need to call the version of RegAsm.exe under c:\windows\microsoft.net\framework64...

如果需要注册到64位hive,可能需要调用c:\windows\microsoft.net\framework64下的RegAsm.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:\windows\microsoft.net\framework\v4.0.3019\regasm.exe.

出现问题的另一种可能性是您可能需要运行 .NET 4.0 版本的 regasm.exe。如果你只是在命令行中输入“regasm”,它会给你你正在运行的 regasm 版本。您可能需要键入 regasm 的 .NET 4.0 版本的完整路径——位于 c:\windows\microsoft.net\framework\v4.0.3019\regasm.exe。

回答by code4life

Have you tried the /tlboption? Try that, and then

您是否尝试过该/tlb选项?试试看,然后

#import "your_tlb_file_lol.tlb" no_namespace

I think the other option might be to gac the assembly and then regasm it.

我认为另一种选择可能是 gac 程序集,然后 regasm 它。

回答by Fábio Rodrigues Fonseca

Try to leave your project in 32 bits because you know your project, creating in Any CPU an active component that runs at 32 with a registry error, so change the build to 32 or preferably to 32.

尝试将您的项目保留为 32 位,因为您了解您的项目,在 Any CPU 中创建一个在 32 位运行并出现注册表错误的活动组件,因此将构建更改为 32 位或最好更改为 32 位。

You Too register you dll with regsvr32

你也用 regsvr32 注册你的 dll