ModuleBuilder.DefinePInvokeMethod 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義了一種 PInvoke 方法。
多載
| 名稱 | Description |
|---|---|
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
定義 |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
定義 |
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 無法包含嵌入的空。
- dllName
- String
定義該方法的 DLL PInvoke 名稱。
- attributes
- MethodAttributes
方法的屬性。
- callingConvention
- CallingConventions
方法是呼叫慣例。
- returnType
- Type
方法的回傳類型。
- parameterTypes
- Type[]
方法參數的類型。
- nativeCallConv
- CallingConvention
原住民呼叫慣例。
- nativeCharSet
- CharSet
該方法的原生字元集。
傳回
定義 PInvoke 的方法。
例外狀況
name 或 dllName 為 null。
包含型態先前已透過 CreateType()
範例
以下範例說明使用 DefinePInvokeMethod 方法,在 Windows API 中為外部非管理方法(MessageBoxA)建立 MethodBuilder。 範例顯示一個訊息框,包含 重試 與 取消 按鈕,並顯示訊息框的回傳值。
Important
要獲得非零的回傳值,你必須在建立 之後MethodBuilder,透過使用 MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags 方法,加入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)
定義 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 無法包含嵌入的空。
- dllName
- String
定義該方法的 DLL PInvoke 名稱。
- 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
要獲得非零的回傳值,你必須在建立 之後MethodBuilder,透過使用 MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags 方法,加入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 是透過輸出自訂屬性來設定的。