我想将ByteArray变量从Python程序传递到用C编写的DLL中,以加速某些在Python中太慢的特定处理。我浏览过网络,试过用byref,cast,memoryviews,addressof组合的Ctypes,但是没有任何效果。有没有什么简单的方法可以做到这一点,而不必把我的旁白复制到其他可以通过的东西中? 以下是我要做的:
/* My C DLL */
__declspec(dllexport) bool FastProc(char *P, int L)
{
/* Do some complex processing on the char buffer */
;
return true;
}
# My Python program
from ctypes import *
def main(argv):
MyData = ByteArray([1,2,3,4,5,6])
dll = CDLL('CHELPER.dll')
dll.FastProc.argtypes = (c_char_p, c_int)
dll.FastProc.restype = c_bool
Result = dll.FastProc(MyData, len(MyData))
print(Result)
但是在将第一个参数(MyData)传递给C函数时,我得到了一个类型错误。在
有没有不需要太多开销的解决方案会浪费C函数的优点?在
奥利维尔
我假设} 创建一个可变字符缓冲区,它是}之间的转换是快速有效的。在
ByteArray
应该是bytearray
。我们可以使用^{ctypes
的ctypes
数组。但是create_string_buffer
不会接受bytearray
,我们需要给它传递一个bytes
对象来初始化它;幸运的是,bytes
和{我没有你的DLL,所以为了测试数组的行为是否正确,我将使用^{} 函数来洗牌它的字符。在
典型输出
^{pr2}$相关问题 更多 >
编程相关推荐