Solved ReadProcessMemory Help for C++ Noob

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat
Status
Not open for further replies.

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
Hello Guys,
I recently got into coding and are still very new to everything.
Last weekend I decided to give ReadProcessMemory a go but ran in some problems.
The goal is to read the current Ammo in the Clip for the Assault Rifle in AssaultCube.
The CMD just prints a 0....:FailFish:
I hope anyone got some ideas :D

[HR][/HR]
C++:
#include <iostream>
#include <Windows.h>
using namespace std;

HWND Fenster;
DWORD ProcessID;
HANDLE hProcess;
const DWORD BaseAdress { 0x50F4F4 };
const DWORD AmmoClipOffset{ 0x150 };
DWORD StaticPlayer;
DWORD AmmoinClipAdress;
int AmmoinClip{ 0 };


int main() {
	    Fenster = FindWindowA(NULL, "AssaultCube"); //Suchen vom Spiel fenster... Ist wie ein Int z.B.
    if (Fenster == NULL){ //Überprüfen ob Spiel Fenster gefunden wurde
		cerr << "Error... Kein Fenster gefunden!" << endl;
		system("Pause");
		exit(-1);
	}

        ProcessID = GetWindowThreadProcessId(Fenster, &ProcessID); // Prozess Id vom definierten Fenster finden und als Hex abspeichern (DWord)
	if (ProcessID == NULL) {
		cerr << "Error....Keine ProcessId" << endl;
		system("Pause");
		exit(-1);
	}

        hProcess = OpenProcess(PROCESS_VM_READ, false, ProcessID); //Den nun definierten Process öffnen um schreiben/ lesen zu können

        ReadProcessMemory(hProcess,(PBYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
	ReadProcessMemory(hProcess,(PBYTE*)(StaticPlayer + AmmoClipOffset),&AmmoinClipAdress, sizeof(DWORD), 0);
	ReadProcessMemory(hProcess, (PBYTE*)AmmoinClipAdress, &AmmoinClip, sizeof(int), 0);
	CloseHandle(hProcess);
	cout << AmmoinClip << endl;
	return 0;
}
 
Last edited:

Teuvin

now I am become Death
Dank Tier VIP
Trump Tier Donator
Dec 8, 2016
403
10,388
65
I mean you are not even adding the .exe addy
C++:
ReadProcessMemory(hProcess,(PBYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
should be

C++:
ReadProcessMemory(hProcess,(PBYTE*)(exeaddy+BaseAdress), &StaticPlayer, sizeof(DWORD), 0);
Get the module base address
 
Last edited:

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,746
40,528
316
The problem seems to be the process id...or better getting the right process id...sadly my c++ isn't good enough yet to create a snapshot :(
I doubt that that is the problem since the checks you've added for that case seem to be fine.
In the following snippet I've added debug checks to the OpenProcess and RPM calls:
C++:
hProcess = OpenProcess(PROCESS_VM_READ, false, ProcessID);
if(!hProcess)
{
     DWORD Err = GetLastError();
     std::cout << "OpenProcess failed: " << std::hex << Err << std::endl;  
}

BOOL Ret = ReadProcessMemory(hProcess,(BYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM1 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess,(BYTE*)(StaticPlayer + AmmoClipOffset),&AmmoinClipAdress, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM2 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess, (BYTE*)AmmoinClipAdress, &AmmoinClip, sizeof(int), 0); 
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM3 failed: " << std::hex << Err << std::endl;  
}
 

Roman_Ablo

Banned
Feb 27, 2017
355
2,402
1
You are doing the whole RPM thing wrong. Why would you cast the address into BYTE**? cast it into a void. And if you initialize vars in C++14 style just use C++14 casting too: reinterpret_cast<void*>

Also I think that the whole sizeof(DWORD) etc. thinghy would work (didnt try), but sizeof(your_variable) is much safer.

Also, use nullptr
 

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,746
40,528
316
It doesn't matter whether you cast it into a BYTE** or BYTE* aslong as you don't do pointer arithmetic with it. SizeOf will work just fine aswell.
Check the return values of the ReadProcessMemory calls. There's most likely something wrong with the pointer path.
 

Traxin

Escobar Tier VIP
Dank Tier Donator
Aug 3, 2015
1,041
25,378
154
I mean you are not even adding the .exe addy
C++:
ReadProcessMemory(hProcess,(PBYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
should be

C++:
ReadProcessMemory(hProcess,(PBYTE*)(exeaddy+BaseAdress), &StaticPlayer, sizeof(DWORD), 0);
Get the module base address
This is assault cube bruh, ain't no aslr in this bitch :p
0x50f4f4 is the absolute address of the player pointer and it doesn't change, added the base address of the module would actually throw it off.
You'd have to do exeBase + 0x10f4f4 if you're going to do it like that.

I'm with Broihon on this one, make sure those RPMs are returning what is expected at each call.
 

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
This is assault cube bruh, ain't no aslr in this bitch :p
0x50f4f4 is the absolute address of the player pointer and it doesn't change, added the base address of the module would actually throw it off.
You'd have to do exeBase + 0x10f4f4 if you're going to do it like that.

I'm with Broihon on this one, make sure those RPMs are returning what is expected at each call.
The problem seems to be the process id...or better getting the right process id...sadly my c++ isn't good enough yet to create a snapshot :(

One more question to the exebase.... The base is like 0x0040000 or so. Can i just do 0x00400000 + 0x10f4f4 for example?
 

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
I doubt that that is the problem since the checks you've added for that case seem to be fine.
In the following snippet I've added debug checks to the OpenProcess and RPM calls:
C++:
hProcess = OpenProcess(PROCESS_VM_READ, false, ProcessID);
if(!hProcess)
{
     DWORD Err = GetLastError();
     std::cout << "OpenProcess failed: " << std::hex << Err << std::endl;  
}

BOOL Ret = ReadProcessMemory(hProcess,(BYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM1 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess,(BYTE*)(StaticPlayer + AmmoClipOffset),&AmmoinClipAdress, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM2 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess, (BYTE*)AmmoinClipAdress, &AmmoinClip, sizeof(int), 0); 
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM3 failed: " << std::hex << Err << std::endl;  
}
Thanks for this....you were right its failing :O

OpenProcess failed: 57
RPM1 failed: 6
RPM2 failed: 6
RPM3 failed: 6
0
 

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
I doubt that that is the problem since the checks you've added for that case seem to be fine.
In the following snippet I've added debug checks to the OpenProcess and RPM calls:
C++:
hProcess = OpenProcess(PROCESS_VM_READ, false, ProcessID);
if(!hProcess)
{
     DWORD Err = GetLastError();
     std::cout << "OpenProcess failed: " << std::hex << Err << std::endl;  
}

BOOL Ret = ReadProcessMemory(hProcess,(BYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM1 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess,(BYTE*)(StaticPlayer + AmmoClipOffset),&AmmoinClipAdress, sizeof(DWORD), 0);
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM2 failed: " << std::hex << Err << std::endl;  
}
ReadProcessMemory(hProcess, (BYTE*)AmmoinClipAdress, &AmmoinClip, sizeof(int), 0); 
if(!Ret)
{
     DWORD Err = GetLastError();
     std::cout << "RPM3 failed: " << std::hex << Err << std::endl;  
}
ERROR_INVALID_HANDLE
6 (0x6)
The handle is invalid.

ERROR_INVALID_PARAMETER
87 (0x57)
The parameter is incorrect.
No clue why thought
 

Onsed1970

Newbie
Aug 10, 2015
19
433
1
ProcessID = GetWindowThreadProcessId(Fenster, &ProcessID);
i believe your problem is here pal. GetWindowThreadProcessId does not return the process id.
from msdn: The return value is the identifier of the thread that created the window.
just change: GetWindowThreadProcessId(Fenster, &ProcessID);
 

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
ProcessID = GetWindowThreadProcessId(Fenster, &ProcessID);
i believe your problem is here pal. GetWindowThreadProcessId does not return the process id.
from msdn: The return value is the identifier of the thread that created the window.
just change: GetWindowThreadProcessId(Fenster, &ProcessID);
Interesting thoughts.... I am getting now the right process ID (i verified it with Task Manager)...sadly the readprocess memory still gives an 0...the errors are gone thought...
So it must be something with the adresses???
:biblethump:
 

BadBoy25550

Newbie
Full Member
Jul 14, 2017
7
54
0
This is what I got now...
C++:
#include <iostream>
#include <Windows.h>
using namespace std;

HWND Fenster;
HANDLE hProcess;
const DWORD BaseAdress{ 0x50F4F4 };
const DWORD AmmoClipOffset{ 0x150 };
DWORD StaticPlayer;
DWORD AmmoinClipAdress;
int AmmoinClip{ 0 };
DWORD ProcessID{ 0 };


int main() {
	Fenster = FindWindowA(NULL, "AssaultCube"); //Suchen vom Spiel fenster... Ist wie ein Int z.B.

	GetWindowThreadProcessId(Fenster, &ProcessID); // Prozess Id vom definierten Fenster finden und als Hex abspeichern (DWord)
	if (ProcessID!=NULL)
	{
		cout << "ProcessID: " << ProcessID << endl;
	}


	hProcess = OpenProcess(PROCESS_VM_READ, false, ProcessID); //Den nun definierten Process öffnen um schreiben/ lesen zu können
	if (!hProcess)
	{
		DWORD Err = GetLastError();
		cout << "OpenProcess failed: " << hex << Err << endl;
	}

	BOOL Ret= ReadProcessMemory(hProcess, (PBYTE*)BaseAdress, &StaticPlayer, sizeof(DWORD), 0);
	if (!Ret)
	{
		DWORD Err = GetLastError();
		cout << "RPM1 failed: " << hex << Err << endl;
	}
	ReadProcessMemory(hProcess, (BYTE*)(StaticPlayer + AmmoClipOffset), &AmmoinClipAdress, sizeof(DWORD), 0);
	if (!Ret)
	{
		DWORD Err = GetLastError();
		cout << "RPM2 failed: " << hex << Err << endl;
	}
	ReadProcessMemory(hProcess, (BYTE*)AmmoinClipAdress, &AmmoinClip, sizeof(int), 0);
	if (!Ret)
	{
		DWORD Err = GetLastError();
		cout << "RPM3 failed: " << hex << Err << endl;
	}
	CloseHandle(hProcess);

	cout << AmmoinClip << endl;
	return 0;
}
 
Status
Not open for further replies.
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods