如何使用ctypes在python中创建int缓冲区

2024-09-28 03:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python写内存。我需要写一个整数,但是对于WriteProcessMemory函数,我需要一个缓冲区。你知道吗

writeProcessMemory = kernel.WriteProcessMemory
writeProcessMemory.argtypes = [ctypes.wintypes.HANDLE, 
ctypes.wintypes.LPVOID, ctypes.wintypes.LPCVOID, ctypes.c_size_t, 
                         ctypes.POINTER(ctypes.c_size_t)]
writeProcessMemory.restype = ctypes.wintypes.BOOL


openProcess = kernel.OpenProcess
openProcess.argtypes = [ctypes.wintypes.DWORD, ctypes.wintypes.BOOL, 
ctypes.wintypes.DWORD]
openProcess.restype = ctypes.wintypes.HANDLE

handle = openProcess(PROCESS_ALL_ACCESS, False, pid)
addr = 0x024EA498
data = ctypes.c_int(1000)
buffer = #i need to create a buffer here

Tags: 内存sizebuffer整数ctypeskernelboolhandle
1条回答
网友
1楼 · 发布于 2024-09-28 03:23:32

你可以使用^{}

buffer = ctypes.create_string_buffer(b"",1000 * 4)

创建1000个4大小整数的0填充缓冲区。这个缓冲区是ctypes.c_char_Array_4000类型,可以作为指针传递给导入的函数。你知道吗

This function creates a mutable character buffer. The returned object is a ctypes array of c_char.

init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.

If a bytes object is specified as first argument, the buffer is made one item larger than its length so that the last element in the array is a NUL termination character. An integer can be passed as second argument which allows specifying the size of the array if the length of the bytes should not be used.

现在调用导入的函数,获取刚才使用以下命令编写的pythonbytes对象:

python_bytes_array = ctypes.string_at(buffer)

并使用struct获取整数值。不需要指定端点。I是4个字节。如果调用的函数使用4字节整数,则它将起作用:

import struct
integer_tuple = struct.unpack("1000I",python_bytes_array)

相关问题 更多 >

    热门问题