[C++] 纯文本查看 复制代码
#include <iostream>
#include <windows.h>
#include <thread>
#include <unordered_map>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <map>
#include <mutex>
using namespace std;
// 共享数据和互斥锁
unordered_map<string, bool> flag_lock = {
{"sun_lock", false},
{"cd_lock", false}
};
string sun_lock_key = "sun_lock";
string cd_lock_key = "cd_lock";
unordered_map<int, bool> h_keys = {
{0, false},
{1, false}
};
// 互斥锁用于保护共享数据
mutex mtx;
// 注册热键
void register_hotkeys() {
// 注册 F1 热键
if (RegisterHotKey(NULL, 0, 0, VK_F1)) {
cout << "注册热键 F1 成功." << endl;
} else {
cerr << "注册热键 F1 失败,错误代码: " << GetLastError() << endl;
}
// 注册 F2 热键
if (RegisterHotKey(NULL, 1, 0, VK_F2)) {
cout << "注册热键 F2 成功." << endl;
} else {
cerr << "注册热键 F2 失败,错误代码: " << GetLastError() << endl;
}
}
// 热键处理函数
void process_hotkeys() {
lock_guard<mutex> lock(mtx); // 锁定互斥锁以保护共享数据
if (h_keys[0]) {
flag_lock[sun_lock_key] = !flag_lock[sun_lock_key];
cout << "阳光修改状态: " << flag_lock[sun_lock_key] << endl;
h_keys[0] = false; // 重置热键状态
}
if (h_keys[1]) {
flag_lock[cd_lock_key] = !flag_lock[cd_lock_key];
cout << "植物无CD状态: " << flag_lock[cd_lock_key] << endl;
h_keys[1] = false; // 重置热键状态
}
}
// 热键处理线程函数
void hotkey_listener() {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_HOTKEY) {
if (msg.wParam < 2) {
{
lock_guard<mutex> lock(mtx); // 锁定互斥锁以保护共享数据
h_keys[msg.wParam] = true;
}
cout << "捕获到热键: " << msg.wParam << endl; // 打印捕获的热键
process_hotkeys(); // 立即处理热键
}
}
}
}
// 修改阳光数量的函数
void sun_mod(HANDLE p_hwnd, long sun_num = 9999) {
LONG data_1, data_2, data_3;
SIZE_T bytesRead;
// 直接修改内存数据
if (!ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead)) {
cerr << "读取 data_1 失败,错误代码: " << GetLastError() << endl;
return;
}
if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x5560), &data_2, sizeof(data_2), &bytesRead)) {
cerr << "读取 data_2 失败,错误代码: " << GetLastError() << endl;
return;
}
if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x768), &data_3, sizeof(data_3), &bytesRead)) {
cerr << "读取 data_3 失败,错误代码: " << GetLastError() << endl;
return;
}
if (!WriteProcessMemory(p_hwnd, (LPVOID)data_3, &sun_num, sizeof(sun_num), &bytesRead)) {
cerr << "写入阳光失败,错误代码: " << GetLastError() << endl;
return;
}
cout << "阳光修改成功,地址: " << hex << data_3 << ", 新值: " << dec << sun_num << endl;
}
// 修改植物无CD的函数
void cd_mod(HANDLE p_hwnd) {
LONG data_1, data_2, data_3;
SIZE_T bytesRead;
if (!ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead)) {
cerr << "读取 data_1 失败,错误代码: " << GetLastError() << endl;
return;
}
if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x768), &data_2, sizeof(data_2), &bytesRead)) {
cerr << "读取 data_2 失败,错误代码: " << GetLastError() << endl;
return;
}
if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x144), &data_3, sizeof(data_3), &bytesRead)) {
cerr << "读取 data_3 失败,错误代码: " << GetLastError() << endl;
return;
}
for (int i = 0; i < 10; i++) {
LONG writeAddress = data_3 + 0x70 + i * 0x70;
SHORT value = 1;
if (!WriteProcessMemory(p_hwnd, (LPVOID)writeAddress, &value, sizeof(value), &bytesRead)) {
cerr << "写入植物无CD失败,位置: " << hex << writeAddress << ",错误代码: " << GetLastError() << endl;
}
}
cout << "植物无CD修改成功." << endl;
}
// 枚举窗口的回调函数
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
char windowTitle[256];
GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
// 检查窗口标题是否完全匹配
if (strcmp(windowTitle, "植物大战僵尸杂交版v2.5") == 0) { // 修正比较条件
cout << "找到目标窗口,标题: " << windowTitle << endl;
*(HWND*)lParam = hwnd; // 找到窗口,保存句柄
return FALSE; // 停止枚举
}
return TRUE; // 继续枚举
}
// 主循环函数
void main_loop() {
while (true) {
DWORD pid = 0;
HWND hwnd = NULL;
// 枚举窗口以找到目标窗口句柄
EnumWindows(EnumWindowsProc, (LPARAM)&hwnd);
if (hwnd != NULL) {
GetWindowThreadProcessId(hwnd, &pid);
if (pid == 0) {
cerr << "无法获取 PID" << endl;
Sleep(10000);
continue;
}
} else {
cerr << "未找到目标窗口." << endl;
Sleep(10000);
continue;
}
HANDLE p_hwnd = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
cout << "游戏 PID: " << pid << endl;
if (p_hwnd == NULL) {
cerr << "无法打开进程,错误代码: " << GetLastError() << endl;
Sleep(10000);
continue;
} else {
cout << "成功打开进程句柄: " << p_hwnd << endl;
}
// 锁定互斥锁以访问共享数据
{
lock_guard<mutex> lock(mtx);
// 根据状态调用相应的修改函数
if (flag_lock[sun_lock_key]) {
cout << "准备调用 sun_mod" << endl; // 调试输出
sun_mod(p_hwnd);
}
if (flag_lock[cd_lock_key]) {
cout << "准备调用 cd_mod" << endl; // 调试输出
cd_mod(p_hwnd);
}
}
CloseHandle(p_hwnd);
Sleep(3000); // 每3秒检查一次
}
}
int main() {
// 注册热键
register_hotkeys();
// 启动热键监听线程
thread hotkey_thread(hotkey_listener);
hotkey_thread.detach(); // 分离线程
// 启动主循环
main_loop();
// 注销热键(这部分代码实际上不会被执行,因为 main_loop 是无限循环)
UnregisterHotKey(NULL, 0);
UnregisterHotKey(NULL, 1);
return 0;
}