调用c++库时,如何在Python中使用ctypes传递字节作为引用
How do I pass a byte as reference in Python using ctypes when calling a c++ library?
我一直在想如何将一些c++函数导入python。我让它们工作,直到其中一个函数需要通过引用传递它的一个属性,我不知道如何使它工作。我一直在遵循这里给出的建议:https://stackoverflow.com/a/252473/7447849它一直工作得很好,直到我尝试了这个:
我有以下c++功能:
bool __stdcall I2CRead(int Instance, BYTE SlaveAddress, BYTE registerAddress, BYTE* ReadBuff, BYTE Length)
这是我尝试过的代码:
i2cread_proto = ctypes.WINFUNCTYPE (ctypes.c_bool, ctypes.c_int, ctypes.c_byte, ctypes.c_byte, ctypes.c_byte, ctypes.c_byte)
i2cread_Params = (1, "instance", 0),(1, "SlaveAddress", 0),(1, "registerAddress", 0),(1, "ReadBuff", 0),(1, "Length", 0), # (parameter direction (1 for input, 2 for output), parameter name, default value)
i2cread = i2cread_proto (("I2CRead", qsfp_lib), i2cread_Params)
readBuff = ctypes.c_byte(0)
if (i2cread(0, 160, address, readBuff, 1)==True):
print(readBuff)
else:
print('Could not read data')
此代码有效,但readBuff保持给定的默认值不变,而不是按原样更改。
我尝试使用byref((,但仍然无法工作(它给了我一个错误的类型错误(。
我可能做错了什么?我对Python不太熟练,所以可能有一个概念我误解了
未测试。请确保使用正确的参数类型。
from ctypes import *
dll = WinDLL('dllname')
dll.I2CRead.argtypes = c_int,c_ubyte,c_ubyte,POINTER(c_ubyte),c_ubyte
dll.I2CRead.restype = c_bool
output = (c_ubyte * 256)() # Create instance of a c_ubyte array to store the output.
result = dll.I2CRead(1,2,3,output,len(output))
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- Ctypes wstring通过引用传递
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- Python ctypes:不会按预期加载 dll
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 调用c++库时,如何在Python中使用ctypes传递字节作为引用
- 如何将BitString转换为ctypes字节数组