using System.Diagnostics;
using System.Runtime.InteropServices;
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, out uint nNumberOfBytesRead);
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, out uint nNumberOfBytesWritten);
public unsafe static void Main(string[] args) {
IntPtr pAgeAddress = new IntPtr(&myAge), pResultAge;
pResultAge = Marshal.AllocHGlobal(4);
if ( !ReadProcessMemory(Process.GetCurrentProcess().Handle, pAgeAddress, pResultAge, 4, out reads) ) {
Console.WriteLine("메모리 읽기 실패!");
Console.WriteLine("메모리 읽기 성공!");
Console.WriteLine("myAge 변수의 값: {0}", myAge);
Console.WriteLine("pResultAge 변수가 가리키는 주소의 값: {0}", *(int *)pResultAge.ToPointer());
Console.WriteLine("pResultAge 변수가 가리키는 주소의 값: {0}", Marshal.ReadInt32(pResultAge));
Marshal.FreeHGlobal(pResultAge);
IntPtr pValue = new IntPtr(&changedAge);
if ( !WriteProcessMemory(Process.GetCurrentProcess().Handle, pAgeAddress, pValue, 4, out written) ) {
Console.WriteLine("메모리 쓰기 실패!");
Console.WriteLine("메모리 쓰기 성공!");
Console.WriteLine("myAge 변수의 값: {0}", myAge);
Console.WriteLine("pValue 변수가 가리키는 주소의 값: {0}", *(int *)pValue.ToPointer());
Console.WriteLine("pValue 변수가 가리키는 주소의 값: {0}", Marshal.ReadInt32(pValue));