Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System****;
using System.Threading;
namespace System.MemoryAccessing
{
/// <summary>
/// Class to read & write memory to/from addresses
/// </summary>
public class MemoryAccessor
{
#region Imports
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
[DllImport("kernel32.dll")]
private static extern Int32 CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
private static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
private static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
#endregion
#region Fields
private Process _tProcess;
private IntPtr _tProcessHandle;
private ProcessAccessTypes _tFlags;
private Dictionary<IntPtr, Thread> _frozenAddresses;
#region Public
/// <summary>
/// Gets the target process object
/// </summary>
public Process TargetProcess
{
get { return this._tProcess; }
}
/// <summary>
/// Gets the target process handle pointer
/// </summary>
public IntPtr TargetProcessHandle
{
get { return this._tProcessHandle; }
}
/// <summary>
/// Gets the target flags (access types)
/// </summary>
public ProcessAccessTypes TargetFlags
{
get { return this._tFlags; }
}
#endregion
#endregion
#region Constructor & Destructor
public MemoryAccessor(Process tProcess, ProcessAccessTypes tFlags = (ProcessAccessTypes.VM_READ | ProcessAccessTypes.VM_WRITE | ProcessAccessTypes.VM_OPERATION))
{
this._tProcess = tProcess;
this._tFlags = tFlags;
this._tProcessHandle = OpenProcess((uint)this._tFlags, 1, (uint)this._tProces*****);
this._frozenAddresses = new Dictionary<IntPtr, Thread>();
}
~MemoryAccessor()
{
try
{
try
{
int error = CloseHandle(this._tProcessHandle);
if (error == 0)
throw new Exception("Failed to close handle!");
}
catch
{
}
foreach (KeyValuePair<IntPtr, Thread> kvp in this._frozenAddresses)
kvp.Value.Abort();
}
catch
{ }
}
#endregion
#region Methods
private void FreezeThreadMethod(int mAddress, uint size)
{
byte[] data = this.ReadMemory(mAddress, size);
while (true)
{
this.WriteMemory(mAddress, data);
Thread.Sleep(100);
}
}
#region Public
public IntPtr GetModuleBaseByName(string mName)
{
ProcessModuleCollection pmc = this._tProcess.Modules;
foreach (ProcessModule pm in pmc)
if (pm.ModuleName == mName) return pm.BaseAddress;
return IntPtr.Zero;
}
public byte[] ReadMemory(int mAddress, uint size = 5)
{
byte[] buffer = new byte[size];
IntPtr tempPtr;
ReadProcessMemory(this._tProcessHandle, (IntPtr)mAddress, buffer, (uint)buffer.Length, out tempPtr);
return buffer;
}
public string ReadString(int mAddress, uint lenght = 5)
{
return Encoding.ASCII.GetString(this.ReadMemory(mAddress, lenght));
}
public int ReadInteger(int mAddress, uint size = 5)
{
return BitConverter.ToInt32(this.ReadMemory(mAddress, size), 0);
}
public float ReadFloat(int mAddress, uint size = 5)
{
return (float)BitConverter.ToDouble(this.ReadMemory(mAddress, size), 0);
}
public char ReadCharacter(int mAddress, uint size = 5)
{
return BitConverter.ToChar(this.ReadMemory(mAddress, size), 0);
}
public double ReadDouble(int mAddress, uint size = 5)
{
return BitConverter.ToDouble(this.ReadMemory(mAddress, size), 0);
}
public void WriteMemory(int mAddress, byte[] bytes)
{
IntPtr tempPtr;
WriteProcessMemory(this._tProcessHandle, (IntPtr)mAddress, bytes, (uint)bytes.Length, out tempPtr);
}
public void WriteString(int mAddress, string str)
{
WriteMemory(mAddress, Encoding.ASCII.GetBytes(str));
}
public void WriteInteger(int mAddress, int value)
{
WriteMemory(mAddress, BitConverter.GetBytes(value));
}
public void WriteFloat(int mAddress, float value)
{
this.WriteMemory(mAddress, BitConverter.GetBytes(value));
}
public void WriteCharacter(int mAddress, char ch)
{
this.WriteMemory(mAddress, Encoding.ASCII.GetBytes(new char[]{ ch }));
}
public void WriteDouble(int mAddress, double value)
{
this.WriteMemory(mAddress, BitConverter.GetBytes(value));
}
public void FreezeMemory(int mAddress, uint size = 5)
{
ThreadStart ts = delegate { this.FreezeThreadMethod(mAddress, size); };
this._frozenAddresses.Add((IntPtr)mAddress, new Thread(ts));
this._frozenAddresses[(IntPtr)mAddress].Start();
}
public void UnfreezeMemory(int mAddress)
{
try { this._frozenAddresses[(IntPtr)mAddress].Abort(); } catch { }
this._frozenAddresses.Remove((IntPtr)mAddress);
}
public void Close()
{
int error = CloseHandle(this._tProcessHandle);
if (error == 0)
throw new Exception("Failed to close handle!");
foreach (KeyValuePair<IntPtr, Thread> kvp in this._frozenAddresses)
kvp.Value.Abort();
}
#region Static
public static Process GetProcessByName(string pName)
{
Process[] procs = Process.GetProcessesByName(pName);
if (procs.Length != 0)
{
if (procs.Length > 1) throw new Exception("Multiple processes detected. Sorry, but it cannot be handled more than one process per session..");
return procs[0];
}
else
return null;
}
public static Process[] GetProcesses()
{
return Process.GetProcesses();
}
#endregion
#endregion
#endregion
}
/// <summary>
/// Enumeration of flags to handle accessed processes
/// </summary>
[Flags]
public enum ProcessAccessTypes
{
TERMINATE = (0x0001),
CREATE_THREAD = (0x0002),
SET_SESSIONID = (0x0004),
VM_OPERATION = (0x0008),
VM_READ = (0x0010),
VM_WRITE = (0x0020),
DUP_HANDLE = (0x0040),
CREATE_PROCESS = (0x0080),
SET_QUOTA = (0x0100),
SET_INFORMATION = (0x0200),
QUERY_INFORMATION = (0x0400)
}
}
Enjoy,.. and thank, if it helped..