Reads data from an area of memory in a specified process. The entire area to be read must be accessible or the operation fails.
BOOL ReadProcessMemory(If the function succeeds, the return value is nonzero.
If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.
The function fails if the requested read operation crosses into an area of the process that is inaccessible.
Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.
BOOL WriteProcessMemory(If the function succeeds, the return value is nonzero.
If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError. The function fails if the requested write operation crosses into an area of the process that is inaccessible.
相信上面这些都很容易看明白吧?一个用于取出数据,一个用于写入数据。
下面以星际争霸的矿石修改为例,简述这两个函数的用法。
先获取当前的矿石数,用ReadProcessMemory
ReadProcessMemory(h, ptr(GoldA + i * 4), @Gold, 4, tt);
h是程序进程的句柄,其中GoldA就是地址偏移基准数值,@Gold是一个byte型的数组buffer,读取到的数据也就存放在里面,接下来的4表示buffer的长度,最后的tt是传出值,它显示了成功读取的长度。
好了,现在读取到了,我们把@Gold的值进行一番修改后,再写回去,使用WriteProcessMemory方法
WriteProcessMemory(h, ptr(GoldA + i * 4), @Gold, 4, tt);
与上面的Read过程一模一样,这样就能够写回去了。
下面附上一段完整代码:
procedure TFormMain.Cheat113;
var
hw: HWND;
pid: DWord;
h: THandle;
tt: Cardinal;
Gold: array[0..3] of byte;
Gas: array[0..3] of byte;
GoldA: integer;
GasA: integer;
i: integer;
const
Gold130 = $508600;
Gas130 = $508630;
begin
hw := FindWindow(nil, 'Brood War');
if hw = 0 then
Exit;
GetWindowThreadProcessId(hw, @pid);
h := OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if h = 0 then
Exit;
Gold[0] := $FF;
Gold[1] := $FF;
Gold[2] := $00;
Gold[3] := $00;
Gas[0] := $FF;
Gas[1] := $FF;
Gas[2] := $00;
Gas[3] := $00;
GoldA := Gold130;
GasA := Gas130;
if (chkMineral.Enabled) and (chkMineral.Checked) then
begin
for i := 0 to 11 do
begin
WriteProcessMemory(h, ptr(GoldA + i * 4), @Gold, 4, tt);
end;
end;
if (chkGas.Enabled) and (chkGas.Checked) then
begin
for i := 0 to 11 do
begin
WriteProcessMemory(h, ptr(GasA + i * 4), @Gas, 4, tt);
end;
end;
CloseHandle(h);
end;