问题描述
我正在尝试从非托管 Visual C++ 解决方案中的 C# COM 项目调用方法,但我不断收到下一个错误
I'm trying to call methods from a C# COM project in an unmanaged Visual C++ solution, but I keep getting the next error
First-chance exception at 0x7697C41F (KernelBase.dll) in Program.exe: 0x04242420 (parameters: 0x31415927, 0x6F310000, 0x00BBDAE8).
在下一段代码
SalesForceNew::IMyObjectClassPtr p;
p.CreateInstance(__uuidof(SalesForceNew::TestObject)); // error
SalesForceNew::MyObject mo = p->getObject(1, "a");
然而,mo
的值符合预期(5,aa").
However the value of mo
is as expected (5, "aa").
我用这行代码导入 tlb 文件:
I import the tlb-file with this line of code:
#import "C:UsersBobDesktopComTestSalesForceNewinx86DebugSalesForceNew.tlb" named_guids
C#项目如下:
界面:
using System.Runtime.InteropServices;
namespace SalesForceNew
{
[ComVisible(true)]
[Guid("22901ACD-CA30-4D3E-B84B-73B707026AE5")]
public interface IMyObjectClass
{
MyObject getObject(int i, string s);
}
[ComVisible(true)]
[StructLayout(LayoutKind.Sequential)]
public struct MyObject
{
public int Getal;
public string Text;
}
}
实现接口的类:
using System.Runtime.InteropServices;
namespace SalesForceNew
{
[ClassInterface(ClassInterfaceType.None)]
[Guid("234A2A35-F270-458D-A67B-C834EB794B27")]
[ComVisible(true)]
public class TestObject : IMyObjectClass
{
public MyObject getObject(int i, string s)
{
return new MyObject() { Getal = i * 5, Text = s + s };
}
}
}
我在 C# COM 项目的属性中检查了选项 Register for COM interop
和 Make assembly COM-Visible
.
I checked the options Register for COM interop
and Make assembly COM-Visible
in the properties of the C# COM project.
UPDATE:如果我们将C# COM 项目的frameworkversion 更改为2.0、3.0 或3.5,则不会出现错误.仅在frameworkversion为4.0或4.5时显示.
UPDATE: the error won't come up if we change the frameworkversion of the C# COM project to 2.0, 3.0 or 3.5. It only shows up when the frameworkversion is 4.0 or 4.5.
推荐答案
异常代码小于 0x80000000 的异常是非致命异常.它们往往被用来传递信息.鞋子适合这里,异常代码 0x04242420 已被逆向工程为 CLRDBG_NOTIFICATION_EXCEPTION_CODE,在谷歌查询中输入数字以查看命中.这个答案来自微软员工可能是最可靠的:
Exceptions whose exception code is less than 0x80000000 are non-fatal exceptions. They tend to be used to pass information. The shoe fits here, exception code 0x04242420 has been reverse-engineered to CLRDBG_NOTIFICATION_EXCEPTION_CODE, type the number in a google query to see the hits. This answer from a Microsoft employee is probably the most reliable one:
出于好奇,我进行了一些挖掘,发现这实际上是一个未记录的异常 (CLRDBG_NOTIFICATION_EXCEPTION_CODE),它显然是 4.0 CLR 中托管调试器使用的 IPC 协议的补充.忽略它应该是完全安全的.
这篇关于在非托管 C++ 项目中使用 C# COM ->0x7697C41F (KernelBase.dll) 处的第一次机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!