作者takebreak (怨念)
看板Python
標題[問題] 關於ctypes structure之使用
時間Tue Aug 2 16:54:28 2011
各位先進好,想請教各位一個問題;
小弟撰寫一個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)
→ juiz:byref(point()) 08/02 18:02
→ juiz:我沒研究過 struct array 在 Python 裡的 memory layout 08/02 18:03
→ juiz:你得自己試一下 08/02 18:03
→ takebreak:謝謝您的指點 小弟將比較byref的差別: ) 08/02 23:36