Memory Functions

From SolarStrike wiki
Revision as of 18:50, 19 November 2012 by Elverion (talk | contribs) (memoryWriteStringPtr)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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


Wow! Great thikning! JK

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


memoryReadDouble

number memoryReadDouble(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.


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


memoryWriteString

number memoryWriteString(handle, address, data)

Writes a string to the specified process handle at the specified address. 'data' will be the string you want to write to memory. You must be very careful that the string is not too long and overwrites memory that is not reserved for the string array or you may cause the target process to crash or glitch.

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 "abc" to 0x0D2E0F90 on MyProcess
myData = "abc";
success = memoryWriteString(myProcess, 0x0D2E0F90, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryWriteStringPtr

number memoryWriteStringPtr(handle, address, offset, data)

Writes a string to the specified process handle at the specified address + offset. 'data' will be the string you want to write to memory. If you do not need an offset, call this function with the offset parameter as 0. You must be very careful that the string is not too long and overwrites memory that is not reserved for the string array or you may cause the target process to crash or glitch.

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 "abc" to (*0x0D2E0F90) + 0xA8 on MyProcess
myData = "abc";
success = memoryWriteStringPtr(myProcess, 0x0D2E0F90, 0xA8, myData);
if( success == false ) then 
  printf("Failed to write memory\n");
end


memoryReadBatch

table memoryReadBatch(handle, address, format)

Reads a chunk of memory, using 'format' to describe length and layout, in a single memory read. If you are reading a large set of variables from a contiguous memory region, this is much faster than individual reads.

'format' should be a string consisting of different characters to denote which variable type you would like to read and numbers to denote the count of variables to read. For example "10i" reads 10 4-byte integers. Upper-case characters will be used for unsigned types where available. If no variables are read, an empty table is returned.

Any variable returned from this function will be sequential in order of the format and returned as a table.

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


Types:

b 1 byte
B 1 unsigned byte
s 2-byte Short
S Unsigned 2-byte short
i 4-byte integers
I Unsigned 4-byte integers
f 4-byte floating-point integer
c A string of characters of a given length
_ Skip a byte (Does not return any variable)

Examples of formats: "iiif" - Returns 3 integers and a float. "4i2f8_s" - Returns 4 integers, 2 floats, skips 4 bytes, then returns a short. "16c" - Returns a string up to 16 characters long.


Example

-- Read as a standard table:
vars = memoryReadBatch(myProcess, 0x0D2E0F90, "10i"); --Read and return a table of 10 integers
for i,v in pairs(vars) do
  print(i, v);
end

-- Read and unpack:
local var1, var2, var3 = unpack(memoryReadBatch(myProcess, 0x0D2E0F90, "3i")); -- Read and return 3 integers