Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I'm trying to read a value in other application which is running in c++, how can I find the address of the variable of the other app in memory? because every time the app starts the address of the variable changes.
this is a sample code :


#include<iostream>
#include<Windows.h>
using namespace std;

int main()
{
	int address = 0x100579C; // this address is kind of problem
	int value=0;
	DWORD processid;
	HWND hwnd;

	hwnd = FindWindow(NULL,"appname");
	if(!hwnd)
		cout<<"Not found"<<endl;

	GetWindowThreadProcessId(hwnd,&processid);
	HANDLE processhandle = OpenProcess(PROCESS_ALL_ACCESS,0,processid);
	if(!processhandle)
		cout<<"Could not get handle"<<endl;

	ReadProcessMemory(processhandle,address,&value,sizeof(value),0);
	cout<<value;
	system("PAUSE");
	return 0;
}
Posted
Updated 31-May-13 2:04am
v2
Comments
Santhosh G_ 1-Jun-13 12:51pm    
Is it possible to use shared memory or other inter process mechanism to share the address from "appname".

1 solution

In most cases programs allocate objects dynamically from the heap and these objects reference each other with pointers. Maybe what you are searching for is pointed by a pointer that is pointed by another pointer and so on. You have to find the root pointer in order to dereference the other pointers. Finding the root pointer can be tricky. If you are lucky then there is a global variable somewhere that contains the root pointer. in this case you should query the base address of the exe or dll for example with EnumProcessModules()[^] or a similar function and you will find the global variable relative to one of the loaded modules. In worst case the root pointer is for example on the stack of the main() function in which case you have to enumerate the threads of the process, you have to find the main thread somehow, you have to query the context of the main thread (GetThreadContext()[^]: registers, including esp the stack pointer ) and then somehow you have to find the pointer in the stack (for example by searchgin some kind of patterns around the stack). But such a pointer can be stored in a lot of places, for example TLS, or what if I "hide" my root pointer for example by passing the pointer to a system call like SetWindowLongPtr and later I query the pointer with GetWindowLongPtr??? In this case you may have to inject code into the app and extract the ptr there... This task can be quite complicated and sometimes the solution is very dirty.
 
Share this answer
 
v2
Comments
mehdi_k 31-May-13 16:57pm    
Thanks for your help, but when an app runs it allocates a memory space to itself, let say from address 0x0000100 to 0x0001000 (if I'm true), so if I could get the starting and ending address of an app I may find the value I want, and then change it.
is there a way to do this and I'm I thinking right?
pasztorpisti 1-Jun-13 2:57am    
Reading your question I'm pretty sure this task is currently far beyond what you can finish successfully. Instead of this task I would first check out some windows memory/process management documentations and after that I would search for the tutorials of some guys who are having fun with similar problems. Without that you are just blindly trying to put together the puzzles.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900