Click here to Skip to main content
14,366,827 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am new to C++ and it is just a pain in the ass to find a single working example of reading process memory.

What I have tried:

After hours of googling, I eventually found a glimmer of hope for this code:

// ConsoleApplication4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	DWORD address = 0x002B45AC; // This is the address that we want to read from
	float value = 9999; // This will store our value. In my case, its an integer, which is the timer
	DWORD pid; //This will store our Process ID, used to read/write into the memory
	HWND hwnd; //Finally a handle to our window
	hwnd = FindWindow(NULL, L"minesweeper"); //Finds the Window called "Minesweeper"
	if (!hwnd) //If none, display an error
	{
		cout << "Window not found!\n";
		cin.get();
	}
	GetWindowThreadProcessId(hwnd, &pid); //Get the process id and place it in pid
	HANDLE phandle = OpenProcess(PROCESS_VM_READ, 0, pid); //Get permission to read
	if (!phandle) //Once again, if it fails, tell us
	{
		cout << "Could not get handle!\n";
		cin.get();
	}

	
	while (1)  //Forever, or until you force close the program
	{
		ReadProcessMemory(phandle, (void*)address, &value, sizeof(value), 0); //Read what is in "address" and store it in "value", then what is the size of what we are going to read which is "value",and finally the number of bytes read, but I dont care about that so I put a 0, or NULL
		cout << value << "\n"; //Display it
		Sleep(1000); //I was reading the timer which increases every second, so I made my program go through that loop every second
		//return 0;
	}
	
}


I'm attempting to read float "value" from a game process and have it displayed on the console. I am also trying to write to process memory, so if anyone could help me with that I would greatly appreciate it!!

Thank you,
Posted
Updated 4-Oct-17 15:36pm
Comments
jeron1 4-Oct-17 16:03pm
   
I've not used this function but, is ReadProcessMemory() returning false? If so what's the GetLastError() say?
MrJack Mcfreder 4-Oct-17 16:59pm
   
GetLastError() is returning 2990, and ReadProcessMemorry() is returning earlier declared "9999" value whereas I want it to read a float value within the game process memory.
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think CPallini's comment is apposite however there are many examples to be found by googling. This: How to use ReadProcessMemory in C++?[^] leads here: Stealing Program's Memory[^] which may give you a start.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
I am new to C++ and it is just a pain in the ass to find a single working example of reading process memory.
That's a hint your are trying to do something too difficult for your current knowledge.

You should start reading the documentation: ReadProcessMemory function (Windows)[^], specifically, carefully read the 'Return value' and the 'Remarks' (they basically suggest the behaviour already proposed by jeron1).
   
Comments
MrJack Mcfreder 4-Oct-17 16:49pm
   
I read it already! That documentation is very mediocre and pointless without a practical example.

It is true I am new C++, but not to C#. I'm simply looking for someone to help me amend my code above or any 'example' suggestion is greatly appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100