C++修改内存
#pragma once
#include<Windows.h>template<class T>
struct datas
{T Value;datas*next;
};
template<class T>
class TPSet
{
public:int size;datas<T>* value;TPSet() {this->size = 0;this->value = NULL;}void TPSet::add(T child){datas<T> *temp = this->value;if (this->size == 0){this->value = new datas<T>;this->value->Value = child;this->value->next = NULL;}else{while (1){if (temp->next == NULL)break;temp = temp->next;}temp->next = new datas<T>;temp->next->Value = child;temp->next->next = NULL;}this->size++;}T get(int id){if (id >= this->size || this->value == NULL)return 0;datas<T> *temp = this->value;for (int i = 0; i < id&&temp != NULL; i++){temp = temp->next;}return temp->Value;}void remove(T num){int id = -1;datas<T> *temp = this->value;for (int i = 0; i < this->size; i++){if (temp->Value == num){id = i;break;}temp = temp->next;}if (id==-1||id >= this->size || this->value == NULL)return;temp = this->value;if (id == 0){this->value = this->value->next;delete temp;}else{datas<T> *tp;for (int i = 1; i < id; i++){temp = temp->next;}tp = temp->next->next;delete temp->next;temp->next = tp;}this->size--;}void TPSet::clear(){if (this->size <= 0)return;datas<T>*sp = this->value;datas<T>*tp;for (int i = 0; i < this->size; i++){tp = sp->next;delete sp;sp = tp;}this->size = 0;}bool contains(T t){if (this->size <= 0)return false;datas<T>*sp = this->value;for (int i = 0; i < this->size; i++){if (sp->Value == t)return true;sp = sp->next;}return false;}~TPSet(){clear();}
};
int getPid(const char *processname);
class GameChanger
{
private:bool canrun;
public:int pid;long Start;HANDLE handle;GameChanger();GameChanger(char *processname);~GameChanger();void SetProcessName(char *processname);template<class T>int GetValue(TPSet<int> *value){if (!this->canrun)return -1;T tmp = (T)this->Start;int len = sizeof(T);for (int i = 0; i < value->size; i++){tmp += value->get(i);ReadProcessMemory(this->handle, (LPVOID)tmp, &tmp, len, 0);}return tmp;}template<class T>void Change(TPSet<int> *value, T Value){if (!this->canrun)return;long tmp =this->Start;int len = sizeof(T);for (int i = 0; i < value->size - 1; i++){tmp += value->get(i);ReadProcessMemory(this->handle, (LPVOID)tmp, &tmp, len, 0);}tmp += value->get(value->size - 1);WriteProcessMemory(this->handle, (LPVOID)tmp, &Value, len, 0);}template<class T>void Change_pos(long tmp,T value){int len = sizeof(T);WriteProcessMemory(this->handle, (LPVOID)tmp, &value, len, 0);}
};
#include"Changer.h"
#include<TlHelp32.h>
#include <psapi.h>
#pragma comment(lib,"psapi.lib")
#include<assert.h>
int getPid(const char *processname)
{HANDLE help = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);PROCESSENTRY32 pe32;pe32.dwSize = sizeof(pe32);BOOL ret = Process32First(help, &pe32);int count = 0;count++;while (ret){ret = Process32Next(help, &pe32);if (!ret)break;if (!strcmp(pe32.szExeFile, processname)){CloseHandle(help);return pe32.th32ProcessID;}}CloseHandle(help);return -1;
}
void GameChanger::SetProcessName(char *processname)
{this->pid = getPid(processname);assert(this->pid != -1);this->handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, this->pid);HMODULE hModule[100] = { 0 };DWORD dwRet = 0;BOOL bRet = EnumProcessModules(handle, (HMODULE*)(hModule),sizeof(hModule), &dwRet);assert(bRet);Start = (DWORD)hModule[0];
}
GameChanger::GameChanger(char *processname)
{canrun = true;this->SetProcessName(processname);
}
GameChanger::GameChanger()
{canrun = false;
}
GameChanger::~GameChanger()
{CloseHandle(this->handle);
}
使用案例
#include<iostream>
#include"Changer.h"
void main()
{GameChanger s("MonsterHunterRise.exe");//扫描怪物猎人内存long t1 = 0x5B6A7B24;s.Change_pos<DWORD>(t1,6);//修改为6
}