[英]How can I read back from a buffer using ctypes?
I have a third-party library, and I need to use one function in python script from it.我有一个第三方库,我需要在 python 脚本中使用一个 function。 Here it is:这里是:
ReadFromBlob(PVOID blob, INT blob_size, PCSTR section, PCSTR key, const void **buffer, UINT * size)
The documentation gives an example of how to use it:该文档给出了如何使用它的示例:
UINT size = 0;
PVOID buffer = NULL;
ReadFromBlob(<blob>, <blob_size>, "MainImage", "Image", &buffer, &size);
I'm not familiar with C, so argument types confusing me.我不熟悉 C,所以参数类型让我感到困惑。 I need to be able to read values from the buffer in python. This is what I have so far:我需要能够从 python 中的缓冲区中读取值。这是我目前所拥有的:
from ctypes import *
lib = cdll.LoadLibrary(path_to_lib)
with open(filepath, 'rb') as file:
data = file.read()
blob_size = c_int(len(data))
blob = cast(c_char_p(data), POINTER(c_char * blob_size.value))
b = bytes()
size = c_uint(len(b))
buffer = cast(cast(b, c_void_p), POINTER(c_char * size.value))
lib.ReadFromBlob(blob, blob_size, b"MainImage", b"Image", buffer, pointer(size))
But I still get an empty buffer in the end.但最后我仍然得到一个空缓冲区。 Please help me.请帮我。
It looks like the function searches the blob for data based on the section and key and returns a pointer into the blob data and a size, so I made a test function that just echoes back the blob and size as the output parameters:看起来 function 根据部分和键在 blob 中搜索数据并返回指向 blob 数据和大小的指针,所以我做了一个测试 function,它只是将 blob 和大小回显为 output 参数:
#include <windows.h>
#include <stdio.h>
__declspec(dllexport)
void ReadFromBlob(PVOID blob, INT blob_size, PCSTR section, PCSTR key, const void **buffer, UINT * size) {
printf("section=\"%s\" key=\"%s\"\n",section,key);
*buffer = blob; // just echo back input data for example
*size = (UINT)blob_size;
}
The types look like Windows types, and ctypes
has a submodule wintypes
with Windows definitions that help get the types right.这些类型看起来像 Windows 类型, ctypes
有一个子模块wintypes
,其中包含 Windows 定义,有助于正确获取类型。 Make sure to set the .argtypes
and .restype
correctly with parallel ctypes
types for the Windows types.确保使用 Windows 类型的并行ctypes
类型正确设置.argtypes
和.restype
。 This helps ctypes
check that arguments are passed correctly.这有助于ctypes
检查 arguments 是否正确传递。
import ctypes as ct
from ctypes import wintypes as w
dll = ct.CDLL('./test')
# Note the parallels between C types and ctypes types.
# PVOID is just "pointer to void" and LPVOID mean the same thing, etc.
dll.ReadFromBlob.argtypes = w.LPVOID,w.INT,w.LPCSTR,w.LPCSTR,ct.POINTER(w.LPCVOID),w.LPUINT
dll.ReadFromBlob.restype = None
# storage for returned values, passed by reference as output parameters
buffer = w.LPCVOID()
size = w.UINT()
dll.ReadFromBlob(b'filedata',8,b'Section',b'Key',ct.byref(buffer),ct.byref(size))
print(ct.cast(buffer,ct.c_char_p).value,size.value)
Output showing the received section and key, and printing the returned blob data and size: Output 显示接收到的部分和密钥,并打印返回的 blob 数据和大小:
section="Section" key="Key"
b'filedata' 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.