我必须在CTypes中使用DLL中的2个函数。这些函数以void*
为参数。但无论我尝试什么,我都无法使它发挥作用。我收到一个错误,告诉我使用了错误的类型。我查了很多帖子并阅读了文档,但我无法弄清楚。
任何帮助,将不胜感激。我在Windows上使用Python 2.7。
我的C函数是:
void WriteRam(unsigned address, unsigned length, void* buffer)
void ReadRam(unsigned address, unsigned length, void* buffer)
在Python中,我正在尝试将列表传递给函数,如下所示:
audioVolume = 32767
for i in range(buffSize):
txBuff.append(int(audioVolume * math.sin(i)) )
WriteRam(0, 64, txBuff)
我的Python函数是:
WriteRam = DPxDll['DPxWriteRam']
def DPxWriteRam(address=None, length=None, buffer=None):
#test = ctypes.c_void_p.from_buffer(buffer) # not working
#p_buffer = ctypes.cast(buffer, ctypes.c_void_p) # not working
p_buffer = ctypes.cast(ctypes.py_object(buffer), ctypes.c_void_p) # not working
#p_buffer = ctypes.c_void_p() # not working
WriteRam.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p]
WriteRam(address, length, ctypes.byref(p_buffer))
答案 0 :(得分:2)
假设txBuff
是一个整数列表,那么你需要将它们打包成一个数组。以下代码应该可以工作,但我无法测试它......
def DPxWriteRam(address, int_list):
int_size = ctypes.sizeof(ctypes.c_int)
item_count = len(int_list)
total_size = int_size * item_count
packed_data = (ctypes.c_int * item_count)(*int_list)
WriteRam(ctypes.c_uint(address), ctypes.c_uint(total_size), packed_data)
DPxWriteRam(whatever, [0, 1, 2, 3])
...虽然如果WriteRam
几乎只是做memcpy()
,那么你可以使用它......
import ctypes
libc = ctypes.CDLL('msvcrt.dll')
def DPxWriteRam(address, int_list):
int_size = ctypes.sizeof(ctypes.c_int)
item_count = len(int_list)
total_size = int_size * item_count
packed_data = (ctypes.c_int * item_count)(*int_list)
libc.memcpy(address, packed_data, total_size)
...我可以测试......
>>> l = range(4)
>>> p = libc.malloc(1000)
>>> DPxWriteRam(p, l)
>>> s = ' ' * 16
>>> libc.memcpy(s, p, 16)
>>> print repr(s)
'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'