zoukankan      html  css  js  c++  java
  • vb.net 动态调用api

    Imports System
    Imports System.Runtime.InteropServices
    
    Public 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 IntPtr
    End Class
    Imports System
    Imports System.Runtime.CompilerServices
    Imports System.Runtime.InteropServices
    Imports System.Text
    
    Public 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 Sub
    End Class
  • 相关阅读:
    PL/SQL 记录集合IS TABLE OF的使用
    PL/SQL 触发器简介
    plsql 显式游标
    MySQL的基本知识 -- 命令
    排序算法--桶排序
    在C++中实现字符串分割--split
    第一篇献给小驰驰
    解决windows管理员已阻止你运行此应用问题
    centos 7.3 快速安装ceph
    python中int的功能简单介绍
  • 原文地址:https://www.cnblogs.com/nanfei/p/3865407.html
Copyright © 2011-2022 走看看