本文整理匯總了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