猿问

可以用C++的VECTOR吗,python怎么传入VECTOR?

最近刚学会用boost.python调用简单的c的函数,但是需要处理的是C++的类,我可以麻烦点把C++类都转化成C语言函数,但是函数参数有数组的怎么办?
好吧,想到个土办法,就是DLL里设个全局数组,python的数组用个循环,将数组的值一个个赋值给DLL里的全局变量。

UYOU
浏览 256回答 2
2回答

宝慕林4294392

给你一个例子看看,你就知道怎么做了C++的接口typedef struct{    unsigned long DeviceType;    int Handle;    int NumberOfClients;    int SerialNumber;    int MaxAllowedClients;}NeoDevice;int _stdcall icsneoFindNeoDevices(unsigned long DeviceTypes,  NeoDevice *pNeoDevices, int *pNumberOfDevices);Python调用的代码:class NeoDevice(Structure):    _fields_ = [("DeviceType",c_ulong),                ("Handle",c_int),                ("NumberOfClients",c_int),                ("SerialNumber",c_int),                ("MaxAllowedClients",c_int)]class cNeoVICan(CCanBase):    def __init__(self):        neoVi = windll.icsneo40        self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevicesif __name__ == "__main__":    canBus = cNeoVICan()    print canBus.icsneoGetDLLVersion()    iNumberOfDevices = (NeoDevice * 10)()    num = c_int()    iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), cast(iNumberOfDevices, POINT(NeoDevice)), byref(num))

莫回无

你既然都要手动写C了,就在C里转换成PyObject不就行了。
随时随地看视频慕课网APP
我要回答