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.

[Visual Basic]
Public SizeParamIndex As Short
[C#]
public short SizeParamIndex;
[C++]
public: short SizeParamIndex;
[JScript]
public var SizeParamIndex : Int16;

Remarks

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.

Example

[Visual Basic] 
Option Strict Off

Imports System.Runtime.InteropServices
Imports 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)> _
    Structure Vertex
        Dim x As Decimal
        Dim y As Decimal
        Dim z As Decimal
    End Structure

    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.

        Declare Auto Sub SomeUnsafeMethod Lib "somelib.dll" ( _
                                      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=1)> data() As Vertex, _
                                      size As Long ) 

        Public Sub SomeMethod()
            Dim verts(3) As Vertex
            SomeUnsafeMethod( verts, verts.Length )
        End Sub

    End Class

End Namespace

Module Test
    Sub Main
        Dim AClass As New SomeClass

        AClass.SomeMethod
        End Sub
End Module

[C#] 
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();
    }
}

[C++] 
#using <mscorlib.dll>

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(Sequential)]
__value struct Vertex
{
   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)] Vertex data __gc[], long size );

int main()
{
   Vertex verts[] = new Vertex[3];

   SomeUnsafeMethod(verts, verts->Length );
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

MarshalAsAttribute Class | MarshalAsAttribute Members | System.Runtime.InteropServices Namespace | SizeConst | LPArray

Show:
© 2014 Microsoft