0

Whenever one of the values I read from memory happens to be a None, no further reading is possible, it will just return empty results, until I close and reopen the process. It happens using this readString function:

def readString(lp_base_address, length):
    try:
        read_buffer = ctypes.create_string_buffer(length)
        lp_number_of_bytes_read = ctypes.c_ulong(0)
        ctypes.windll.kernel32.ReadProcessMemory(self.handle, lp_base_address, read_buffer, length, lp_number_of_bytes_read)
        bufferArray = bytearray(read_buffer)
        found_terminator = bufferArray.find(b'\x00')
        if found_terminator != -1:
            return bufferArray[:found_terminator].decode('utf-8')
        print("[ReadMemory/Error]: terminator not found.\naddress: %s" % hex(lp_base_address))
        return "ERROR"
    except (BufferError, ValueError, TypeError) as error:
        if self.handle:
            self.close()
        self.error_code = self.get_last_error()
        error = {'msg': str(error), 'Handle': self.handle, 'PID': self.pid,
                 'Name': self.name, 'ErrorCode': self.error_code}
        ReadWriteMemoryError(error)

for pointer in listOfManyPointers():
    result = readString(pointer, 50)

This is how it is fixed currently:

for pointer in listOfManyPointers():
    result = readString(pointer, 50)
    if result == None: # TODO fix and remove this block
        process.close()
        connectToProcess()
emilector
  • 35
  • 1
  • 8
  • Not sure what you mean by "reading None". Better to make a [mcve] that can be copied and run with no changes and reproduce the problem, instead of an incomplete snippet of code. – Mark Tolonen Mar 05 '23 at 19:27
  • 1
    Per edit, where is `result` defined and where is `None` returned? The two return values are either a decoded buffer or `"ERROR"`. – Mark Tolonen Mar 05 '23 at 19:29
  • I edited for clarification @MarkTolonen – emilector Mar 05 '23 at 19:36
  • 1
    Again, `None` is not returned from `readString`, and this isn't a [mcve]. – Mark Tolonen Mar 05 '23 at 19:36
  • It definitely returns None. I'm running this exact sample. Not sure how to make a MRE, since you would not have the same pointers/memory on your device? How would I copy that? – emilector Mar 05 '23 at 19:40
  • 1
    In the code shown, there are two `return` statements. Neither are `None`. – Mark Tolonen Mar 05 '23 at 19:41
  • 1
    Even if the MRE only works on your system, seeing code that would actually reproduces your problem is informative. It's also possible (as I did in my [answer](https://stackoverflow.com/a/75644549/235698) to the other question you asked) to refer to memory in Python itself and make a reproducible example. – Mark Tolonen Mar 05 '23 at 19:43
  • I did not think of the exception as edited, sorry... This explains the None return. The error is thrown when executing `res = bufferArray[:found_terminator].decode('utf-8')`. The error message is "invalid start byte". Is there a way to catch this without having to restart the process after? @MarkTolonen – emilector Mar 05 '23 at 19:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252315/discussion-between-mark-tolonen-and-emilector). – Mark Tolonen Mar 05 '23 at 19:54

0 Answers0