下面是我使用ctype访问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 通过指针,这些值不会获取changed.remains 0
发布于 2012-08-29 20:26:11
问题是in_field是一个新的c_int对象,它占用的内存与原始结构不同。您需要的是c_int.from_buffer (docs),它共享原始对象的内存。下面是一个例子:
用cl /LD x.c编译的Windows DLL源x.c
struct MyStruct
{
int one;
int two;
};
__declspec(dllexport) struct MyStruct myStruct = {1,2};Python脚本:
from ctypes import *
class MyStruct(Structure):
_fields_ = [
("one", c_int),
("two", c_int)]
def __repr__(self):
return 'MyStruct({},{})'.format(self.one,self.two)
dll = CDLL('x')
struct = MyStruct.in_dll(dll,"myStruct")
alias1 = c_int.from_buffer(struct, MyStruct.one.offset)
alias2 = c_int.from_buffer(struct, MyStruct.two.offset)
print struct
print 'before',alias1,alias2
struct.one = 10
struct.two = 20
print 'after',alias1,alias2输出:
MyStruct(1,2)
before c_long(1) c_long(2)
after c_long(10) c_long(20)https://stackoverflow.com/questions/12174008
复制相似问题