首页 > 解决方案 > ReadProcessMemory 无效句柄

问题描述

我正在尝试使用 Python 从另一个进程中读取一个值。

我遇到了这个答案,尽管它似乎不起作用。

我的代码:

from ctypes import *
from ctypes.wintypes import *
OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory
CloseHandle = windll.kernel32.CloseHandle
PROCESS_ALL_ACCESS = 0x1F0FFF
pid = 4580
address = 0x04782FF8
buffer = c_uint()
bufferSize = sizeof(buffer)
bytesRead = c_ulong(0)
processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
    print("Success:", buffer)
else:
    print("Failed.")

CloseHandle(processHandle)

GetLastError()似乎返回 6,这意味着句柄无效。

虽然,OpenProcess()返回一个非零值,并且GetLastError()不显示任何内容。

我尝试编辑传入的第一个参数OpenProcess()(我将其设为 0x0010),但仍然没有结果。

标签: python-3.xwinapi

解决方案


进程 ID 是 dec,而不是 hex,这破坏了一些东西。

我还必须用 create_string_buffer(4) 替换 c_uint() 作为缓冲区。

现在似乎工作正常!


推荐阅读