From ctypes import *
CFG = Windll. cfgmgr32
Rervals = {
Zero X 00000000: "cr_success ",
Zero X 00000001: "cr_default ",
Zero X 00000002: "cr_out_of_memory ",
Zero X 00000003: "cr_invalid_pointer ",
Zero X 00000004: "cr_invalid_flag ",
Zero X 00000005: "cr_invalid_devnode ",
Zero X 00000006: "cr_invalid_res_des ",
Zero X 00000007: "cr_invalid_log_conf ",
Zero X 00000008: "cr_invalid_arbitrator ",
0x00000009: "cr_invalid_nodelist ",
0x0000000a: "cr_devnode_has_reqs ",
0x0000000b: "cr_invalid_resourceid ",
0x0000000c: "cr_dlvxd_not_found ",
0x0000000d: "cr_no_such_devnode ",
0x0000000e: "cr_no_more_log_conf ",
0x0000000f: "cr_no_more_res_des ",
Zero X 00000010: "cr_already_such_devnode ",
Zero X 00000011: "cr_invalid_range_list ",
Zero X 00000012: "cr_invalid_range ",
0x00000013: "cr_failure ",
Zero X 00000014: "cr_no_such_logical_dev ",
0x00000015: "cr_create_blocked ",
Zero X 00000016: "cr_not_system_vm ",
0x00000017: "cr_remove_vetoed ",
Zero X 00000018: "cr_apm_vetoed ",
Zero X 00000019: "cr_invalid_load_type ",
0x0000001a: "cr_buffer_small ",
0x0000001b: "cr_no_arbitrator ",
0x0000001c: "cr_no_registry_handle ",
0x0000001d: "cr_registry_error ",
0x0000001e: "cr_invalid_device_id ",
0x0000001f: "cr_invalid_data ",
0x00000020: "cr_invalid_api ",
Zero X 00000021: "cr_devloader_not_ready ",
Zero X 00000022: "cr_need_restart ",
0x00000023: "cr_no_more_hw_profiles ",
Zero X 00000024: "cr_device_not_there ",
Zero X 00000025: "cr_no_such_value ",
Zero X 00000026: "cr_wrong_type ",
0x00000027: "cr_invalid_priority ",
Zero X 00000028: "cr_not_disableable ",
0x00000029: "cr_free_resources ",
0x0000002a: "cr_query_vetoed ",
0x0000002b: "cr_cant_0000_irq ",
0x0000002c: "cr_no_dependent ",
0x0000002d: "cr_same_resources ",
0x0000002e: "cr_no_such_registry_key ",
0x0000002f: "cr_invalid_machinename ",
Zero X 00000030: "cr_remote_comm_failure ",
0x00000031: "cr_machine_unavailable ",
0x00000032: "cr_no_cm_services ",
0x00000033: "cr_access_denied ",
0x00000034: "cr_call_not_implemented ",
0x00000035: "cr_invalid_property ",
Zero X 00000036: "cr_device_interface_active ",
Zero X 00000037: "cr_no_such_device_interface ",
Zero X 00000038: "cr_invalid_reference_string ",
Zero X 00000039: "cr_invalid_conflict_list ",
0x0000003a: "cr_invalid_index ",
0x0000003b: "cr_invalid_structure_size ",
0x0000003c: "num_cr_results"
}
Cm_drp_devicedesc = 1
Cm_drp_driver = 0x0000a
Null = 0
Def get_dev_desc (devinst ):
Buf = (c_wchar * 1024 )()
Blen = c_int (1024)
Cr = cfg. cm_get_devnode_registry_propertyw (devinst, cm_drp_devicedesc, null, Buf, byref (blen), 0)
If Cr = 0:
Return Buf. Value
Else:
Return "Err (% d): % s" % (devinst, rervals [Cr])
Def get_dev_id (devinst ):
Buf = (c_wchar * 1024 )()
Blen = c_int (1024)
Cr = cfg. cm_get_device_idw (devinst, Buf, byref (blen), 0)
If Cr = 0:
Return Buf. Value
Else:
Return "Err (% d): % s" % (devinst, rervals [Cr])
Drivers = []
Def get_dev_driver (devinst ):
Global drivers
Buf = (c_wchar * 1024 )()
Blen = c_int (1024)
Cr = cfg. cm_get_devnode_registry_propertyw (devinst, cm_drp_driver, null, Buf, byref (blen), 0 );
If Cr = 0:
Drivers. append (BUF. value)
Return Buf. Value
Else:
Return "Err (% d): % s" % (devinst, rervals [Cr])
From XML. Dom. minidom import *
Def dev_xml ():
Def dev_child (devinst, tree, lev, DOM ):
Devparent = c_int (devinst)
Devchild = c_int (0)
Devnextchild = c_int (0)
If cfg. cm_get_child (byref (devchild), devparent, 0) = 0:
Desc = get_dev_desc (devchild. value)
Devid = get_dev_id (devchild. value)
Driver = get_dev_driver (devchild. value)
Node = Dom. createelement ("device ")
Node. setattribute ("devinst", STR (devchild. Value ))
Node. setattribute ("DESC", DESC)
Node. setattribute ("lev", STR (lev ))
Node. setattribute ("devid", devid)
Node. setattribute ("driver", driver)
Tree. appendchild (node)
Dev_child (devchild. Value, node, column + 1, DOM)
While cfg. cm_get_sibling (byref (devnextchild), devchild, 0) = 0:
Devchild. value = devnextchild. Value
Desc = get_dev_desc (devchild. value)
Devid = get_dev_id (devchild. value)
Driver = get_dev_driver (devchild. value)
Node = Dom. createelement ("device ")
Node. setattribute ("devinst", STR (devchild. Value ))
Node. setattribute ("DESC", DESC)
Node. setattribute ("lev", STR (lev ))
Node. setattribute ("devid", devid)
Node. setattribute ("driver", driver)
Tree. appendchild (node)
Dev_child (devchild. Value, node, column + 1, DOM)
DOM = Document ()
Dom. appendchild (DOM. createelement ("devicetree "))
Devinst = c_int (0)
Devinstnext = c_int (0)
Ev = 0
If 0 = cfg. cm_locate_devnodew (byref (devinst), 0, 0 ):
Desc = get_dev_desc (devinst. value)
Devid = get_dev_id (devinst. value)
Driver = get_dev_driver (devinst. value)
Node = Dom. createelement ("device ")
Node. setattribute ("devinst", STR (devinst. Value ))
Node. setattribute ("DESC", DESC)
Node. setattribute ("lev", STR (lev ))
Node. setattribute ("devid", devid)
Node. setattribute ("driver", driver)
Dom.doc umentelement. appendchild (node)
While 0 = cfg. cm_get_sibling (byref (devinstnext), devinst, 0 ):
Devinst. value = devinstnext. Value
Desc = get_dev_desc (devinst. value)
Devid = get_dev_id (devinst. value)
Driver = get_dev_driver (devinst. value)
Node = Dom. createelement ("device ")
Node. setattribute ("devinst", STR (devinst. Value ))
Node. setattribute ("DESC", DESC)
Node. setattribute ("lev", STR (lev ))
Node. setattribute ("devid", devid)
Node. setattribute ("driver", driver)
Dom.doc umentelement. appendchild (node)
For child in dom.doc umentelement. childnodes:
K = int (child. getattribute ("devinst "))
Dev_child (K, child, column + 1, DOM)
Return Dom. toprettyxml ()
Import time
St = Time. Time ()
Xml = dev_xml ()
ET = Time. Time ()
Print ("use time:", ET-St)
Open ("D: \ devicetree. xml", "WB"). Write (XML. encode ("utf8 "))
# Print (XML)
# Http://www.2cto.com/kf/201112/113918.html