Are there better ways to implement what I have done - a Console Memory Hacker for the game Call of Duty Black Ops single player mode. Comments, Performance, Style, anything. And, it needs Multi-Byte Character Set in properties to run without errors.
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
const char* PROCESS_NAME = "BlackOps.exe";
const unsigned short POLL_RATE = 100;
const unsigned int POINTS_ADDRESS = 0x01C0A6C8;
const unsigned int HEALTH_ADDRESS = 0x01A7987C;
const unsigned int CLIP_1_ADDRESS = 0x01C08F00;
const unsigned int AMMO_1_ADDRESS = 0x01C08E88;
const unsigned int CLIP_2_ADDRESS = 0x01C08F18;
const unsigned int AMMO_2_ADDRESS = 0x01C08E98;
const unsigned int GRENADES_ADDRESS = 0x01C08F08;
const unsigned int HACK_POINTS_VALUE = 100000;
const unsigned short HACK_HEALTH_VALUE = 1000;
const unsigned short HACK_CLIP_1_VALUE = 1000;
const unsigned short HACK_AMMO_1_VALUE = 1000;
const unsigned short HACK_CLIP_2_VALUE = 1000;
const unsigned short HACK_AMMO_2_VALUE = 1000;
const unsigned short HACK_GRENADES_VALUE = 10;
DWORD get_process_id_by_process_name(const char* process_name_)
{
PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
HANDLE processes_snapshot;
DWORD process_id = 0;
// search for the process name
processes_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(processes_snapshot, &process_entry))
{
do
{
if (!strcmp(process_entry.szExeFile, process_name_))
{
process_id = process_entry.th32ProcessID;
break;
}
} while (Process32Next(processes_snapshot, &process_entry));
}
// clean up resources
CloseHandle(processes_snapshot);
return process_id;
}
int main(int argc_, char** argv_)
{
// get game process id
std::cout << "Looking for game..." << std::endl;
DWORD process_id = NULL;
while (!process_id)
{
process_id = get_process_id_by_process_name(PROCESS_NAME);
Sleep(POLL_RATE);
}
std::cout << "Game found." << std::endl;
// open game handle
HANDLE process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id);
if (process_handle)
{
std::cout << "Handle obtained to game" << std::endl;
}
else
{
std::cout << "Handle not obtained to game." << std::endl;
exit(1);
}
// initialize variables
bool lock_points = false;
bool lock_clip_1 = false;
bool lock_ammo_1 = false;
bool lock_clip_2 = false;
bool lock_ammo_2 = false;
bool lock_health = false;
bool lock_grenades = false;
bool update_screen = true;
while (true)
{
// only update screen when needed
if (update_screen)
{
system("cls");
std::cout << "Call of Duty Black Ops Console Memory Hacker" << std::endl;
std::cout << "============================================" << std::endl;
std::cout << " [F1] - (" << (lock_points ? "on ": "off") << ") Lock Points to 100,000" << std::endl;
std::cout << " [F2] - (" << (lock_clip_1 ? "on ": "off") << ") Lock Clip 1 to 1,000" << std::endl;
std::cout << " [F3] - (" << (lock_ammo_1 ? "on ": "off") << ") Lock Ammo 1 to 1,000" << std::endl;
std::cout << " [F4] - (" << (lock_clip_2 ? "on ": "off") << ") Lock Clip 2 to 1,000" << std::endl;
std::cout << " [F5] - (" << (lock_ammo_2 ? "on ": "off") << ") Lock Ammo 2 to 1,000" << std::endl;
std::cout << " [F6] - (" << (lock_health ? "on ": "off") << ") Lock Health to 1,000" << std::endl;
std::cout << " [F7] - (" << (lock_grenades ? "on ": "off") << ") Lock Grenades to 10" << std::endl;
update_screen = false;
}
// check for associated keystrokes to menu
if (GetAsyncKeyState(VK_F1))
{
lock_points = !lock_points;
update_screen = true;
}
if (GetAsyncKeyState(VK_F2))
{
lock_clip_1 = !lock_clip_1;
update_screen = true;
}
if (GetAsyncKeyState(VK_F3))
{
lock_ammo_1 = !lock_ammo_1;
update_screen = true;
}
if (GetAsyncKeyState(VK_F4))
{
lock_clip_2 = !lock_clip_2;
update_screen = true;
}
if (GetAsyncKeyState(VK_F5))
{
lock_ammo_2 = !lock_ammo_2;
update_screen = true;
}
if (GetAsyncKeyState(VK_F6))
{
lock_health = !lock_health;
update_screen = true;
}
if (GetAsyncKeyState(VK_F7))
{
lock_grenades = !lock_grenades;
update_screen = true;
}
// perform hack(s) if they are activated
if (lock_points)
{
unsigned int write_value = HACK_POINTS_VALUE;
WriteProcessMemory(process_handle, (LPVOID)POINTS_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_clip_1)
{
unsigned int write_value = HACK_CLIP_1_VALUE;
WriteProcessMemory(process_handle, (LPVOID)CLIP_1_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_ammo_1)
{
unsigned int write_value = HACK_AMMO_1_VALUE;
WriteProcessMemory(process_handle, (LPVOID)AMMO_1_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_clip_2)
{
unsigned int write_value = HACK_CLIP_2_VALUE;
WriteProcessMemory(process_handle, (LPVOID)CLIP_2_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_ammo_2)
{
unsigned int write_value = HACK_AMMO_2_VALUE;
WriteProcessMemory(process_handle, (LPVOID)AMMO_2_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_health)
{
unsigned int write_value = HACK_HEALTH_VALUE;
WriteProcessMemory(process_handle, (LPVOID)HEALTH_ADDRESS, &write_value, sizeof(write_value), NULL);
}
if (lock_grenades)
{
unsigned int write_value = HACK_GRENADES_VALUE;
WriteProcessMemory(process_handle, (LPVOID)GRENADES_ADDRESS, &write_value, sizeof(write_value), NULL);
}
// Delay for POLL_RATE before iterating again
Sleep(POLL_RATE);
}
}