最近刚学会用boost.python调用简单的c的函数,但是需要处理的是C++的类,我可以麻烦点把C++类都转化成C语言函数,但是函数参数有数组的怎么办?好吧,想到个土办法,就是DLL里设个全局数组,python的数组用个循环,将数组的值一个个赋值给DLL里的全局变量。
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
给你一个例子看看,你就知道怎么做了
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.icsneoFindNeoDevices if __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)) |
添加回答
举报
0/150
提交
取消