Python 使用 ctypes 传递参数,参数无效

标签 python c pointers ctypes .so

我是 python 的新手,最近使用 python 编程 CCD 相机拍照。我想使用 ctypes 在 .so 文件中调用一个函数。 函数原型(prototype)为:

INT is_ImageFile (HIDS hCam, UINT nCommand, void* pParam, UINT cbSizeOfParam)

HIDS 是 uint 类型。官方用c代码给出的例子是:

is_ImageFile(m_hCam, IS_IMAGE_FILE_CMD_SAVE, (void*)&ImageFileParams,
                   sizeof(ImageFileParams));

其中 ImageFileParams 是结构类型:

typedef struct{
wchar_t* pwchFileName;
UINT     nFileType;
UINT     nQuality;
char**   ppcImageMem;
UINT*    pnImageID;
BYTE     reserved[32];}IMAGE_FILE_PARAMS;

在我的 .py 文件中,我尝试以这种方式定义结构:

class IMAGE_FILE_PARAMS(Structure):
_fields_=[("pwchFileNmae",c_wchar_p),("nFileType",c_uint),("nQuality",c_uint),("ppcImageMem",POINTER(c_char_p)),("pnImageID",POINTER(c_char_p)),("reserved",c_byte*32)]

并以这种方式定义一些成员:

ImageFileParams.pwchFileName = c_wchar_p("/home/user/aa.jpg")
ImageFileParams.nQuality=c_uint(80)
ImageFileParams.nFileType=c_uint(1)

然后调用函数:

is_ImageFile(hCam, IS_IMAGE_FILE_CMD_SAVE, cast(pointer(ImageFileParams),c_void_p),c_uint(sizeof(ImageFileParams)))

但我总是收到一个错误,指示参数无效。有什么问题?

最佳答案

我相信您是因为最后一个参数而收到无效参数错误:

c_uint(sizeof(ImageFileParams))

我检查了文档,some example codes在他们的 IDS 网站上,我认为你需要这样调用它(假设你导入他们的 ueye 库:

is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, ImageFileParams,ueye.sizeof(ImageFileParams))

关于Python 使用 ctypes 传递参数,参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489118/

相关文章:

python - Selenium 防止重定向

c++ - 将udp数据包发送到本地局域网很奇怪

python - DataFrame.to_json() 的段错误

c - 这里出了什么问题?我没有连接我的字符串?

c - 为什么这个解n皇后的复杂度这么大?

c - 如何使用C中的指针将十六进制转换为二进制

c - ANSI C 中的别名 : Is a = (double *) (&a) allowed

在 R 中滚动你自己的链表/树?

python - 将 ctypes 与 jython 一起使用

Python:从路径中提取文件夹名称