在非托管C ++项目中使用C#COM – > 0x7697C41F的第一次机会exception(KernelBase.dll)

我试图在非托管的Visual C ++解决方案中调用C#COM项目中的方法,但我不断收到下一个错误

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”)。

我用这行代码导入tlb文件:

 #import "C:\Users\Bob\Desktop\ComTest\SalesForceNew\bin\x86\Debug\SalesForceNew.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 }; } } } 

我检查了选项Register for COM interop和在C#COM项目的属性Register for COM interop Make assembly COM-Visible


更新:如果我们将C#COM项目的frameworkversion更改为2.0,3.0或3.5,则不会出现错误。 它仅在frameworkversion为4.0或4.5时显示。

exception代码小于0x80000000的exception是非致命exception。 它们往往被用来传递信息。 鞋适合这里,exception代码0x04242420已经反向设计到CLRDBG_NOTIFICATION_EXCEPTION_CODE,在谷歌查询中键入数字以查看命中。 微软员工的答案可能是最可靠的答案 :

出于好奇,我做了一点挖掘,发现这实际上是一个未记录的exception(CLRDBG_NOTIFICATION_EXCEPTION_CODE),它显然是4.0 CLR中托管调试器使用的IPC协议的补充。 忽视它应该是完全安全的。