最近剛學會用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
提交
取消