For the sake of simplicity this call can be wrapped up in two utility functions - one of which returns a Long value from a pointer in another process' memory and another wich returns a string from a pointer in another application's memory thus:-
VB Code:
Private Declare Function ReadProcessMemoryBytes Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, lpBuffer As Byte, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long Public Function LongFromOutOfprocessPointer(ByVal hProcess As Long, ByVal lpAddress As Long) As Long Dim lRet As Long Dim lBytesWritten As Long Call ReadProcessMemory(hProcess, lpAddress, ByVal VarPtr(lRet), Len(lRet), lBytesWritten) If lBytesWritten > 0 Then LongFromOutOfprocessPointer = lRet End If End Function Public Function StringFromOutOfProcessPointer(ByVal hProcess As Long, ByVal lpString As Long, ByVal Length As Long, ByVal Unicode As Boolean) As String Dim buf() As Byte Dim lRet As Long Dim lBytesWritten As Long Dim sTemp As String ReDim buf(Length) As Byte lRet = ReadProcessMemoryBytes(hProcess, lpString, buf(0), Length, lBytesWritten) If lBytesWritten = 0 And Err.LastDllError = 0 Then While lBytesWritten = 0 And Length > 0 Length = Length - 1 lRet = ReadProcessMemoryBytes(hProcess, lpString, buf(0), Length, lBytesWritten) Wend Else If Err.LastDllError Then Debug.Print LastSystemError End If End If If lRet <> 0 Then If Unicode Then StringFromOutOfProcessPointer = StrConv(buf, vbFromUnicode) Else For lRet = 0 To lBytesWritten If buf(lRet) = 0 Then Exit For End If sTemp = sTemp & Chr$(buf(lRet)) Next lRet StringFromOutOfProcessPointer = sTemp End If Else If Err.LastDllError Then Debug.Print LastSystemError End If End If
In the second one I have added som error trapping which I recommend using after any API call. The code for LastSystemError is:
VB Code:
Private Declare Function FormatMessage Lib "kernel32" _ Alias "FormatMessageA" (ByVal dwFlags As Long, _ ByVal lpSource As Long, _ ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, _ ByVal lpBuffer As String, _ ByVal nSize As Long, _ Arguments As Long) As Long '\\ -- [ LastSystemError ]---------------------------------- '\\ Returns the message from the system which describes the '\\ last dll error to occur, as '\\ held in Err.LastDllError. This function should be '\\ called as soon after the API call '\\ which might have errored, as this member can be reset '\\ to zero by subsequent API calls. '\\ -------------------------------------------------------- Public Function LastSystemError() As String Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Dim sError As String * 500 '\\ Preinitilise a string buffer to put any error message into Dim lErrNum As Long Dim lErrMsg As Long lErrNum = Err.LastDllError lErrMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lErrNum, 0, sError, Len(sError), 0) LastSystemError = Trim(sError) End Function




Reply With Quote