gpt4 book ai didi

python - 在 Python 中访问结构元素

转载 作者:行者123 更新时间:2023-12-01 04:12:41 24 4
gpt4 key购买 nike

我在访问结构元素时遇到问题。请注意,我已经使用以下命令将一些内容读入结构中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

最佳答案

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

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

关于python - 在 Python 中访问结构元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com