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 


Reading same value via .dll soure code as in cheat engine

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

Joined: 18 Mar 2013
Posts: 2

PostPosted: Mon Mar 18, 2013 2:52 pm    Post subject: Reading same value via .dll soure code as in cheat engine Reply with quote

Hi all,

maybe it is just a twister in my thinking, but currently I am not getting any further with my problem implementing a mem read into my source code. Lets assume the following:

This code will be used through a .dll loaded by the process at startup:
Code:

void GetThisProcessHandle(){
   try{
      DWORD CurrentProcessID; // -- this will store the process ID of the current process used to read from the memory
      CurrentProcessID = GetCurrentProcessId();
      globalProcessID = CurrentProcessID;
      if( CurrentProcessID ){
         globalProcHandle = OpenProcess(PROCESS_VM_READ, 0, CurrentProcessID); // -- get permission to read
         globalBaseAddress = (DWORD)globalProcHandle;
      }
   }catch( ... ){
      // -- just catch any errors
   }
}

int GetState(){
   try{
      DWORD AddressState = 0x00A7BCAE; // -- this is the address that we want to read from
      int ValueState = -1;             // -- this will store the value of the state

      if( globalProcHandle ){
         ReadProcessMemory(globalProcHandle, (void*)AddressState, &ValueState, sizeof(ValueState), 0);
      }
      return ValueState;
   }catch( ... ){
      return -2;
   }
}


Triggering the value change and reading it via cheat engine, the result is as expected (either true or false || 1 or 0), but when I use those two functions, I always get (in this case) -65335 as a return value. Does anyone might can give me a hint what I am doing wrong !?!?

Thank you very much in advance ...

Cheers
NoIP



BoolValue.png
 Description:
 Filesize:  23.97 KB
 Viewed:  3504 Time(s)

BoolValue.png


Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Mon Mar 18, 2013 4:02 pm    Post subject: Reply with quote

One thing to note:
Code:
globalBaseAddress = (DWORD)globalProcHandle;


This is not valid, the handle you open is not the base address of the module / executable.

As for your reading problem, you are trying to read 4 bytes instead of 1:
Code:
      int ValueState = -1;             // -- this will store the value of the state

      if( globalProcHandle ){
         ReadProcessMemory(globalProcHandle, (void*)AddressState, &ValueState, sizeof(ValueState), 0);
      }


Instead, change the sizeof to just be 1 for the size to read.

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

Joined: 02 Sep 2012
Posts: 323

PostPosted: Mon Mar 18, 2013 4:46 pm    Post subject: Reply with quote

As Wiccaan said that isn't the base address... but your main problem is you you're reading 4 bytes (sizeof(int)) rather than 1, so actually only read 1 byte and change you're variable to be the same size as the data you're reading, or initialize your variable to zero rather than -1...

Plus if you're inside the process already, why even use ReadProcessMemory?

You can just do: (since you're value is only a byte you probably want to store in a byte sized value also... you're using an int which is 4 bytes)
Code:

BYTE ValueState = *(BYTE*)AddressState;

//Or if you really wanted it to be in a 4 byte value and use your int
int ValueState = 0;
BYTE TheValue = *(BYTE*)AddressState;
ValueState = TheValue;


If you wanted to use ReadProcessMemory though for some reason... Then (since you're inside the process) use this to get it's base address instead of what you have:

Code:

globalBaseAddress = (DWORD)GetModuleHandleW(0); // or GetModuleHandleA(0);


And also change:
Code:

//to either this:
BYTE ValueState = -1;
//char ValueState = -1; //same thing if you don't include <Windows.h>
ReadProcessMemory(globalProcHandle, (void*)AddressState, &ValueState, sizeof(ValueState), 0);

//or you could use an int, but don't set it to -1 (0xFFFFFFFF) as if you only read one byte into it, it will it's not going to equal 1 or 0 after reading that byte into it... it will be some negative value (more like -255 or -256) as you'll be only setting that one byte, and since you've initialized it to -1 instead of zero, it will look like: 0xFFFFFF01 or 0xFFFFFF00

int ValueState = 0;
ReadProcessMemory(globalProcHandle, (void*)AddressState, &ValueState, 1, 0);

_________________
Back to top
View user's profile Send private message
NoIPDotCom
How do I cheat?
Reputation: 0

Joined: 18 Mar 2013
Posts: 2

PostPosted: Tue Mar 19, 2013 7:40 am    Post subject: Reply with quote

@Wic: That really get me going Very Happy thank u so much !!!

So far I am using this to do the job:
Code:

bool GetState( DWORD Address ){
   try{
      bool Value = 0;

      if( globalProcHandle ){
         ReadProcessMemory(globalProcHandle, (void*)Address, &Value, sizeof(Value), 0);
      }
      return Value;
   }catch( ... ){
      return 0;
   }
}


@Steve: You've giving me a lot to think about. Really appreciate that !!! Very Happy

So as far as I understand this ...
Code:

BYTE ValueState = *(BYTE*)AddressState;


... could replace my whole function to access the state as if it is true or false ...

In general I would like to have the least impact with most highest reliability possible ... I just started with C++ and each day I finally find out what I don't know ... so here too ... thank u so much Steve!

Cheers
NoIP
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 Discussions 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