Code:
Public Class Memory
Public Declare Function ReadMemoryBytes Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As IntPtr, <Out()> ByVal lpBuffer() As Byte, ByVal dwSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Int32
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As ProcessAccessFlags, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
Public Declare Function ReadMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Int32, ByVal lpBaseAddress As Int32, ByRef lpBuffer As Int32, ByVal nSize As Int32, ByRef lpNumberOfBytesWritten As Int32) As Int32
'Public Declare Function WriteProcessMemory Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As System.UInt32, <Out()> ByRef lpNumberOfBytesWritten As Int32) As Boolean
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As UInt32, ByRef lpNumberOfBytesWritten As UInt32) As Boolean
Enum ProcessAccessFlags : uint
All = &H1F0FFF
End Enum
Public Function GetProcess(ByVal ProcessTitle As String) As Long()
Dim strReturn As Long
Dim ReturnArray(100) As Long
Dim idcount As Integer = 0
Dim proc As Process = Process.GetCurrentProcess
For Each proc In Process.GetProcesses
If proc.MainWindowTitle = ProcessTitle Then
ReturnArray(idcount) = proc.Id
strReturn = proc.Id
idcount = idcount + 1
End If
Next
ReDim Preserve ReturnArray(idcount - 1)
Return ReturnArray
End Function
Public Function ReadMemory(ByVal address() As Long, ByVal strIDVal As Long, ByVal dwSize As Integer) As Byte()
Dim strReturn As Long
If dwSize < 1 Then Return Nothing
Dim buffer(dwSize - 1) As Byte
Dim bytesRead As Integer = 0
Dim winhandle As Long = OpenProcess(ProcessAccessFlags.All, True, strIDVal)
For i = 0 To UBound(address) - 1
ReadMemory(winhandle, address(i) + strReturn, strReturn, 4, 0)
Next
If ReadMemorybytes(winhandle, address(UBound(address)) + strReturn, buffer, dwSize, bytesRead) AndAlso bytesRead > 0 Then
Return buffer
CloseHandle(winhandle)
Else
Return Nothing
End If
End Function
Public Sub WriteMemory(ByVal address() As Long, ByVal strIDVal As Long, ByVal value As Integer)
Dim strreturn As Long
Dim data() As Byte = BitConverter.GetBytes(value)
Dim winhandle As IntPtr = OpenProcess(ProcessAccessFlags.All, True, strIDVal)
For i = 0 To UBound(address)
If i = UBound(address) Then
WriteProcessMemory(winhandle, address(i) + strreturn, data, data.Length, 0&)
End If
ReadMemory(winhandle, address(i) + strreturn, strreturn, 4, 0)
Next
End Sub
Public Function ReadMemoryArray(ByVal memRead As String) As Long()
Dim Flags As String = "0"
Dim count As Integer = 0
Dim AppPath As String
AppPath = Environment.CurrentDirectory()
Dim sr As New System****.StreamReader("mytest.txt") 'Declare a streamreader
Dim CurrentSection As String = "" 'Flag to track what section we are currently in
Dim ThisLine As String = sr.ReadLine() 'Read in the first line
Dim PArray(10) As Long
Dim ArraySplit() As String
Do Until Flags = "5"
If InStr(LCase(ThisLine), LCase(memRead)) Then
Flags = "1"
GoTo NextLine
End If
Select Case Flags
Case "1"
If ThisLine = "" Then
Flags = "5"
GoTo nextline
End If
ArraySplit = Split(ThisLine, "=")
PArray(count) = ArraySplit(1)
count = count + 1
End Select
If sr.Peek = -1 Then
Flags = "5"
End If
nextline:
ThisLine = sr.ReadLine()
Loop
sr.Close() 'close the file
ReDim Preserve PArray(count - 1)
Return PArray
End Function
End Class