1

CTypes の DLL から 2 つの関数を使用する必要があります。これらの関数にはvoid*as 引数があります。でも、どう頑張ってもうまくいかない。間違った型を使用しているというエラーが表示されます。多くの投稿を見てドキュメントを読みましたが、わかりません。どんな助けでも大歓迎です。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))
4

1 に答える 1

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'
于 2013-05-21T19:04:08.917 に答える