Hey, hier mal meine simple xMemory Klasse für Einsteiger was Memory Editing angeht.
Have fun =)
Lg
Code:
/*
* xMemory Class
* by Shawak
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
class xMemory
{
// Global Variable
public static IntPtr ProcessHandle = IntPtr.Zero;
// Native Functions
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(int DesiredAccess, bool InheritHandle, int ProcessID);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr Handle);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadProcessMemory(IntPtr ProcessHandle, IntPtr Address, ref byte[] Buffer, int Size, ref int NumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr ProcessHandle, IntPtr Address, byte[] Buffer, int Size, ref int NumberOfBytesWritten);
public static uint DELETE = 0x00010000;
public static uint READ_CONTROL = 0x00020000;
public static uint WRITE_DAC = 0x00040000;
public static uint WRITE_OWNER = 0x00080000;
public static uint SYNCHRONIZE = 0x00100000;
public static uint END = 0xFFF; //if you have Windows XP or Windows Server 2003 you must change this to 0xFFFF
public static uint PROCESS_ALL_ACCESS = (DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | END);
// API-Implentation
static internal bool OpenProcess(int PID)
{
xMemory.ProcessHandle = xMemory.OpenProcess((int)PROCESS_ALL_ACCESS, false, PID); // 0x1f0fff
return (xMemory.ProcessHandle == IntPtr.Zero ? false : true);
}
static internal bool CloseHandle()
{
if (xMemory.ProcessHandle == IntPtr.Zero)
return false;
return xMemory.CloseHandle(xMemory.ProcessHandle);
}
// Declarations
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemoryByte(IntPtr Handle, int Address, ref byte Value, int Size = 1);
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemoryShort(IntPtr Handle, int Address, ref short Value, int Size = 2);
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemoryInteger(IntPtr Handle, int Address, ref int Value, int Size = 4);
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemoryLong(IntPtr Handle, int Address, ref long Value, int Size = 8);
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemorySingle(IntPtr Handle, int Address, ref float Value, int Size = 4);
[DllImport("KERNEL32", EntryPoint = "ReadProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool ReadMemoryDouble(IntPtr Handle, int Address, ref double Value, int Size = 8);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemoryByte(IntPtr Handle, int Address, ref byte Value, int Size = 1);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemoryShort(IntPtr Handle, int Address, ref short Value, int Size = 2);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemoryInteger(IntPtr Handle, int Address, ref int Value, int Size = 4);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemoryLong(IntPtr Handle, int Address, ref long Value, int Size = 8);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemorySingle(IntPtr Handle, int Address, ref float Value, int Size = 4);
[DllImport("KERNEL32", EntryPoint = "WriteProcessMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool WriteMemoryDouble(IntPtr Handle, int Address, ref double Value, int Size = 8);
// Read Functions
public static byte ReadByte(int Address, int Size = 1)
{
byte value = 0;
ReadMemoryByte(ProcessHandle, Address, ref value, Size);
return value;
}
public static short ReadShort(int Address, int Size = 2)
{
short value = 0;
ReadMemoryShort(ProcessHandle, Address, ref value, Size);
return value;
}
public static int ReadInteger(int Address, int Size = 4)
{
int value = 0;
ReadMemoryInteger(ProcessHandle, Address, ref value, Size);
return value;
}
public static long ReadLong(int Address, int Size = 8)
{
long value = 0;
ReadMemoryLong(ProcessHandle, Address, ref value, Size);
return value;
}
public static float ReadSingle(int Address, int Size = 8)
{
float value = 0;
ReadMemorySingle(ProcessHandle, Address, ref value, Size);
return value;
}
public static double ReadDouble(int Address, int Size = 8)
{
double value = 0;
ReadMemoryDouble(ProcessHandle, Address, ref value, Size);
return value;
}
public static string ReadString(int Address)
{
byte[] b = new byte[] { 0 };
string str = "";
bool ret = false;
do
{
ret = ReadMemoryByte(ProcessHandle, Address, ref b[0]);
Address += 0x1;
if (ret == true)
if (b[0] != 0)
str += System.Text.Encoding.UTF8.GetString(b);
} while (b[0] != 0);
return str;
}
// Write Functions
public static bool WriteByte(int Address, byte Value, int Size = 1)
{
return WriteMemoryByte(ProcessHandle, Address, ref Value, Size);
}
public static bool WriteShort(int Address, short Value, int Size = 2)
{
return WriteMemoryShort(ProcessHandle, Address, ref Value, Size);
}
public static bool WriteInteger(int Address, int Value, int Size = 4)
{
return WriteMemoryInteger(ProcessHandle, Address, ref Value, Size);
}
public static bool WriteLong(int Address, long Value, int Size = 8)
{
return WriteMemoryLong(ProcessHandle, Address, ref Value, Size);
}
public static bool WriteSingle(int Address, float Value, int Size = 8)
{
return WriteMemorySingle(ProcessHandle, Address, ref Value, Size);
}
public static bool WriteDouble(int Address, double Value, int Size = 8)
{
return WriteMemoryDouble(ProcessHandle, Address, ref Value, Size);
}
// Protection
[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, uint flNewProtect, ref uint lpflOldProtect);
public object GetBytesLength(string str)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] bytes = enc.GetBytes(str);
return bytes.Length;
}
public static uint RemoveProtect(int Address, int BytesLength)
{
uint oldProtection = 0;
VirtualProtectEx(ProcessHandle, new IntPtr(Address), new IntPtr(BytesLength), 0x40, ref oldProtection);
return oldProtection;
}
public static bool AddProtect(int Address, int BytesLength, uint oldProtection)
{
VirtualProtectEx(ProcessHandle, new IntPtr(Address), new IntPtr(BytesLength), oldProtection, ref oldProtection);
return true;
}
}
Lg