Advertisement
Guest User

Mechan

a guest
Jan 4th, 2009
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7.  
  8. namespace gtaivtest {
  9.     class ProcessMemory {
  10.         public IntPtr openedHandle;
  11.         private Process m_process;
  12.         public UInt32 MainModuleAddress {
  13.             get { return (UInt32)m_process.MainModule.BaseAddress.ToInt32(); }
  14.         }
  15.  
  16.         [DllImport("kernel32.dll")]
  17.         static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  18.  
  19.         public bool Open(Process process) {
  20.             m_process = process;
  21.             if(process.HandleCount > 0) {
  22.                 openedHandle = OpenProcess(0x1F0FFF, true, process.Id);
  23.                 return true;
  24.             } else return false;
  25.  
  26.         }
  27.  
  28.         [DllImport("kernel32.dll")]
  29.         static extern Int32 ReadProcessMemory(IntPtr OpenedHandle, IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
  30.  
  31.         private byte[] MemoryRead(IntPtr OpenedHandle, IntPtr BaseAddress, UInt32 Size, ref IntPtr Bytes) {
  32.             byte[] buffer = new byte[Size];
  33.             ReadProcessMemory(OpenedHandle, BaseAddress, buffer, Size, out Bytes);
  34.             return buffer;
  35.         }
  36.  
  37.         public UInt32 ReadU32(UInt32 address) {
  38.             IntPtr bytes = new IntPtr();
  39.             byte[] outp = MemoryRead(openedHandle, (IntPtr)address, sizeof(UInt32), ref bytes);
  40.  
  41.             return BitConverter.ToUInt32(outp, 0);
  42.         }
  43.  
  44.         public float ReadF32(UInt32 address) {
  45.             IntPtr bytes = new IntPtr();
  46.             byte[] outp = MemoryRead(openedHandle, (IntPtr)address, sizeof(float), ref bytes);
  47.  
  48.             return BitConverter.ToSingle(outp, 0);
  49.         }
  50.  
  51.         [DllImport("kernel32.dll")]
  52.         static extern Int32 CloseHandle(IntPtr hObject);
  53.  
  54.         public int Close() {
  55.             int rtn;
  56.             rtn = CloseHandle(openedHandle);
  57.             return rtn;
  58.         }
  59.     }
  60.     class Program {
  61.         //0x9D2D80 : int __cdecl createObject(int modelID, float x, float y, float z, int* handle, int flags)
  62.         delegate int createObject(int modelID, float x, float y, float z, out IntPtr handle, int flags);
  63.  
  64.         static void Main(string[] args) {
  65.  
  66.             Process process = Process.GetProcessesByName("gtaiv")[0];
  67.  
  68.             ProcessMemory memory = new ProcessMemory();
  69.             if(memory.Open(process) == false) {
  70.                 Console.WriteLine("Failed to open GTA4");
  71.                 return;
  72.             }
  73.  
  74.             IntPtr handle = IntPtr.Zero;
  75.             createObject createObj = Marshal.GetDelegateForFunctionPointer((IntPtr)(memory.MainModuleAddress + 0x9D2D80), typeof(createObject)) as createObject;
  76.            
  77.             int retValue = createObj(0x18F25AC7, 120, 830, 15, out handle, 1); //AccessViolationException: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  78.             memory.Close();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement