次の方法で共有


MarshalAsAttribute.SizeParamIndex フィールド

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

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

解説

このフィールドは、COM 対応マネージ オブジェクトを構築する場合に使用します。 SizeParamIndex は、COM クライアントから呼び出され、パラメータの 1 つが配列のマネージ メソッドでだけ有効です。マーシャラはアンマネージ配列のサイズを決定できないため、この情報は別のパラメータで渡されます。

サイズを格納するパラメータは、値渡しによる整数である必要があります。 UnmanagedType.LPArraySizeParamIndexMarshalAsAttribute.SizeConst の両方を指定した場合、これらの値の和がサイズの合計となります。このフィールドは、COM オブジェクトを呼び出すマネージ コードには影響を与えません。詳細については、「 配列に対する既定のマーシャリング 」を参照してください。

使用例

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

MarshalAsAttribute クラス | MarshalAsAttribute メンバ | System.Runtime.InteropServices 名前空間 | SizeConst | LPArray