SolvedProblem With Memory Reading

Posts 16–17 of 17 · Page 2 of 2
Quote Originally Posted by Broderick View Post
This is a quick function I just wrote to read a string from the remote process:

Code:
    Private Shared Function ReadRemoteString(ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, Optional ByVal encoding As Encoding = Nothing) As String
        If encoding Is Nothing Then encoding = encoding.ASCII 'default encoding value.

        Dim builder As New StringBuilder() 'easiest and cleanest way to build an unknown-length string.
        Dim buffer(255) As Byte 'fucking despise VB's array declarations
        Dim nbytes As Integer = 0
        Dim terminator As Integer = -1

        While terminator < 0 AndAlso ReadProcessMemory(hProcess, lpAddress, buffer, buffer.Length, nbytes) AndAlso nbytes > 0
            lpAddress = New IntPtr(lpAddress.ToInt32() + nbytes) 'advance where we're reading from.
            nbytes = builder.Length 'micro-optimization for .IndexOf
            builder.Append(encoding.GetString(buffer)) 'append the data to the StringBuilder
            terminator = builder.ToString().IndexOf(ControlChars.NullChar, nbytes) 'check if there's a null-byte in the string yet (strings are null-terminated)
        End While

        Return builder.ToString().Substring(0, terminator) 'return the data up til the null terminator.
    End Function
My ReadProcessMemory P/Invoke was this:
Code:
    <DllImport("kernel32.dll")>
    Private Shared Function ReadProcessMemory(ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal szBuffer As Integer, <Out()> ByRef nBytesRead As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
The following imports are required:
Code:
Imports System.Text
Imports System.Runtime.InteropServices
An example usage would be this:

Code:
Dim hProcess As IntPtr = OpenProcess( ... )
Dim value As String = ReadRemoteString(hProcess, New IntPtr(&H1F028), Encoding.Unicode)
MessageBox.Show(value)

Ty for sharing me such a nice writed code. Gonna need it in future for sure
Quote Originally Posted by Hassan View Post


You can change that from compiler options and make it like C#
Yes but seeing as I only write code in VB when I'm helping people here, having to explain compiler option changes every time I use an array is just a pain in the ass.
Posts 16–17 of 17 · Page 2 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us