Advertisement
Guest User

Calculator Reader

a guest
Aug 3rd, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.38 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.  
  5.     End Sub
  6.  
  7.     Private _memManager As New MemoryManager 'New word is important! -I'm not explaining this.
  8.  
  9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.         If _memManager.TryAttachToProcess("Calculator") Then
  11.             Dim _myScore As Int32 = _memManager.ReadInt32(&HAC4E4C)
  12.             ' Done using MemoryManager for reading. Close it.
  13.             _memManager.DetachFromProcess()
  14.             MessageBox.Show("Your current 3D Pinball score is: " & _myScore.ToString())
  15.         Else
  16.             MessageBox.Show("MemoryManager unable to attach to 3D Pinball for ram reading")
  17.         End If
  18.     End Sub
  19.  
  20.     Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAcess As UInt32, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Int32) As IntPtr
  21.     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
  22.     Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
  23.  
  24.     Public Class MemoryManager
  25.         Private _targetProcess As Process = Nothing 'to keep track of it. not used yet.
  26.         Private _targetProcessHandle As IntPtr = IntPtr.Zero 'Used for ReadProcessMemory
  27.         Private PROCESS_ALL_ACCESS As UInt32 = &H1F0FFF
  28.         Private PROCESS_VM_READ As UInt32 = &H10
  29.         Public Function TryAttachToProcess(ByVal windowCaption As String) As Boolean
  30.             Dim _allProcesses() As Process = Process.GetProcesses
  31.             For Each pp As Process In _allProcesses
  32.                 If pp.MainWindowTitle.ToLower.Contains(windowCaption.ToLower) Then
  33.                     'found it! proceed.
  34.                     Return TryAttachToProcess(pp)
  35.                 End If
  36.             Next
  37.             MessageBox.Show("Unable to find process '" & windowCaption & ".' Is running?")
  38.             Return False
  39.         End Function
  40.  
  41.         Public Function TryAttachToProcess(ByVal proc As Process) As Boolean
  42.             If _targetProcessHandle = IntPtr.Zero Then 'not already attached
  43.                 _targetProcess = proc
  44.                 _targetProcessHandle = OpenProcess(ReadProcessMemoryRights.PROCESS_ALL_ACCESS, False, _targetProcess.Id)
  45.                 If _targetProcessHandle = 0 Then
  46.                     TryAttachToProcess = False
  47.                     MessageBox.Show("OpenProcess() FAIL! Are you Administrator??")
  48.                 Else
  49.                     'if we get here, all connected and ready to use ReadProcessMemory()
  50.                     TryAttachToProcess = True
  51.                     MessageBox.Show("OpenProcess() OK")
  52.                 End If
  53.             Else
  54.                 MessageBox.Show("Already attached! (Please Detach first?)")
  55.                 TryAttachToProcess = False
  56.             End If
  57.         End Function
  58.  
  59.         Public Sub DetachFromProcess()
  60.             If Not (_targetProcessHandle = IntPtr.Zero) Then
  61.                 _targetProcess = Nothing
  62.                 Try
  63.                     CloseHandle(_targetProcessHandle)
  64.                     _targetProcessHandle = IntPtr.Zero
  65.                     MessageBox.Show("MemReader::Detach() OK")
  66.                 Catch ex As Exception
  67.                     MessageBox.Show("MemoryManager::DetachFromProcess::CloseHandle error " & Environment.NewLine & ex.Message)
  68.                 End Try
  69.             End If
  70.         End Sub
  71.  
  72.  
  73.         Public Function ReadInt32(ByVal addr As IntPtr) As Int32
  74.             Dim _dataBytes(3) As Byte
  75.             ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 4, vbNull)
  76.             Return BitConverter.ToInt32(_bytes, 0)
  77.         End Function
  78.  
  79.         Public Function ReadInt64(ByVal addr As IntPtr) As Int64
  80.             Dim _dataBytes(7) As Byte
  81.             ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 8, vbNull)
  82.             Return BitConverter.ToInt64(_bytes, 0)
  83.         End Function
  84.  
  85.         Public Function ReadUInt32(ByVal addr As IntPtr) As UInt32
  86.             Dim _dataBytes(3) As Byte
  87.             ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 4, vbNull)
  88.             Return BitConverter.ToUInt32(_bytes, 0)
  89.         End Function
  90.     End Class
  91.  
  92. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement