[英]Marshalling dynamic size array into struct
how can i define a struct with a dynamic sized array? 如何使用动态大小的数组定义结构?
is it right? 这样对吗?
struct MyStruc {
public int len;
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]
public int buf[];
}
Assuming that you want a struct containing a pointer to the array. 假设您需要一个包含指向数组的指针的结构。
Declare the pointer to the array as IntPtr
and marshal the array contents manually with Marshal.AllocHGlobal
, Marshal.Copy
etc. 将指向数组的指针声明为
IntPtr
并使用Marshal.AllocHGlobal
, Marshal.Copy
等手动编组数组内容。
Assuming that you want a variable sized struct rather than a struct containing a pointer to the array. 假设您需要一个可变大小的结构而不是包含指向该数组的指针的结构。
You cannot marshal a variable sized struct using p/invoke. 您不能使用p / invoke封送变量大小的结构。 You have at least these two options:
您至少有以下两个选项:
Marshal.AllocHGlobal
, Marshal.Copy
etc. Marshal.AllocHGlobal
, Marshal.Copy
等手动Marshal.AllocHGlobal
结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.