Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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

This field is used when building managed objects that work with COM. SizeParamIndex is only valid on managed methods that are called from COM clients, where one of the parameters is an array. 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. This field does not have any effect on managed code that calls COM objects. 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft