首页 > 代码库 > vb.net 动态调用api
vb.net 动态调用api
Imports SystemImports System.Runtime.InteropServicesPublic Class DllInvoke Public Sub New(ByVal DLLPath As String) Me.hLib = DllInvoke.LoadLibrary(DLLPath) End Sub Protected Overrides Sub Finalize() Try DllInvoke.FreeLibrary(Me.hLib) Finally MyBase.Finalize() End Try End Sub <DllImport("kernel32.dll")> _ Private Shared Function FreeLibrary(ByVal [lib] As IntPtr) As Boolean End Function <DllImport("kernel32.dll")> _ Private Shared Function GetProcAddress(ByVal [lib] As IntPtr, ByVal funcName As String) As IntPtr End Function Public Function Invoke(ByVal APIName As String, ByVal t As Type) As [Delegate] Return Marshal.GetDelegateForFunctionPointer(DllInvoke.GetProcAddress(Me.hLib, APIName), t) End Function <DllImport("kernel32.dll")> _ Private Shared Function LoadLibrary(ByVal path As String) As IntPtr End Function Private hLib As IntPtrEnd Class
Imports SystemImports System.Runtime.CompilerServicesImports System.Runtime.InteropServicesImports System.TextPublic Class test Private Declare Function MessageBoxA Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Integer, ByVal lpText As StringBuilder, ByVal lpCaption As StringBuilder, ByVal wType As Integer) As Long Private Delegate Function MessageBoxADelegate(ByVal hwnd As Integer, ByVal lpText As StringBuilder, ByVal lpCaption As StringBuilder, ByVal wType As Integer) As Long Public Shared Sub Main() Dim invoke As New DllInvoke("user32.dll") Dim mode As MessageBoxADelegate = DirectCast(invoke.Invoke("MessageBoxA", GetType(MessageBoxADelegate)), MessageBoxADelegate) mode.Invoke(0, New StringBuilder("haha"), New StringBuilder("title"), 0) Console.WriteLine("ok") Console.ReadLine() End SubEnd Class
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。