Da ich für ein Projekt, an welchem ich atm arbeite meine Memory Class neugeschrieben habe, und diese auch schon im Premium Längsten veröffentlicht habe, dachte ich mir, ich kann sie geradesogut auch direkt öffentlich stellen.
Die Kommentare sind zu ignorieren, die habe ich nur geschrieben, als mir langweilig war, und ich mich nicht aufs coden konzentrieren konnte, weil meine Freundin mich zugelabert hat
Code dürfte weitestgehend selbsterklärend sein, falls trotzdem noch Fragen da sind, stehe ich zur Verfügung
Code:
using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class ShanksMemory
{
public ShanksMemory()
{
}
//API Imports, don't mind those
[DllImport("kernel32.dll")]
private static extern Int32 ReadProcessMemoryByte(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
IntPtr pHandel;
#region Basic Stuff
/* Opens ProcessHandle
* Remember to use this function before trying to write or read Memory
*/
public bool Process_Handle(string ProcessName)
{
try
{
Process[] ProcList = Process.GetProcessesByName(ProcessName);
if (ProcList.Length == 0)
return false;
else
{
pHandel = ProcList[0].Handle;
return true;
}
}
catch (Exception ex)
{ Console.Beep(); Console.WriteLine("Process_Handle - " + ex.Message); return false; }
}
//Just to make things a little easier
private byte[] Read(int Address, int Length)
{
byte[] Buffer = new byte[Length];
IntPtr Zero = IntPtr.Zero;
ReadProcessMemory(pHandel, (IntPtr)Address, Buffer, (UInt32)Buffer.Length, out Zero);
return Buffer;
}
//Same as above
private void Write(int Address, int Value)
{
byte[] Buffer = BitConverter.GetBytes(Value);
IntPtr Zero = IntPtr.Zero;
WriteProcessMemory(pHandel, (IntPtr)Address, Buffer, (UInt32)Buffer.Length, out Zero);
}
#endregion
#region Write Functions (Integer, String, Bytes)
// Writes Integers
public void WriteInteger(int Address, int Value)
{
Write(Address, Value);
}
//Writes String (Length is not necessary)
public void WriteString(int Address, string Text)
{
byte[] Buffer = new ASCIIEncoding().GetBytes(Text);
IntPtr Zero = IntPtr.Zero;
WriteProcessMemory(pHandel, (IntPtr)Address, Buffer, (UInt32)Buffer.Length, out Zero);
}
//Writes Bytes (Probably don't need that though)
public void WriteBytes(int Address, byte[] Bytes)
{
IntPtr Zero = IntPtr.Zero;
WriteProcessMemory(pHandel, (IntPtr)Address, Bytes, (uint)Bytes.Length, out Zero);
}
//Writes a NOP on selected Adress
public void WriteNOP(int Address)
{
byte[] Buffer = new byte[] { 0x90, 0x90, 0x90, 0x90, 0x90 };
IntPtr Zero = IntPtr.Zero;
WriteProcessMemory(pHandel, (IntPtr)Address, Buffer, (UInt32)Buffer.Length, out Zero);
}
#endregion
#region Read Functions (Integer & String)
//Reads Integer
public int ReadInteger(int Address, int Length = 4)
{
return BitConverter.ToInt32(Read(Address, Length), 0);
}
//Reads String
public string ReadString(int Address, int Length = 4)
{
return new ASCIIEncoding().GetString(Read(Address, Length));
}
//Reads Bytes
public byte[] ReadBytes(int Address, int Length)
{
return Read(Address, Length);
}
//Mostly for Patternscan
private byte ReadByte(int Address, int Length)
{
byte Buffer = 1;
IntPtr Zero = IntPtr.Zero;
ReadProcessMemoryByte(pHandel, (IntPtr)Address, Buffer, (UInt32)Buffer, out Zero);
return Buffer;
}
// WIP vvvvvvvvvvvvvvv WIP //
/*
* Description: Loops through Module Adresses until it finds the respective Adress matching the signature
*
* Author: Kevin 'Rorc'
* Implemented: 16.3.2014
*
*/
public IntPtr ReadPattern(String GameName,String ModuleName,Byte[] Signature)
{
Int32 BaseAdress = 0, EndAdress = 0;
foreach (ProcessModule PM in Process.GetProcessesByName(GameName)[0].Modules)
{
if (ModuleName ==PM.ModuleName)
{
BaseAdress = (Int32)PM.BaseAddress;
EndAdress = BaseAdress + PM.ModuleMemorySize;
}
}
Int32 curAddr = BaseAdress;
do
{
for (int i= 0; i <= Signature.Length; i--)
{
if (ReadByte(curAddr + i,1) == (Byte)Signature[i])
{
if (Signature.Length -1 == 1)
{
return (IntPtr)curAddr;
}
}
curAddr += 1;
}
} while (curAddr < EndAdress);
return (IntPtr)0;
}
#endregion
}






