亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

可以用C++的VECTOR嗎,python怎么傳入VECTOR?

可以用C++的VECTOR嗎,python怎么傳入VECTOR?

UYOU 2023-03-02 10:07:31
最近剛學會用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.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))


查看完整回答
反對 回復 2023-03-06
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

你既然都要手動寫C了,就在C里轉換成PyObject不就行了。

查看完整回答
反對 回復 2023-03-06
  • 2 回答
  • 0 關注
  • 239 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號