ModuleBuilder.DefinePInvokeMethod メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
PInvoke メソッドを定義します。
オーバーロード
| 名前 | 説明 |
|---|---|
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
指定した名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、および |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
指定した名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、および |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
指定した名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvokeフラグを使用して、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 が null。
含まれる型は、以前に CreateType()
例
次の例では、DefinePInvokeMethod メソッドを使用して、Windows API で外部アンマネージド メソッド (MessageBoxA) のMethodBuilderを作成する方法を示します。 次の使用例は、[ 再試行 ] ボタンと [キャンセル] ボタンを含むメッセージ ボックスを表示し、メッセージ ボックスの戻り値を表示します。
Important
0 以外の戻り値を取得するには、MethodBuilder.GetMethodImplementationFlagsメソッドとMethodBuilder.SetImplementationFlags メソッドを使用して、MethodBuilderを作成した後、メソッド実装フラグにMethodImplAttributes.PreserveSigを追加する必要があります。
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace PInvoke
{
public class Example
{
const int MB_RETRYCANCEL = 5;
static void Main()
{
AssemblyName myAssemblyName = new AssemblyName("TempAssembly");
// Define a dynamic assembly in the current application domain.
AssemblyBuilder myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(
myAssemblyName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("TempModule");
Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) };
// Define a PInvoke method.
MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod(
"MessageBoxA",
"user32.dll",
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
paramTypes,
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.
piMethodBuilder.SetImplementationFlags(
piMethodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// Create global methods.
myModuleBuilder.CreateGlobalFunctions();
// Arguments for calling the method.
Object[] arguments = { 0, "Hello World", "Title", MB_RETRYCANCEL };
MethodInfo pinvokeMethod = myModuleBuilder.GetMethod("MessageBoxA");
Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
Console.WriteLine("Message box returned: {0}",
pinvokeMethod.Invoke(null, arguments));
}
}
}
/* This code example produces input similar to the following:
Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
*/
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Namespace PInvoke
Public Class Example
Const MB_RETRYCANCEL As Integer = 5
Shared Sub Main()
Dim myAssemblyName As New AssemblyName("TempAssembly")
' Define a dynamic assembly in the current application domain.
Dim myAssemblyBuilder As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in "TempAssembly" assembly.
Dim myModuleBuilder As ModuleBuilder = _
myAssemblyBuilder.DefineDynamicModule("TempModule")
Dim paramTypes() As Type = _
{ GetType(Integer), GetType(string), GetType(string), GetType(Integer) }
' Define a PInvoke method.
Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
"MessageBoxA", _
"user32.dll", _
MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, _
GetType(Integer), _
paramTypes, _
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.
piMethodBuilder.SetImplementationFlags( _
piMethodBuilder.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
' Create global methods.
myModuleBuilder.CreateGlobalFunctions()
' Arguments for calling the method.
Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }
Dim pinvokeMethod As MethodInfo = _
myModuleBuilder.GetMethod("MessageBoxA")
Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
Console.WriteLine("Message box returned: {0}", _
pinvokeMethod.Invoke(Nothing, arguments))
End Sub
End Class
End Namespace
' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4
注釈
一部の DLL インポート属性 (System.Runtime.InteropServices.DllImportAttribute の説明を参照) は、このメソッドの引数として指定できません。 このような属性は、メソッドのカスタム属性を出力することによって設定する必要があります。 たとえば、DLL インポート属性 PreserveSig は、カスタム属性を出力することによって設定されます。
適用対象
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
指定した名前、メソッドが定義されている DLL の名前、メソッドの属性、メソッドの呼び出し規則、メソッドの戻り値の型、メソッドのパラメーターの型、およびPInvokeフラグを使用して、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 メソッド。
例外
メソッドが静的でない場合、または包含型がインターフェイスの場合、またはメソッドが以前に定義された場合の抽象メソッドの場合。
name または dllName が null。
含まれる型は、以前に CreateType()
例
次の例では、DefinePInvokeMethod メソッドを使用して、Windows API で外部アンマネージド メソッド (MessageBoxA) のMethodBuilderを作成する方法を示します。 次の使用例は、[ 再試行 ] ボタンと [キャンセル] ボタンを含むメッセージ ボックスを表示し、メッセージ ボックスの戻り値を表示します。
Important
0 以外の戻り値を取得するには、MethodBuilder.GetMethodImplementationFlagsメソッドとMethodBuilder.SetImplementationFlags メソッドを使用して、MethodBuilderを作成した後、メソッド実装フラグにMethodImplAttributes.PreserveSigを追加する必要があります。
この例では、 DefinePInvokeMethod メソッドの異なるオーバーロードを使用しますが、手法は同じです。
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace PInvoke
{
public class Example
{
const int MB_RETRYCANCEL = 5;
static void Main()
{
AssemblyName myAssemblyName = new AssemblyName("TempAssembly");
// Define a dynamic assembly in the current application domain.
AssemblyBuilder myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(
myAssemblyName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("TempModule");
Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) };
// Define a PInvoke method.
MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod(
"MessageBoxA",
"user32.dll",
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
paramTypes,
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.
piMethodBuilder.SetImplementationFlags(
piMethodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// Create global methods.
myModuleBuilder.CreateGlobalFunctions();
// Arguments for calling the method.
Object[] arguments = { 0, "Hello World", "Title", MB_RETRYCANCEL };
MethodInfo pinvokeMethod = myModuleBuilder.GetMethod("MessageBoxA");
Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
Console.WriteLine("Message box returned: {0}",
pinvokeMethod.Invoke(null, arguments));
}
}
}
/* This code example produces input similar to the following:
Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
*/
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Namespace PInvoke
Public Class Example
Const MB_RETRYCANCEL As Integer = 5
Shared Sub Main()
Dim myAssemblyName As New AssemblyName("TempAssembly")
' Define a dynamic assembly in the current application domain.
Dim myAssemblyBuilder As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in "TempAssembly" assembly.
Dim myModuleBuilder As ModuleBuilder = _
myAssemblyBuilder.DefineDynamicModule("TempModule")
Dim paramTypes() As Type = _
{ GetType(Integer), GetType(string), GetType(string), GetType(Integer) }
' Define a PInvoke method.
Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
"MessageBoxA", _
"user32.dll", _
MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, _
GetType(Integer), _
paramTypes, _
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.
piMethodBuilder.SetImplementationFlags( _
piMethodBuilder.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
' Create global methods.
myModuleBuilder.CreateGlobalFunctions()
' Arguments for calling the method.
Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }
Dim pinvokeMethod As MethodInfo = _
myModuleBuilder.GetMethod("MessageBoxA")
Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
Console.WriteLine("Message box returned: {0}", _
pinvokeMethod.Invoke(Nothing, arguments))
End Sub
End Class
End Namespace
' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4
注釈
一部の DLL インポート属性 ( DllImportAttributeの説明を参照) は、このメソッドの引数として指定できません。 このような属性は、メソッドのカスタム属性を出力することによって設定する必要があります。 たとえば、DLL インポート属性 PreserveSig は、カスタム属性を出力することによって設定されます。