我想使用的PInvoke马歇尔结构数组另一个结构内从C到C#。 AFAIK,不行。
所以取而代之,在C结构,我宣布一个PTR我的数组,malloc的。问题:1)如何在C#端声明等价物? 2)如何分配和使用C#端的等价物?如何使用的PInvoke为C结构数组的指针,以C#
//The C code
typedef struct {
int a;
int b; } A;
typedef struct {
int c;
// A myStruct[100]; // can't do this, so:
A *myStruct; } B;
//The c# code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class A{
int a;
int b;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class B{
int c;
// can't declare array of [100] A structures...
?
}
[编辑]:不知何故,我错误地解释了我在其他地方读到的有关c#端固定对象数组的问题。 我可以固定在C数组的大小所以编好了,但后来我得到“对象引用不设置到对象的实例”用时:
data.B[3].a = 4567;
那么,其他地方阅读一下这个错误可能是,我加了这个方法:
public void initA()
{
for (int i = 0; i < 100; i++) { B[i] = new A(); }
}
再次,编译好了,但是同样的错误信息。
你试过了A [] myStruct吗? – 2012-08-15 12:13:03
编译好。但我该如何分配存储空间并使用它(我仍在学习c#)。谢谢。 – PaeneInsula 2012-08-15 12:20:14
只是好奇:*为什么*你不能在C中使用'A myStruct [100];'?那会让C#的侧面容易得多... – 2012-08-15 13:21:13