本文整理汇总了VB.NET中System.Reflection.Emit.TypeBuilder.DefinePInvokeMethod方法的典型用法代码示例。如果您正苦于以下问题:VB.NET TypeBuilder.DefinePInvokeMethod方法的具体用法?VB.NET TypeBuilder.DefinePInvokeMethod怎么用?VB.NET TypeBuilder.DefinePInvokeMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefinePInvokeMethod方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
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
输出:
Testing PInvoke method... GetTickCount returned: 1313078714 Saving: PInvokeTest.dll