Home > Software engineering >  No parameters of dynamic invocation DLL function, why not successful?
No parameters of dynamic invocation DLL function, why not successful?

Time:11-10

No parameters of dynamic invocation DLL function, why not successful?

'class module: APIClass,
Option Explicit
'==============================================================================
'
the data type definition'==============================================================================
Private Type VariableBuffer
VariableParameter () As Byte
End Type
'==============================================================================
'the API function declaration
'==============================================================================
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress call 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)
'==============================================================================
'members define
'==============================================================================
'class global variables in
Private m_opIndex As Long
Private m_OpCode () As Byte
'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The Public Function ExecuteAPI (LibPath As String, APIScript As String) As Long
Dim hProcAddress As Long, hModule As Long, X As Long, As Long Y
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 containing () As Byte, IsHaveParameter As Boolean
Dim ParameterArray () As String, OutputArray () As Long
StringSize=0
ReDim StrByteArray (StringSize)
'recognition function name
APIScript RetLong=InStr (1, "", vbTextCompare)
If RetLong=0 Then
'no parameters function
FunctionName=APIScript
IsHaveParameter=False
The Else
'function with parameter
FunctionName=Left (APIScript RetLong - 1)
IsHaveParameter=True

'recognition function parameter
FunctionParameter=Right (APIScript, Len (APIScript) - RetLong)

'analysis function parameter
ParameterArray=Split (FunctionParameter, ", ")

'the initialization function memory size
ReDim OutputArray (UBound (ParameterArray))

'
format function parametersFor X=0 To UBound (ParameterArray)
If IsNumeric (Trim (ParameterArray (X)))=True Then
LongCount=CLng (Trim (ParameterArray (X)))
OutputArray (X)=LongCount
The Else
StringInfo=Mid (Trim (ParameterArray (X)), 2, Len (ParameterArray (X)) - 3)
Then If Len (StringInfo)=0
OutputArray (X)=CLng (VarPtr (Null))
The Else
ReDim Preserve StrByteArray (StringSize)
ByteArray containing=StrConv (StringInfo vbFromUnicode)
ReDim Preserve StrByteArray (StringSize)
ByteArray containing=StrConv (StringInfo vbFromUnicode)
ReDim Preserve StrByteArray (StringSize). VariableParameter (UBound (ByteArray containing) + 1)
CopyMemory StrByteArray (StringSize). VariableParameter (0), ByteArray containing (0), UBound (ByteArray containing) + 1
OutputArray (X)=CLng (VarPtr (StrByteArray (StringSize). VariableParameter (0)))
StringSize=StringSize + 1
StringSize=StringSize + 1
End the If
End the If
Next X
ReDim m_OpCode (400 + 6 * UBound (OutputArray)) 'keep to write m_OpCode
End the If

'read the API library
HModule=LoadLibrary (ByVal LibPath)
If hModule=0 Then
ExecuteAPI=0 'Library read failure
The Exit Function
End the If

'get function address
HProcAddress=GetProcAddress call (hModule, ByVal FunctionName)
If hProcAddress=0 Then
Read ExecuteAPI=0 'function failure
FreeLibrary hModule
The Exit Function
End the If

If IsHaveParameter=True Then
'the situation of the parameters in the execution of
ExecuteAPI=CallWindowProc (GetCodeStart (hProcAddress, OutputArray), 0, 1, 2, 3)
The Else
'with no arguments in the execution of
the condition of theExecuteAPI=CallWindowProc (hProcAddress, 0, 1, 2, 3)
End the If


'release library spaceFreeLibrary hModule
End the Function

Private Function GetCodeStart (ByVal lngProc As Long, arrParams ()) As Long As Long
Dim lngIndex As Long, lngCodeStart As Long
LngCodeStart=(VarPtr (m_OpCode (0)) Or & amp; HF) + 1
M_opIndex=lngCodeStart - VarPtr (m_OpCode (0))
For lngIndex=0 To m_opIndex - 1
M_OpCode (lngIndex)=& amp; HCC
Next lngIndex
For lngIndex=UBound (arrParams) Step 1 To 0
AddByteToCode & amp; H68
AddLongToCode arrParams (lngIndex)
Next lngIndex
AddByteToCode & amp; HE8
AddLongToCode lngProc - VarPtr (m_OpCode (m_opIndex)) - 4
AddByteToCode & amp; HC2
AddByteToCode & amp; H10
AddByteToCode & amp; H0
GetCodeStart=lngCodeStart
End the Function

Private Sub AddLongToCode (lData As Long)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • API
  • Related