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++]Writing to memory problem

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Lothrik
Newbie cheater
Reputation: 0

Joined: 18 Mar 2009
Posts: 18

PostPosted: Wed Mar 31, 2010 2:11 pm    Post subject: [C++]Writing to memory problem Reply with quote

I'm able to read/write from 0F000000~1F000000 just fine. This memory region has AllocationProtect=Read/Write.

I can read from 00400000~00800000 just fine. This memory region has AllocationProtect=Execute/Write Copy.

If I try to write to any address in 00400000~00800000 however, the program almost always terminates, and my memory reader doesn't show any change at the addresses I tried to write to.

This is how I write to memory:
Code:
        template <class T> static void WriteMemory(const DWORD writeAddress, const T writeData)
        {
            if (writeAddress == 0)
                return;

            T *pWriteAddress = reinterpret_cast<T *>(writeAddress);

            if (pWriteAddress == 0)
                return;

            *pWriteAddress = writeData;
        }


This is how I read from memory:
Code:
        static MULTI_TYPE ReadMemory(const DWORD readAddress)
        {
            if (readAddress == 0)
                return TypeMgr::GEN_MULTI_TYPE((BYTE)0);

            uint64 *pReadAddress = reinterpret_cast<uint64 *>(readAddress);

            if (pReadAddress == 0)
                return TypeMgr::GEN_MULTI_TYPE((BYTE)0);

            return TypeMgr::GEN_MULTI_TYPE(*pReadAddress);
        }


.. and just for reference, this is what MULTI_TYPE means:
Code:
union MULTI_TYPE
{
    BYTE      bVal;
    WORD      wVal;
    DWORD    dwVal;
    float     fVal;
    double    dVal;
    uint64 ui64Val;
};


I've tried using VirtualProtect to change the AllocationProtect on the addresses I try to write to, but it ALWAYS fails, even if I change my Token Privileges, with this (just to clarify, I have checked and this AdjustPrivileges() function does "work", at least in the sense that no errors occur).

Code:
bool AdjustPrivileges()
{
   HANDLE hToken;
   TOKEN_PRIVILEGES newTP;
   TOKEN_PRIVILEGES oldTP;
   DWORD dwSize = sizeof(TOKEN_PRIVILEGES);
   LUID Luid;

   if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
   {
      if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
         return true;
      return false;
   }

   if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &Luid))
   {
      CloseHandle(hToken);
      return false;
   }

   ZeroMemory(&newTP, sizeof(newTP));
   newTP.PrivilegeCount = 1;
   newTP.Privileges[0].Luid = Luid;
   newTP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

   // Adjust Token Privileges
   if (!AdjustTokenPrivileges(hToken, FALSE, &newTP, sizeof(TOKEN_PRIVILEGES), &oldTP, &dwSize))
   {
      CloseHandle(hToken);
      return false;
   }
   // close handle
   CloseHandle(hToken);

   return true;
}


If I try to write to the problematic addresses in question, it works just fine, and my memory reader reports the changes Cheat Engine made instantly.

I'm at a bit of a loss for what I can do. My program is an injected DLL.
Back to top
View user's profile Send private message MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Mar 31, 2010 2:57 pm    Post subject: Reply with quote

VirtualQuery() to make sure you are getting regions of allocated memory.
Back to top
View user's profile Send private message
Lothrik
Newbie cheater
Reputation: 0

Joined: 18 Mar 2009
Posts: 18

PostPosted: Wed Mar 31, 2010 3:34 pm    Post subject: Reply with quote

Got the VirtualQuery to work:
PAGE_EXECUTE_READWRITE
MEM_COMMIT
MEM_IMAGE

Once I go in-game though, I get the message "Can't write to PAGE_EXECUTE_READ"

Edit: Hurrah! Finally got it to work with VirtualProtect ;D

Only problem now is the fact that the game thinks I have Cheat Engine open (haha, happens sometimes, only real fix is to relog/restart Windows it seems).

=)
Back to top
View user's profile Send private message MSN Messenger
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