TypeBuilder.DefinePInvokeMethod メソッド

定義

PInvoke メソッドを定義します。

オーバーロード

名前 説明
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

PInvoke メソッドの名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvokeフラグを定義します。

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

PInvoke メソッドの名前、メソッドが定義されている DLL の名前、エントリ ポイントの名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvoke フラグを定義します。

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)

PInvokeメソッドの名前、メソッドが定義されている DLL の名前、エントリ ポイントの名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、PInvoke フラグ、およびパラメーターと戻り値の型のカスタム修飾子を定義します。

DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

PInvoke メソッドの名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvokeフラグを定義します。

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
Public Function DefinePInvokeMethod (name As String, dllName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

パラメーター

name
String

PInvoke メソッドの名前。 name 埋め込み null を含めることはできません。

dllName
String

PInvoke メソッドが定義されている DLL の名前。

attributes
MethodAttributes

メソッドの属性。

callingConvention
CallingConventions

メソッドの呼び出し規則。

returnType
Type

メソッドの戻り値の型。

parameterTypes
Type[]

メソッドのパラメーターの型。

nativeCallConv
CallingConvention

ネイティブ呼び出し規約。

nativeCharSet
CharSet

メソッドのネイティブ文字セット。

返品

定義された PInvoke メソッド。

例外

メソッドは静的ではありません。

-または-

親型はインターフェイスです。

-または-

メソッドは抽象メソッドです。

-または-

メソッドは以前に定義されています。

-または-

nameまたはdllNameの長さは 0 です。

name または dllNamenull

包含型は、 CreateType()を使用して以前に作成されています。

次の例では、DefinePInvokeMethod メソッドを使用してPInvoke メソッドを作成する方法と、MethodBuilder.GetMethodImplementationFlagsメソッドとMethodBuilder.SetImplementationFlagsメソッドを使用して、MethodBuilderを作成した後でメソッド実装フラグにMethodImplAttributes.PreserveSig フラグを追加する方法を示します。

Important

0 以外の戻り値を取得するには、 MethodImplAttributes.PreserveSig フラグを追加する必要があります。

この例では、1 つの動的モジュールと、PInvoke メソッドを含む 1 つの型 (MyType) を持つ動的アセンブリを作成します。 PInvoke メソッドは、Win32 GetTickCount関数を表します。

この例を実行すると、 PInvoke メソッドが実行されます。 また、動的アセンブリを PInvokeTest.dllとして保存します。 Ildasm.exe (IL 逆アセンブラー) を使用して、MyType クラスと、static (Visual Basic の Shared) PInvoke メソッドを調べることができます。 静的MyType.GetTickCount メソッドを使用するVisual Basicまたは C# プログラムをコンパイルするには、csc.exe または vbc.exeの実行時に DLL への参照 (たとえば、/r:PInvokeTest.dll) を含めることができます。

using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        // Create the AssemblyBuilder.
        AssemblyName asmName = new AssemblyName("PInvokeTest");
        AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
            asmName,
            AssemblyBuilderAccess.RunAndSave
        );

        // Create the module.
        ModuleBuilder dynamicMod =
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");

        // Create the TypeBuilder for the class that will contain the
        // signature for the PInvoke call.
        TypeBuilder tb = dynamicMod.DefineType(
            "MyType",
            TypeAttributes.Public | TypeAttributes.UnicodeClass
        );

        MethodBuilder mb = tb.DefinePInvokeMethod(
            "GetTickCount",
            "Kernel32.dll",
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
            CallingConventions.Standard,
            typeof(int),
            Type.EmptyTypes,
            CallingConvention.Winapi,
            CharSet.Ansi);

        // Add PreserveSig to the method implementation flags. NOTE: If this line
        // is commented out, the return value will be zero when the method is
        // invoked.
        mb.SetImplementationFlags(
            mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

        // The PInvoke method does not have a method body.

        // Create the class and test the method.
        Type t = tb.CreateType();

        MethodInfo mi = t.GetMethod("GetTickCount");
        Console.WriteLine("Testing PInvoke method...");
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));

        // Produce the .dll file.
        Console.WriteLine("Saving: " + asmName.Name + ".dll");
        dynamicAsm.Save(asmName.Name + ".dll");
    }
}

/* This example produces output similar to the following:

Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
 */
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class Example
    
    Public Shared Sub Main() 
        
        ' Create the AssemblyBuilder.
        Dim asmName As New AssemblyName("PInvokeTest")
        Dim dynamicAsm As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
                AssemblyBuilderAccess.RunAndSave)
        
        ' Create the module.
        Dim dynamicMod As ModuleBuilder = _
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
        
        ' Create the TypeBuilder for the class that will contain the 
        ' signature for the PInvoke call.
        Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
            TypeAttributes.Public Or TypeAttributes.UnicodeClass)
        
        Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
            "GetTickCount", _
            "Kernel32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            Type.EmptyTypes, _
            CallingConvention.Winapi, _
            CharSet.Ansi)

        ' Add PreserveSig to the method implementation flags. NOTE: If this line
        ' is commented out, the return value will be zero when the method is
        ' invoked.
        mb.SetImplementationFlags( _
            mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
        
        ' The PInvoke method does not have a method body.
        
        ' Create the class and test the method.
        Dim t As Type = tb.CreateType()

        Dim mi As MethodInfo = t.GetMethod("GetTickCount")
        Console.WriteLine("Testing PInvoke method...")
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))

        ' Produce the .dll file.
        Console.WriteLine("Saving: " & asmName.Name & ".dll")
        dynamicAsm.Save(asmName.Name & ".dll")
    
    End Sub  
End Class 

' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll

注釈

一部の DLL インポート属性 ( DllImportAttributeの説明を参照) は、このメソッドの引数として指定できません。 たとえば、メソッドが値を返す場合、PInvoke メソッドの作成後に DLL インポート属性MethodImplAttributes.PreserveSigを追加する必要があります。 この例では、これを行う方法を示します。

適用対象

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

PInvoke メソッドの名前、メソッドが定義されている DLL の名前、エントリ ポイントの名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvoke フラグを定義します。

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::String ^ entryName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
Public Function DefinePInvokeMethod (name As String, dllName As String, entryName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

パラメーター

name
String

PInvoke メソッドの名前。 name 埋め込み null を含めることはできません。

dllName
String

PInvoke メソッドが定義されている DLL の名前。

entryName
String

DLL 内のエントリ ポイントの名前。

attributes
MethodAttributes

メソッドの属性。

callingConvention
CallingConventions

メソッドの呼び出し規則。

returnType
Type

メソッドの戻り値の型。

parameterTypes
Type[]

メソッドのパラメーターの型。

nativeCallConv
CallingConvention

ネイティブ呼び出し規約。

nativeCharSet
CharSet

メソッドのネイティブ文字セット。

返品

定義された PInvoke メソッド。

例外

メソッドは静的ではありません。

-または-

親型はインターフェイスです。

-または-

メソッドは抽象メソッドです。

-または-

メソッドは以前に定義されています。

-または-

namedllName、またはentryNameの長さは 0 です。

namedllName、または entryNamenull

包含型は、 CreateType()を使用して以前に作成されています。

次のコード例では、DefinePInvokeMethod メソッドを使用してPInvoke メソッドを作成する方法と、MethodBuilder.GetMethodImplementationFlagsメソッドとMethodBuilder.SetImplementationFlagsメソッドを使用して、MethodBuilderを作成した後にメソッド実装フラグにMethodImplAttributes.PreserveSig フラグを追加する方法を示します。

Important

0 以外の戻り値を取得するには、 MethodImplAttributes.PreserveSig フラグを追加する必要があります。

この例では、1 つの動的モジュールと、PInvoke メソッドを含む 1 つの型 (MyType) を持つ動的アセンブリを作成します。 PInvoke メソッドは、Win32 GetTickCount関数を表します。

この例を実行すると、 PInvoke メソッドが実行されます。 また、動的アセンブリを PInvokeTest.dllとして保存します。 Ildasm.exe (IL 逆アセンブラー) を使用して、MyType クラスと、static (Visual Basic の Shared) PInvoke メソッドを調べることができます。 静的MyType.GetTickCount メソッドを使用するVisual Basicまたは C# プログラムをコンパイルするには、csc.exe または vbc.exeの実行時に DLL への参照 (たとえば、/r:PInvokeTest.dll) を含めることができます。

using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        // Create the AssemblyBuilder.
        AssemblyName asmName = new AssemblyName("PInvokeTest");
        AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
            asmName,
            AssemblyBuilderAccess.RunAndSave
        );

        // Create the module.
        ModuleBuilder dynamicMod =
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");

        // Create the TypeBuilder for the class that will contain the
        // signature for the PInvoke call.
        TypeBuilder tb = dynamicMod.DefineType(
            "MyType",
            TypeAttributes.Public | TypeAttributes.UnicodeClass
        );

        MethodBuilder mb = tb.DefinePInvokeMethod(
            "GetTickCount",
            "Kernel32.dll",
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
            CallingConventions.Standard,
            typeof(int),
            Type.EmptyTypes,
            CallingConvention.Winapi,
            CharSet.Ansi);

        // Add PreserveSig to the method implementation flags. NOTE: If this line
        // is commented out, the return value will be zero when the method is
        // invoked.
        mb.SetImplementationFlags(
            mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

        // The PInvoke method does not have a method body.

        // Create the class and test the method.
        Type t = tb.CreateType();

        MethodInfo mi = t.GetMethod("GetTickCount");
        Console.WriteLine("Testing PInvoke method...");
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));

        // Produce the .dll file.
        Console.WriteLine("Saving: " + asmName.Name + ".dll");
        dynamicAsm.Save(asmName.Name + ".dll");
    }
}

/* This example produces output similar to the following:

Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
 */
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class Example
    
    Public Shared Sub Main() 
        
        ' Create the AssemblyBuilder.
        Dim asmName As New AssemblyName("PInvokeTest")
        Dim dynamicAsm As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
                AssemblyBuilderAccess.RunAndSave)
        
        ' Create the module.
        Dim dynamicMod As ModuleBuilder = _
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
        
        ' Create the TypeBuilder for the class that will contain the 
        ' signature for the PInvoke call.
        Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
            TypeAttributes.Public Or TypeAttributes.UnicodeClass)
        
        Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
            "GetTickCount", _
            "Kernel32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            Type.EmptyTypes, _
            CallingConvention.Winapi, _
            CharSet.Ansi)

        ' Add PreserveSig to the method implementation flags. NOTE: If this line
        ' is commented out, the return value will be zero when the method is
        ' invoked.
        mb.SetImplementationFlags( _
            mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
        
        ' The PInvoke method does not have a method body.
        
        ' Create the class and test the method.
        Dim t As Type = tb.CreateType()

        Dim mi As MethodInfo = t.GetMethod("GetTickCount")
        Console.WriteLine("Testing PInvoke method...")
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))

        ' Produce the .dll file.
        Console.WriteLine("Saving: " & asmName.Name & ".dll")
        dynamicAsm.Save(asmName.Name & ".dll")
    
    End Sub  
End Class 

' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll

注釈

一部の DLL インポート属性 ( DllImportAttributeの説明を参照) は、このメソッドの引数として指定できません。 たとえば、メソッドが値を返す場合、PInvoke メソッドの作成後に DLL インポート属性MethodImplAttributes.PreserveSigを追加する必要があります。 この例では、これを行う方法を示します。

適用対象

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)

PInvokeメソッドの名前、メソッドが定義されている DLL の名前、エントリ ポイントの名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、PInvoke フラグ、およびパラメーターと戻り値の型のカスタム修飾子を定義します。

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::String ^ entryName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
Public Function DefinePInvokeMethod (name As String, dllName As String, entryName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

パラメーター

name
String

PInvoke メソッドの名前。 name 埋め込み null を含めることはできません。

dllName
String

PInvoke メソッドが定義されている DLL の名前。

entryName
String

DLL 内のエントリ ポイントの名前。

attributes
MethodAttributes

メソッドの属性。

callingConvention
CallingConventions

メソッドの呼び出し規則。

returnType
Type

メソッドの戻り値の型。

returnTypeRequiredCustomModifiers
Type[]

メソッドの戻り値の型に必要なカスタム修飾子 ( IsConst など) を表す型の配列。 戻り値の型に必要なカスタム修飾子がない場合は、 nullを指定します。

returnTypeOptionalCustomModifiers
Type[]

メソッドの戻り値の型の省略可能なカスタム修飾子 ( IsConstなど) を表す型の配列。 戻り値の型に省略可能なカスタム修飾子がない場合は、 nullを指定します。

parameterTypes
Type[]

メソッドのパラメーターの型。

parameterTypeRequiredCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、 IsConstなど、対応するパラメーターに必要なカスタム修飾子を表します。 特定のパラメーターに必要なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも必要なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

parameterTypeOptionalCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、 IsConstなど、対応するパラメーターの省略可能なカスタム修飾子を表します。 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 省略可能なカスタム修飾子を持つパラメーターがない場合は、配列の配列の代わりに null を指定します。

nativeCallConv
CallingConvention

ネイティブ呼び出し規約。

nativeCharSet
CharSet

メソッドのネイティブ文字セット。

返品

定義されたPInvoke メソッドを表すMethodBuilder

例外

メソッドは静的ではありません。

-または-

親型はインターフェイスです。

-または-

メソッドは抽象メソッドです。

-または-

メソッドは以前に定義されています。

-または-

namedllName、またはentryNameの長さは 0 です。

-または-

parameterTypeRequiredCustomModifiersまたはparameterTypeOptionalCustomModifiersのサイズがparameterTypesのサイズと等しくありません。

namedllName、または entryNamenull

この型は、以前に CreateType() を使用して作成されました。

-または-

現在の動的な型の場合、 IsGenericType プロパティは trueされますが、 IsGenericTypeDefinition プロパティは false

次のコード例では、DefinePInvokeMethod メソッドを使用してPInvoke メソッドを作成する方法と、MethodBuilder.GetMethodImplementationFlagsメソッドとMethodBuilder.SetImplementationFlagsメソッドを使用して、MethodBuilderを作成した後にメソッド実装フラグにMethodImplAttributes.PreserveSig フラグを追加する方法を示します。

この例では、1 つの動的モジュールと、PInvoke メソッドを含む 1 つの型 (MyType) を持つ動的アセンブリを作成します。 PInvoke メソッドは、Win32 GetTickCount関数を表します。

Important

0 以外の戻り値を取得するには、 MethodImplAttributes.PreserveSig フラグを追加する必要があります。

Note

この例では、カスタム修飾子を指定しないオーバーロードを使用します。 カスタム修飾子を指定するには、代わりにこのメソッド オーバーロードを使用するようにサンプル コードを変更します。

この例を実行すると、 PInvoke メソッドが実行されます。 また、動的アセンブリを PInvokeTest.dllとして保存します。 Ildasm.exe (IL 逆アセンブラー) を使用して、MyType クラスと、static (Visual Basic の Shared) PInvoke メソッドを調べることができます。 静的MyType.GetTickCount メソッドを使用するVisual Basicまたは C# プログラムをコンパイルするには、csc.exe または vbc.exeの実行時に DLL への参照 (たとえば、/r:PInvokeTest.dll) を含めることができます。

using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        // Create the AssemblyBuilder.
        AssemblyName asmName = new AssemblyName("PInvokeTest");
        AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
            asmName,
            AssemblyBuilderAccess.RunAndSave
        );

        // Create the module.
        ModuleBuilder dynamicMod =
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");

        // Create the TypeBuilder for the class that will contain the
        // signature for the PInvoke call.
        TypeBuilder tb = dynamicMod.DefineType(
            "MyType",
            TypeAttributes.Public | TypeAttributes.UnicodeClass
        );

        MethodBuilder mb = tb.DefinePInvokeMethod(
            "GetTickCount",
            "Kernel32.dll",
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
            CallingConventions.Standard,
            typeof(int),
            Type.EmptyTypes,
            CallingConvention.Winapi,
            CharSet.Ansi);

        // Add PreserveSig to the method implementation flags. NOTE: If this line
        // is commented out, the return value will be zero when the method is
        // invoked.
        mb.SetImplementationFlags(
            mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

        // The PInvoke method does not have a method body.

        // Create the class and test the method.
        Type t = tb.CreateType();

        MethodInfo mi = t.GetMethod("GetTickCount");
        Console.WriteLine("Testing PInvoke method...");
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));

        // Produce the .dll file.
        Console.WriteLine("Saving: " + asmName.Name + ".dll");
        dynamicAsm.Save(asmName.Name + ".dll");
    }
}

/* This example produces output similar to the following:

Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
 */
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class Example
    
    Public Shared Sub Main() 
        
        ' Create the AssemblyBuilder.
        Dim asmName As New AssemblyName("PInvokeTest")
        Dim dynamicAsm As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
                AssemblyBuilderAccess.RunAndSave)
        
        ' Create the module.
        Dim dynamicMod As ModuleBuilder = _
            dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
        
        ' Create the TypeBuilder for the class that will contain the 
        ' signature for the PInvoke call.
        Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
            TypeAttributes.Public Or TypeAttributes.UnicodeClass)
        
        Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
            "GetTickCount", _
            "Kernel32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            Type.EmptyTypes, _
            CallingConvention.Winapi, _
            CharSet.Ansi)

        ' Add PreserveSig to the method implementation flags. NOTE: If this line
        ' is commented out, the return value will be zero when the method is
        ' invoked.
        mb.SetImplementationFlags( _
            mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
        
        ' The PInvoke method does not have a method body.
        
        ' Create the class and test the method.
        Dim t As Type = tb.CreateType()

        Dim mi As MethodInfo = t.GetMethod("GetTickCount")
        Console.WriteLine("Testing PInvoke method...")
        Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))

        ' Produce the .dll file.
        Console.WriteLine("Saving: " & asmName.Name & ".dll")
        dynamicAsm.Save(asmName.Name & ".dll")
    
    End Sub  
End Class 

' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll

注釈

一部の DLL インポート属性 ( DllImportAttributeの説明を参照) は、このメソッドの引数として指定できません。 たとえば、メソッドが値を返す場合、PInvoke メソッドの作成後に DLL インポート属性MethodImplAttributes.PreserveSigを追加する必要があります。 この例では、これを行う方法を示します。

適用対象