下面是我使用 ctypes 访问 dll 值的代码。
我的目的是存储结构字段地址。每当结构中的值发生更改时,我都可以访问地址并获取更改的值。
DUMMY_DLL_PATH = "dummyModel.dll"
class MyStruct(ctypes.Structure):
_fields_ = [("field_one", ctypes.c_int),
("field_two", ctypes.c_int)]
d_m = ctypes.cdll.LoadLibrary(DUMMY_DLL_PATH)
d_i = MyStruct.in_dll(d_m,"dummy_In")
in_field = ctypes.c_int(d_i.field_one)
#storing the address
b = ctypes.addressof(in_field)
b_v = ctypes.cast(b,ctypes.POINTER(ctypes.c_int))
k= b_v.contents
print 'before',d_i.field_one,k.value
#changing the value
d_i.field_one = 10
print 'After',d_i.field_one,k.value
输出:
Before 0 0
After 10 0
通过指针,值不会改变。仍然为 0