Massaging an array of dynamic sizes into a structure

How can I define a structure with an array of dynamic size?

Is it correct?

struct MyStruc { public int len; [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] public int buf[]; } 
+3
source share
1 answer

Assuming you need a structure containing a pointer to an array.

Declare the pointer to the array as IntPtr and manually create the contents of the array using Marshal.AllocHGlobal , Marshal.Copy , etc.

Assuming you want a variable-sized structure, not a structure containing an array pointer.

You cannot marshal a variable-sized structure using p / invoke. You have at least these two options:

  • Divide the structure into two parameters.
  • Marshal structures manually using Marshal.AllocHGlobal , Marshal.Copy , etc.
+3
source

All Articles