clover82625
clover82625
采纳率33.3%
2019-02-14 13:52 阅读 492

C++ 结构体在C#中托管调用,编译报错误is not supported by the language

5

初学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#下可以使用?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐