Small contrib: C++ mem reading class menu

User Tag List

Results 1 to 3 of 3
  1. #1
    Overflow's Avatar Member
    Reputation
    -10
    Join Date
    Mar 2009
    Posts
    21
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Small contrib: C++ mem reading class

    Mostly seen C# code and no C++ code at all for reading memory, so for those too lazy to convert from C# or unable W/e


    MemHandler.h
    Code:
    #if _MSC_VER > 1000
    #pragma once
    #endif 
    
    #ifndef  __MEMREADER_H 
    #define __MEMREADER_H
    
    #include <string>
    
    class MemHandler
    {
    private:
    	unsigned long m_bytesRead;
    	unsigned long m_bytesWritten;
    public:
    	//Static addresses
    	const static unsigned int ADR_CLIENT_CON_BASE = 0x11CB310; 
    	const static unsigned int ADR_PLAYER_BASE = 0x127F13C;
    	const static unsigned int ADR_MOUSE_OVER_TARGET = 0x10A68D0;
    	const static unsigned int ADR_ZONE = 0x010A68B4;//010A68B4
    	const static unsigned int ADR_SUB_ZONE = 0x010A68B8;
    
    	//Static offsets
    	const static unsigned int OFFSET_TYPE = 0x14;
    	const static unsigned int OFFSET_DESC = 0x8;
    	const static unsigned int OFFSET_LEVEL = 0x35 * 4;
    	const static unsigned int OFFSET_CURR_HEALTH = 0x17 * 4;
    
    	//Dynamic addresses
    	unsigned int CLIENT_CON_BASE; //[0x11CB310]
    	unsigned int PLAYER_BASE; //[[[0x127F13C] + 0x30] + 0x28]
    	unsigned int OBJ_MGR_BASE;//[[CLIENT_CON_BASE] + 0x28A4] 
    	unsigned int FIRST_OBJ_BASE;
    
    	//object types
    	const static int OBJ_ITEM = 1;
    	const static int OBJ_CONT = 2;
    	const static int OBJ_NPCS = 3;
    	const static int OBJ_PLAY = 4;
    	const static int OBJ_GAME = 5;
    	const static int OBJ_DYNA = 6;
    	const static int OBJ_CORP = 7;
    
    	//globals
    	HWND g_hWnd;
    	HANDLE g_hProcess;
    	unsigned long g_pID;
    
    	bool setDebugPrivileges();
    	int init(const char * wndName);
    	int initBaseAddresses();
    	MemHandler();
    
    	unsigned long read(unsigned long address, unsigned long size, LPVOID lpVal);
    	unsigned long readChars(unsigned long address, char * &pszVal);
    	short readShortValue(unsigned int address);
    	int readString(unsigned int address, char * buf, int length);
    	long readLongValue(unsigned int address);
    	unsigned int readUnsignedIntValue(unsigned int address);
    	unsigned long readUnsignedLongValue(unsigned int address);
    	int readIntValue(unsigned int address);
    	INT64 readIntValue64(unsigned int address);
    	float readFloatValue(unsigned int address);
    	DWORD readDwordValue(unsigned int address);
    	
    };
    
    #endif
    MemHandler.cpp
    Code:
    #include <cstdlib> 
    #include <iostream> 
    #include <Windows.h>
    #include "MemHandler.h"
    
    
    
    bool debug = false;
    
    
    MemHandler::MemHandler(){
    
    }
    
    short MemHandler::readShortValue(unsigned int address){
    	short s;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &s, sizeof(s), NULL);
    	
    	if(debug) std::cout << "Short: " << address << " = " << s << "\n";
    
    	return s;
    }
    
    float MemHandler::readFloatValue(unsigned int address){
    	float f;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &f, sizeof(f), NULL);
    	
    	if(debug) std::cout << "Float: " << address << " = " << f << "\n";
    
    	return f;
    }
    
    long MemHandler::readLongValue(unsigned int address)
    {
    
    	long l;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &l, sizeof(l), NULL);
    
    	if(debug)
    		std::cout << "readLongValue for:" << address << " = " << l << "\n";
    
    	return l;
    
    }
    
    int MemHandler::readIntValue(unsigned int address)
    {
    
    	int i;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &i, sizeof(i), NULL);
    
    	if(debug)
    		std::cout << "readIntValue for:" << address << " = " << i << "\n";
    
    	return i;
    
    }
    
    
    unsigned int MemHandler::readUnsignedIntValue(unsigned int address)
    {
    
    	unsigned int ui;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &ui, sizeof(ui), NULL);
    	
    	if(debug)
    		std::cout << "readUnsignedIntValue for:" << address << " = " << ui << "\n";
    
    	return ui;
    
    }
    
    unsigned long MemHandler::readUnsignedLongValue(unsigned int address)
    {
    
    	unsigned long ul;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &ul, sizeof(ul), NULL);
    	
    	if(debug)
    		std::cout << "ULong:" << address << " = " << ul << "\n";
    
    	return ul;
    
    }
    
    
    DWORD MemHandler::readDwordValue(unsigned int address)
    {
    
    	DWORD dw;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &dw, sizeof(dw), NULL);
    
    	if(debug)
    		std::cout << "readDwordValue for:" << address << " = " << dw << "\n";
    
    	return dw;
    
    }
    
    unsigned long MemHandler::read(unsigned long address, unsigned long size, LPVOID lpVal)
    {
    	m_bytesRead = 0;
    	ReadProcessMemory( g_hProcess, (LPVOID)address, lpVal, size, &m_bytesRead );
    
    	return m_bytesRead;
    }
    
    unsigned long MemHandler::readChars(unsigned long address, char * &pszVal)
    {
    	return read(address, sizeof(char *), (LPVOID)&pszVal);
    }
    
    int MemHandler::readString(unsigned int address, char * buf, int length)
    {
    
    	m_bytesRead = 0;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, buf, length, &m_bytesRead);
    
    	if(debug)
    		std::cout << "readString for:" << address << " = " << buf << "\n";
    
    	return m_bytesRead;
    }
    
    INT64 MemHandler::readIntValue64(unsigned int address)
    {
    
    	INT64 i;
    	
    	ReadProcessMemory(g_hProcess, (LPVOID) address, &i, sizeof(i), NULL);
    
    	if(debug)
    		std::cout << "readStrValue32 for:" << address << " = " << i << "\n";
    
    	return i;
    
    }
    
    
    
    int MemHandler::init(const char * wndName)
    {
    	g_hWnd = FindWindow(NULL, wndName); 
        GetWindowThreadProcessId(g_hWnd, &g_pID); 
        g_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, g_pID); 
    
    	if(g_hProcess)
    		return 1;
    	else
    		return 0;
    }
    
    int MemHandler::initBaseAddresses()
    {
    	CLIENT_CON_BASE = this->readUnsignedIntValue(ADR_CLIENT_CON_BASE);
    	OBJ_MGR_BASE = this->readUnsignedIntValue(CLIENT_CON_BASE + 0x28A4);
    	FIRST_OBJ_BASE = this->readUnsignedIntValue(OBJ_MGR_BASE + 0xAC);
    	PLAYER_BASE = this->readUnsignedIntValue(ADR_PLAYER_BASE);
    	PLAYER_BASE = this->readUnsignedIntValue((PLAYER_BASE+0x30));
    	PLAYER_BASE = this->readUnsignedIntValue((PLAYER_BASE+0x28));
    
    	return 1;
    }
    
    
    
    bool MemHandler::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; 
    }
    If I do not get too much flaming for this I will post my class for input (sendinput/postmessage)

    I will also soon post my bot since it is closing in on something worth using

    Hope this can help someone.

    Small contrib: C++ mem reading class
  2. #2
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Constructive critisism:

    In the event your object falls out of scope or is manually destructed, you haven't defined a destructor so usermode handles will be leaked.

    On that note, you provide no way to do any cleanup at all, so even if you wanted to cleanly destroy the object its impossible.

    Header files should be self-sufficient. Move the #include <Windows.h> to your header file or remove the use of typedefs from that header file such as LPVOID

    You should avoid using globals like that, even though its a built-in type so you don't have to really worry about construction-based race conditions its still poor style.

    I don't really understand the point of the initialization function, all that would be fine in the object constructor. If it fails just throw an exception.

    Data should generally be marked as private and accessibly only through 'getters' and if its appropriate, 'setters'. This is especially true for things like handles, which should be abstracted away from the users of your class.

    If you want users to have some more control in terms of debug output I suggest allowing them to pass a stream buffer pointer to the constructor. If they pass a stream buffer then you can replace std::couts internal buffer with that one. That allows the user to completely control where the output is written, it could be anywhere from a seperate console, to WoWs console, to written to screen using D3D, anything at all.

  3. #3
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And oh, for the object type etc, it would make a LOT more sense to use an enumerated type. Not only does that help when passing or returning an object's type (because C++ is strongly typed), it makes the purpose and usage more obvious and intuitive.

    EDIT:
    On more. You should avoid the use of C-style casts and instead use the C++ style casts. Not only are they more visible and hence easier to parse for when doing code analysis and cleanup, they also have set purposes and help to enforce and distinguish between 'safe' casting and 'unsafe' casting.

    EDIT2:
    Bah, another one. You shouldn't use SCREAMING_CAPS in member names, such a naming scheme is generally reserved for preprocessor use to make it blatantly obvious that a macro is being used, using screaming caps for a regular identifier can quickly become confusing when client code is using your class.
    Last edited by Cypher; 04-10-2009 at 06:42 AM.

Similar Threads

  1. Mem Read - Gold Stand
    By nopaxx in forum WoW Memory Editing
    Replies: 13
    Last Post: 12-12-2009, 08:54 PM
  2. Mem Read with Java
    By iknowuwantme in forum Programming
    Replies: 1
    Last Post: 09-19-2009, 05:07 PM
  3. mem-reading fishbot - state of bite ?
    By ninar1 in forum WoW Memory Editing
    Replies: 10
    Last Post: 09-08-2009, 11:33 AM
  4. [C# + Win32] Memory Reading Class Example
    By joetheodd in forum WoW Memory Editing
    Replies: 29
    Last Post: 08-04-2009, 08:35 PM
  5. [C++]Memory Reading Class
    By opulent in forum WoW Memory Editing
    Replies: 3
    Last Post: 05-11-2009, 06:55 AM
All times are GMT -5. The time now is 11:45 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search