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++ Read memory

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
wongerlt
How do I cheat?
Reputation: 0

Joined: 26 May 2015
Posts: 2

PostPosted: Tue May 26, 2015 11:30 am    Post subject: C++ Read memory Reply with quote

Hello,
I want to read memmory with pointer, me code without pointers:

Code:

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

int main()
{
SetConsoleTitle ("Test");
DWORD processId;
HANDLE hProcess;

DWORD address = 0x000AA768;

processId = 6336;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
int value;
ReadProcessMemory(hProcess, (LPVOID)address, &value, 4, 0);
std::cout<<"Value:"<<value<<std::endl;
return 0;
}


Cheat engine:
"game.exe"+00EE1C00
pointer = 320
so "game.exe"+00EE1C00+320 = address

how to get full address with ponter?
Back to top
View user's profile Send private message
P.EduC
How do I cheat?
Reputation: 0

Joined: 15 May 2015
Posts: 2

PostPosted: Tue May 26, 2015 5:29 pm    Post subject: Reply with quote

Pointer: "game.exe"+00EE1C00
Offset1: 320

Code:
DWORD Pointer, Offset1, Resultado;
Pointer = ((DWORD)GetModuleBaseAddress(processId , "game.exe") + 0x00EE1C00);
printf("Pointer: 0x%08X \n", Pointer);

ReadProcessMemory(hProcess, (void*)Pointer, &Offset1, 4, NULL);
Offset1 += 0x320;
printf("Offset1: 0x%08X \n\n", Offset1);

ReadProcessMemory(hProcess, (void*)Offset1, &Result, 4, NULL);
printf("Result: %i \n\n", Result);
Back to top
View user's profile Send private message
Krampus
Cheater
Reputation: 0

Joined: 22 Nov 2014
Posts: 41

PostPosted: Wed May 27, 2015 12:03 am    Post subject: Reply with quote

Code:
template <class T>
void read(DWORD addy, T& var) {
   ReadProcessMemory(m_prcHndl, (LPCVOID)addy, &var, sizeof(var), NULL);
}

template <class T>
T read(DWORD addy) {
   T temp;
   ReadProcessMemory(m_prcHndl, (LPCVOID)addy, &temp, sizeof(temp), NULL);

   return temp;
}

template <class T>
void readPtr(int numOfOffsets, DWORD offsets[], DWORD base, T& var) {
   DWORD temp = read<DWORD>(base);

   int i;
   for (i = 0; i < numOfOffsets - 1; i++) {
      temp = read<DWORD>(temp + offsets[i]);
   }

   read<T>(temp + offsets[i], var);
}


Just a snip from my simple memory class. Pseudo code for the usage:
Code:
DWORD address = 0xDEADBEEF;
DWORD offsets[] = { 0xFE, 0xBE, 0xAD, 0xDE };
DWORD result;
readPtr<DWORD>(4, offsets, address, result);



Good luck,
Krampus

_________________
There is no spoon.
Back to top
View user's profile Send private message
aasi888
How do I cheat?
Reputation: 0

Joined: 29 Jul 2009
Posts: 6

PostPosted: Sun Sep 20, 2015 3:33 pm    Post subject: Reply with quote

The code in the first post worked. I just don't want to search for the PID every time I restart the game.

1. How to grab PID from active window?
2. How to grab all PIDs from programs with certain window title?

I tried #1, but this code seems to be giving me random numbers (The PIDs I get do not match any process in my task manager). Each run gives a different result, even though cmd.exe window is focused every time.


read_test3.cpp
Code:

#include <iostream>
#include <string>
#include <windows.h>

int main()
{
   int pid = GetCurrentProcessId();
   std::cout<< pid << "\n\n\n\n\n\n\n\n\n";
   return 0;
}


PS I'm fairly new in c++. Seems odd that I can't even add "sleep 2000;" in there w/o compiling errors.
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Sep 20, 2015 4:21 pm    Post subject: Reply with quote

You get a different PID because the executable is a different process than your command prompt.
If you look in your Task Manager, you'll notice cmd.exe is there and when you run your test, read_test3.exe pops up briefly.
Same can also be true for some games. The process you attach isn't necessarily the window.

To find the PID of a window, you can use:
Code:
HWND WindowHandle = FindWindow(nullptr, L"Window Title");
DWORD PID;
GetWindowThreadProcessId(WindowHandle, &PID);
PVOID hProcess = OpenProcess((0x0010) | (0x0400), 0, PID);

You should have access to "Sleep(2000)" since you included <Windows.h>.
I do believe it needs to be capitalized.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sun Sep 20, 2015 6:26 pm    Post subject: Reply with quote

If you want to get the active / foreground window there is:
- GetForegroundWindow

If you want to find all windows matching the same name, you can use:
- FindWindow
- GetWindow

You would call FindWindow with the name of the window title or class name you wish to look for, then inside of a loop you would call GetWindow with the GW_HWNDNEXT definition as the 2nd parameter. Like this:
Code:
auto hWnd = ::FindWindow(nullptr, "Minesweeper");
while (hWnd != 0)
{
    hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
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