[問題] 關於ctypes structure之使用

看板Python作者 (怨念)時間14年前 (2011/08/02 08:54), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/2 (看更多)
各位先進好,想請教各位一個問題; 小弟撰寫一個DLL檔, 結合其它函式庫, 包裹準備要在python中使用的函式, 然而在使用structure交換資料時出現了問題, 我想達到的目的是將結構傳入函式中處理, 由函式將值寫入結構的記憶體位置達到目的; python: ----------------------------------- #宣告結構struct class point(Structure): _fields_ = [("x", c_int), ("y", c_int)] #宣告target由20個point組成 target = point*20 ----------------------------------- dll 實作函式: ----------------------------------- DLLEXPORT void test_fuc(struct point *b) { b->x = 1; b->y = 2; } ---------------------------------- python呼叫: ----------------------------------- from ctypes import * class point(Structure): _fields_ = [("x", c_int), ("y", c_int)] x = cdll.LoadLibrary('dll_testing.dll') x.test_fuc(pointer(target())) #我試過此種寫法但失敗 但無法實質存取內容 ------------------------------------ 若想要將target放入函式中, 請問該如何撰寫呢? 煩請先進給予指點了: ) 十分感謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.118.206.153 ※ 編輯: takebreak 來自: 140.118.206.153 (08/02 16:54)

08/02 18:02, , 1F
byref(point())
08/02 18:02, 1F

08/02 18:03, , 2F
我沒研究過 struct array 在 Python 裡的 memory layout
08/02 18:03, 2F

08/02 18:03, , 3F
你得自己試一下
08/02 18:03, 3F

08/02 23:36, , 4F
謝謝您的指點 小弟將比較byref的差別: )
08/02 23:36, 4F
文章代碼(AID): #1EDxj74P (Python)
文章代碼(AID): #1EDxj74P (Python)