简体   繁体   English

将动态大小数组编组到struct中

[英]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.AllocHGlobalMarshal.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: 您至少有以下两个选项:

  1. Break the struct into two parameters. 将结构分为两个参数。
  2. Marshal the struct manually with Marshal.AllocHGlobal , Marshal.Copy etc. 使用Marshal.AllocHGlobalMarshal.Copy等手动Marshal.AllocHGlobal结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM