I’m having trouble accessing a structure element. Notice that I have read something into the structure using sscanf (to simulate my actual routine) but Python does not seem to realize that. So I added a statement to assign a value, then Python realizes there is something there. This only happens when I’m reading into a strcuture … as I have read into a c_ulong and that works just fine.
JavaScript
x
16
16
1
class vendrRecord(Structure):
2
_pack_ = 1 # pack the struct
3
_fields_ = [
4
("ytdPayments" ,c_ulong),
5
]
6
7
VendrRecord = vendrRecord()
8
libc = cdll.msvcrt
9
libc.sscanf(b"1 2 3", b"%d", byref(VendrRecord)) # read something into the structure
10
dsi_lib = ctypes.cdll.LoadLibrary(find_library('DsiLibrary_dll')) # using my library just to give access to a dump routine
11
dsi_lib.DsmDumpBytes( byref(VendrRecord), 4 ) # this prints 0000: 01 00 00 00
12
13
print(vendrRecord.ytdPayments) # this prints <Field type=c_ulong, ofs=0, size=4>. I expected it to print 1
14
vendrRecord.ytdPayments = 2
15
print(vendrRecord.ytdPayments) # this prints 2
16
Advertisement
Answer
You are printing the class variable not the instance variable (note the case):
JavaScript
1
11
11
1
from ctypes import *
2
3
class vendrRecord(Structure):
4
_fields_ = [("ytdPayments",c_ulong)]
5
6
VendrRecord = vendrRecord()
7
libc = cdll.msvcrt
8
libc.sscanf(b"1 2 3", b"%d", byref(VendrRecord)) # read something into the structure
9
print(vendrRecord.ytdPayments) # class name
10
print(VendrRecord.ytdPayments) # instance name
11
Output:
JavaScript
1
3
1
<Field type=c_ulong, ofs=0, size=4>
2
1
3