Search code examples
c++memoryprocesshandle

Getting a different address from a HANDLE in C++ every time I re-compile and run the program?


I'm trying to get the starting address of a process in C++ by getting the handle and printing its value. I tried getting the starting address of Spider Solitaire through Cheat Engine and it gives me "300905A4D" (currently that is). I try

HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid) 

and it gives me a different number/address every time I restart my C++ program. I keep Spider solitaire running at all times, and cheat engine keeps giving me the same starting address, but when I print

cout << &pHandle; 

my c++ program gives me a different number every time. What am I doing wrong here? Also, my C++ code works fine. I can write to memory and all that, I just wonder why I'm not getting the same address Cheat Engine is giving me, and why it's a different one every time.


Solution

  • Because you're printing the address of the process handle variable, and hat may be different every time your program is run -- the OS decides upon every launch of your application where to put your process and its variables in the memory.