Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++ | Help with WriteProcessMemory | Preview Included
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Wed Aug 15, 2018 2:28 am    Post subject: C++ | Help with WriteProcessMemory | Preview Included Reply with quote

Hey guys, currently making a game hack and I'm really new and only know basic stuff so please keep it simple.

I will post a image of my code. But here is the problem I am trying to WPM with new ammo value but it does not work. But when I removed the second RPM line it works. The line that I removed shows current ammo. How can I show current ammo and also write new ammo? I don't know what I'm doing wrong.

=========================================

DWORD Off1 = 0x0; // ammo offset
DWORD Off2 = 0x14; // ammo offset
DWORD Off3 = 0x384; // ammo offset
DWORD Base = 0x0509b74; // local player base pointer (static pointer?)
DWORD Ammooff = 0x150; // final ammo offset ?????
DWORD BaseAdd = 0x400000; // Local Player Base Address (ac_client.exe)
DWORD OffsetLocal = 0x109b74; // Local Player Base Offset (ac_client.exe + offset)
int LocalPlayerBasePointer;
int NewAmmovalue = 696;





DWORD Process_ID;

GetWindowThreadProcessId(hWnd, &Process_ID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Process_ID);

if (!hProcess)

{

MessageBox(NULL, "Cannot Find Process", "Error", MB_OK + MB_ICONERROR);

}

else
{

ReadProcessMemory(hProcess, (LPCVOID*)(BaseAdd + OffsetLocal), &LocalPlayerBasePointer, sizeof(LocalPlayerBasePointer), NULL);
cout << LocalPlayerBasePointer << endl;
system("PAUSE");
}

{ ReadProcessMemory(hProcess, (LPCVOID*)(LocalPlayerBasePointer + Ammooff), &LocalPlayerBasePointer, sizeof(LocalPlayerBasePointer), NULL);
cout << LocalPlayerBasePointer << endl;
system("PAUSE");



WriteProcessMemory(hProcess, (VOID*)(LocalPlayerBasePointer + Ammooff), &NewAmmovalue, sizeof(NewAmmovalue), NULL);
cout << NewAmmovalue << endl;
system("PAUSE");
}
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Wed Aug 15, 2018 8:28 am    Post subject: Reply with quote

Just find existing trainer source code and compare it to yours? https://guidedhacking.com/threads/assault-cube-c-trainer.7989/
_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Wed Aug 15, 2018 8:24 pm    Post subject: Reply with quote

FreeER wrote:
Just find existing trainer source code and compare it


I always compare but cannot figure out what is wrong with mine. :/ Can anyone help?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Wed Aug 15, 2018 9:49 pm    Post subject: Reply with quote

You're using some of the values incorrectly, or just assuming things incorrectly.

Firstly, you should avoid using 'PROCESS_ALL_ACCESS' as it can cause issues depending on the Windows version you are running on. Instead, specify the exact flags you need. (Failure to run things as admin will cause all access flags to fail, and depending on what Windows version you compile on, it wont work at all on others.)

On your 2nd read/write calls, you are assuming LocalPlayerBasePointer's value is going to be what it should be all the time, which it wont be since you are overwriting it. You should start reading the pointer over again to ensure you are reading the proper location.

You are also printing out 'NewAmmovalue' at the end. WriteProcessMemory does not alter the value after the call by using it like that. So it will always be the value you set it before, 696.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Aug 16, 2018 6:07 am    Post subject: Reply with quote

pLearner wrote:
FreeER wrote:
Just find existing trainer source code and compare it
I always compare but cannot figure out what is wrong with mine. :/ Can anyone help?

Lets see, put them up side by side
1. they provided the full code
2. They have a function to get the address the module ("ac_client.exe") is at in memory

3. They loop until the game is found

4. Part of your code runs whether the process was found or not, that's of course better than the other code in which all of it runs regardless of whether it's found or not (well other than the infinite loop at the start, but with something that runs based on key presses it's likely the game could be closed inbetween finding it and running the code to read/write values).

5. They read the pointer once.

You read it twice

6. They store the addresses in variables (where you could easily print them to see they're what you expect)


By no means am I saying that the other is the best way to do it, but it's certainly better in several ways.

_________________
https://github.com/FreeER/ has a few CE related repos


Last edited by FreeER on Thu Aug 16, 2018 6:34 am; edited 1 time in total
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Thu Aug 16, 2018 6:20 am    Post subject: Reply with quote

[quote="FreeER"][quote="pLearner"]
FreeER wrote:


Thank you, really helped me alot. I see where I did wrong. The second read line I used it twice. I not changed it and stored to a new variable. Now it works.
I can read the current ammo then overwrite it with new value. Funny how one little small thing can cause havoc.
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Thu Aug 16, 2018 10:41 pm    Post subject: Reply with quote

Another quick question. Have a look at the code starting from RPM.

1) How do I get it to keep the function seperate. Like example one function then end and start next function. So if I add something to the first one it does not affect the second one? Was playing around and when I put cout << hex << for the first line it makes all turn into hex which I want to keep it seperate.

2) When I’m RPM for current ammo and the current ammo is displayed in the console. How do I update it? So when I shoot it will keep updating display of current ammo?


Thanks guys appreciate your help sorry for my rookie ness!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Fri Aug 17, 2018 3:33 am    Post subject: Reply with quote

1. Reset the options back to normal after you are done.
<< hex << will cause it to use hex values.
<< dec << will cause it to use decimal values.

2. In a console, you can either keep spamming the console with information or use the console API to specifically overwrite a certain location in the console. If you want visual information like this its better to start working on a UI instead.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Sat Aug 18, 2018 4:44 am    Post subject: Reply with quote

I ran into another problem again. I was trying to make my variables simpler and easier to read. Once I done this it does not work and reads the wrong stuff. I don't know what causing this can someone help me?

I know it's the RPM + WPM part and the final declaration variables I think. Before it was working but when I tried to simple the variables down it doesn't work now. Hmmmm? If you need the full code please ask.

==========================================

DWORD AmmoOffset = 0x150; // Final Current Ammo Offset? (Not sure what to call it)
DWORD ClientBaseAddress = 0x400000; // Local Player Base Address (ac_client.exe)
DWORD ClientOffset = 0x109b74; // Local Player Base Offset (ac_client.exe + offset)

// STORED VALUES //
int LocalPlayerBasePointer; // Where the address is stored from = ClientBaseAddress + ClientOffset
int CurrentAmmo; // Where the address is stored from = LocalPlayerBasePointer + AmmoOffset
int NewAmmovalue; // New Ammo value to write to memmory over current ammo....

// FINAL DECLARATIONS //
DWORD ClientAddress = ClientBaseAddress + ClientOffset;
DWORD RifleAmmoAddress = ClientAddress + AmmoOffset;



DWORD Process_ID;
GetWindowThreadProcessId(hWnd, &Process_ID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Process_ID);

if (!hProcess)

{
MessageBox(NULL, "Cannot Find Process", "Error", MB_OK + MB_ICONERROR);
}


{
ReadProcessMemory(hProcess, (LPCVOID*)(ClientAddress), &LocalPlayerBasePointer, sizeof(LocalPlayerBasePointer), NULL);
cout << "This is the address of ClientBaseAddress + ClientOffset = " << LocalPlayerBasePointer << endl;
system("PAUSE");
// Text current disabled only enable for testing
}

{ cout << "=================================================================== " << endl;
cout << "WELCOME TO THE ASSAULT CUBE CONSOLE TRAINER " << endl;
cout << "=================================================================== " << endl;

ReadProcessMemory(hProcess, (LPCVOID*)(RifleAmmoAddress), &CurrentAmmo, sizeof(CurrentAmmo), NULL);
cout << "This is your Current Ammo = " << CurrentAmmo << endl;
system("PAUSE");

cout << "BEFORE AMMO HACK IS ENABLED ENTER THE AMOUNT AMMO YOU WANT: " << endl;
system("PAUSE");
cin >> NewAmmovalue;


WriteProcessMemory(hProcess, (VOID*)(RifleAmmoAddress), &NewAmmovalue, sizeof(NewAmmovalue), NULL);
cout << "This is the Ammo Hack that will be written. " << NewAmmovalue << endl;
system("PAUSE");
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sat Aug 18, 2018 9:03 am    Post subject: Reply with quote

so go back to your working code and change one thing at a time.
_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Sat Aug 18, 2018 7:12 pm    Post subject: Reply with quote

I have but still don’t know what is wrong. When I delete everything under the final declarations section. Then in the RPM/WPM section I replace RifleAmmoAddreess with LocalPlayerBasePointer + AmmoOffset it works but when I try make it simpler and easier to read like above code it won’t work?
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sat Aug 18, 2018 8:25 pm    Post subject: Reply with quote

Quote:
When I delete everything under the final declarations section
Like I said, do only one thing at a time. Do one name change and test it, then make one variable and test it, etc. Don't change multiple things, even if you change from doing +0x150 to +someVariable don't change two things at the same time by creating the variable and changing the expression to +someVariable, do one and then the other. Don't change a line and move it around which can change the order things get done in. Let alone "delete everything" even just within one "section". One thing.

Then when it stops working you will know exactly what caused it because only one change will have been made inbetween it working and not working. And of course it will be trivial to add a cout etc. to see what the difference is because you know that there's only one thing to check.


And yeah if you want someone else to tell you what change broke it then, yes you should provide the full code for both versions of the code, working and broken. And explain exactly which parts are broken, do you still find the process, still get the right base address, still read the player base pointer correctly, etc. It can save considerable time since we didn't write the code so don't just "know" what's right or wrong (and there's always multiple ways to achieve the same result) but have to actually read through the code to figure out what it's doing and what might be messing up and the more we have to guess at the longer that will take (and the more likely we'll guess wrong on something, wasting time and causing frustration).

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Sun Aug 19, 2018 12:22 am    Post subject: Reply with quote

In the RPM/WPM section when I remove the (RifleAmmoAddress) and replace it with: (LocalPlayerBasePointer + AmmoOffset), it works.

But when I try make it simpler and easier to read by putting:
DWORD RifleAmmoAddress = LocalPlayerBasePointer + AmmoOffset
or
DWORD RifleAmmoAddress = ClientAddress + AmmoOffset

It will not work. Need some help totally don't know what and why this is going on.....
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sun Aug 19, 2018 7:19 am    Post subject: Reply with quote

Print them out and see what the difference is... Obviously if you were just replacing one value/expression with a variable that has the exact same value, it would work.

is the offset set right? Is the LocalPlayerBasePointer/ClientAddress right? Is it a pointer where you're supposed to use RPM? Are you moving the variable creation up before you've set the other two variables or did you create it directly before the line where you were going to use it? Have you declared it twice, setting it in side an if statement where the outer variable wouldn't be set (aka shadowing)? I certainly don't have any way to know based only on what you've given. Hence why I told you in my last post that you should share the full code for both the working version and the broken version.

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
DEVCORE
Cheater
Reputation: 0

Joined: 11 Aug 2018
Posts: 28

PostPosted: Sun Aug 19, 2018 7:41 am    Post subject: Reply with quote

FreeER wrote:
Print them out and see what the difference is... Obviously if you were just replacing one value/expression with a variable that has the exact same value, it would work.

is the offset set right? Is the LocalPlayerBasePointer/ClientAddress right? Is it a pointer where you're supposed to use RPM? Are you moving the variable creation up before you've set the other two variables or did you create it directly before the line where you were going to use it? Have you declared it twice, setting it in side an if statement where the outer variable wouldn't be set (aka shadowing)? I certainly don't have any way to know based only on what you've given. Hence why I told you in my last post that you should share the full code for both the working version and the broken version.


Yes the addresses are all right I have found them in CE. Also the 2nd line RPM and WPM are ment to be from pointers. Like pointer address.

I will post full code once on the computer.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites