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.
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:
Marshal.AllocHGlobal
, Marshal.Copy
etc.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With