SRL Archive - C/C++ Help and Tutorials

Using pointers to edit memory locationa? C++


Ethien

2009-04-08 15:49:14

Hey I started C++ a few weeks ago and I read into memory editing programs. I was like oh hey sounds simple I'd just do something like:


int *ptr;
ptr=0x22ff77;
*ptr=23;
Or at least a version of this to change the value of 0x22ff77 to a number. Is it because its looking in local script memory or what?
If someone could help me with this it would be appreciated.

~Ethien

ShowerThoughts

2009-04-08 16:01:19

It should work, but for game hacking you should hook it first to a game, I think.

Ethien

2009-04-08 16:09:39

When I first started this it wasn't for game hacking but I realize the potential. How would you hook this to a program anyways?

Ethien

2009-04-08 16:12:44

By the way, I used this to check if the said memory was edited:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int *first_val;
int test;
test=10;
int *ptr;
ptr=&test;

cout<<"Press enter to loop memory adress"<<endl;
system("PAUSE");

cout<<"You are using the memory adress of "<<ptr<<" Wich has the value of "<<*ptr<<" Is this correct?"<<endl;
system("PAUSE");
first_val=ptr;

int i;
for(i=0;i<1;i+=i)
{
if(first_val!=ptr)
{
break;
cout<<"Memory was changed to "<<ptr<<endl;
}
}

}

and to change it I used:


#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int changed_val;

int address;
cout<<"Please type in a memory adress to change..."<<endl;


cin>>address;
*ptr =address;


cout<<"What would you like to change this value to?"<<endl;

cin>>changed_val;

cout<<"You are about to change the value of the adress "<<*ptr<<" To "<<changed_val<<endl;

system("PAUSE");
*ptr=changed_val;



return 0;

}

Quickmarch

2009-04-08 18:22:34

Maybe you would need to change the Device Context.

Yakman

2009-04-08 19:28:58

each program runs in its own address space, so you cant have one program editing memory of another program just like that.

you need to make a part of your program run in the address space of another program.

for windows, you may be intrested in dll injection.

Ethien

2009-04-08 23:47:07

So is dll inject a way of hooking to a program?

Yakman

2009-04-09 07:20:16

depends on what you mean by hooking

Ethien

2009-04-09 12:00:02

Alright, so lets say I have a simple program called "holder.exe". It has a variable which is temp=10. Now I have another program that I want to hook to "Holder.exe" so I can change the value of temp.

boberman

2009-04-09 13:27:47

It is going to vary from OS to OS. Your program is given a virtual memory space. So two programs could have the addresses in them of 0x1000, that address is relative to the program itself.

The windows API has a call that will return a pointer to data outside of the program (I don't remember what it is at this point). Just know that you are going to have to rely on your os's API in order to change data in another running program. (Unless you do a dll injection like Yakman suggests)

Ethien

2009-04-09 13:48:05

Alright thanks a lot, one last thing. I've tried to change the address of a pointer manually using hex, but this never works. whats the correct syntax?

Yakman

2009-04-09 17:26:15

you need to cast it


int* pointer = (int*)0xdeadbeef;

*pointer = 5;

Ethien

2009-04-09 17:54:46

Ok so (int*) is telling it that the hex is an address?

Yakman

2009-04-09 20:57:41

yes, but it doesnt have to be hex, the hex is just another way of representing a number


int* pointer = (int*)564564564;

also works