ModuleBuilder.DefinePInvokeMethod Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir PInvoke yöntem tanımlar.
Aşırı Yüklemeler
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Belirtilen ada, yöntemin tanımlandığı DLL'nin adına, yöntemin özniteliklerine, yöntemin çağırma kuralına, yöntemin dönüş türüne, yöntemin parametrelerinin türlerine ve |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Belirtilen ada, yöntemin tanımlandığı DLL'nin adına, yöntemin özniteliklerine, yöntemin çağırma kuralına, yöntemin dönüş türüne, yöntemin parametrelerinin türlerine ve |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
Belirtilen ada, yöntemin tanımlandığı DLL'nin adına, yöntemin özniteliklerine, yöntemin çağırma kuralına, yöntemin dönüş türüne, yöntemin parametrelerinin türlerine ve PInvoke bayraklara sahip bir PInvoke yöntem tanımlar.
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);
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
Parametreler
- name
- String
yönteminin PInvoke adı.
name katıştırılmış null değerleri içeremez.
- dllName
- String
Yöntemin tanımlandığı DLL'nin PInvoke adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
yönteminin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- nativeCallConv
- CallingConvention
Yerel çağırma kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
Tanımlanan PInvoke yöntem.
Özel durumlar
Yöntemi statik değil veya içeren tür bir arabirimse.
-veya-
yöntemi soyut bir yöntemdir.
-veya-
Yöntemi önceden tanımlanmıştı.
name veya dllName şeklindedir null.
İçeren tür daha önce kullanılarak oluşturulmuştur CreateType()
Örnekler
Aşağıdaki örnekte, Windows API'sinde DefinePInvokeMethod bir dış yönetilmeyen yöntem için oluşturmak MethodBuilder üzere yönteminin MessageBoxAkullanımı gösterilmektedir. Örnek, Yeniden Dene ve İptal düğmeleri içeren bir ileti kutusu görüntüler ve ileti kutusundan döndürülen değeri görüntüler.
Önemli
Sıfır olmayan bir dönüş değeri almak için, ve MethodBuilder.SetImplementationFlags yöntemlerini kullanarak MethodBuilder.GetMethodImplementationFlags öğesini oluşturduktan MethodBuildersonra yöntem uygulama bayraklarına eklemeniz MethodImplAttributes.PreserveSig gerekir.
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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (bkz. System.Runtime.InteropServices.DllImportAttribute açıklaması) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Bu tür öznitelikler yöntemi için özel bir öznitelik yayılarak ayarlanmalıdır. Örneğin, DLL içeri aktarma özniteliği PreserveSig özel bir öznitelik yayılarak ayarlanır.
Not
.NET Framework 2.0 Service Pack 1'den başlayarak, bu üye artık bayrağını ReflectionPermissionFlag.ReflectionEmit gerektirmezReflectionPermission. (Bkz. Yansıma Yaymada Güvenlik Sorunları.) Bu işlevselliği kullanmak için uygulamanızın .NET Framework 3.5 veya sonraki bir sürümü hedeflemesi gerekir.
Şunlara uygulanır
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
- Kaynak:
- ModuleBuilder.cs
Belirtilen ada, yöntemin tanımlandığı DLL'nin adına, yöntemin özniteliklerine, yöntemin çağırma kuralına, yöntemin dönüş türüne, yöntemin parametrelerinin türlerine ve PInvoke bayraklara sahip bir PInvoke yöntem tanımlar.
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);
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
Parametreler
- name
- String
yönteminin PInvoke adı.
name katıştırılmış null değerleri içeremez.
- dllName
- String
Yöntemin tanımlandığı DLL'nin PInvoke adı.
- entryName
- String
DLL'deki giriş noktasının adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
yönteminin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- nativeCallConv
- CallingConvention
Yerel çağırma kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
Tanımlanan PInvoke yöntem.
Özel durumlar
yöntemi statik değildir veya içeren tür bir arabirimse veya yöntemin önceden tanımlandığı yöntemin soyut olup olmadığı.
name veya dllName şeklindedir null.
İçeren tür daha önce kullanılarak oluşturulmuştur CreateType()
Örnekler
Aşağıdaki örnekte, Windows API'sinde DefinePInvokeMethod bir dış yönetilmeyen yöntem için oluşturmak MethodBuilder üzere yönteminin MessageBoxAkullanımı gösterilmektedir. Örnek, Yeniden Dene ve İptal düğmeleri içeren bir ileti kutusu görüntüler ve ileti kutusundan döndürülen değeri görüntüler.
Önemli
Sıfır olmayan bir dönüş değeri almak için, ve MethodBuilder.SetImplementationFlags yöntemlerini kullanarak MethodBuilder.GetMethodImplementationFlags öğesini oluşturduktan MethodBuildersonra yöntem uygulama bayraklarına eklemeniz MethodImplAttributes.PreserveSig gerekir.
Bu örnekte yöntemin DefinePInvokeMethod farklı bir aşırı yüklemesi kullanılır, ancak teknik aynıdır.
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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Bu tür öznitelikler yöntemi için özel bir öznitelik yayılarak ayarlanmalıdır. Örneğin, DLL içeri aktarma özniteliği PreserveSig özel bir öznitelik yayılarak ayarlanır.
Not
.NET Framework 2.0 Service Pack 1'den başlayarak, bu üye artık bayrağını ReflectionPermissionFlag.ReflectionEmit gerektirmezReflectionPermission. (Bkz. Yansıma Yaymada Güvenlik Sorunları.) Bu işlevselliği kullanmak için uygulamanızın .NET Framework 3.5 veya sonraki bir sürümü hedeflemesi gerekir.