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

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

问题描述

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

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_ROOT \ Wow6432Node.如果您的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_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.

如果需要注册到64位配置单元,则可能需要在c:\ windows \ microsoft.net \ framework64 ...下调用RegAsm.exe版本.

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...

出现问题的另一种可能性是,您可能需要运行regasm.exe的.NET 4.0版本.如果仅在命令行中输入"regasm",它将为您提供正在运行的regasm版本.您可能需要键入regasm的.NET 4.0版本的完整路径-位于c:\ windows \ microsoft.net \ framework \ v4.0.3019 \ 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.

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

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