在Python中访问structure元素

2024-09-30 06:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法访问结构元素。注意,我已经用 但Python似乎没有意识到这一点。所以我加了一个声明 为了分配一个值,Python会意识到其中有一些东西。只有当我读入 结构。。。正如我读到的一个库隆,这是很好的工作。在

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

Tags: thetothisprints结构packlibcprint
1条回答
网友
1楼 · 发布于 2024-09-30 06:23:04

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

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

输出:

^{pr2}$

相关问题 更多 >

    热门问题