2

How do I use WriteProcessMemory to change ammo of my current ammo address? I know how to read it I just don't know how to write it. Is SetAmmo . I thought i'm doing everything wright?

Code:

#include <iostream>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>
using namespace std;

 DWORD pid;
 DWORD Ammo = 0x07823C5EC;
 int MyAmmo;
 int SetAmmo = 1;
int main(){

    HWND hwnd = FindWindowA(0, ("Garry's Mod"));

    GetWindowThreadProcessId(hwnd, &pid);
    HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pid);

    ReadProcessMemory(pHandle, (LPVOID)Ammo, &MyAmmo, sizeof(MyAmmo), 0);
    cout<<"Current Ammo = "<<MyAmmo<<endl;

    WriteProcessMemory(pHandle, (LPVOID)Ammo, &SetAmmo, sizeof(SetAmmo), 0);
    system("Pause");
    return 0;
}  
JJJ
  • 32,902
  • 20
  • 89
  • 102
Zack Oliver
  • 107
  • 6

1 Answers1

0

you open the process with read access:

HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pid);

try:

HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
mattideluxe
  • 468
  • 2
  • 12
  • Don't use `PROCESS_ALL_ACCESS`. Only request the minimum rights you actually need. To use `ReadProcessMemory`, all you need is the `PROCESS_VM_READ` right. To use `WriteProcessMemory`, all you need is `PROCESS_VM_WRITE` and `PROCESS_VM_OPERATION` rights. This is spelled out in the documentation. So use this instead: `HANDLE pHandle = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, pid);` – Remy Lebeau Jun 08 '17 at 15:07