windows枚举串口
1. 枚举键值
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
2. SETUPAPI方式
1 int EnumPortsWdm() 2 { 3 int i, dwDetDataSize; 4 TCHAR fname[256], locinfo[256]; 5 HDEVINFO hDevInfo; 6 SP_DEVICE_INTERFACE_DETAIL_DATA* pDetData; 7 SP_DEVICE_INTERFACE_DATA ifcData; 8 SP_DEVINFO_DATA devdata; 9 10 static const GUID ___GUID_CLASS_COMPORT = \ 11 { 0x86E0D1E0L, 0x8089, 0x11D0, { 0x9C, 0xE4, 0x08, 0x00, 0x3E, 0x30, 0x1F, 0x73 } }; 12 /*static const GUID ___GUID_CLASS_COMPORT = \ 13 { 0xAD498944, 0x762F, 0x11D0, { 0x8D, 0xCB, 0x00, 0xC0, 0x4F, 0xC3, 0x35, 0x8C } };*/ 14 15 16 hDevInfo = SetupDiGetClassDevs( &___GUID_CLASS_COMPORT, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE ); 17 if ( hDevInfo == INVALID_HANDLE_VALUE ) 18 { 19 return -1; 20 } 21 22 dwDetDataSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA ) + 256; 23 pDetData = ( SP_DEVICE_INTERFACE_DETAIL_DATA* )calloc( 1, dwDetDataSize ); 24 if ( !pDetData ) 25 { 26 SetupDiDestroyDeviceInfoList( hDevInfo ); 27 return -1; 28 } 29 30 ifcData.cbSize = sizeof( SP_DEVICE_INTERFACE_DATA ); 31 pDetData->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA ); 32 33 for ( i = 0; SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &___GUID_CLASS_COMPORT, i, &ifcData ); i ++ ) 34 { 35 memset( &devdata, 0, sizeof( SP_DEVINFO_DATA ) ); 36 devdata.cbSize = sizeof( SP_DEVINFO_DATA ); 37 38 if ( !SetupDiGetDeviceInterfaceDetail( hDevInfo, &ifcData, pDetData, dwDetDataSize, NULL, &devdata ) ) 39 { 40 break; 41 } 42 43 MessageBox( NULL, pDetData->DevicePath, TEXT( "PATH" ), MB_OK ); 44 if ( SetupDiGetDeviceRegistryProperty( hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL, ( PBYTE )fname, sizeof( fname ), NULL ) ) 45 { 46 MessageBox( NULL, fname, TEXT( "NAME" ), MB_OK ); 47 } 48 49 if ( SetupDiGetDeviceRegistryProperty( hDevInfo, &devdata, SPDRP_LOCATION_INFORMATION, NULL, ( PBYTE )locinfo, sizeof( locinfo ), NULL ) ) 50 { 51 if ( StrCmpN( locinfo, TEXT( "USB" ), 3 ) == 0 ) 52 { 53 MessageBox( NULL, TEXT( "USB" ), TEXT( "TYPE" ), MB_OK ); 54 } 55 } 56 } 57 58 free( pDetData ); 59 SetupDiDestroyDeviceInfoList( hDevInfo ); 60 return 0; 61 }