Difference between revisions of "Memory Functions"

From SolarStrike wiki
Jump to: navigation, search
(memoryWriteFloatPtr)
Line 2: Line 2:
  
 
The difference between signed and unsigned types is also important. Signed types are those which can be positive or negative. Unsigned types are not denoted as either positive or negative, and so can encompass larger numbers. For example, a byte (also known as a char [character]), can hold a value of -128 to +127. An unsigned byte can hold a value of 0 to 255. If you mix the reading/writing of signed with unsigned, the values will appear erroneous. That is because signed -125 would be equal to unsigned 3.
 
The difference between signed and unsigned types is also important. Signed types are those which can be positive or negative. Unsigned types are not denoted as either positive or negative, and so can encompass larger numbers. For example, a byte (also known as a char [character]), can hold a value of -128 to +127. An unsigned byte can hold a value of 0 to 255. If you mix the reading/writing of signed with unsigned, the values will appear erroneous. That is because signed -125 would be equal to unsigned 3.
 +
 +
== Note on memory reading functions ==
 +
All memory reading functions return a single value: either a number or string. Functions such as memoryReadInt() or memoryReadFloat() will return a number, while functions like memoryReadString() will return strings. If, for any reason, these functions fail, they will return a nil value, unlike the return values for memory writing functions.
 +
 +
 +
== Note on memory read/write pointer functions ==
 +
Memory functions that deal with memory pointers, such as memoryReadPtr() or memoryWritePtr(), can also accept a table of offsets (representing a chain of pointers) rather than a single offset. An example of this shortcut is given below:
 +
 +
'''Short example:'''
 +
<source lang="lua">
 +
offsets = {0x04, 0x24, 0x4C};
 +
HP = memoryReadIntPtr(myProcess, 0x004D2400, offsets);
 +
</source>
 +
 +
'''Long example:'''
 +
<source lang="lua">
 +
offsets = {0x04, 0x24, 0x4C};
 +
tmp1 = memoryReadIntPtr(myProcess, 0x004D2400, offsets[1]);
 +
tmp2 = memoryReadIntPtr(myProcess, tmp1, offsets[2]);
 +
HP = memoryReadInt(myProcess, tmp2 + offsets[3]);
 +
-- Notice how for the last memory read operation we add offset 3 rather than use a pointer read function
 +
</source>
 +
  
 
== memoryReadByte ==
 
== memoryReadByte ==

Revision as of 21:36, 17 December 2009

Note that many of the functions here are very similar to each other. The only real difference between functions like memoryReadByte() and memoryReadInt() is the size (number of bytes) they read from the target process's memory space. You should use memory searching/editing software such as ArtMoney or Cheat Engine to figure out the size of the data type you are trying to read before you use these functions; reading the wrong number of bytes can result in erroneous information.

The difference between signed and unsigned types is also important. Signed types are those which can be positive or negative. Unsigned types are not denoted as either positive or negative, and so can encompass larger numbers. For example, a byte (also known as a char [character]), can hold a value of -128 to +127. An unsigned byte can hold a value of 0 to 255. If you mix the reading/writing of signed with unsigned, the values will appear erroneous. That is because signed -125 would be equal to unsigned 3.

Note on memory reading functions

All memory reading functions return a single value: either a number or string. Functions such as memoryReadInt() or memoryReadFloat() will return a number, while functions like memoryReadString() will return strings. If, for any reason, these functions fail, they will return a nil value, unlike the return values for memory writing functions.


Note on memory read/write pointer functions

Memory functions that deal with memory pointers, such as memoryReadPtr() or memoryWritePtr(), can also accept a table of offsets (representing a chain of pointers) rather than a single offset. An example of this shortcut is given below:

Short example:

offsets = {0x04, 0x24, 0x4C};
HP = memoryReadIntPtr(myProcess, 0x004D2400, offsets);

Long example:

offsets = {0x04, 0x24, 0x4C};
tmp1 = memoryReadIntPtr(myProcess, 0x004D2400, offsets[1]);
tmp2 = memoryReadIntPtr(myProcess, tmp1, offsets[2]);
HP = memoryReadInt(myProcess, tmp2 + offsets[3]);
-- Notice how for the last memory read operation we add offset 3 rather than use a pointer read function


memoryReadByte

number memoryReadByte(handle, address)

Reads and returns a single byte (-128 -> +127) from the specified process handle and address. The specified address may be in either decimal or hexadecimal form.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will read 0x0D2E0F90 on MyProcess and store it into HP
HP = memoryReadByte(myProcess, 0x0D2E0F90);
if( HP <= 100 ) then 
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUByte

number memoryReadUByte(handle, address)

Exactly like memoryReadByte(), except it will return an unsigned byte (0 -> 255) from the specified process handle and address.


memoryReadShort

number memoryReadShort(handle, address)

Reads a short (2 bytes, -32,768 -> +32,767) from the specified process handle and address.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into HP
HP = memoryReadShort(myProcess, 0x0D2E0F90);
if( HP <= 100 ) then 
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUShort

number memoryReadUShort(handle, address)

Exactly like memoryReadShort(), except it will return an unsigned short (0 -> 65,535) from the specified process handle and address.


memoryReadInt

number memoryReadInt(handle, address)

Reads an int (4 bytes, -2,147,483,648 -> +2,147,483,647) from the specified process handle and address.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into HP
HP = memoryReadInt( myProcess, 0x0D2E0F90);
if( HP <= 100 ) then 
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUInt

number memoryReadUInt(handle, address)

Exactly like memoryReadInt(), except it will return an unsigned int (0 -> 4,294,967,295) from the specified process handle and address.


memoryReadFloat

number memoryReadFloat(handle, address)

Reads a float (4 bytes; floating point integer - may contain values such as 1.5 or 192083.0) from the specified handle and address.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into HP
HP = memoryReadFloat( myProcess, 0x0D2E0F90);
if( HP <= 100 ) then 
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadFloat

number memoryReadFloat(handle, address)

Reads a double (8 bytes; floating point integer - may contain values such as 1.5 or 192083.0) from the specified handle and address.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into HP
HP = memoryReadFloat( myProcess, 0x0D2E0F90);
if( HP <= 100 ) then 
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadString

string memoryReadString(handle, address)

string memoryReadString(handle, address, length)

Reads a NULL-terminated string from the specified process handle and address. Make sure it's NULL-terminated (or use optional 'length' parameter), or you will get an infinite loop!

If 'length' is specified, then this function will read up to 'length' characters (not including NULL-terminator). The NULL-terminator will be appended for you in this case.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into name
name = memoryReadString( myProcess, 0x0D2E0F90);
printf("Your character\'s name is %s\n", name);


memoryReadUString

string memoryReadUString(handle, address)

string memoryReadUString(handle, address, length)

Reads a NULL-terminated Unicode string from the specified process handle and address. Make sure it's NULL-terminated (or use optional 'length' parameter), or you will get an infinite loop!

This function will also return a second value: true or false. If the value is true, the function was successful, otherwise, it has failed to read the memory. In the case of a failure, the first value returned will also be 0.

If 'length' is specified, then this function will read up to 'length' characters (not including NULL-terminator). The NULL-terminator will be appended for you in this case.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read 0x0D2E0F90 on MyProcess and store it into name
name = memoryReadUString( myProcess, 0x0D2E0F90);
printf("Your character\'s name is %s\n", name);


memoryReadBytePtr

number memoryReadBytePtr(handle, address, offset)

Reads a single byte from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadByte except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadBytePtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUBytePtr

number memoryReadUBytePtr(handle, address)

Exactly like memoryReadBytePtr(), except it will return an unsigned byte from the specified process handle and address.


memoryReadShortPtr

number memoryReadShortPtr(handle, address, offset)

Reads a short from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadShort except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadShortPtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUShortPtr

number memoryReadUShortPtr(handle, address)

Exactly like memoryReadShortPtr(), except it will return an unsigned short from the specified process handle and address.


memoryReadIntPtr

number memoryReadIntPtr(handle, address, offset)

Reads an int from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadInt except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadIntPtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadUIntPtr

number memoryReadUIntPtr(handle, address)

Exactly like memoryReadIntPtr(), except it will return an unsigned int from the specified process handle and address.


memoryReadUIntPtr

number memoryReadIntPtr(handle, address, offset)

Reads an unsigned int from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadUInt except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadUIntPtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadFloatPtr

number memoryReadFloatPtr(handle, address, offset)

Reads a float from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadFloat except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadUIntPtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadDoublePtr

number memoryReadDoublePtr(handle, address, offset)

Reads a double from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadDouble except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0.

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into HP
HP = memoryReadUIntPtr( myProcess, 0x0D2E0F90, 0xA8);
if( HP <= 100 ) then
  printf("Hp[%d] dropping too low!",HP);
end


memoryReadStringPtr

string memoryReadStringPtr(handle, address, offset)

string memoryReadStringPtr(handle, address, offset, length)

Reads a NULL-terminated string from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadString except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0. Make sure it's NULL-terminated (or use optional 'length' parameter), or you will get an infinite loop!

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into name
name =  memoryReadStringPtr( myProcess, 0x0D2E0F90, 0xA8);
printf("Your character\'s name is", name);


memoryReadUStringPtr

string memoryReadUStringPtr(handle, address, offset)

string memoryReadUStringPtr(handle, address, offset, length)

Reads a NULL-terminated Unicode string from the specified address + offset from process denoted by 'handle'. This is exactly like memoryReadUString except that it reads from a pointer. If you do not need an offset, call this function with the offset parameter as 0. Make sure it's NULL-terminated (or use optional 'length' parameter), or you will get an infinite loop!

'handle' should be the variable that you obtained using openProcess().

Example

-- This will read (*0x0D2E0F90) + 0xA8 on MyProcess and store it into name
name =  memoryReadUStringPtr( myProcess, 0x0D2E0F90, 0xA8);
printf("Your character\'s name is", name);


memoryWriteByte

number memoryWriteByte(handle, address, data)

Writes a single byte (signed or unsigned; it doesn't mater while writing) to the specified process handle at the specified address. 'data' will be the value you want to write to memory.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to 0x0D2E0F90 on MyProcess
myData = 123;
success = memoryWriteByte(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteShort

number memoryWriteShort(handle, address, data)

Writes a short (signed or unsigned; it doesn't mater while writing) to the specified process handle at the specified address.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to 0x0D2E0F90 on MyProcess
myData = 123;
success = memoryWriteShort(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteInt

number memoryWriteInt(handle, address, data)

Writes an int (signed or unsigned; it doesn't mater while writing) to the specified process handle at the specified address. 'data' will be the value you want to write to memory.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to 0x0D2E0F90 on MyProcess
myData = 123;
success = memoryWriteInt(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteFloat

number memoryWriteFloat(handle, address, data)

Writes a float (signed or unsigned; it doesn't mater while writing) to the specified process handle at the specified address. 'data' will be the value you want to write to memory.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123.5 to 0x0D2E0F90 on MyProcess
myData = 123.5;
success = memoryWriteFloat(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end

memoryWriteDouble

number memoryWriteDouble(handle, address, data)

Writes a double (signed or unsigned; it doesn't mater while writing) to the specified process handle at the specified address. 'data' will be the value you want to write to memory.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123.5 to 0x0D2E0F90 on MyProcess
myData = 123.5;
success = memoryWriteDouble(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteBytePtr

number memoryWriteBytePtr(handle, address, offset, data)

Writes a single byte to the specified process handle at the specified address + offset. 'data' will be the value you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = 123;
success = memoryWriteBytePtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteShortPtr

number memoryWriteShortPtr(handle, address, offset, data)

Writes a short to the specified process handle at the specified address + offset. 'data' will be the value you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = 123;
success = memoryWriteShortPtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteIntPtr

number memoryWriteIntPtr(handle, address, offset, data)

Writes an int to the specified process handle at the specified address + offset. 'data' will be the value you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123 to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = 123;
success = memoryWriteIntPtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteFloatPtr

number memoryWriteFloatPtr(handle, address, offset, data)

Writes a float to the specified process handle at the specified address + offset. 'data' will be the value you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123.5 to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = 123.5;
success = memoryWriteFloatPtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteDoublePtr

number memoryWriteDoublePtr(handle, address, offset, data)

Writes a double to the specified process handle at the specified address + offset. 'data' will be the value you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0.

This function will return true or false. If the value is true, the function was successful, otherwise, it has failed to write to memory.

'handle' should be the variable that you obtained using openProcess().


Example

-- This will write 123.5 to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = 123.5;
success = memoryWriteDoublePtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end