Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ctypes在python中创建int缓冲区_Python_Python 3.x_Memory - Fatal编程技术网

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

如何使用ctypes在python中创建int缓冲区,python,python-3.x,memory,Python,Python 3.x,Memory,我正在尝试用python编写内存。我需要写一个整数,但是对于WriteProcessMemory函数,我需要一个缓冲区 writeProcessMemory = kernel.WriteProcessMemory writeProcessMemory.argtypes = [ctypes.wintypes.HANDLE, ctypes.wintypes.LPVOID, ctypes.wintypes.LPCVOID, ctypes.c_size_t,

我正在尝试用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
你可以用

创建1000个4大小整数的0填充缓冲区。此缓冲区的类型为
ctypes.c\u char\u Array\u 4000
,可以作为指针传递给导入的函数

此函数用于创建可变字符缓冲区。返回的对象是c_char的ctypes数组

init_或_size必须是指定数组大小的整数,或者是用于初始化数组项的字节对象

如果将bytes对象指定为第一个参数,则缓冲区将比其长度大一个项目,以便数组中的最后一个元素是NUL终止字符。整数可以作为第二个参数传递,该参数允许在不应使用字节长度的情况下指定数组的大小

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

python_bytes_array = ctypes.string_at(buffer)
注意
ctypes.string_at(address,size=-1)
如果指定了size,则将其用作size,否则假定字符串以零结尾

并使用
struct
获取整数值。无需指定端点<代码>I为4个字节。如果调用的函数使用4字节整数,则它将起作用:

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

创建一个字符缓冲区,其大小乘以C中int的大小。注意,int的大小不是标准化的,但这不会将字符写入内存吗?抱歉,如果这是一个愚蠢的问题,我只是想理解它如果你打开一个交互式控制台,
导入ctypes
导入struct
并运行你得到的那3行代码
struct。错误:解包需要4000字节的缓冲区
使用
python 3.8.6
import struct
integer_tuple = struct.unpack("1000I",python_bytes_array)