Code:
using System;
using System.Runtime.InteropServices;
namespace Magic
{
public static class SMemory
{
public const byte ASCII_CHAR_LENGTH = (byte) 1;
public const byte UNICODE_CHAR_LENGTH = (byte) 2;
public const int DEFAULT_MEMORY_SIZE = 4096;
public static int ReadRawMemory(IntPtr hProcess, uint dwAddress, IntPtr lpBuffer, int nSize)
{
int lpBytesRead = 0;
try
{
if (!Imports.ReadProcessMemory(hProcess, dwAddress, lpBuffer, nSize, out lpBytesRead))
throw new Exception("ReadProcessMemory failed");
else
return lpBytesRead;
}
catch
{
return 0;
}
}
public static byte[] ReadBytes(IntPtr hProcess, uint dwAddress, int nSize)
{
IntPtr num = IntPtr.Zero;
byte[] destination;
try
{
num = Marshal.AllocHGlobal(nSize);
int length = SMemory.ReadRawMemory(hProcess, dwAddress, num, nSize);
if (length != nSize)
throw new Exception("ReadProcessMemory error in ReadBytes");
destination = new byte[length];
Marshal.Copy(num, destination, 0, length);
}
catch
{
return (byte[]) null;
}
finally
{
if (num != IntPtr.Zero)
Marshal.FreeHGlobal(num);
}
return destination;
}
public static object ReadObject(IntPtr hProcess, uint dwAddress, Type objType)
{
IntPtr num1 = IntPtr.Zero;
try
{
int num2 = Marshal.SizeOf(objType);
num1 = Marshal.AllocHGlobal(num2);
if (SMemory.ReadRawMemory(hProcess, dwAddress, num1, num2) != num2)
throw new Exception("ReadProcessMemory error in ReadObject.");
else
return Marshal.PtrToStructure(num1, objType);
}
catch
{
return (object) null;
}
finally
{
if (num1 != IntPtr.Zero)
Marshal.FreeHGlobal(num1);
}
}
public static byte ReadByte(IntPtr hProcess, uint dwAddress)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 1);
if (numArray == null)
throw new Exception("ReadByte failed.");
else
return numArray[0];
}
public static sbyte ReadSByte(IntPtr hProcess, uint dwAddress)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 1);
if (numArray == null)
throw new Exception("ReadSByte failed.");
else
return (sbyte) numArray[0];
}
public static ushort ReadUShort(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 2);
if (numArray == null)
throw new Exception("ReadUShort failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToUInt16(numArray, 0);
}
public static ushort ReadUShort(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadUShort(hProcess, dwAddress, false);
}
public static short ReadShort(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 2);
if (numArray == null)
throw new Exception("ReadShort failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToInt16(numArray, 0);
}
public static short ReadShort(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadShort(hProcess, dwAddress, false);
}
public static uint ReadUInt(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 4);
if (numArray == null)
throw new Exception("ReadUInt failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToUInt32(numArray, 0);
}
public static uint ReadUInt(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadUInt(hProcess, dwAddress, false);
}
public static int ReadInt(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 4);
if (numArray == null)
throw new Exception("ReadInt failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToInt32(numArray, 0);
}
public static int ReadInt(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadInt(hProcess, dwAddress, false);
}
public static ulong ReadUInt64(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 8);
if (numArray == null)
throw new Exception("ReadUInt64 failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToUInt64(numArray, 0);
}
public static ulong ReadUInt64(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadUInt64(hProcess, dwAddress, false);
}
public static long ReadInt64(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 8);
if (numArray == null)
throw new Exception("ReadInt64 failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToInt64(numArray, 0);
}
public static long ReadInt64(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadInt64(hProcess, dwAddress, false);
}
public static float ReadFloat(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 4);
if (numArray == null)
throw new Exception("ReadFloat failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToSingle(numArray, 0);
}
public static float ReadFloat(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadFloat(hProcess, dwAddress, false);
}
public static double ReadDouble(IntPtr hProcess, uint dwAddress, bool bReverse)
{
byte[] numArray = SMemory.ReadBytes(hProcess, dwAddress, 8);
if (numArray == null)
throw new Exception("ReadDouble failed.");
if (bReverse)
Array.Reverse((Array) numArray);
return BitConverter.ToDouble(numArray, 0);
}
public static double ReadDouble(IntPtr hProcess, uint dwAddress)
{
return SMemory.ReadDouble(hProcess, dwAddress, false);
}
public static string ReadASCIIString(IntPtr hProcess, uint dwAddress, int nLength)
{
IntPtr num = IntPtr.Zero;
try
{
int nSize = nLength;
num = Marshal.AllocHGlobal(nSize + 1);
Marshal.WriteByte(num, nLength, (byte) 0);
if (SMemory.ReadRawMemory(hProcess, dwAddress, num, nSize) != nSize)
throw new Exception();
else
return Marshal.PtrToStringAnsi(num);
}
catch
{
return string.Empty;
}
finally
{
if (num != IntPtr.Zero)
Marshal.FreeHGlobal(num);
}
}
public static string ReadUnicodeString(IntPtr hProcess, uint dwAddress, int nLength)
{
IntPtr num = IntPtr.Zero;
try
{
int nSize = nLength * 2;
num = Marshal.AllocHGlobal(nSize + 2);
Marshal.WriteInt16(num, nLength * 2, (short) 0);
if (SMemory.ReadRawMemory(hProcess, dwAddress, num, nSize) != nSize)
throw new Exception();
else
return Marshal.PtrToStringUni(num);
}
catch
{
return string.Empty;
}
finally
{
if (num != IntPtr.Zero)
Marshal.FreeHGlobal(num);
}
}
private static int WriteRawMemory(IntPtr hProcess, uint dwAddress, IntPtr lpBuffer, int nSize)
{
IntPtr iBytesWritten = IntPtr.Zero;
if (!Imports.WriteProcessMemory(hProcess, dwAddress, lpBuffer, nSize, out iBytesWritten))
return 0;
else
return (int) iBytesWritten;
}
public static bool WriteBytes(IntPtr hProcess, uint dwAddress, byte[] lpBytes, int nSize)
{
IntPtr num1 = IntPtr.Zero;
try
{
num1 = Marshal.AllocHGlobal(Marshal.SizeOf((object) lpBytes[0]) * nSize);
Marshal.Copy(lpBytes, 0, num1, nSize);
int num2 = SMemory.WriteRawMemory(hProcess, dwAddress, num1, nSize);
if (nSize != num2)
throw new Exception("WriteBytes failed! Number of bytes actually written differed from request.");
}
catch
{
return false;
}
finally
{
if (num1 != IntPtr.Zero)
Marshal.FreeHGlobal(num1);
}
return true;
}
public static bool WriteBytes(IntPtr hProcess, uint dwAddress, byte[] lpBytes)
{
return SMemory.WriteBytes(hProcess, dwAddress, lpBytes, lpBytes.Length);
}
public static bool WriteObject(IntPtr hProcess, uint dwAddress, object objBuffer, Type objType)
{
IntPtr num1 = IntPtr.Zero;
try
{
int num2 = Marshal.SizeOf(objType);
num1 = Marshal.AllocHGlobal(num2);
Marshal.StructureToPtr(objBuffer, num1, false);
int num3 = SMemory.WriteRawMemory(hProcess, dwAddress, num1, num2);
if (num2 != num3)
throw new Exception("WriteObject failed! Number of bytes actually written differed from request.");
}
catch
{
return false;
}
finally
{
if (num1 != IntPtr.Zero)
{
Marshal.DestroyStructure(num1, objType);
Marshal.FreeHGlobal(num1);
}
}
return true;
}
public static bool WriteObject(IntPtr hProcess, uint dwAddress, object objBuffer)
{
return SMemory.WriteObject(hProcess, dwAddress, objBuffer, objBuffer.GetType());
}
public static bool WriteByte(IntPtr hProcess, uint dwAddress, byte Value)
{
byte[] lpBytes = new byte[1]
{
Value
};
return SMemory.WriteBytes(hProcess, dwAddress, lpBytes, 1);
}
public static bool WriteSByte(IntPtr hProcess, uint dwAddress, sbyte Value)
{
byte[] lpBytes = new byte[1]
{
(byte) Value
};
return SMemory.WriteBytes(hProcess, dwAddress, lpBytes, 1);
}
public static bool WriteUShort(IntPtr hProcess, uint dwAddress, ushort Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 2);
}
public static bool WriteShort(IntPtr hProcess, uint dwAddress, short Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 2);
}
public static bool WriteUInt(IntPtr hProcess, uint dwAddress, uint Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 4);
}
public static bool WriteInt(IntPtr hProcess, uint dwAddress, int Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 4);
}
public static bool WriteUInt64(IntPtr hProcess, uint dwAddress, ulong Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 8);
}
public static bool WriteInt64(IntPtr hProcess, uint dwAddress, long Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 8);
}
public static bool WriteFloat(IntPtr hProcess, uint dwAddress, float Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 4);
}
public static bool WriteDouble(IntPtr hProcess, uint dwAddress, double Value)
{
byte[] bytes = BitConverter.GetBytes(Value);
return SMemory.WriteBytes(hProcess, dwAddress, bytes, 8);
}
public static bool WriteASCIIString(IntPtr hProcess, uint dwAddress, string Value)
{
IntPtr num1 = IntPtr.Zero;
try
{
int length = Value.Length;
num1 = Marshal.StringToHGlobalAnsi(Value);
int num2 = SMemory.WriteRawMemory(hProcess, dwAddress, num1, length);
if (length != num2)
throw new Exception();
}
catch
{
return false;
}
finally
{
if (num1 != IntPtr.Zero)
Marshal.FreeHGlobal(num1);
}
return true;
}
public static bool WriteUnicodeString(IntPtr hProcess, uint dwAddress, string Value)
{
IntPtr num1 = IntPtr.Zero;
try
{
int nSize = Value.Length * 2;
num1 = Marshal.StringToHGlobalUni(Value);
int num2 = SMemory.WriteRawMemory(hProcess, dwAddress, num1, nSize);
if (nSize != num2)
throw new Exception();
}
catch
{
return false;
}
finally
{
if (num1 != IntPtr.Zero)
Marshal.FreeHGlobal(num1);
}
return true;
}
public static uint AllocateMemory(IntPtr hProcess, int nSize, uint dwAllocationType, uint dwProtect)
{
return Imports.VirtualAllocEx(hProcess, 0U, nSize, dwAllocationType, dwProtect);
}
public static uint AllocateMemory(IntPtr hProcess, int nSize)
{
return SMemory.AllocateMemory(hProcess, nSize, 4096U, 64U);
}
public static uint AllocateMemory(IntPtr hProcess)
{
return SMemory.AllocateMemory(hProcess, 4096);
}
public static bool FreeMemory(IntPtr hProcess, uint dwAddress, int nSize, uint dwFreeType)
{
if ((int) dwFreeType == 32768)
nSize = 0;
return Imports.VirtualFreeEx(hProcess, dwAddress, nSize, dwFreeType);
}
public static bool FreeMemory(IntPtr hProcess, uint dwAddress)
{
return SMemory.FreeMemory(hProcess, dwAddress, 0, 32768U);
}
}
}