PostVB Memory Class x86

Posts 18 of 8 · Page 1 of 1
VB Memory Class x86
Hello people, I made a C# memory class a few weeks ago, I just thought I would make a version available for Visual Basic.

Please not that I used a online converter for this and I haven't tested it. http://converter.telerik.com/(Please note, I haven't looked through the website for download links. So if there's a download link, And you download it, It's your own risk and problem if anything happens to you.)


Features:
WriteBytes
WriteByte
WriteInt
WriteString
WriteFloat
ReadBytes
ReadByte
ReadInt
ReadString
ReadString Advanced
ReadFloat
PatternScan


Code:
Class Memory
    Private pHandel As IntPtr = IntPtr.Zero
    Private attachedProcess As Process = Nothing
    Private buffer As Byte()

    Public Function Open_pHandel(processName As String) As Boolean
        Dim proc As Process() = Process.GetProcessesByName(processName)

        If proc.Length = 0 Then
            Return False
        ElseIf proc.Length > 1 Then
            Throw New Exception("More then one process found.")
        Else
            attachedProcess = proc(0)
            pHandel = proc(0).Handle
            Return True
        End If
    End Function

#Region "x86"
#Region "Write"
    Public Function WriteBytes(address As Integer, value As Byte(), Optional virtualProtect As Boolean = False) As Boolean
        Dim countOfUsed As Integer = 0
        Dim result As Boolean = False
        Dim oldProtection As UInteger = 0


        If virtualProtect Then
            Win32.x86.VirtualProtectEx(pHandel, address, CUInt(value.Length), &H40, oldProtection)
        End If

        result = Win32.x86.WriteProcessMemory(pHandel, address, value, CUInt(value.Length), countOfUsed)

        If virtualProtect Then
            Win32.x86.VirtualProtectEx(pHandel, address, CUInt(value.Length), oldProtection, oldProtection)
        End If

        Return result
    End Function

    Public Function WriteInt(address As Integer, value As Integer, Optional virtualProtect As Boolean = False) As Boolean
        buffer = BitConverter.GetBytes(value)
        Return WriteBytes(address, buffer, virtualProtect)
    End Function

    Public Function WriteByte(address As Integer, value As Byte, Optional virtualProtect As Boolean = False) As Boolean
        buffer = New Byte() {value}
        Return WriteBytes(address, buffer, virtualProtect)
    End Function

    Public Function WriteString(address As Integer, value As String, Optional virtualProtect As Boolean = False) As Boolean
        buffer = Encoding.ASCII.GetBytes(value)
        'No unicode support atm
        Return WriteBytes(address, buffer, virtualProtect)
    End Function

    Public Function WriteFloat(address As Integer, value As Single, Optional virtualProtect As Boolean = False) As Boolean
        buffer = BitConverter.GetBytes(value)
        Return WriteBytes(address, buffer, virtualProtect)
    End Function
#End Region
#Region "Read"
    Public Function ReadBytes(address As Integer, length As Integer) As Byte()
        Dim readBytes__1 As Byte() = New Byte(length - 1) {}
        Dim numBytesChanged As Integer = 0

        Win32.x86.ReadProcessMemory(pHandel, address, readBytes__1, CUInt(length), numBytesChanged)

        Return readBytes__1
    End Function

    Public Function ReadInt(address As Integer) As Integer
        Return BitConverter.ToInt32(ReadBytes(address, 4), 0)
    End Function

    Public Function ReadByte(address As Integer) As Byte
        Return ReadBytes(address, 1)(0)
    End Function

    Public Function ReadString(address As Integer, length As Integer) As String
        'No unicode support
        Return Encoding.ASCII.GetString(ReadBytes(address, length))
    End Function

    Public Function ReadStringAdvanced(address As Integer, Optional maxStringLength As Integer = 1000) As String
        'No unicode support
        Dim result As String = Nothing
        Dim currentByte As Byte = 0

        For i As Integer = 0 To maxStringLength - 1
            currentByte = ReadByte(address + i)

            If currentByte = &H0 Then
                Exit For
            Else
                result += ChrW(currentByte)

            End If
        Next

        Return result
    End Function

    Public Function ReadFloat(address As Integer) As Single
        Return BitConverter.ToSingle(ReadBytes(address, 4), 0)
    End Function
#End Region
#Region "Scans"
    Public Function PatternScan(pattern As String) As Integer
        Dim splitPattern As String() = pattern.Split(" "c)
        Dim indexValid As Boolean() = New Boolean(splitPattern.Length - 1) {}
        Dim indexValue As Byte() = New Byte(splitPattern.Length - 1) {}

        Dim tempByte As Byte = CByte(&H0)

        For i As Integer = 0 To splitPattern.Length - 1
            indexValid(i) = Not (splitPattern(i) = "??" OrElse splitPattern(i) = "?")
            If [Byte].TryParse(splitPattern(i), tempByte) Then
                indexValue(i) = tempByte
            Else
                indexValid(i) = False
            End If
        Next

        Dim startOfMemory As Integer = attachedProcess.MainModule.BaseAddress.ToInt32()
        Dim endOfMemory As Integer = attachedProcess.MainModule.ModuleMemorySize

        For currentMemAddy As Integer = startOfMemory To endOfMemory - 1
            Dim complete As Boolean = False
            For i As Integer = 0 To splitPattern.Length - 1
                If Not indexValid(i) Then
                    Continue For
                End If

                tempByte = ReadByte(currentMemAddy + i)

                If tempByte <> indexValue(i) Then
                    Exit For
                End If

                If i = splitPattern.Length - 1 Then
                    complete = True
                End If

                If complete Then
                    Exit For
                End If
            Next

            If complete Then
                Return currentMemAddy
            End If
        Next

        Throw New Exception("Pattern not found!")
        Return 0
    End Function
#End Region
#End Region

    Private NotInheritable Class Win32
        Private Sub New()
        End Sub
        Public NotInheritable Class NativeMethods
            Private Sub New()
            End Sub
#Region "IsWow64Process"
            Public Shared Function IsWow64Process(handel As IntPtr) As Boolean
                Dim isTarget64Bit As Boolean = False
                IsWow64Process(handel, isTarget64Bit)
                Return isTarget64Bit
            End Function

            <DllImport("kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
            Private Shared Function IsWow64Process(<[In]> process As IntPtr, <Out> ByRef wow64Process As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
            End Function
#End Region
        End Class

        Public NotInheritable Class x64
            Private Sub New()
            End Sub
            <DllImport("kernel32.dll", SetLastError:=True)> _
            Public Shared Function VirtualProtectEx(hProcess As IntPtr, lpAddress As Long, dwSize As UInt32, flNewProtect As UInteger, ByRef lpflOldProtect As UInteger) As Boolean
            End Function

            <DllImport("kernel32.dll")> _
            Public Shared Function ReadProcessMemory(hProcess As IntPtr, lpBaseAddress As Long, <[In], Out> buffer As Byte(), size As UInt32, ByRef lpNumberOfBytesWritten As Integer) As Boolean
            End Function

            <DllImport("kernel32.dll")> _
            Public Shared Function WriteProcessMemory(hProcess As IntPtr, lpBaseAddress As Long, <[In], Out> buffer As Byte(), size As UInt32, ByRef lpNumberOfBytesWritten As Integer) As Boolean
            End Function
        End Class

        Public NotInheritable Class x86
            Private Sub New()
            End Sub
            <DllImport("kernel32.dll", SetLastError:=True)> _
            Public Shared Function VirtualProtectEx(hProcess As IntPtr, lpAddress As Integer, dwSize As UInt32, flNewProtect As UInteger, ByRef lpflOldProtect As UInteger) As Boolean
            End Function

            <DllImport("kernel32.dll")> _
            Public Shared Function ReadProcessMemory(hProcess As IntPtr, lpBaseAddress As Integer, <[In], Out> buffer As Byte(), size As UInt32, ByRef lpNumberOfBytesWritten As Integer) As Boolean
            End Function

            <DllImport("kernel32.dll")> _
            Public Shared Function WriteProcessMemory(hProcess As IntPtr, lpBaseAddress As Integer, <[In], Out> buffer As Byte(), size As UInt32, ByRef lpNumberOfBytesWritten As Integer) As Boolean
            End Function
        End Class
    End Class
End Class
usage:
Code:
        Dim mem = New Memory()
        If mem.Open_pHandel("processName") Then
            Dim address = mem.PatternScan("02 255 12 41 ?? 54 ? 12 ? 52 ?? 23")
            mem.WriteBytes(address, New Byte() {&H0, &H0})
        End If
(Usage took me like 10 minutes to do. Stupid VB syntax)


Don't forget to press thanks if you find this useful!
i can't get the pattern scanner to work, it always returned pattern not found even tho cheat engine can find the pattern
EDIT : it works now, made a little mistake
well kinda having another prob with :
currentMemAddy = startOfMemory to endOfMemory - 1

sometimes the base address is bigger than the end of memory, or vice versa. thus it can't go "To it"
which results in patternScan to return 0.
what i did was : For currentMemAddy As Integer = startOfMemory To startOfMemory + endOfMemory
just so i can have a little range to scan into. is their a better way?
Quote Originally Posted by Sazor98 View Post
sometimes the base address is bigger than the end of memory, or vice versa. thus it can't go "To it"
That's because endOfMemory actually refers to the size of the module not the end in memory. The size is relative and not absulute so your solution is actually the correct way to go.
Works for mono?
Quote Originally Posted by Ethede View Post
Works for mono?
What's mono?
Quote Originally Posted by Silent View Post


What's mono?
A cross platform port for the .NET Framework.

But to answer the question.

Quote Originally Posted by Ethede View Post
Works for mono?
I doubt that it will work as it uses Win32 API which is Windows specific.. (obviously)
Posts 18 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?