TypeBuilder.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öntemi tanımlar.
Aşırı Yüklemeler
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Adı, yöntemin tanımlandığı DLL'nin adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Adı, yöntemin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet) |
Adı, yönteminin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri, |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yöntemin tanımlandığı DLL'nin adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve PInvoke bayraklar verilen bir PInvoke yöntemi 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-
Üst tür bir arabirimdir.
-veya-
yöntemi soyut bir yöntemdir.
-veya-
Yöntemi önceden tanımlanmıştı.
-veya-
veya dllName uzunluğu name sıfırdır.
name veya dllName şeklindedir null.
İçeren tür daha önce kullanılarak CreateType()oluşturulmuştur.
Örnekler
Aşağıdaki örnekte yöntemi oluşturmak PInvoke için yönteminin nasıl kullanılacağı DefinePInvokeMethod ve ve MethodBuilder.SetImplementationFlags yöntemlerini kullanarak yöntemini oluşturduktan sonra yöntemi uygulama bayraklarına bayrağının MethodBuilder.GetMethodImplementationFlagsMethodBuildernasıl ekleneceği MethodImplAttributes.PreserveSig gösterilmektedir.
Önemli
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Örnek, yöntemini içeren PInvoke tek bir dinamik modül ve tek bir türü MyTypeolan bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dll olarak kaydeder. sınıfını ve static içerdiği (Shared Visual Basic'te) yöntemini incelemek MyType için Ildasm.exe (IL Disassembler)PInvoke kullanabilirsiniz. csc.exe veya vbc.exe çalıştırdığınızda DLL'ye başvuru ekleyerek statik MyType.GetTickCount yöntemi kullanan bir Visual Basic veya C# programı derleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra PInvoke DLL içeri aktarma özniteliği MethodImplAttributes.PreserveSig eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Şunlara uygulanır
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yöntemin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve PInvoke bayraklar verilen bir PInvoke yöntemi 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ğil.
-veya-
Üst tür bir arabirimdir.
-veya-
yöntemi soyut bir yöntemdir.
-veya-
Yöntemi önceden tanımlanmıştı.
-veya-
, dllNameveya entryName uzunluğu namesıfırdır.
name, dllNameveya entryName şeklindedir null.
İçeren tür daha önce kullanılarak CreateType()oluşturulmuştur.
Örnekler
Aşağıdaki kod örneğinde yöntemi oluşturmak için yönteminin nasıl kullanılacağı DefinePInvokeMethod ve ve yöntemlerini kullanarak yöntemini oluşturduktan sonra yöntemi uygulama bayraklarına bayrağının MethodBuilder.GetMethodImplementationFlagsMethodBuilderMethodBuilder.SetImplementationFlags nasıl ekleneceği MethodImplAttributes.PreserveSig gösterilmektedir.PInvoke
Önemli
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Örnek, yöntemini içeren PInvoke tek bir dinamik modül ve tek bir türü MyTypeolan bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dll olarak kaydeder. sınıfını ve static içerdiği (Shared Visual Basic'te) yöntemini incelemek MyType için Ildasm.exe (IL Disassembler)PInvoke kullanabilirsiniz. csc.exe veya vbc.exe çalıştırdığınızda DLL'ye başvuru ekleyerek statik MyType.GetTickCount yöntemi kullanan bir Visual Basic veya C# programı derleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra PInvoke DLL içeri aktarma özniteliği MethodImplAttributes.PreserveSig eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Şunlara uygulanır
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yöntemin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametre türleri, PInvoke bayraklar ve parametreler ve dönüş türü için özel değiştiriciler verilen bir PInvoke yöntemi 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 ^> ^ 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);
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
Parametreler
- name
- String
Yönteminin PInvoke adı.
name katıştırılmış null içeremez.
- dllName
- String
Yönteminin tanımlandığı DLL'nin PInvoke adı.
- entryName
- String
DLL'deki giriş noktasının adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
Yöntemin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- returnTypeRequiredCustomModifiers
- Type[]
yönteminin dönüş türü için gibi gerekli özel değiştiricileri IsConsttemsil eden tür dizisi. Dönüş türünde gerekli özel değiştirici yoksa belirtin null.
- returnTypeOptionalCustomModifiers
- Type[]
yönteminin dönüş türü için gibi isteğe bağlı özel değiştiricileri IsConsttemsil eden tür dizisi. Dönüş türünde isteğe bağlı özel değiştirici yoksa belirtin null.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- parameterTypeRequiredCustomModifiers
- Type[][]
Tür dizileri dizisi. Her tür dizisi, gibi IsConstilgili parametre için gerekli özel değiştiricileri temsil eder. Belirli bir parametrenin gerekli özel değiştiricileri yoksa, bir tür dizisi yerine belirtin null . Parametrelerin hiçbirinin özel değiştiricileri gerekli değilse, dizi dizisi yerine belirtin null .
- parameterTypeOptionalCustomModifiers
- Type[][]
Tür dizileri dizisi. Her tür dizisi, IsConstgibi ilgili parametre için isteğe bağlı özel değiştiricileri temsil eder. Belirli bir parametrenin isteğe bağlı özel değiştiricisi yoksa, tür dizisi yerine belirtin null . Parametrelerin hiçbirinin isteğe bağlı özel değiştiricileri yoksa, dizi dizisi yerine belirtin null .
- nativeCallConv
- CallingConvention
Yerel çağrı kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
MethodBuilder Tanımlı PInvoke yöntemi temsil eden bir.
Özel durumlar
Yöntemi statik değil.
-veya-
Üst tür bir arabirimdir.
-veya-
yöntemi soyut bir yöntemdir.
-veya-
Yöntemi önceden tanımlanmıştı.
-veya-
, dllNameveya entryName uzunluğu namesıfırdır.
-veya-
veya parameterTypeOptionalCustomModifiers boyutuparameterTypeRequiredCustomModifiers, boyutuna parameterTypeseşit değildir.
name, dllNameveya entryName şeklindedir null.
Türü daha önce kullanılarak CreateType()oluşturulmuştur.
-veya-
Geçerli dinamik tür IsGenericType için özelliği şeklindedir true, ancak IsGenericTypeDefinition özelliği şeklindedir false.
Örnekler
Aşağıdaki kod örneğinde yöntemi oluşturmak için yönteminin nasıl kullanılacağı DefinePInvokeMethod ve ve yöntemlerini kullanarak yöntemini oluşturduktan sonra yöntemi uygulama bayraklarına bayrağının MethodBuilder.GetMethodImplementationFlagsMethodBuilderMethodBuilder.SetImplementationFlags nasıl ekleneceği MethodImplAttributes.PreserveSig gösterilmektedir.PInvoke
Örnek, yöntemini içeren PInvoke tek bir dinamik modül ve tek bir türü MyTypeolan bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Önemli
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Not
Örnek, özel değiştiriciler belirtmeyen bir aşırı yükleme kullanır. Özel değiştiricileri belirtmek için örnek kodu bunun yerine bu yöntem aşırı yüklemesini kullanacak şekilde değiştirin.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dll olarak kaydeder. sınıfını ve static içerdiği (Shared Visual Basic'te) yöntemini incelemek MyType için Ildasm.exe (IL Disassembler)PInvoke kullanabilirsiniz. csc.exe veya vbc.exe çalıştırdığınızda DLL'ye başvuru ekleyerek statik MyType.GetTickCount yöntemi kullanan bir Visual Basic veya C# programı derleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına bakın) bu yöntemin DllImportAttributebağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra PInvoke DLL içeri aktarma özniteliği MethodImplAttributes.PreserveSig eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Not
Özel değiştiriciler hakkında daha fazla bilgi için bkz. ECMA C# ve Ortak Dil Altyapısı Standartları ve Standart ECMA-335 - Ortak Dil Altyapısı (CLI).