Python ctypes如何跳过可选参数

时间:2017-12-15 15:57:34

标签: python c wrapper ctypes

我在标题包装中有一个功能

IMPORT_FUNCTION int WINAPI GetStuff(int id, StuffStruct* stuff, StuffList *stuffList=NULL);

我用

包装这个功能
mydll.GetStuff.argtypes = [c_int, POINTER(StuffStruct), POINTER(StuffList)]

也试过跳过

mydll.GetStuff.argtypes = [c_int, POINTER(StuffStruct)]

我需要调用此函数而不指定last参数。我尝试了None并创建了一个像这个POINTER(StuffList)()的空指针 我觉得我应该使用原型,但我现在还不知道。

stuff = StuffStruct()
np = POINTER(StuffList)()
mydll.GetStuff(2, byref(stuff), None) # tried this 
mydll.GetStuff(2, byref(stuff), np) # tried this 
mydll.GetStuff(2, byref(stuff)) # tried this

2 个答案:

答案 0 :(得分:0)

尝试:

mydll.GetStuff(2, byref(stuff), byref(0))

mydll.GetStuff(2, byref(stuff), byref(None))

np = POINTER([])

答案 1 :(得分:0)

None相当于Python中的NULL。为所有三个参数声明argtypes。以下是正确的:

mydll.GetStuff.argtypes = [c_int, POINTER(StuffStruct), POINTER(StuffList)]    

stuff = StuffStruct()
mydll.GetStuff(2, byref(stuff), None)

ctypes不知道可选参数,所以你必须传递一些东西。你总是可以在Python函数中包装ctypes调用然后调用它:

def GetStuff(id,stuff,stuffList=None):
    mydll.GetStuff(id,stuff,stuffList)