-2
Hello! I am trying to write data in INT type memory, and it is not working, Readprocessmemory worked normally! But the Writeprocessmemory does not.
I don’t know if I’m doing anything wrong more if you can help I appreciate!
Code I have so far.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace CSGO_InfiniteAmmo
{
class Program
{
[DllImport("Kernel32.dll")]
private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("Kernel32.dll")]
private static extern Boolean ReadProcessMemory(IntPtr hProcess, int lpBaseAddress, out int lpBuffer, int nSize, int lpNumberOfBytesRead);
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
int dwSize,
out IntPtr lpNumberOfBytesWritten);
private const int PROCESS_VM_READ = 0x0010;
static private int pId = 0;
static private int lpBaseAddress = 0x0058059C;
static private int getAdressValue = 0;
static IntPtr hProcess = IntPtr.Zero;
static void Main()
{
pId = Process.GetProcessesByName("project")[0].Id;
if (pId == 0)
return;
hProcess = OpenProcess(PROCESS_VM_READ, false, pId);
if (ReadProcessMemory(hProcess, 0x6A4ED8E4, out getAdressValue, 4, 0))
Console.WriteLine("Output: " + getAdressValue.ToString());
Write(hProcess, 0x6A4ED8E4, 25);
}
static bool Write(IntPtr handle, int address, int valor)
{
byte[] dataBuffer = { (byte)5 };
IntPtr bytesWritten = IntPtr.Zero;
WriteProcessMemory(handle, (IntPtr)address, dataBuffer, dataBuffer.Length, out bytesWritten);
Console.WriteLine(bytesWritten);
if (bytesWritten == IntPtr.Zero)
{
Console.WriteLine("Não foi escrito nenhum valor!");
return false;
}
if (bytesWritten.ToInt32() < dataBuffer.Length)
{
Console.WriteLine(" Escrevemos {0} de {1} bytes!", bytesWritten.ToInt32(), dataBuffer.Length.ToString());
return false;
}
return true;
}
}
}
In the line in which it opens the case
hProcess = OpenProcess(PROCESS_VM_READ, false, pId);
you pass to the parameterDWORD dwDesiredAccess
the valuePROCESS_VM_READ
if you consult the documentation will see that the value is needed to read the memory in a process that usesReadProcessMemory()
but notWriteProcessMemory()
. To useWriteProcessMemory()
pass as parameterdwDesiredAccess
the valuePROCESS_ALL_ACCESS
or elsePROCESS_VM_READ | PROCESS_VM_WRITE
– Augusto Vasques