ctypes允许创建一个结构,其中字段本身可以指向函数。我有那部分成功地工作了。但是,我无法弄清楚如何为结构中的函数设置“paramflags”。我只能为DLL直接定义的那些函数设置它。
示例...此代码示例对我来说似乎很有效,并且利用了许多优秀的ctypes功能:
# rough ctypes code for specify the IN/OUT paramters for a funct that does work
MyDLL = CDLL(dllpath)
# first parameter is return code, the second I want to be an OUT
prototype = CFUNCTYPE(ctypes.c_uint,POINTER(ctypes.c_uint))
# following documentation, I turn my parameter in to an OUT
paramflags = ( (2,) )
MyFunc = prototype("MyFunc",MyDLL)
# another nice feature after prototype has be called/created,
# we can specify error check
MyFunc.errcheck = MyErrorCheck
# at this point, MyFunc can return my OUT parameter.
value = MyFunc()
我的问题是:
如果我必须将我的函数定义为结构的成员,我如何获得这些相同的功能(paramflags和errcheck)?
我可以将属性设置为CFUNCTYPE,但由于我无法进行实际创建,因此我不理解如何在结构定义中设置paramflags或errcheck。
class MyStruct(ctypes.Structure):
_fields_ = [ ("MyFunc", ctypes.CFUNCTYPE(ctypes.c_uint,POINTER(ctypes.c_uint))) ]
MyDLL = CDLL(dllpath)
ptr = MyDLL.FunctionReturnsPtr()
Foo = cast(ptr,POINTER(MyStruct))
# if i could figure out how to set the paramflags,
# then I would now be able to do:
value = Foo.MyFunc()