MarshalAsAttribute::SizeParamIndex Field
Indicates the zero-based parameter that contains the count of array elements, similar to size_is in COM.
Assembly: mscorlib (in mscorlib.dll)
The SizeParamIndex field supports managed-to-unmanaged and unmanaged-to-managed calls. It does not have any effect on managed code that calls COM objects.
Depending on the managed type and the attributes applied to it, the array can be passed as a safe array or C-style array.
When arrays are passed as C-style arrays, the marshaler cannot determine the size of the array. Therefore, to pass an managed array to an unmanaged function or method, you must provide two arguments:
The array, defined by reference or value.
The array size, defined by reference or value.
The zero-based index of the array size parameter is defined by using the SizeParamIndex field.
If you specify both SizeParamIndex and MarshalAsAttribute::SizeConst with a UnmanagedType::LPArray field, the sum of the fields' values produces a size total.
For more information, see Default Marshaling for Arrays.
using namespace System; using namespace System::Runtime::InteropServices; // Force the layout of your fields to the C-style struct layout. // Without this, the .NET Framework will reorder your fields. [StructLayoutAttribute(LayoutKind::Sequential)] value struct Vertex { public: float x; float y; float z; }; // Add [In] or [In, Out] attributes as appropriate. // Marshal as a C-style array of Vertex, where the second (SizeParamIndex is zero-based) // parameter (size) contains the count of array elements. [DllImport("SomeDLL.dll")] extern void SomeUnsafeMethod( [MarshalAs(UnmanagedType::LPArray,SizeParamIndex=1)]array<Vertex>^data, long size ); int main() { array<Vertex>^verts = gcnew array<Vertex>(3); SomeUnsafeMethod( verts, verts->Length ); }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1