初学C#调用C++代码,想在C#中调用C++ 动态库的一个结构体,但编译时,报“XXX is not supported by the language”错误,不知道怎么修改。
代码如下:
C++:
#define TEST_STRUCT(name) [StructLayoutAttribute(LayoutKind::Sequential, Pack = 1)]\
typedef public ref struct name
namespace CPlusPlusDll {
TEST_STRUCT(A)
{
unsigned long a;
void operator=(const unsigned long val)
{
a = val;
}
unsigned long GetID()
{
return a;
}
}A;
TEST_STRUCT(B)
{
A aTest;
unsigned long c;
}B;
TEST_STRUCT(C)
{
B b;
C()
{
b.aTest = b.c;
}
}C;
}
C#:
using System;
using System.Runtime.InteropServices;
using CPlusPlusDll;
namespace CSharpTest
{
class Program
{
static void Main(string[] args)
{
B b = new B();
b.a.GetID();
int len = Marshal.SizeOf(typeof(B));
Console.WriteLine("length of B is: {0}", len);
Console.Read();
}
}
}
编译报错:
'CPlusPlusDll.C.b' is not supported by the language
请高手指点一下,要如何修改才能完全编译通过,在C#下可以使用?