Buffers - Rob--/memoryjs GitHub Wiki

Details

Buffers allow us to read and write generic structures to memory. To read and write generic structures to and from memory it requires converting that structure to or from a buffer. We recommend using the concentrate and dissolve libraries mentioned below, but they are not required for reading or writing buffers.

For a complete buffer example, view our buffer example.

Writing a Generic Structure

In order to write a generic structure to memory, we first need to convert the object to a buffer. We recommend using the concentrate library.

// Step 1: Create your object you want to write
const vector = {
  x: 1.23,
  y: 4.56,
  z: 7.89,
};

// Step 2: Create a buffer from the object using the `concentrate` library
const buffer = Concentrate()
  .floatle(vector.x)
  .floatle(vector.y)
  .floatle(vector.z)
  .result();

// Step 3: Write the buffer to memory
memoryjs.writeBuffer(processObject.handle, structAddress, buffer);

Reading a Generic Structure

In order to read a generic structure from memory, we first need to read a buffer from memory and then parse the buffer into an object using concentrate's sister library, dissolve.


// Step 1: Read the buffer from memory, ensuring the `size` parameter is correct
const buffer = memoryjs.readBuffer(processObject.handle, address, size);

// Step 2: Create the buffer parser using the `dissolve` library
const parser = Dissolve().loop(function(end) {
  this
    .floatle("x")
    .floatle("y")
    .floatle("z")
    .tap(function() {
      this.push(this.vars);
      this.vars = {};
    });
});
  
parser.on("readable", function() {
  let e;
  while (e = parser.read()) {
    console.log(e);
  }
});

// Step 3: Write the buffer to the parser to consume
parser.write(buffer);

Functions

readBuffer(handle, address, size[, callback])

  • handle - the handle of the process, retrieved from calling openProcess
  • address - the address in memory to read from
  • size - the number of bytes to read into the buffer
  • callback - has two parameters:
    • error - error message (if one occurred)
    • buffer - the bytes read from memory at the given address in a buffer

returns (if no callback provided): the bytes read from memory at the given address in a buffer

Reads a given number of bytes at a given address in memory into a buffer.

// synchronously
const buffer = memoryjs.readBuffer(processObject.handle, address, size);

// asynchronously
memoryjs.readBuffer(processObject.handle, address, size, (error, buffer) => {

});

writeBuffer(handle, address, buffer)

  • handle - the handle of the process, retrieved from calling openProcess
  • address - the address in memory to write to
  • buffer - the buffer to write to memory

Writes a buffer to memory.

memoryjs.writeBuffer(processObject.handle, address, buffer);