首次提交

This commit is contained in:
cuijingwei@brisonus.com 2025-02-18 20:39:39 +08:00
commit bb72fbac83
8 changed files with 161 additions and 0 deletions

3
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.13 (PythonProject3)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13 (pythonProject3) (2)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pythonProject3.iml" filepath="$PROJECT_DIR$/.idea/pythonProject3.iml" />
</modules>
</component>
</project>

10
.idea/pythonProject3.iml generated Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.13 (pythonProject3) (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

122
main.py Normal file
View File

@ -0,0 +1,122 @@
import ctypes
# 定义内部结构体
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),
]
def get_field_address(struct_instance, field_path):
# 分割字段路径
fields = field_path.split('.')
# 获取起始结构体的基地址
current_address = ctypes.addressof(struct_instance)
current_type = type(struct_instance)
# 遍历path的每个field
for field in fields:
# 在 _fields_ 中查找字段
found = False
# 遍历当前结构体的所有field
for f_name, f_type in current_type._fields_:
# 如果结构体中的field和当前查找的field一致
if f_name == field:
found = True
# 计算偏移量
offset = sum(
getattr(current_type, fname).offset for fname, _ in current_type._fields_ if fname == field)
current_address += offset
# 如果还有下一个字段,更新当前类型
if field != fields[-1]:
# 判断当前的field是不是type且是不是结构体类型
if isinstance(f_type, type) and issubclass(f_type, ctypes.Structure):
current_type = f_type
else:
raise ValueError(f"字段 '{field}' 不是结构体类型")
# 跳出当前循环
break
if not found:
raise ValueError(f"在结构体中找不到字段 '{field}'")
return current_address
# 使用示例
def print_all_addresses(struct_instance, prefix=""):
struct_type = type(struct_instance)
for field_name, field_type in struct_type._fields_:
full_path = f"{prefix}{field_name}"
# 如果是结构体类型,递归打印其成员
if isinstance(field_type, type) and issubclass(field_type, ctypes.Structure):
nested_struct = getattr(struct_instance, field_name)
print_all_addresses(nested_struct, f"{full_path}.")
else:
try:
address = get_field_address(struct_instance, field_name)
print(f"{full_path} 的地址: {hex(address)}")
except ValueError as e:
print(e)
# 计算成员变量的offset
def get_filed_offset(struct_instance, field_path):
print(type(struct_instance))
print(issubclass(type(struct_instance), ctypes.Structure))
if isinstance(struct_instance, type) and issubclass(struct_instance, ctypes.Structure):
base_address = ctypes.addressof(struct_instance)
field_address = get_field_address(struct_instance, field_path)
return field_address - base_address
return 0
# 测试代码
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)}")

4
param_config.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="" ?>
<params>
<param name="module1.test1" id="" offset="" type="int32" value="123"/>
</params>

1
typetest.py Normal file
View File

@ -0,0 +1 @@
print(isinstance(int, type))