public bool WriteBytes(IntPtr address, byte[] buffer, Classes.MemoryProtection overrideProtectionType = Classes.MemoryProtection.DoNothing) { if (address == IntPtr.Zero || buffer == null || buffer.Length < 1) { return(false); } if (_mainReference.ProcessHandle == IntPtr.Zero) { throw new Exception("Read/Write Handle cannot be Zero"); } if (_mainReference == null) { throw new Exception("Reference to Main Class cannot be null"); } if (!_mainReference.IsValid) { throw new Exception("Reference to Main Class reported an Invalid State"); } Classes.MemoryProtection oldProtect = Classes.MemoryProtection.Invalid; if (overrideProtectionType != Classes.MemoryProtection.DoNothing && overrideProtectionType != Classes.MemoryProtection.Invalid) { #if x86 Win32.PInvoke.VirtualProtectEx(_mainReference.ProcessHandle, address, new IntPtr(buffer.Length), overrideProtectionType, out oldProtect); #else bool success = Win32.PInvoke.VirtualProtectEx(_mainReference.ProcessHandle, address, new IntPtr(buffer.LongLength), overrideProtectionType, out oldProtect); #endif } #if x86 Win32.PInvoke.WriteProcessMemory(_mainReference.ProcessHandle, address, buffer, buffer.Length, out IntPtr numBytesWritten); #else Win32.PInvoke.WriteProcessMemory(_mainReference.ProcessHandle, address, buffer, buffer.LongLength, out IntPtr numBytesWritten); #endif if (oldProtect != Classes.MemoryProtection.Invalid) { Win32.PInvoke.VirtualProtectEx(_mainReference.ProcessHandle, address, new IntPtr(buffer.LongLength), oldProtect, out oldProtect); } #if x86 return(numBytesWritten.ToInt32() == buffer.Length); #else return(numBytesWritten.ToInt64() == buffer.LongLength); #endif }
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, Classes.MemoryProtection flNewProtect, out Classes.MemoryProtection lpflOldProtect);
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, ulong dwSize, Classes.AllocationType flAllocationType, Classes.MemoryProtection flProtect);
public Task <byte[]> AsyncReadBytes(IntPtr address, IntPtr size, Classes.MemoryProtection overrideProtectionType = Classes.MemoryProtection.DoNothing) { return(Task.Run(() => ReadBytes(address, size, overrideProtectionType))); }
public Task <bool> AsyncWriteBytes(IntPtr address, byte[] buffer, Classes.MemoryProtection overrideProtectionType = Classes.MemoryProtection.DoNothing) { return(Task.Run(() => WriteBytes(address, buffer, overrideProtectionType))); }