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++ ReadProcessMemory problem

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

Joined: 17 Feb 2007
Posts: 30

PostPosted: Sat Dec 20, 2008 5:02 am    Post subject: C++ ReadProcessMemory problem Reply with quote

So I tried to read the static memory address for player X address in C++ and it didnt work. I have cheat engine open with the same memory address being viewed with the correct X location.

The current output is just 2. always 2. Im running vista and there is always problems with my permissions. I thought OpenProcess as ACCESS_ALL and running as administrator would do the trick but I guess not.

heres my code
Code:
#include <cstdlib>
#include <iostream>
#include <Windows.h>

//memory addresses
DWORD PLAYER_X = 0x012E1B7C;

int main(int argc, char *argv[])
{
    long x;
    unsigned long pID;
   
    HWND hWnd = FindWindow(NULL, "World of Warcraft");
    GetWindowThreadProcessId(hWnd, &pID);
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);
   
    ReadProcessMemory(hProcess, (LPVOID) PLAYER_X, &x, sizeof(x), NULL);
   
    std::cout<< x << "\n";
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

_________________
C++
autoIT
Java PrOgRaMmEr
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sat Dec 20, 2008 5:15 am    Post subject: Reply with quote

Code:
long x = 0;
Is it 0 now?

Also make sure your functions success and if they fail, do ::GetLastError();
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Sat Dec 20, 2008 5:49 am    Post subject: Reply with quote

Code:
#include <cstdlib>
#include <iostream>
#include <Windows.h>

//memory addresses
DWORD PLAYER_X = 0x012E1B7C;

int main(int argc, char *argv[])
{
    int x = 0;
    unsigned long pID;
   
    HWND hWnd = FindWindow(NULL, "World of Warcraft");
    // Better use ClassName
    GetWindowThreadProcessId(hWnd, &pID);
   
    HANDLE hProcess = OpenProcess(PROCESS_VM_READ, FALSE, pID);
   // I think PROCESS_VM_READ is enough
   
    ReadProcessMemory(hProcess, (LPVOID)PLAYER_X, &x, 4, NULL);
   // I don't know why you took sizeof(x), the 4th parameter is the NumberOfBytesTORead, for example: 4.
   
    std::cout<< x << "\n";
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Sat Dec 20, 2008 6:07 am    Post subject: Reply with quote

http://msdn.microsoft.com/en-us/library/aa446619(VS.85).aspx
Back to top
View user's profile Send private message MSN Messenger
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sat Dec 20, 2008 8:53 am    Post subject: Reply with quote

system("PAUSE")?, delete this crap.
Back to top
View user's profile Send private message MSN Messenger
0_00_0
Cheater
Reputation: 0

Joined: 17 Feb 2007
Posts: 30

PostPosted: Sat Dec 20, 2008 11:41 am    Post subject: Reply with quote

Jani wrote:
Code:
long x = 0;
Is it 0 now?

Also make sure your functions success and if they fail, do ::GetLastError();

Ah yes very smart. It does remain at zero when I initialize it to zero.

noz3001 wrote:
http://msdn.microsoft.com/en-us/library/aa446619(VS.85).aspx


I am 99.9% sure this is my problem. I have to give myself privileges before I can read the memory. Im just really foggy how to use the privilege tokens.

_________________
C++
autoIT
Java PrOgRaMmEr
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sat Dec 20, 2008 12:14 pm    Post subject: Reply with quote

Quick snippet I wrote for my Engine a couple days ago, if you want to use PROCESS_ALL_ACCESS.

Code:
BOOL SetDebugPrivileges()
{
   BOOL               bRET = FALSE;
   TOKEN_PRIVILEGES   tp;
   HANDLE             hToken;

   if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid))
   {
      if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
      {
         if (hToken != INVALID_HANDLE_VALUE)
         {
            tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            tp.PrivilegeCount = 1;
            if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, 0, 0))
               bRET = TRUE;
            CloseHandle(hToken);
         }
      }
   }
   return bRET;
}

_________________
Back to top
View user's profile Send private message
0_00_0
Cheater
Reputation: 0

Joined: 17 Feb 2007
Posts: 30

PostPosted: Sat Dec 20, 2008 12:37 pm    Post subject: Reply with quote

lurc wrote:
Quick snippet I wrote for my Engine a couple days ago, if you want to use PROCESS_ALL_ACCESS.

Code:
...


this worked perfectly. your the best. I was having a little difficulty with the MSDN one. I always hate their examples.

edit:+rep:P

_________________
C++
autoIT
Java PrOgRaMmEr
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Dec 22, 2008 5:51 am    Post subject: Reply with quote

::GetLastError(); would have returned 5, in other words ERROR_ACCESS_DENIED (probably, I'm not 100% sure does it return this when you don't have the proper access rights). -> Problem solved :)
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