Modification of game memory using DELPHI

Source: Internet
Author: User
To modify the specified address data of a specified program, we need to use two api functions: ReadProcessMemory and WriteProcessMemory.

Download is the definition of the function:

BOOL ReadProcessMemory (
HANDLE hProcess,
LPCVOID lpBaseAddress,
LPVOID lpBuffer,
SIZE_T nSize,
SIZE_T * lpNumberOfBytesRead
);
Parameters

HProcess
[In] A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process.
LpBaseAddress
[In] A pointer to the base address in the specified process from which to read. before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.
LpBuffer
[Out] A pointer to a buffer that contains es the contents from the address space of the specified process.
NSize
[In] The number of bytes to be read from the specified process.
LpNumberOfBytesRead
[Out] A pointer to a variable that sums es the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.
Return Value

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.

Writeprocessmemory

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 (
Handle hprocess,
Lpvoid lpbaseaddress,
Lpcvoid lpbuffer,
Size_t nsize,
Size_t * lpnumberofbyteswritten
);
Parameters

Hprocess
[In] a handle to the process memory to be modified. The handle must have process_vm_write and process_vm_operation access to the process.
Lpbaseaddress
[In] A pointer to the base address in the specified process to which data is written. before data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for write access, and if it is not accessible, the function fails.
LpBuffer
[In] A pointer to the buffer that contains data to be written in the address space of the specified process.
NSize
[In] The number of bytes to be written to the specified process.
Lpnumberofbyteswritten
[Out] a pointer to a variable that represents es the number of bytes transferred into the specified process. This parameter is optional. If lpnumberofbyteswritten is null, the parameter is ignored.
Return Value

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.

 

 

The following uses the ore modification of Starcraft as an example to describe the usage of these two functions.

Obtain the current number of ore with readprocessmemory

Readprocessmemory (H, PTR (Golda + I * 4), @ gold, 4, TT );

H is the handle of the program process, where Golda is the base value of the address offset, @ gold is a byte array buffer, and the read data is stored in it, the next 4 represents the buffer length, and the last TT is the outgoing value, which shows the length of the successful read.

Now we have read the data. After modifying the value of @ gold, write it back and use the writeprocessmemory method.

Writeprocessmemory (H, PTR (Golda + I * 4), @ gold, 4, TT );

The read process is exactly the same as the read process above, so that the data can be written back.

The following is a complete code

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.