MarshalAsAttribute.SizeParamIndex Field

Indicates which parameter contains the count of array elements, much like size_is in COM, and is zero-based.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

public short SizeParamIndex
public short SizeParamIndex
public var SizeParamIndex : short
Not applicable.

This field is used when building managed objects that work with COM. Since the marshaler cannot determine the size of an unmanaged array, this information is passed in a separate parameter.

The parameter containing the size must be an integer that is passed by value. If you specify both the SizeParamIndex and MarshalAsAttribute.SizeConst with a UnmanagedType.LPArray, the sum of their values produces a size total. For additional information, see Default Marshaling for Arrays.

using System.Runtime.InteropServices;
using SomeNamespace;

namespace SomeNamespace
{
    // Force the layout of your fields to the C style struct layout.
    // Without this, the .NET Framework will reorder your fields.
    [StructLayout(LayoutKind.Sequential)]
    public struct Vertex
    {
    	float	x;
	float	y;
    	float	z;
    }

    class SomeClass
    {
        // Add [In] or [In, Out] attributes as approppriate.
        // 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")]
        public static extern void SomeUnsafeMethod(
                                      [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] Vertex[] data,
                                      long size );

        public void SomeMethod()
        {
            Vertex[] verts = new Vertex[3];
            SomeUnsafeMethod( verts, verts.Length );
        }

    }
}

class Test
{
	public static void Main()
    {
        SomeClass AClass = new SomeClass();

        AClass.SomeMethod();
    }
}

package SomeNamespace; 
import System.Runtime.InteropServices.*;
import SomeNamespace.*; 

/** @attribute StructLayout(LayoutKind.Sequential)
 */
public class Vertex
{
    private float x;
    private float y;
    private float z;
} //Vertex

class SomeClass
{
    // Add [In] or [In, Out] attributes as approppriate.
    // Marshal as a C style array of Vertex, where the second
    // (SizeParamIndex is zero-based)
    //  parameter (size) contains the count of array elements.
    /** @attribute DllImport("SomeDll.dll")
     */
    public static native void SomeUnsafeMethod(
        /** @attribute MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)
         */
        Vertex data[], long size);

    public void SomeMethod()
    {
        Vertex verts[] = new Vertex[3];
        SomeUnsafeMethod(verts, verts.length);
    } //SomeMethod
} //SomeClass

class Test
{
    public static void main(String[] args)
    {
        SomeClass aClass = new SomeClass();
        aClass.SomeMethod();
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: