将C ++二维固定长度char数组封装为结构成员
我试图调用一个非托管C ++函数,它有一个结构作为输入参数。 结构在头文件中定义如下:
struct MyStruct { int siOrder; char aaszNames[6][25]; int siId[6]; int siTones[6]; };
我试图将托管结构声明如下:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct MyStruct { public int siOrder; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst=150)] public string aaszNames; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=6, ArraySubType=UnmanagedType.I4)] public int[] siId; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=6, ArraySubType=UnmanagedType.I4)] public int[] siTones; }
但没有任何成功。 我猜测编组失败了,因为aaszNames实际上是一个由六个25长的空终止字符串组成的数组。 我尝试将aaszNames声明为
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=150)] public char[] aaszNames;
必要时用空值填充数组。 但是,再一次,没有。
有什么我想念的吗? 我错了什么? 编组这个2-D char数组的最佳方法是什么?
请给我任何提示。
尝试使用多个C#结构:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct MyStruct_Name { [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 25)] public string name; } [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MyStruct { public int siOrder; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6)] public MyStruct_Name aaszNames; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)] public int[] siId; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)] public int[] siTones; }
这就是我传递C风格字符串数组的方式。
别忘了创建aaszNames的内容! marshaller讨厌空引用。
MyStruct foo = new MyStruct(); for (int i = 0; i < 6; i++) { foo.aaszNames[i] = new MyStruct_Name(); foo.aaszNames[i].name = ""; }
祝好运!
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=150)] public char[] aaszNames;
那种编组类型看起来很好。 可能在函数调用或内存分配错误中出现问题/
我会写一个小的c程序来检查C结构的字节大小。
然后我会按照另一个建议来逐字段提取数据。
从C的角度来看,/ 0被视为包含在6个字节中的正常字符,而C#将使用长度为5并且隐藏/ 0。
char aaszNames[6][25];
C ++类型的char是8位〜
但是C#Type的字符是Unicode,(16位)!
所以C ++的char类型< - > C#类型的字节
上述就是C#学习教程:将C ++二维固定长度char数组封装为结构成员分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/957442.html