Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

VS 2008 Could someone help me with this class (memory) ?

$
0
0
Hi !
I am creating a tool for GTA:SA game, that gets values from addresses like money ( http://www.gtamodding.com/index.php?...esses_%28SA%29 ) and then saves them, and later i will want it to be like game stats, that i will be able to show.

The problem is, that i want to get the value from health address, that is dynamic.However, its very easy to get that address using Cheat Engine, (just add new address manually, and then use pointer + health addres to get valid address.
The pointer is 0xB6F5F0 and its always the same,
Health address that i have to add to the pointer is CPed +0x540, so i have to add pointer value and 0x540 value to get valid address, that i can read its value.
I found this class, that has functions like "readDynamicInt" etc, or "getAddress"
Code:

Module accessMemory

#Region "Declarations"

    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer

    Private Declare Function WriteProcessMemory1 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function WriteProcessMemory2 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Single
    Private Declare Function WriteProcessMemory3 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Long

    Private Declare Function ReadProcessMemory1 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function ReadProcessMemory2 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Single
    Private Declare Function ReadProcessMemory3 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Long

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer

    Const PROCESS_ALL_ACCESS = &H1F0FF

#End Region

#Region "Memory Functions"

    '//
    '//  Write Functions Below //
    '//

    Public Function writeDynamicInt(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Integer)
  Try
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    WriteInteger(processName, newAddress, Value)
    Return True
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Function writeDynamicFloat(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Single)
  Try
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    WriteFloat(processName, newAddress, Value)
    Return True
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Function writeDynamicLong(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Long)
  Try
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    WriteLong(processName, newAddress, Value)
    Return True
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Sub WriteFloat(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Single, Optional ByVal nsize As Integer = 4)
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Exit Sub
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Exit Sub
  End If
  Dim hAddress As Integer
  Dim vBuffer As Single
  hAddress = Address
  vBuffer = Value
  WriteProcessMemory2(hProcess, hAddress, vBuffer, nsize, 0)
  CloseHandle(hProcess)
    End Sub

    Public Sub WriteLong(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Long, Optional ByVal nsize As Integer = 4)
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Exit Sub
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Exit Sub
  End If

  Dim hAddress As Integer
  Dim vBuffer As Long

  hAddress = Address
  vBuffer = Value
  WriteProcessMemory3(hProcess, hAddress, vBuffer, nsize, 0)
  CloseHandle(hProcess)
    End Sub

    Public Sub WriteNOPs(ByVal ProcessName As String, ByVal Address As Long, ByVal NOPNum As Integer)
  Dim C As Integer
  Dim B As Integer
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Exit Sub
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Exit Sub
  End If

  B = 0
  For C = 1 To NOPNum
    Call WriteProcessMemory1(hProcess, Address + B, &H90, 1, 0&)
    B = B + 1
  Next C
  CloseHandle(hProcess)
    End Sub

    Public Sub WriteInteger(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Integer, Optional ByVal nsize As Integer = 4)
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Exit Sub
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Exit Sub
  End If

  Dim hAddress, vBuffer As Integer
  hAddress = Address
  vBuffer = Value
  WriteProcessMemory1(hProcess, hAddress, CInt(vBuffer), nsize, 0)
  CloseHandle(hProcess)
    End Sub

    Public Function writeBytes(ByVal processName As String, ByVal baseAddress As Integer, ByVal Bytes As String, ByVal byteLength As Integer)
  Try
    Dim oldByte As Integer = ReadInteger(processName, baseAddress)
    Dim revByte As String = byteReverse(oldByte.ToString("X"))
    Dim byteTemp As String = Bytes & revByte.Remove(0, byteLength)
    Dim byteNew As String = Val("&H" & byteReverse(byteTemp))
    WriteInteger(processName, baseAddress, byteNew)
    Return True
  Catch ex As Exception
    Return False
  End Try
    End Function

    '//
    '//  Read Functions Below  //
    '//
    '//  Write Functions Above //
    '//

    Public Function ReadInteger(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Integer
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If

  Dim hAddress, vBuffer As Integer
  hAddress = Address
  ReadProcessMemory1(hProcess, hAddress, vBuffer, nsize, 0)
  CloseHandle(hProcess)
  Return vBuffer
    End Function

    Public Function ReadFloat(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Single
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If

  Dim hAddress As Integer
  Dim vBuffer As Single

  hAddress = Address
  ReadProcessMemory2(hProcess, hAddress, vBuffer, nsize, 0)
  CloseHandle(hProcess)
  Return vBuffer
    End Function

    Public Function ReadLong(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Long
  If ProcessName.EndsWith(".exe") Then
    ProcessName = ProcessName.Replace(".exe", "")
  End If
  Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  If MyP.Length = 0 Then
    MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If
  Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  If hProcess = IntPtr.Zero Then
    MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
    Return vbEmpty
    Exit Function
  End If

  Dim hAddress As Integer
  Dim vBuffer As Long

  hAddress = Address
  ReadProcessMemory3(hProcess, hAddress, vBuffer, nsize, 0)
  CloseHandle(hProcess)
  Return vBuffer
    End Function

    Public Function readDynamicInt(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  Try
    Dim newValue As Integer
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    newValue = ReadInteger(processName, newAddress)
    Return newValue
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Function readDynamicFloat(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  Try
    Dim newValue As Single
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    newValue = ReadFloat(processName, newAddress)
    Return newValue
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Function readDynamicLong(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  Try
    Dim newValue As Long
    Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
    newValue = ReadLong(processName, newAddress)
    Return newValue
  Catch ex As Exception
    Return False
  End Try
    End Function

    Public Function readBytes(ByVal processName As String, ByVal baseAddress As String, ByVal byteLength As Integer)
  Try
    Dim oldByte As Integer = ReadInteger(processName, baseAddress)
    Dim revByte As String = byteReverse(oldByte.ToString("X"))
    Dim byteNew As String = revByte.Remove(byteLength)
    Return byteNew
  Catch ex As Exception
    Return False
  End Try
    End Function

    '//
    '//  Other Functions Below //
    '//

    Public Function getAddress(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  Dim genAddress As String = baseAddress
  Dim offSplit As String() = Offsets.Split(New Char() {":"c})
  Dim offTemp As String
  For Each offTemp In offSplit
    Dim grabAddress As Integer = ReadInteger(processName, genAddress)
    Dim offConvert As Integer = Convert.ToInt32(offTemp, 16)
    Dim addAddress = grabAddress + offConvert
    genAddress = "&H" & addAddress.ToString("X")
  Next
  Return genAddress
    End Function

    Public Function byteReverse(ByVal Input As String) As String
  Try
    Dim byteTemp As New List(Of String)
    Dim newByte As String = ""
    For i = 0 To Input.Length - 1 Step 2
    If i + 1 < Input.Length Then
    byteTemp.Add(Input.Substring(i, 2))
    Else
    byteTemp.Add(Input.Substring(i))
    End If
    Next
    For i = 0 To byteTemp.Count - 1
    newByte = byteTemp(i).ToString & newByte
    Next
    Return newByte
  Catch ex As Exception
    Return False
  End Try
    End Function

#End Region

End Module

I just want to use readDynamicFloat (health address is float type, but i may be wrong, and i need to use other function)
So i tried using it like this:
Label1.Text = readDyamicFloat("gta_sa.exe", &HB6F5F0, "540")
but when i try to use it, it shows a msgbox with "Failed to open gta_sa" and i have tried to use this class before, and it also was giving that error.However i have tried other class, to try to write something to a address, and it worked for me.But i want to read a dynamic address, and its the only class i found with those functions.I checked the code, and it detects the process, but it cant open it, could someone help me/fix this ?
Im using Windows XP SP3 and Visual Basic 2008 Express

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>