Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- Private _memManager As New MemoryManager 'New word is important! -I'm not explaining this.
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- If _memManager.TryAttachToProcess("Calculator") Then
- Dim _myScore As Int32 = _memManager.ReadInt32(&HAC4E4C)
- ' Done using MemoryManager for reading. Close it.
- _memManager.DetachFromProcess()
- MessageBox.Show("Your current 3D Pinball score is: " & _myScore.ToString())
- Else
- MessageBox.Show("MemoryManager unable to attach to 3D Pinball for ram reading")
- End If
- End Sub
- Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAcess As UInt32, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Int32) As IntPtr
- Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
- Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
- Public Class MemoryManager
- Private _targetProcess As Process = Nothing 'to keep track of it. not used yet.
- Private _targetProcessHandle As IntPtr = IntPtr.Zero 'Used for ReadProcessMemory
- Private PROCESS_ALL_ACCESS As UInt32 = &H1F0FFF
- Private PROCESS_VM_READ As UInt32 = &H10
- Public Function TryAttachToProcess(ByVal windowCaption As String) As Boolean
- Dim _allProcesses() As Process = Process.GetProcesses
- For Each pp As Process In _allProcesses
- If pp.MainWindowTitle.ToLower.Contains(windowCaption.ToLower) Then
- 'found it! proceed.
- Return TryAttachToProcess(pp)
- End If
- Next
- MessageBox.Show("Unable to find process '" & windowCaption & ".' Is running?")
- Return False
- End Function
- Public Function TryAttachToProcess(ByVal proc As Process) As Boolean
- If _targetProcessHandle = IntPtr.Zero Then 'not already attached
- _targetProcess = proc
- _targetProcessHandle = OpenProcess(ReadProcessMemoryRights.PROCESS_ALL_ACCESS, False, _targetProcess.Id)
- If _targetProcessHandle = 0 Then
- TryAttachToProcess = False
- MessageBox.Show("OpenProcess() FAIL! Are you Administrator??")
- Else
- 'if we get here, all connected and ready to use ReadProcessMemory()
- TryAttachToProcess = True
- MessageBox.Show("OpenProcess() OK")
- End If
- Else
- MessageBox.Show("Already attached! (Please Detach first?)")
- TryAttachToProcess = False
- End If
- End Function
- Public Sub DetachFromProcess()
- If Not (_targetProcessHandle = IntPtr.Zero) Then
- _targetProcess = Nothing
- Try
- CloseHandle(_targetProcessHandle)
- _targetProcessHandle = IntPtr.Zero
- MessageBox.Show("MemReader::Detach() OK")
- Catch ex As Exception
- MessageBox.Show("MemoryManager::DetachFromProcess::CloseHandle error " & Environment.NewLine & ex.Message)
- End Try
- End If
- End Sub
- Public Function ReadInt32(ByVal addr As IntPtr) As Int32
- Dim _dataBytes(3) As Byte
- ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 4, vbNull)
- Return BitConverter.ToInt32(_bytes, 0)
- End Function
- Public Function ReadInt64(ByVal addr As IntPtr) As Int64
- Dim _dataBytes(7) As Byte
- ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 8, vbNull)
- Return BitConverter.ToInt64(_bytes, 0)
- End Function
- Public Function ReadUInt32(ByVal addr As IntPtr) As UInt32
- Dim _dataBytes(3) As Byte
- ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 4, vbNull)
- Return BitConverter.ToUInt32(_bytes, 0)
- End Function
- End Class
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement