param_service/main.py

54 lines
1.5 KiB
Python
Raw Normal View History

2025-02-18 20:39:39 +08:00
import ctypes
2025-02-19 14:35:44 +08:00
from field_addr_resolution import *
from struct_def import *
2025-02-18 20:39:39 +08:00
# 定义内部结构体
class InnerStructLevel2(ctypes.Structure):
_pack_ = 1
_fields_ = [
("inner_field1", ctypes.c_char),
("inner_field2", ctypes.c_double),
("inner_field3", ctypes.c_double),
]
# 定义内部结构体
class InnerStructLevel1(ctypes.Structure):
_pack_ = 1
_fields_ = [
("inner_field1", ctypes.c_int),
("inner_field2", ctypes.c_double),
("inner_level2_field3", InnerStructLevel2)
]
# 定义外部结构体
class OuterStruct(ctypes.Structure):
_pack_ = 1
_fields_ = [
("field1", ctypes.c_int),
("nested", InnerStructLevel1), # 嵌套结构体
("field2", ctypes.c_char * 10),
]
# 测试代码
if __name__ == "__main__":
outer = OuterStruct()
# # 打印所有字段的地址
# print_all_addresses(outer)
print(get_filed_offset(outer, "nested.inner_level2_field3"))
# # 获取特定嵌套字段的地址
# print(f'0x{ctypes.addressof(outer):08X}')
# nested_field_address = get_field_address(outer, "nested.inner_level2_field3")
#
# print(f"\n直接访问嵌套字段 nested.inner_field2 的地址: {hex(nested_field_address)}")
#
# # 获取特定嵌套字段的地址
# nested_field_address = get_field_address(outer, "nested.inner_level2_field3.inner_field3")
#
# print(f"\n直接访问嵌套字段 nested.inner_level_field3.inner_field1 的地址: {hex(nested_field_address)}")