我试图调用从C#COM项目方法在非托管的Visual C ++的解决方案,但我不断收到一个错误
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
,并Make assembly COM-Visible
在C#COM项目的属性。
更新:该错误不上去,如果我们改变了C#COM项目的frameworkversion到2.0,3.0或3.5。 它只是显示了当frameworkversion为4.0或4.5。