python - Python ctypes 从 void* 创建字节数组
问题描述
我使用 ctypes 来调用 ac 函数
void *crypto_data(void *buffer, int length, int *encrypt_len)
{
void *encrypt = malloc(length+25);
...
*encrypt_len = length + 25;
return encrypt;
}
lib.crypto_data.argtypes = [c_void_p, c_int, c_void_p]
lib.crypto_data.restype = c_void_p
my_len = c_int()
out_buffer = c_int()
out_buffer = lib.crypto_data(gzip_data, len(gzip_data), byref(my_len))
print out_buffer #this out_buffer is the address of malloc
如何从这个分配的内存中创建字节数组?
解决方案
我相信您可以使用 c_char 或 c_uchar 和缓冲区长度创建一个新类型:
mytype = ctypes.c_char * my_len
然后尝试创建from_address
C 函数返回的该类型的实例:
addr = crypto_data(args...)
array_of_char = mytype.from_address(addr)
请参阅此问题where malloc
is used as the source of the data。
推荐阅读
- linux - 在容器内重新启动 Apache2 时出错
- shell - 从 AppleScript 解析文本
- python - 如何将熊猫数据框转换为多索引数据框
- django - 是否可以在包含标签内插入带有变量的字符串?
- javascript - 根据当前视频时间戳显示字幕
- vba - 从 Outlook VBA 下载附件
- javascript - 按下 HTML 按钮不会向 socket.io 发送调用
- c# - 哪种方法最好,制作多个文件还是单个文件?
- html - 带有 JSON 数据的 Angular 8 动态表
- javascript - 如何在本机反应中显示来自两个 Firestore 集合的数据