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++ Reading variables from address

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
deleted user 343211
Cheater
Reputation: 0

Joined: 09 Feb 2013
Posts: 29

PostPosted: Sat Feb 09, 2013 10:15 pm    Post subject: C++ Reading variables from address Reply with quote

Hello, is there a function that will allow me to pass through the process name and variable address, and return the value of the address?
Here is my current code:
Code:

int ReadVariable(LPCSTR pName, LPVOID address){
   int point1;
   char* value[4];
   SIZE_T stBytes = 0;
   
   HWND hwnd;
   HANDLE phandle;
   DWORD pid;
   
   hwnd = FindWindow(NULL, pName);
   if (hwnd != 0) {
      GetWindowThreadProcessId(hwnd, &pid);
      phandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, 0, pid);
   } else {
      cout << pName << " is not open.";
      cout << "Press ENTER to exit." << endl;
      cin.get();
      CloseHandle(phandle);
      return 0;
   }
   
   if (phandle != 0) {
      cout << "The pointer is " << address << endl; //Print the pointer
      ReadProcessMemory(phandle, address, &point1, 4, &stBytes); //Get the address that the pointer is pointing at
      cout << "The pointer is pointing at " << point1 << " (DEC)" << endl; //Print the address that the pointer is pointing at
      ReadProcessMemory(phandle, (LPVOID)point1, &value, 6, &stBytes); //Get the value that is in the address pointed by the pointer
      cout << "The value in the non-static address is " << (char*)value << endl << endl; //Print the value
      cout << "Press ENTER to exit." << endl;
      cin.get();
   } else {
      cout << "Couldn't get a handle";
      cin.get();
   }
   CloseHandle(phandle);
}

The current code outputs random characters when I pass through an address from Cheat Engine.
Thanks.
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: Sat Feb 09, 2013 10:27 pm    Post subject: Reply with quote

You're casting an integer value to char*, so it is trying to convert it to a string.

You are also not correctly initializing the buffer to store the string data.
Code:
 char* value[4];


Should be:
Code:
char value[255]; // adjust size accordingly..


Given that your second ReadProcessMemory wants to read 6 bytes, you can size it to that or just leave it larger in case you reuse the buffer for other string values.

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

Joined: 09 Feb 2013
Posts: 29

PostPosted: Sat Feb 09, 2013 10:34 pm    Post subject: Reply with quote

Wiccaan wrote:
You're casting an integer value to char*, so it is trying to convert it to a string.

You are also not correctly initializing the buffer to store the string data.
Code:
 char* value[4];


Should be:
Code:
char value[255]; // adjust size accordingly..


Given that your second ReadProcessMemory wants to read 6 bytes, you can size it to that or just leave it larger in case you reuse the buffer for other string values.

I changed the char, but it's still the same.
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: Sat Feb 09, 2013 10:38 pm    Post subject: Reply with quote

You sure the addresses are correct that are being read?
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
deleted user 343211
Cheater
Reputation: 0

Joined: 09 Feb 2013
Posts: 29

PostPosted: Sat Feb 09, 2013 10:41 pm    Post subject: Reply with quote

Wiccaan wrote:
You sure the addresses are correct that are being read?

I'm getting them from Cheat Engine, under the "Address" tab.
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: Sat Feb 09, 2013 10:43 pm    Post subject: Reply with quote

Set some breakpoints and step through the code and make sure that things are getting their proper values. For one, check and make sure the initial pointer you are reading is the proper value.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
deleted user 343211
Cheater
Reputation: 0

Joined: 09 Feb 2013
Posts: 29

PostPosted: Sat Feb 09, 2013 10:44 pm    Post subject: Reply with quote

Wiccaan wrote:
Set some breakpoints and step through the code and make sure that things are getting their proper values. For one, check and make sure the initial pointer you are reading is the proper value.

Ok, I'll do that and tell you the results.
Edit:
When I pass through "C49EE22C" the address variable gets set to "0x0046fd7c". Is that right?
Here's the output of some variables:
&point1 0x0026f730 {-253001723} int *
&stBytes 0x0026f728 {6} unsigned long *
&value 0x0026f734 {0 '\0', 0 '\0', 0 '\0', 1 '\x1', 0 '\0', 0 '\0', 38 '&', 0 '\0', 80 'P', 78 'N', 17 '\x11', ...} char[255] *
address 0x0026f88c void *
phandle 0x000014c0 void *
point1 -253001723 int
value 0x0026f734 "" char[255]
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 Feb 10, 2013 2:39 pm    Post subject: Reply with quote

What do you mean pass through"C49EE22C"?
If you are passing that as a string to the above function, that wont work.

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

Joined: 09 Feb 2013
Posts: 29

PostPosted: Sun Feb 10, 2013 3:27 pm    Post subject: Reply with quote

Wiccaan wrote:
What do you mean pass through"C49EE22C"?
If you are passing that as a string to the above function, that wont work.

Oh, that's probably the problem then.
What's the right way to send an address like that?
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: Mon Feb 11, 2013 7:31 am    Post subject: Reply with quote

Pass it as the raw hex value, 0xC49EE22C.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
deleted user 343211
Cheater
Reputation: 0

Joined: 09 Feb 2013
Posts: 29

PostPosted: Mon Feb 11, 2013 8:02 am    Post subject: Reply with quote

Wiccaan wrote:
Pass it as the raw hex value, 0xC49EE22C.

It's telling me: "Error: argument of type "unsigned int" is incompatible with parameter "LPVOID"
Am I still doing it wrong?
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: Mon Feb 11, 2013 7:49 pm    Post subject: Reply with quote

TheChickenWings wrote:
Wiccaan wrote:
Pass it as the raw hex value, 0xC49EE22C.

It's telling me: "Error: argument of type "unsigned int" is incompatible with parameter "LPVOID"
Am I still doing it wrong?


Judging by this, you are probably just copy pasting code. You should take the time to learn the language, or at least the basics. This error is extremely easy to fix by either changing the expected argument type, or by casting. I recommend you take the time to go and learn the basics.

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

Joined: 09 Feb 2013
Posts: 29

PostPosted: Mon Feb 11, 2013 8:01 pm    Post subject: Reply with quote

Wiccaan wrote:
TheChickenWings wrote:
Wiccaan wrote:
Pass it as the raw hex value, 0xC49EE22C.

It's telling me: "Error: argument of type "unsigned int" is incompatible with parameter "LPVOID"
Am I still doing it wrong?


Judging by this, you are probably just copy pasting code. You should take the time to learn the language, or at least the basics. This error is extremely easy to fix by either changing the expected argument type, or by casting. I recommend you take the time to go and learn the basics.

Ok, I just needed to get this done quick.
Thanks.
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
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