码迷,mamicode.com
首页 > Windows程序 > 详细

vb.net 动态调用api

时间:2014-07-24 14:44:25      阅读:562      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   for   re   c   

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

vb.net 动态调用api,布布扣,bubuko.com

vb.net 动态调用api

标签:des   style   blog   color   io   for   re   c   

原文地址:http://www.cnblogs.com/nanfei/p/3865407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!