VB dynamic call API function module

Source: Internet
Author: User
'// Girl does not cry (QQ: 191035066) @ 22:03:37
Option
Explicit

Private Type variablebuffer
Variableparameter () As Byte
End Type

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 )

Private M_opindex As Long
Private M_opcode () As Byte

Public Function Shellapi ( Byval Libpath $, apiparam $) As Long
Dim Hprocaddress As Long , Hmodule As Long , X As Long , Y As Long
Dim Retlong As Long , Functionname As String , Functionparameter As String
Dim Longcount As Long , Stringinfo As String , Strbytearray () As Variablebuffer
Dim Stringsize As Long , Bytearray () As Byte , Ishaveparameter As Boolean
Dim Parameterarray () As String , Outputarray () As Long
Stringsize = 0
Redim Strbytearray (stringsize)

Retlong =Instr ( 1 , Apiparam, " " , Vbtextcompare)
If Retlong = 0 Then
Functionname = apiparam
Ishaveparameter = False
Else
Functionname = Left (Apiparam, retlong- 1 )
Ishaveparameter = True
Functionparameter = Right (Apiparam, Len (Apiparam)-retlong)
Parameterarray = Split (Functionparameter, " , " )
Redim Outputarray ( Ubound (Parameterarray ))

For X =0 To Ubound (Parameterarray)
If Isnumeric ( Trim (Parameterarray (X) = True Then
Longcount = Clng ( Trim (Parameterarray (X )))
Outputarray (x) = longcount
Else
Stringinfo =Mid ( Trim (Parameterarray (x )), 2 , Len (Parameterarray (x ))- 3 )
If Len (Stringinfo) = 0 Then
Outputarray (x) = Clng (Varptr (null ))
Else
Redim Preserve Strbytearray (stringsize)
Bytearray = Strconv (Stringinfo, vbfromunicode)
Redim Preserve Strbytearray (stringsize). variableparameter ( Ubound (Bytearray) + 1 )
Copymemory strbytearray (stringsize). variableparameter ( 0 ), Bytearray ( 0 ), Ubound (Bytearray) +1
Outputarray (x) = Clng (Varptr (strbytearray (stringsize). variableparameter ( 0 )))
Stringsize = stringsize + 1
End If
End If
Next X
Redim M_opcode ( 400 +6 * Ubound (Outputarray ))
End If

Hmodule = loadlibrary ( Byval Libpath)
If Hmodule = 0 Then
Shellapi = 0
Msgbox " Loadlibrary ("" " & Libpath & " "") Function call failed! " , Vbcritical
Exit Function
End If

Hprocaddress = getprocaddress (hmodule, Byval Functionname)
If Hprocaddress = 0 Then
Shellapi = 0
Msgbox " Getprocaddress ("" " & Functionname & " "") Function call failed! " , Vbcritical
Freelibrary hmodule
Exit Function
End If

If Ishaveparameter = True Then
Shellapi = callwindowproc (getcodestart (hprocaddress, outputarray ), 0 , 1 , 2 , 3 )
Else
Shellapi = callwindowproc (hprocaddress, 0 , 1 , 2 , 3 )
End If

Freelibrary hmodule
End Function

Private Function Getcodestart ( Byval Lngproc As Long , Arrparams () As Long ) As Long
Dim Lngindex As Long , LngcodestartAs Long
Lngcodestart = (varptr (m_opcode ( 0 )) Or & HF) + 1
M_opindex = lngcodestart-varptr (m_opcode ( 0 ))
For Lngindex = 0 To M_opindex- 1
M_opcode (lngindex) = & HCC
Next Lngindex
For Lngindex = Ubound (Arrparams) To 0 Step - 1
Addbytetocode & H68
Addlongtocode arrparams (lngindex)
Next Lngindex
Addbytetocode & he8
Addlongtocode lngproc-varptr (m_opcode (m_opindex ))- 4
Addbytetocode & hc2
Addbytetocode & H10
Addbytetocode & H0
Getcodestart = lngcodestart
End Function

Private Sub Addlongtocode (ldata As Long )
Copymemory m_opcode (m_opindex), ldata, 4
M_opindex = m_opindex + 4
End sub

Private Sub Addinttocode (idata As Integer )
Copymemory m_opcode (m_opindex), idata, 2
M_opindex = m_opindex + 2
End sub

Private Sub Addbytetocode (bdata As Byte )
M_opcode (m_opindex) = bdata
M_opindex = m_opindex + 1
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.