如何在python中将整数转换为ctypes.c_byte

时间:2016-06-08 17:49:29

标签: python kernel

我正在尝试在Python 3.5中使用Read and Write ProcessMemory形式kernel32.dll

我得到ReadProcessMemory这样的工作:

def ReadMemInt(adress):
    buffer = (ctypes.c_byte * 8)()
    bytesRead = ctypes.c_ulonglong(0)
    if ctypes.windll.kernel32.ReadProcessMemory(processHandle, adress, buffer, len(buffer), ctypes.byref(bytesRead)):
        return struct.unpack('Q', buffer)[0]
    else:
        return -1

现在我有点坚持让WPM工作,因为我不知道如何将我的变量转换成字节......

这是我迄今为止所尝试过的:

def WiteMemInt(adress, Val):
    buffer = ctypes.c_byte(Val)
    bytesRead = ctypes.c_ulonglong(0)
    ctypes.windll.kernel32.WriteProcessMemory(processHandle, adress, buffer, 4, ctypes.byref(bytesRead))

我希望这个问题符合StackOverflow的水平!

1 个答案:

答案 0 :(得分:0)

搞定了:

    def WriteMem(self, adress, Val, type):
        buffer = (ctypes.c_byte * self.GetSize(type))(*struct.pack(type, Val))
        bytesRead = ctypes.c_ulonglong(0)
        if ctypes.windll.kernel32.WriteProcessMemory(self.processHandle, adress, buffer, len(buffer), ctypes.byref(bytesRead)):
            return True
        else:
            return False