So as the title states, I'm having an issue with this memory write function. It does work, but it seems to write some extra bytes into the next address above the target address. I'm not quite sure how to fix it since my knowledge in visual basic and memory in general isn't that great. I thought I would make post here for feedback while I try to find some stuff on google as well.
Here is the code I have atm. Read works fine.
Credits for the code: Pingo @ [Only registered and activated users can see links. Click Here To Register...]
EDIT: So I thought I fixed the problem, by looking at the code and changing
to
... But it only made one thing work, and everything else not work properly.
Here is the code I have atm. Read works fine.
Code:
Public Declare Function ReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByVal buffer As Byte(), ByVal size As Integer, ByVal lpNumberOfBytesRead As Integer) As Boolean
Public Declare Function WriteProcessMemory Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByVal buffer As Byte(), ByVal size As Integer, ByVal lpNumberOfBytesWritten As Integer) As Boolean
Private Function GetAddress(ByVal Proc As Process, ByVal Address_Offsets As String) As Integer
On Error Resume Next
Dim tmp As String() = Address_Offsets.Split(" ")
Dim _Addy As Integer = -1
''''''Gets the base address'''''''
If (tmp(0).IndexOf("+", StringComparison.Ordinal) = -1) Then
_Addy = Integer.Parse(tmp(0), System.Globalization.NumberStyles.HexNumber)
Else
Dim BS As String() = tmp(0).Split("+")
For Each M As ProcessModule In Proc.Modules
If (M.ModuleName.ToLower = BS(0).ToLower) Then
_Addy = M.BaseAddress.ToInt32 + Integer.Parse(BS(1), System.Globalization.NumberStyles.HexNumber)
End If
Next
End If
''''''''''''''''''''''''''''''''''
If _Addy = -1 Then
Return -1
End If
If (tmp.Length = 1) Then
Return _Addy
End If
''''''Gets the pointer''''''''''''
Dim buff As Byte() = New Byte(3) {}
ReadProcessMemory(Proc.Handle, _Addy, buff, 4, 0)
_Addy = BitConverter.ToInt32(buff, 0)
For i As Integer = 1 To tmp.Length - 1
Dim Off As Integer = Integer.Parse(tmp(i), System.Globalization.NumberStyles.HexNumber)
ReadProcessMemory(Proc.Handle, _Addy + Off, buff, 4, 0)
_Addy = If(i <> (tmp.Length - 1), BitConverter.ToInt32(buff, 0), _Addy + Off)
Next i
''''''''''''''''''''''''''''''''''
Return _Addy
End Function
Public Function Read(ByVal EXENAME As String, ByVal Address_Offsets As String, ByVal MemType As Object) As Object
If (Address_Offsets <> String.Empty) Then
Dim Proc As Process() = Process.GetProcessesByName(EXENAME)
If Proc.Length = 0 Then
Return -1
End If
Dim buff As Byte()
Select Case Array.IndexOf(Of Object)(New Object() {GetType(Byte), GetType(Integer), GetType(UInt32), GetType(String), GetType(Single), GetType(Double), GetType(UInt16)}, MemType)
Case 0
buff = New Byte(1) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 1, 0)
Return buff(0)
Case 1
buff = New Byte(3) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 4, 0)
Return BitConverter.ToInt32(buff, 0)
Case 2
buff = New Byte(3) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 4, 0)
Return BitConverter.ToUInt32(buff, 0)
Case 3
buff = New Byte(19) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 20, 0)
Return BitConverter.ToString(buff, 0)
Case 4
buff = New Byte(3) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 4, 0)
Return BitConverter.ToSingle(buff, 0)
Case 5
buff = New Byte(7) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 8, 0)
Return BitConverter.ToDouble(buff, 0)
Case 6
buff = New Byte(3) {}
ReadProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, 4, 0)
Return BitConverter.ToUInt16(buff, 0)
Case -1
Return -1
End Select
End If
Return -1
End Function
Public Function Write(ByVal EXENAME As String, ByVal Address_Offsets As String, ByVal Value As Object, ByVal MemType As Object)
If (Address_Offsets <> String.Empty And Value <> Nothing) Then
Dim Proc As Process() = Process.GetProcessesByName(EXENAME)
If Proc.Length = 0 Then
Return -1
End If
Dim buff As Byte() = Nothing
Select Case Array.IndexOf(Of Object)(New Object() {GetType(Byte), GetType(Integer), GetType(UInt16), GetType(UInt32), GetType(String), GetType(Single), GetType(Double)}, MemType)
Case 0
buff = BitConverter.GetBytes(Byte.Parse(Value))
Exit Select
Case 1
buff = BitConverter.GetBytes(Integer.Parse(Value))
Exit Select
Case 2
buff = BitConverter.GetBytes(UInt32.Parse(Value))
Exit Select
Case 3
buff = BitConverter.GetBytes(UInt16.Parse(Value))
Exit Select
Case 4
buff = System.Text.ASCIIEncoding.ASCII.GetBytes(Value)
Exit Select
Case 5
buff = BitConverter.GetBytes(Single.Parse(Value))
Exit Select
Case 6
buff = BitConverter.GetBytes(Double.Parse(Value))
Exit Select
End Select
If Not buff Is Nothing Then
WriteProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, buff.LongLength, 0)
End If
End If
Return -1
End Function
EDIT: So I thought I fixed the problem, by looking at the code and changing
Code:
WriteProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, buff.LongLength, 0)
Code:
WriteProcessMemory(Proc(0).Handle, GetAddress(Proc(0), Address_Offsets), buff, buff.LongLength - 1, 0)