简体   繁体   English

如何使用 ctypes 从缓冲区中读回?

[英]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)
  • blob - some pointer? blob - 一些指针? to bytes to read from到要读取的字节
  • blob_size - blob size in bytes blob_size - 以字节为单位的 blob 大小
  • section and key - string values like "Image"部分和键 - 字符串值,如“图像”
  • buffer - bytes to read to buffer - 要读取的字节
  • size - buffer size 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.

相关问题 如何使用 ctypes 从字节数据中分配动态缓冲区? - How can I malloc a dynamic buffer from byte data with ctypes? 如何从 ctypes.Structure 取回原始缓冲区? - How do I get back original buffer from a ctypes.Structure? 使用Ctypes在C中写入在python中分配的C缓冲区,然后在python中再次读回 - Write buffer in C which is allocated in python using ctypes, and read back again in python 如何在ctypes中从C写入字符串缓冲区? - How do I write to string buffer from C in ctypes? 使用 GetFirmwareEnvironmentVariableW 将 UEFI 变量读入具有 ctypes、WinDLL 和 WinAPI 的缓冲区 - Read a UEFI variable into a buffer with ctypes, WinDLL and WinAPI, using GetFirmwareEnvironmentVariableW 我如何从 ctypes 读取返回指针的值? - How do i read the values of a returned pointer from ctypes? 如何使用ctypes库检查Python中消息框按钮的输入? - How can I check the input from a message box's buttons in Python using the ctypes library? 如何使用 ctypes 找到从 DLL 文件导入的函数所需的参数? - How can I find the required parameters for a function imported from a DLL file using ctypes? 使用ctypes从Windows中的剪贴板中读取文本 - Read text from clipboard in Windows using ctypes 如何使用 OpenCV 从缓冲区读取图像? - How to read image from buffer using OpenCV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM