public bool WriteProcessMemory(UIntPtr address, byte[] buffer, bool absoluteAddressing = false) { int numOfBytes = 0; if (!absoluteAddressing) { address = new UIntPtr(address.ToUInt32() & ~0x80000000U); } IntPtr processAddress = absoluteAddressing ? new IntPtr((long)address.ToUInt64()) : new IntPtr((long)ConvertAddressEndianess(new UIntPtr(address.ToUInt32() + (ulong)ProcessMemoryOffset.ToInt64()), buffer.Length)); // Safety bounds check if (processAddress.ToInt64() < ProcessMemoryOffset.ToInt64()) { return(false); } if (processAddress.ToInt64() + buffer.Length >= ProcessMemoryOffset.ToInt64() + Config.RamSize) { return(false); } return(Kernal32NativeMethods.ProcessWriteMemory(_processHandle, processAddress, buffer, (IntPtr)buffer.Length, ref numOfBytes)); }
public byte[] ReadRamLittleEndian(UIntPtr address, int length, bool absoluteAddress = false) { byte[] readBytes = new byte[length]; uint localAddress; if (absoluteAddress) { localAddress = (uint)(address.ToUInt64() - (ulong)ProcessMemoryOffset.ToInt64()); } else { localAddress = ConvertAddressEndianess(address.ToUInt32() & ~0x80000000, length); } if (localAddress + length > _ram.Length) { return(new byte[length]); } Buffer.BlockCopy(_ram, (int)localAddress, readBytes, 0, length); return(readBytes); }
public bool WriteRam(byte[] buffer, uint address, int bufferStart = 0, int?length = null, bool safeWrite = true) { address &= ~0x80000000U; if (length == null) { length = buffer.Length - bufferStart; } if (CheckReadonlyOff()) { return(false); } bool success = true; // Attempt to pause the game before writing bool preSuspended = IsSuspended; if (safeWrite) { Suspend(); } // Take care of first alignment int bufPos = bufferStart; uint alignment = _fixAddress[address & 0x03] + 1U; if (alignment < 4) { byte[] writeBytes = new byte[Math.Min(alignment, length.Value)]; Array.Copy(buffer, bufPos, writeBytes, 0, writeBytes.Length); success &= WriteProcessMemory(new UIntPtr(address), writeBytes.Reverse().ToArray()); length -= writeBytes.Length; bufPos += writeBytes.Length; address += alignment; } // Take care of middle if (length >= 4) { byte[] writeBytes = new byte[length.Value & ~0x03]; for (int i = 0; i < writeBytes.Length; bufPos += 4, i += 4) { writeBytes[i] = buffer[bufPos + 3]; writeBytes[i + 1] = buffer[bufPos + 2]; writeBytes[i + 2] = buffer[bufPos + 1]; writeBytes[i + 3] = buffer[bufPos]; } success &= WriteProcessMemory(new UIntPtr(address + (ulong)ProcessMemoryOffset.ToInt64()), writeBytes, true); address += (uint)writeBytes.Length; length -= writeBytes.Length; } // Take care of last if (length > 0) { byte[] writeBytes = new byte[length.Value]; Array.Copy(buffer, bufPos, writeBytes, 0, writeBytes.Length); success &= WriteProcessMemory(new UIntPtr(address), writeBytes.Reverse().ToArray()); } // Resume stream if (safeWrite && !preSuspended) { Resume(); } return(success); }
public bool ReadProcessMemory(int address, byte[] buffer, bool absoluteAddressing = false) { if (_process == null) { return(false); } int numOfBytes = 0; return(Kernal32NativeMethods.ProcessReadMemory(_processHandle, absoluteAddressing ? new IntPtr(address) : new IntPtr(address + ProcessMemoryOffset.ToInt64()), buffer, (IntPtr)buffer.Length, ref numOfBytes)); }
public bool WriteProcessMemory(UIntPtr address, byte[] buffer, bool absoluteAddressing = false) { int numOfBytes = 0; if (!absoluteAddressing) { address = new UIntPtr(address.ToUInt32() & ~0x80000000U); } return(Kernal32NativeMethods.ProcessWriteMemory(_processHandle, absoluteAddressing ? new IntPtr((long)address.ToUInt64()) : new IntPtr((long)ConvertAddressEndianess(new UIntPtr(address.ToUInt32() + (ulong)ProcessMemoryOffset.ToInt64()), buffer.Length)), buffer, (IntPtr)buffer.Length, ref numOfBytes)); }