访问Python

时间:2016-01-09 16:49:45

标签: python

我无法访问结构元素。请注意,我已经在结构中使用了一些东西 sscanf(模拟我的实际例程)但Python似乎没有意识到这一点。所以我补充说 为了分配一个值,那么Python意识到那里有一些东西。这只发生在我读书时 strcuture ...因为我已经阅读了c_ulong并且工作得很好。

class vendrRecord(Structure):
    _pack_ = 1                                            # pack the struct
    _fields_ = [
        ("ytdPayments"           ,c_ulong),
        ]

VendrRecord = vendrRecord()
libc = cdll.msvcrt
libc.sscanf(b"1 2 3", b"%d", byref(VendrRecord))   # read something into the structure    
dsi_lib  = ctypes.cdll.LoadLibrary(find_library('DsiLibrary_dll'))  # using my library just to give access to a dump routine
dsi_lib.DsmDumpBytes( byref(VendrRecord), 4 )       # this prints 0000: 01 00 00 00

print(vendrRecord.ytdPayments)          # this prints <Field type=c_ulong, ofs=0, size=4>. I expected it to print 1 
vendrRecord.ytdPayments = 2
print(vendrRecord.ytdPayments)          # this prints 2

1 个答案:

答案 0 :(得分:2)

您正在打印类变量而不是实例变量(请注意大小写):

from ctypes import *

class vendrRecord(Structure):
    _fields_ = [("ytdPayments",c_ulong)]

    VendrRecord = vendrRecord()
    libc = cdll.msvcrt
    libc.sscanf(b"1 2 3", b"%d", byref(VendrRecord))   # read something into the structure    
    print(vendrRecord.ytdPayments) # class name
    print(VendrRecord.ytdPayments) # instance name

输出:

<Field type=c_ulong, ofs=0, size=4>
1