<s> import ctypes <EOL> import re <EOL> def ValidHandle ( value , func , arguments ) : <EOL> if value == <NUM_LIT:0> : <EOL> raise ctypes . WinError ( ) <EOL> return value <EOL> import serial <EOL> from serial . win32 import ULONG_PTR , is_64bit <EOL> from ctypes . wintypes import HANDLE <EOL> from ctypes . wintypes import BOOL <EOL> from ctypes . wintypes import HWND <EOL> from ctypes . wintypes import DWORD <EOL> from ctypes . wintypes import WORD <EOL> from ctypes . wintypes import LONG <EOL> from ctypes . wintypes import ULONG <EOL> from ctypes . wintypes import LPCSTR <EOL> from ctypes . wintypes import HKEY <EOL> from ctypes . wintypes import BYTE <EOL> NULL = <NUM_LIT:0> <EOL> HDEVINFO = ctypes . c_void_p <EOL> PCTSTR = ctypes . c_char_p <EOL> PTSTR = ctypes . c_void_p <EOL> CHAR = ctypes . c_char <EOL> LPDWORD = PDWORD = ctypes . POINTER ( DWORD ) <EOL> LPBYTE = PBYTE = ctypes . c_void_p <EOL> ACCESS_MASK = DWORD <EOL> REGSAM = ACCESS_MASK <EOL> def byte_buffer ( length ) : <EOL> """<STR_LIT>""" <EOL> return ( BYTE * length ) ( ) <EOL> def string ( buffer ) : <EOL> s = [ ] <EOL> for c in buffer : <EOL> if c == <NUM_LIT:0> : break <EOL> s . append ( chr ( c & <NUM_LIT> ) ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> class GUID ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , DWORD ) , <EOL> ( '<STR_LIT>' , WORD ) , <EOL> ( '<STR_LIT>' , WORD ) , <EOL> ( '<STR_LIT>' , BYTE * <NUM_LIT:8> ) , <EOL> ] <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . Data1 , <EOL> self . Data2 , <EOL> self . Data3 , <EOL> '<STR_LIT>' . join ( [ "<STR_LIT>" % d for d in self . Data4 [ : <NUM_LIT:2> ] ] ) , <EOL> '<STR_LIT>' . join ( [ "<STR_LIT>" % d for d in self . Data4 [ <NUM_LIT:2> : ] ] ) , <EOL> ) <EOL> class SP_DEVINFO_DATA ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , DWORD ) , <EOL> ( '<STR_LIT>' , GUID ) , <EOL> ( '<STR_LIT>' , DWORD ) , <EOL> ( '<STR_LIT>' , ULONG_PTR ) , <EOL> ] <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . ClassGuid , self . DevInst ) <EOL> PSP_DEVINFO_DATA = ctypes . POINTER ( SP_DEVINFO_DATA ) <EOL> PSP_DEVICE_INTERFACE_DETAIL_DATA = ctypes . c_void_p <EOL> setupapi = ctypes . windll . LoadLibrary ( "<STR_LIT>" ) <EOL> SetupDiDestroyDeviceInfoList = setupapi . SetupDiDestroyDeviceInfoList <EOL> SetupDiDestroyDeviceInfoList . argtypes = [ HDEVINFO ] <EOL> SetupDiDestroyDeviceInfoList . restype = BOOL <EOL> SetupDiClassGuidsFromName = setupapi . SetupDiClassGuidsFromNameA <EOL> SetupDiClassGuidsFromName . argtypes = [ PCTSTR , ctypes . POINTER ( GUID ) , DWORD , PDWORD ] <EOL> SetupDiClassGuidsFromName . restype = BOOL <EOL> SetupDiEnumDeviceInfo = setupapi . SetupDiEnumDeviceInfo <EOL> SetupDiEnumDeviceInfo . argtypes = [ HDEVINFO , DWORD , PSP_DEVINFO_DATA ] <EOL> SetupDiEnumDeviceInfo . restype = BOOL <EOL> SetupDiGetClassDevs = setupapi . SetupDiGetClassDevsA <EOL> SetupDiGetClassDevs . argtypes = [ ctypes . POINTER ( GUID ) , PCTSTR , HWND , DWORD ] <EOL> SetupDiGetClassDevs . restype = HDEVINFO <EOL> SetupDiGetClassDevs . errcheck = ValidHandle <EOL> SetupDiGetDeviceRegistryProperty = setupapi . SetupDiGetDeviceRegistryPropertyA <EOL> SetupDiGetDeviceRegistryProperty . argtypes = [ HDEVINFO , PSP_DEVINFO_DATA , DWORD , PDWORD , PBYTE , DWORD , PDWORD ] <EOL> SetupDiGetDeviceRegistryProperty . restype = BOOL <EOL> SetupDiGetDeviceInstanceId = setupapi . SetupDiGetDeviceInstanceIdA <EOL> SetupDiGetDeviceInstanceId . argtypes = [ HDEVINFO , PSP_DEVINFO_DATA , PTSTR , DWORD , PDWORD ] <EOL> SetupDiGetDeviceInstanceId . restype = BOOL <EOL> SetupDiOpenDevRegKey = setupapi . SetupDiOpenDevRegKey <EOL> SetupDiOpenDevRegKey . argtypes = [ HDEVINFO , PSP_DEVINFO_DATA , DWORD , DWORD , DWORD , REGSAM ] <EOL> SetupDiOpenDevRegKey . restype = HKEY <EOL> advapi32 = ctypes . windll . LoadLibrary ( "<STR_LIT>" ) <EOL> RegCloseKey = advapi32 . RegCloseKey <EOL> RegCloseKey . argtypes = [ HKEY ] <EOL> RegCloseKey . restype = LONG <EOL> RegQueryValueEx = advapi32 . RegQueryValueExA <EOL> RegQueryValueEx . argtypes = [ HKEY , LPCSTR , LPDWORD , LPDWORD , LPBYTE , LPDWORD ] <EOL> RegQueryValueEx . restype = LONG <EOL> DIGCF_PRESENT = <NUM_LIT:2> <EOL> DIGCF_DEVICEINTERFACE = <NUM_LIT:16> <EOL> INVALID_HANDLE_VALUE = <NUM_LIT:0> <EOL> ERROR_INSUFFICIENT_BUFFER = <NUM_LIT> <EOL> SPDRP_HARDWAREID = <NUM_LIT:1> <EOL> SPDRP_FRIENDLYNAME = <NUM_LIT:12> <EOL> DICS_FLAG_GLOBAL = <NUM_LIT:1> <EOL> DIREG_DEV = <NUM_LIT> <EOL> KEY_READ = <NUM_LIT> <EOL> Ports = serial . to_bytes ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> PortName = serial . to_bytes ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> def comports ( ) : <EOL> GUIDs = ( GUID * <NUM_LIT:8> ) ( ) <EOL> guids_size = DWORD ( ) <EOL> if not SetupDiClassGuidsFromName ( <EOL> Ports , <EOL> GUIDs , <EOL> ctypes . sizeof ( GUIDs ) , <EOL> ctypes . byref ( guids_size ) ) : <EOL> raise ctypes . WinError ( ) <EOL> for index in range ( guids_size . value ) : <EOL> g_hdi = SetupDiGetClassDevs ( <EOL> ctypes . byref ( GUIDs [ index ] ) , <EOL> None , <EOL> NULL , <EOL> DIGCF_PRESENT ) <EOL> devinfo = SP_DEVINFO_DATA ( ) <EOL> devinfo . cbSize = ctypes . sizeof ( devinfo ) <EOL> index = <NUM_LIT:0> <EOL> while SetupDiEnumDeviceInfo ( g_hdi , index , ctypes . byref ( devinfo ) ) : <EOL> index += <NUM_LIT:1> <EOL> hkey = SetupDiOpenDevRegKey ( <EOL> g_hdi , <EOL> ctypes . byref ( devinfo ) , <EOL> DICS_FLAG_GLOBAL , <EOL> <NUM_LIT:0> , <EOL> DIREG_DEV , <EOL> KEY_READ ) <EOL> port_name_buffer = byte_buffer ( <NUM_LIT> ) <EOL> port_name_length = ULONG ( ctypes . sizeof ( port_name_buffer ) ) <EOL> RegQueryValueEx ( <EOL> hkey , <EOL> PortName , <EOL> None , <EOL> None , <EOL> ctypes . byref ( port_name_buffer ) , <EOL> ctypes . byref ( port_name_length ) ) <EOL> RegCloseKey ( hkey ) <EOL> if string ( port_name_buffer ) . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> szHardwareID = byte_buffer ( <NUM_LIT> ) <EOL> if not SetupDiGetDeviceInstanceId ( <EOL> g_hdi , <EOL> ctypes . byref ( devinfo ) , <EOL> ctypes . byref ( szHardwareID ) , <EOL> ctypes . sizeof ( szHardwareID ) - <NUM_LIT:1> , <EOL> None ) : <EOL> if not SetupDiGetDeviceRegistryProperty ( <EOL> g_hdi , <EOL> ctypes . byref ( devinfo ) , <EOL> SPDRP_HARDWAREID , <EOL> None , <EOL> ctypes . byref ( szHardwareID ) , <EOL> ctypes . sizeof ( szHardwareID ) - <NUM_LIT:1> , <EOL> None ) : <EOL> if ctypes . GetLastError ( ) != ERROR_INSUFFICIENT_BUFFER : <EOL> raise ctypes . WinError ( ) <EOL> szHardwareID_str = string ( szHardwareID ) <EOL> if szHardwareID_str . startswith ( '<STR_LIT>' ) : <EOL> m = re . search ( r'<STR_LIT>' , szHardwareID_str , re . I ) <EOL> if m : <EOL> if m . group ( <NUM_LIT:4> ) : <EOL> szHardwareID_str = '<STR_LIT>' % ( m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:2> ) , m . group ( <NUM_LIT:4> ) ) <EOL> else : <EOL> szHardwareID_str = '<STR_LIT>' % ( m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:2> ) ) <EOL> szFriendlyName = byte_buffer ( <NUM_LIT> ) <EOL> if not SetupDiGetDeviceRegistryProperty ( <EOL> g_hdi , <EOL> ctypes . byref ( devinfo ) , <EOL> SPDRP_FRIENDLYNAME , <EOL> None , <EOL> ctypes . byref ( szFriendlyName ) , <EOL> ctypes . sizeof ( szFriendlyName ) - <NUM_LIT:1> , <EOL> None ) : <EOL> yield string ( port_name_buffer ) , '<STR_LIT>' , szHardwareID_str <EOL> else : <EOL> yield string ( port_name_buffer ) , string ( szFriendlyName ) , szHardwareID_str <EOL> SetupDiDestroyDeviceInfoList ( g_hdi ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import serial <EOL> for port , desc , hwid in sorted ( comports ( ) ) : <EOL> print "<STR_LIT>" % ( port , desc , hwid ) </s>
