首页 C/C++修行正文

C++ 之 读取其他进程内存地址,并修改该内存地址的值

欲儿 C/C++修行 2022-07-01 467 0

编译环境:控制台程序,Unicode字符集

代码如下:

#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <Tlhelp32.h>
#include <string>
using namespace std;



/*Char*转TCHAR*/
LPWSTR ConvertCharToLPWSTR(const char* szString)
{
	int dwLen = strlen(szString) + 1;

	int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度

	LPWSTR lpszPath = new WCHAR[dwLen];

	MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);

	return lpszPath;

}




int main()
{
	cout << "请输入窗口标题:";
	string title;
	cin >> title;
	cout << "请输入内存地址16位:";
	string straddress;
	cin >> straddress;
	cout << "请输入修改为多少值(10进制):";
	string newthing;
	cin >> newthing;
	string name = "0x" + straddress;
	DWORD address = std::strtoul(name.c_str(), NULL, 16);//转换为DWORD类型变量
	HWND hWnd;
	LPCWSTR  newititle = ConvertCharToLPWSTR(title.c_str());
	hWnd = FindWindow(NULL, newititle);//根据窗口标题获取窗口句柄
	DWORD PID;
	GetWindowThreadProcessId(hWnd, &PID);//存入PID
	HANDLE hprocess;
	hprocess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);//获取进程
	DWORD value = 0;//用来存原值是多少
	DWORD newvalue = std::strtoul(newthing.c_str(), NULL, 10);//转换为DWORD类型变量
	DWORD size = 0;
	if (false == ReadProcessMemory(hprocess, (void*)address, &value, sizeof(DWORD), &size))
	{
		cout << "读取失败" << endl;
	}
	else
	{
		cout << "原始的值(10进制):" << value << endl;
	}
	if (false == WriteProcessMemory(hprocess, (void*)address, &newvalue, sizeof(DWORD), &size))
	{
		cout << "修改失败" << endl;
	}
	else
	{
		cout << "恭喜你,修改成功!" << endl;
	}

	while (true);


}


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论