嘿,夠簡單吧!下面是動態(tài)調用MessageBoxA的源代碼,上面的步驟被封裝到RunDll32函數中,可放到模塊(CallAPIbyName.bas)中:
Dim s1() As Byte, s2() As Byte
Dim ret As Long
s1 = StrConv("Hello~World", vbFromUnicode)
s2 = StrConv("VBNote", vbFromUnicode)
ret = RunDll32("user32", "MessageBoxA", hwnd, VarPtr(s1(0)), VarPtr(s2(0)), 0&)
CallAPIbyName.bas中的源代碼:
Option Explicit
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Public m_opIndex As Long ′寫入位置
Private m_OpCode() As Byte ′Assembly 的OPCODE
Public Function RunDll32(LibFileName As String, ProcName As String, ParamArray Params()) As Long
Dim hProc As Long
Dim hModule As Long
ReDim m_OpCode(400 + 6 * UBound(Params)) ′保留用來寫m_OpCode
′讀取API庫
hModule = LoadLibrary(ByVal LibFileName)
If hModule = 0 Then
MsgBox "Library讀取失敗!"
Exit Function
End If
′取得函數地址
hProc = GetProcAddress(hModule, ByVal ProcName)
If hProc = 0 Then
MsgBox "函數讀取失敗!", vbCritical
FreeLibrary hModule
Exit Function
End If
′執(zhí)行Assembly Code部分
RunDll32 = CallWindowProc(GetCodeStart(hProc, Params), 0, 1, 2, 3)
FreeLibrary hModule ′釋放空間
End Function
Private Function GetCodeStart(ByVal lngProc As Long, ByVal arrParams As Variant) As Long
相關推薦:北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |