TypeBuilder.DefinePInvokeMethod TypeBuilder.DefinePInvokeMethod TypeBuilder.DefinePInvokeMethod Method

定義

PInvoke メソッドを定義します。Defines a PInvoke method.

オーバーロード

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

PInvoke メソッドに名前を指定して、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーター型、および PInvoke フラグを定義します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

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

PInvoke メソッドに名前を指定して、メソッドが定義されている DLL の名前、エントリ ポイント名、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーター型、および PInvoke フラグを定義します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the name of the entry point, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

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

PInvoke メソッドを定義します。名前、メソッドが定義されている DLL の名前、エントリ ポイント名、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーター型、PInvoke フラグ、およびパラメーターと戻り値の型のカスタム修飾子を指定します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the name of the entry point, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, the PInvoke flags, and custom modifiers for the parameters and return type.

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

PInvoke メソッドに名前を指定して、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーター型、および PInvoke フラグを定義します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

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

パラメーター

name
String String String

PInvoke メソッドの名前。The name of the PInvoke method. name に埋め込み null 値を含めることはできません。name cannot contain embedded nulls.

dllName
String String String

PInvoke メソッドが定義されている DLL の名前。The name of the DLL in which the PInvoke method is defined.

attributes
MethodAttributes MethodAttributes MethodAttributes

メソッドの属性。The attributes of the method.

callingConvention
CallingConventions CallingConventions CallingConventions

メソッドの呼び出し規則。The method's calling convention.

returnType
Type Type Type

メソッドの戻り値の型。The method's return type.

parameterTypes
Type[]

メソッドのパラメーター型。The types of the method's parameters.

nativeCallConv
CallingConvention CallingConvention CallingConvention

ネイティブ呼び出し規則。The native calling convention.

nativeCharSet
CharSet CharSet CharSet

メソッドのネイティブ文字セット。The method's native character set.

戻り値

定義された PInvoke メソッド。The defined PInvoke method.

例外

メソッドは静的ではありません。The method is not static.

- または --or- 親の型がインターフェイスです。The parent type is an interface.

- または --or- 抽象メソッドです。The method is abstract.

- または --or- メソッドは以前に定義されています。The method was previously defined.

- または --or- name または dllName の長さが 0 です。The length of name or dllName is zero.

name または dllNamenull です。name or dllName is null.

含んでいる型が CreateType() を使用して以前に作成されています。The containing type has been previously created using CreateType().

次の例は、メソッドを使用DefinePInvokeMethodしてPInvokeメソッドを作成する方法MethodImplAttributes.PreserveSigと、を作成MethodBuilderMethodBuilder.GetMethodImplementationFlagsた後に、およびMethodBuilder.SetImplementationFlagsを使用して、メソッドの実装フラグにフラグを追加する方法を示しています。メソッド。The following example demonstrates how to use the DefinePInvokeMethod method to create a PInvoke method, and how to add the MethodImplAttributes.PreserveSig flag to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

重要

0以外の戻り値を取得するには、 MethodImplAttributes.PreserveSigフラグを追加する必要があります。To get a non-zero return value, you must add the MethodImplAttributes.PreserveSig flag.

この例では、1つの動的モジュールと1つの型MyType() を使用PInvokeして、メソッドを含む動的アセンブリを作成します。The example creates a dynamic assembly with one dynamic module and a single type, MyType, that contains the PInvoke method. メソッドPInvokeは、Win32 GetTickCount関数を表します。The PInvoke method represents the Win32 GetTickCount function.

この例を実行すると、 PInvokeメソッドが実行されます。When the example is run, it executes the PInvoke method. また、動的アセンブリを PInvokeTest として保存します。It also saves the dynamic assembly as PInvokeTest.dll. Ildasm.exe (IL 逆アセンブラー)を使用しMyTypeて、クラスとstaticそれに含まれる (Shared Visual Basic) PInvokeメソッドを調べることができます。You can use the Ildasm.exe (IL Disassembler) to examine the MyType class and the static (Shared in Visual Basic) PInvoke method it contains. Csc.exe または vbc.exe の実行C#時に DLL へのMyType.GetTickCount参照を含めることによって、静的メソッドを使用する Visual Basic またはプログラムをコンパイルできます ( /r:PInvokeTest.dll例:)。You can compile a Visual Basic or C# program that uses the static MyType.GetTickCount method by including a reference to the DLL when you run csc.exe or vbc.exe; for example, /r:PInvokeTest.dll.

using namespace System;
using namespace System::Text;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

    void main()
    {
        // Create the AssemblyBuilder.
        AssemblyName^ asmName = gcnew 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,
            int::typeid,
            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(nullptr, nullptr));

        // 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: 1314410994
Saving: 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
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説明を参照) を、このメソッドの引数として指定することはできません。Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. たとえば、メソッドが値を返すMethodImplAttributes.PreserveSig場合は、 PInvokeメソッドの作成後に DLL インポート属性を追加する必要があります。For example, the DLL import attribute MethodImplAttributes.PreserveSig must be added after the PInvoke method is created, if the method returns a value. この例では、その方法を示します。The example shows how to do this.

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

PInvoke メソッドに名前を指定して、メソッドが定義されている DLL の名前、エントリ ポイント名、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーター型、および PInvoke フラグを定義します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the name of the entry point, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

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

パラメーター

name
String String String

PInvoke メソッドの名前。The name of the PInvoke method. name に埋め込み null 値を含めることはできません。name cannot contain embedded nulls.

dllName
String String String

PInvoke メソッドが定義されている DLL の名前。The name of the DLL in which the PInvoke method is defined.

entryName
String String String

DLL 内のエントリ ポイントの名前。The name of the entry point in the DLL.

attributes
MethodAttributes MethodAttributes MethodAttributes

メソッドの属性。The attributes of the method.

callingConvention
CallingConventions CallingConventions CallingConventions

メソッドの呼び出し規則。The method's calling convention.

returnType
Type Type Type

メソッドの戻り値の型。The method's return type.

parameterTypes
Type[]

メソッドのパラメーター型。The types of the method's parameters.

nativeCallConv
CallingConvention CallingConvention CallingConvention

ネイティブ呼び出し規則。The native calling convention.

nativeCharSet
CharSet CharSet CharSet

メソッドのネイティブ文字セット。The method's native character set.

戻り値

定義された PInvoke メソッド。The defined PInvoke method.

例外

メソッドは静的ではありません。The method is not static.

または-or- 親の型がインターフェイスです。The parent type is an interface.

または-or- 抽象メソッドです。The method is abstract.

- または --or- メソッドは以前に定義されています。The method was previously defined.

または-or- namedllName、または entryName の長さが 0 です。The length of name, dllName, or entryName is zero.

namedllName、または entryNamenull です。name, dllName, or entryName is null.

含んでいる型が CreateType() を使用して以前に作成されています。The containing type has been previously created using CreateType().

次のコード例では、メソッドをDefinePInvokeMethod使用してPInvokeメソッドを作成する方法MethodImplAttributes.PreserveSigと、を作成MethodBuilderMethodBuilder.GetMethodImplementationFlagsた後に、およびを使用して、メソッドの実装フラグにフラグを追加する方法を示します。MethodBuilder.SetImplementationFlagsメソッド。The following code example demonstrates how to use the DefinePInvokeMethod method to create a PInvoke method, and how to add the MethodImplAttributes.PreserveSig flag to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

重要

0以外の戻り値を取得するには、 MethodImplAttributes.PreserveSigフラグを追加する必要があります。To get a non-zero return value, you must add the MethodImplAttributes.PreserveSig flag.

この例では、1つの動的モジュールと1つの型MyType() を使用PInvokeして、メソッドを含む動的アセンブリを作成します。The example creates a dynamic assembly with one dynamic module and a single type, MyType, that contains the PInvoke method. メソッドPInvokeは、Win32 GetTickCount関数を表します。The PInvoke method represents the Win32 GetTickCount function.

この例を実行すると、 PInvokeメソッドが実行されます。When the example is run, it executes the PInvoke method. また、動的アセンブリを PInvokeTest として保存します。It also saves the dynamic assembly as PInvokeTest.dll. Ildasm.exe (IL 逆アセンブラー)を使用しMyTypeて、クラスとstaticそれに含まれる (Shared Visual Basic) PInvokeメソッドを調べることができます。You can use the Ildasm.exe (IL Disassembler) to examine the MyType class and the static (Shared in Visual Basic) PInvoke method it contains. Csc.exe または vbc.exe の実行C#時に DLL へのMyType.GetTickCount参照を含めることによって、静的メソッドを使用する Visual Basic またはプログラムをコンパイルできます ( /r:PInvokeTest.dll例:)。You can compile a Visual Basic or C# program that uses the static MyType.GetTickCount method by including a reference to the DLL when you run csc.exe or vbc.exe; for example, /r:PInvokeTest.dll.

using namespace System;
using namespace System::Text;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

    void main()
    {
        // Create the AssemblyBuilder.
        AssemblyName^ asmName = gcnew 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,
            int::typeid,
            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(nullptr, nullptr));

        // 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: 1314410994
Saving: 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
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説明を参照) を、このメソッドの引数として指定することはできません。Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. たとえば、メソッドが値を返すMethodImplAttributes.PreserveSig場合は、 PInvokeメソッドの作成後に DLL インポート属性を追加する必要があります。For example, the DLL import attribute MethodImplAttributes.PreserveSig must be added after the PInvoke method is created, if the method returns a value. この例では、その方法を示します。The example shows how to do this.

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

PInvoke メソッドを定義します。名前、メソッドが定義されている DLL の名前、エントリ ポイント名、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーター型、PInvoke フラグ、およびパラメーターと戻り値の型のカスタム修飾子を指定します。Defines a PInvoke method given its name, the name of the DLL in which the method is defined, the name of the entry point, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, the PInvoke flags, and custom modifiers for the parameters and return type.

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

パラメーター

name
String String String

PInvoke メソッドの名前。The name of the PInvoke method. name に埋め込み null 値を含めることはできません。name cannot contain embedded nulls.

dllName
String String String

PInvoke メソッドが定義されている DLL の名前。The name of the DLL in which the PInvoke method is defined.

parameterTypeOptionalCustomModifiers
Type[][]

型の配列の配列。An array of arrays of types. 型の各配列は、 IsConstのような、対応するパラメーターの省略可能なカスタム修飾子を表します。Each array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。If a particular parameter has no optional custom modifiers, specify null instead of an array of types. どのパラメーターにも省略可能なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

nativeCallConv
CallingConvention CallingConvention CallingConvention

ネイティブ呼び出し規則。The native calling convention.

nativeCharSet
CharSet CharSet CharSet

メソッドのネイティブ文字セット。The method's native character set.

entryName
String String String

DLL 内のエントリ ポイントの名前。The name of the entry point in the DLL.

attributes
MethodAttributes MethodAttributes MethodAttributes

メソッドの属性。The attributes of the method.

callingConvention
CallingConventions CallingConventions CallingConventions

メソッドの呼び出し規則。The method's calling convention.

returnType
Type Type Type

メソッドの戻り値の型。The method's return type.

returnTypeRequiredCustomModifiers
Type[]

メソッドの戻り値の型の IsConstなど、必須のカスタム修飾子を表す型の配列。An array of types representing the required custom modifiers, such as IsConst, for the return type of the method. 戻り値の型が必須のカスタム修飾子を持たない場合は、null を指定します。If the return type has no required custom modifiers, specify null.

returnTypeOptionalCustomModifiers
Type[]

メソッドの戻り値の型の IsConst のような、省略可能なカスタム修飾子を表す型の配列。An array of types representing the optional custom modifiers, such as IsConst, for the return type of the method. 戻り値の型に省略可能のカスタム修飾子がない場合は、null を指定します。If the return type has no optional custom modifiers, specify null.

parameterTypes
Type[]

メソッドのパラメーター型。The types of the method's parameters.

parameterTypeRequiredCustomModifiers
Type[][]

型の配列の配列。An array of arrays of types. 型の各配列は、IsConst のような、対応するパラメーターの必須のカスタム修飾子を表します。Each array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. 特定のパラメーターに必須のカスタム修飾子がない場合は、型の配列の代わりに null を指定します。If a particular parameter has no required custom modifiers, specify null instead of an array of types. どのパラメーターにも必須のカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

戻り値

定義された PInvoke メソッドを表す MethodBuilderA MethodBuilder representing the defined PInvoke method.

例外

メソッドは静的ではありません。The method is not static.

または-or- 親の型がインターフェイスです。The parent type is an interface.

または-or- 抽象メソッドです。The method is abstract.

- または --or- メソッドは以前に定義されています。The method was previously defined.

または-or- namedllName、または entryName の長さが 0 です。The length of name, dllName, or entryName is zero.

- または --or- parameterTypeRequiredCustomModifiers または parameterTypeOptionalCustomModifiers のサイズは parameterTypes のサイズと同じではありません。The size of parameterTypeRequiredCustomModifiers or parameterTypeOptionalCustomModifiers does not equal the size of parameterTypes.

namedllName、または entryNamenull です。name, dllName, or entryName is null.

型は CreateType() を使用して既に作成されました。The type was previously created using CreateType().

- または --or- 現在の動的な型では、IsGenericType プロパティは true ですが、IsGenericTypeDefinition プロパティは false です。For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

次のコード例は、[], type][[[],]type, type,[typeの使用方法を示しています。][xref: TypeBuilder% 2a PInvoke > メソッドを作成するメソッド、およびを作成した後にメソッドの実装フラグにMethodImplAttributes.PreserveSigフラグを追加する方法について説明します。 ] <。メソッドMethodBuilder.GetMethodImplementationFlagsMethodBuilder.SetImplementationFlagsメソッドを使用します。 MethodBuilderThe following code example demonstrates how to use the [], Type[], Type[], Type[][], Type[]<xref:System.Reflection.Emit.TypeBuilder.DefinePInvokeMethod%2A> method to create a PInvoke method, and how to add the MethodImplAttributes.PreserveSig flag to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

この例では、1つの動的モジュールと1つの型MyType() を使用PInvokeして、メソッドを含む動的アセンブリを作成します。The example creates a dynamic assembly with one dynamic module and a single type, MyType, that contains the PInvoke method. メソッドPInvokeは、Win32 GetTickCount関数を表します。The PInvoke method represents the Win32 GetTickCount function.

重要

0以外の戻り値を取得するには、 MethodImplAttributes.PreserveSigフラグを追加する必要があります。To get a non-zero return value, you must add the MethodImplAttributes.PreserveSig flag.

注意

この例では、カスタム修飾子を指定しないオーバーロードを使用しています。The example uses an overload that does not specify custom modifiers. カスタム修飾子を指定するには、代わりにこのメソッドオーバーロードを使用するようにコード例を変更します。To specify custom modifiers, change the example code to use this method overload instead.

この例を実行すると、 PInvokeメソッドが実行されます。When the example is run, it executes the PInvoke method. また、動的アセンブリを PInvokeTest として保存します。It also saves the dynamic assembly as PInvokeTest.dll. Ildasm.exe (IL 逆アセンブラー)を使用しMyTypeて、クラスとstaticそれに含まれる (Shared Visual Basic) PInvokeメソッドを調べることができます。You can use the Ildasm.exe (IL Disassembler) to examine the MyType class and the static (Shared in Visual Basic) PInvoke method it contains. Csc.exe または vbc.exe の実行C#時に DLL へのMyType.GetTickCount参照を含めることによって、静的メソッドを使用する Visual Basic またはプログラムをコンパイルできます ( /r:PInvokeTest.dll例:)。You can compile a Visual Basic or C# program that uses the static MyType.GetTickCount method by including a reference to the DLL when you run csc.exe or vbc.exe; for example, /r:PInvokeTest.dll.

using namespace System;
using namespace System::Text;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

    void main()
    {
        // Create the AssemblyBuilder.
        AssemblyName^ asmName = gcnew 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,
            int::typeid,
            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(nullptr, nullptr));

        // 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: 1314410994
Saving: 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
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説明を参照) を、このメソッドの引数として指定することはできません。Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. たとえば、メソッドが値を返すMethodImplAttributes.PreserveSig場合は、 PInvokeメソッドの作成後に DLL インポート属性を追加する必要があります。For example, the DLL import attribute MethodImplAttributes.PreserveSig must be added after the PInvoke method is created, if the method returns a value. この例では、その方法を示します。The example shows how to do this.

注意

カスタム修飾子の詳細については、ECMA Partition II メタデータのドキュメントを参照してください。For more information on custom modifiers, see the ECMA Partition II Metadata documentation. ドキュメントはオンラインで入手できます。MSDN の「ECMA C# and Common Language Infrastructure Standards」 (ECMA の C# および共通言語基盤の標準規格) と、ECMA のインターナショナル Web サイトにある「Standard ECMA-335 - Common Language Infrastructure (CLI)」を参照してください。The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

適用対象