Results 1 to 3 of 3

Thread: Read/Writting Floats,Doubles,Longs, and String.

  1. #1

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Question Read/Writting Floats,Doubles,Longs, and String.

    Hi, I've been researching all over Google for how to Read/Write Floats, Doubles, Longs and String to memory. I believe I have the reading Longs, and Floats working properly. I'll post my Module (which contains all my Read/Writting). If you could as to be so kind and Help me. It'd be greatly appreciated! Thanks!

    Here's my Module:

    Code:
    Module ReadWritingMemory
        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 Integer
        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 Integer
        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 Integer
        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 Integer
        Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
        Const PROCESS_ALL_ACCESS = &H1F0FF
    
        Public Sub WriteInteger(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Sub
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Sub
            End If
    
            Dim hAddress, vBuffer As Integer
            hAddress = Address
            vBuffer = Value
            WriteProcessMemory1(hProcess, hAddress, CInt(vBuffer), vBuffer.ToString.Length, 0)
        End Sub
    
        Public Sub WriteFloat(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Sub
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Sub
            End If
    
            Dim hAddress As Integer
            Dim vBuffer As Single
    
            hAddress = Address
            vBuffer = Value
            WriteProcessMemory2(hProcess, hAddress, vBuffer, vBuffer.ToString.Length, 0)
        End Sub
    
        Public Sub WriteLong(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Sub
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Sub
            End If
    
            Dim hAddress As Integer
            Dim vBuffer As Long
    
            hAddress = Address
            vBuffer = Value
            WriteProcessMemory3(hProcess, hAddress, vBuffer, vBuffer.ToString.Length, 0)
        End Sub
    
        Public Function ReadInteger(ByVal ProcessName As String, ByVal Address As Integer) 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Function
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Function
            End If
    
            Dim hAddress, vBuffer As Integer
            hAddress = Address
            ReadProcessMemory1(hProcess, hAddress, vBuffer, 4, 0)
            Return vBuffer
        End Function
    
        Public Function ReadFloat(ByVal ProcessName As String, ByVal Address As Integer) 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Function
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Function
            End If
    
            Dim hAddress As Integer
            Dim vBuffer As Single
    
            hAddress = Address
            ReadProcessMemory2(hProcess, hAddress, vBuffer, 4, 0)
            Return vBuffer
        End Function
    
        Public Function ReadLong(ByVal ProcessName As String, ByVal Address As Integer) 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
                MessageBox.Show(ProcessName & " isn't open!")
                Exit Function
            End If
            Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
            If hProcess = IntPtr.Zero Then
                MessageBox.Show("Failed to open " & ProcessName & "!")
                Exit Function
            End If
    
            Dim hAddress As Integer
            Dim vBuffer As Long
    
            hAddress = Address
            ReadProcessMemory3(hProcess, hAddress, vBuffer, 4, 0)
            Return vBuffer
        End Function
    
    End Module

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Read/Writting Floats,Doubles,Longs, and String.

    I am pretty sure you can pass a byte array as the buffer to the WriteProcess memory call. Strings are either going to be fixed length or, more than likely, null terminated. To write a string, you could probably convert it to bytes and then add a null terminator and write it to the process. Obviously if it is different than the current string in that memory location, that could cause problems in the other app.

    For reading strings, if it is a null terminated string (look for a 00 in your hex editor), you can just read chunks of 4 bytes until you hit a 00.

  3. #3

    Thread Starter
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: Read/Writting Floats,Doubles,Longs, and String.

    If you are able to, could you post an example of how to do it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width