srounet / Pymem

A python library for windows, providing the needed functions to start working on your own with memory editing.
MIT License
303 stars 45 forks source link

add read_unicode_string #124

Closed qop1832 closed 5 months ago

qop1832 commented 5 months ago
def read_unicode_string(self, address):
    if address == 0:
        return ''

    if not self.pm.process_handle:
        raise pymem.exception.ProcessError('You must open a process before calling this method')

    buffer = bytearray()
    length = 0
    try:
        while True:
            data = self.pm.read_bytes(address + length, 2)
            buffer += data
            length += 2
            if len(data) < 2 or (data[1] == 0x00 and data[0] == 0x00):
                break
    except:
        pass

    return buffer.decode('utf-16-le', errors='ignore').rstrip('\x00')
StarrFox commented 5 months ago

you can just pass the utf-16 encoding to read_string added in https://github.com/srounet/Pymem/pull/121

qop1832 commented 5 months ago

你可以将 utf-16 编码传递给#121中添加的 read_string

read_string(addr,encoding="utf-16-le")会报错.

StarrFox commented 5 months ago

are you using the latest version?