python3; ctypes的;指向数组的指针

时间:2017-07-09 14:23:13

标签: python pointers dll ctypes

我想通过ctypes模块调用一个函数(AES_crypt),该函数来自xx.dll,这是我的代码(python 3.6.1):

def fillprototype(f, restype, argtypes):
f.restype  = restype
f.argtypes = argtypes

def tuple2pointer(number):
    size     = len(number)
    c_number = (c_ulong * size)(*number)
    cnum     = cast(c_number, POINTER(c_ulong))
    return cnum

type_array_ulong = POINTER(c_ulong)

fillprototype(AES_crypt,        c_ulong, [type_array_ulong, 
type_array_ulong, c_ulong, type_array_ulong, type_array_ulong, 
type_array_ulong, c_ulong, c_ulong] )

iv  = tuple2pointer(iv)
pa  = tuple2pointer(pa)
pt  = tuple2pointer(pt)
ct  = tuple2pointer(ct)
rt  = tuple2pointer(rt)
key = tuple2pointer(key)

AES_crypt(
        byref(rt),
        byref(ct), 
        blk_len, 
        byref(iv), 
        byref(key),
        byref(pa),
        AES_DECRYPT,
        AES_ECB_MODE
        )

C函数的原型是:

AES_crypt(
               UINT32 *dataout,
               UINT32 *datain, 
               UINT32 block_len, 
               UINT32 *iv, 
               UINT32 *ks,
               UINT32 *para,
               UINT32 enc_dec_sel,
               UINT32 ecb_cbc_sel
              );

但是,调试信息是:

Traceback (most recent call last):
File "D:\Project\python\aes_vs\aes_vs\aes_vs.py", line 113, in 
<module>
AES_ECB_MODE
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_c_ulong 
instance instead of pointer to LP_c_ulong

我想知道为什么以及我能做些什么? 谢谢!

0 个答案:

没有答案