[英]How to pass a python list to C function (dll) using ctypes
背景
我在Python中有一些分析软件,我必须将4096个字节的列表( 看起来像这样 [80, 56, 49, 50, 229, 55, 55, 0, 77, ......]
)传递给dll ,以便dll将其写入设备。
必须从python调用的c函数(在dll中)是
int _DLL_BUILD_ IO_DataWrite(HANDLE hDevice, unsigned char* p_pBuff, unsigned char p_nByteCntInBuff);
我无法访问dll代码
方法尝试
我试图声明一个数据类型
data_tx = (ctypes.c_uint8 * len(data))(*data)
并称为功能
ret = self.sisdll.IO_DataWrite(self.handle, ctypes.byref(data_tx), ctypes.c_uint8(pending_bytes))
问题
似乎没有错误,但无法正常工作。 API调用适用于C和C ++。
我做对了吗? 有人能为我指出错误吗?
您可以尝试实现的目标可以像这样完成。
接口头,例如functions.h
#include <stdint.h>
#include "functions_export.h" // Defining FUNCTIONS_API
FUNCTIONS_API int GetSomeData(uint32_t output[32]);
C源代码,functions.c
#include "functions.h"
int GetSomeData(uint32_t output[32]) {
output[0] = 37;
}
在python中,您只需编写
import ctypes
hDLL = ctypes.cdll.LoadLibrary("functions.dll")
output = (ctypes.c_uint32 * 32)()
hDLL.GetSomeData(ctypes.byref(output))
print(output[0])
您应该在屏幕上看到数字37。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.