python - 使用 python ctypes 和 libc 将 void 指针写入二进制文件

标签 python ctypes fwrite void-pointers libc

我正在使用 python ctypes 和 libc 与供应商提供的 DLL 文件进行交互。 DLL 文件的目的是从相机获取图像。

图像采集似乎运行无误;我遇到的问题是访问数据。

图像采集函数将 ctypes.c_void_p 作为图像数据的参数。

简化如下:

"""
typedef struct AvailableData
{
    void* initial_readout;
    int64 readout_count;
} imageData;
"""

class AvailableData(ctypes.Structure):
    _fields_ = [("initial_readout", ctypes.c_void_p), 
                ("readout_count", ctypes.c_longlong)]

"""
Prototype
Acquire(
CamHandle                 camera,
int64                       readout_count,
int                       readout_time_out,
AvailableData*         available,
AcquisitionErrorsMask* errors );
"""

>>> imageData = AvailableData()
>>> Acquire.argtypes = CamHandle, ctypes.c_longlong, ctypes.c_int, 
         ctypes.POINTER(AvailableData), ctypes.POINTER(AcquisitionErrorsMask)
>>> Acquire.restype = ctypes.c_void_p

>>> status = Acquire(camera, readout_count, readout_time_out, imageData, errors)

我不完全理解这个函数到底在做什么,因为在我运行这个函数之后,imageData.initial_readout 似乎是一个“long”类型(甚至不是 ctypes.c_long:只是 '长')。然而,它也有与之相关的值(value)。我假设这是数据存储位置的起始地址。

>>> type(imageData.initial_readout)
<type 'long'>
>>> imageData.initial_readout
81002560L

我目前访问数据的方法是使用 libc.fopen、libc.fwrite、libc.fclose,如下所示:

>>> libc = ctypes.cdll.msvcrt

>>> fopen = libc.fopen
>>> fwrite = libc.fwrite
>>> fclose = libc.fclose
>>> fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p
>>> fopen.restype = ctypes.c_void_p

>>> fopen.restype = ctypes.c_void_p
>>> fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
>>> fwrite.restype = ctypes.c_size_t

>>> fclose = libc.fclose
>>> fclose.argtypes = ctypes.c_void_p,
>>> fclose.restype = ctypes.c_int

>>> fp = fopen('output3.raw', 'wb')
>>> print 'fwrite returns: ',fwrite(ctypes.c_void_p(imageData.initial_readout), readoutstride.value, 1, fp)
fwrite returns:  0
>>> fclose(fp)

其中 readoutstride = 2097152 对应于 16 位像素的 1024x1024 数组。

文件“output3.raw”出现在 Windows 资源管理器中,但是,它有 0 kbytes,当我尝试使用(例如使用图像查看器)打开它时,它说文件是空的。

我看到 fwrite 返回值 0(但应该返回值 1)

如果您对我在这里做错了什么有任何想法,我将不胜感激。提前谢谢你。

最佳答案

指定函数的argtypesrestype

import ctypes

libc = ctypes.windll.msvcrt

fopen = libc.fopen
fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p,
fopen.restype = ctypes.c_void_p

fwrite = libc.fwrite
fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
fwrite.restype = ctypes.c_size_t

fclose = libc.fclose
fclose.argtypes = ctypes.c_void_p,
fclose.restype = ctypes.c_int

fp = fopen('output3.raw', 'wb')
fwrite('Hello', 5, 1, fp)
fclose(fp)

关于python - 使用 python ctypes 和 libc 将 void 指针写入二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257137/

相关文章:

python - 使用python加速将大型数据集从txt文件插入到mySQL

python - 通过乘法创建列表

python - 如何从在C层进行内存分配的python脚本中将float *数组传递给C方法

Python 3.x : AttributeError: 'str' object has no attribute 'append'

python - ctypes 加载具有依赖关系的 c 共享库

python-3.x - python ctypes 支持 size-0 数组吗?

c - 为什么会出现段错误(核心转储)?

python 2.7.3。 . .写入.jpg/.png 图像文件?

c - 使用 fread 从文件中读取奇怪的文本

Python 错误 : PyThreadState_Get: no current thread after compiling an extension