Marshalling dynamic size array into struct

前端 未结 1 1629
野的像风
野的像风 2021-01-06 00:33

how can i define a struct with a dynamic sized array?

is it right?

struct MyStruc { 

    public int len; 
    [MarshalAs(UnmanagedType.LPArray, Siz         


        
1条回答
  •  一整个雨季
    2021-01-06 01:03

    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.

    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. 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.

    0 讨论(0)
提交回复
热议问题