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