MarshalAsAttribute.SizeParamIndex フィールド

定義

COM の size_is のように、0 から始まる配列要素のカウントを格納しているパラメーターを示します。

public: short SizeParamIndex;
public short SizeParamIndex;
val mutable SizeParamIndex : int16
Public SizeParamIndex As Short 

フィールド値

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 );
}
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();
    }
}
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

注釈

フィールドは SizeParamIndex 、マネージドからアンマネージドへの呼び出しとアンマネージドからマネージドへの呼び出しをサポートします。 COM オブジェクトを呼び出すマネージド コードには影響しません。

マネージド型と、それに適用される属性に応じて、配列を安全な配列または C スタイルの配列として渡すことができます。

配列が C スタイルの配列として渡されると、マーシャラーは配列のサイズを決定できません。 したがって、マネージド配列をアンマネージ関数またはメソッドに渡すには、次の 2 つの引数を指定する必要があります。

  • 参照または値によって定義される配列。

  • 参照または値によって定義される配列サイズ。

配列サイズ パラメーターの 0 から始まるインデックスは、 フィールドを SizeParamIndex 使用して定義されます。

フィールドで と MarshalAsAttribute.SizeConst の両方SizeParamIndexUnmanagedType.LPArray指定すると、フィールドの値の合計によってサイズ合計が生成されます。

詳細については、「配列の 既定のマーシャリング」を参照してください。

適用対象

こちらもご覧ください