int FindCommPort( CComboBox *pComboBox )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
hDevInfo = SetupDiGetClassDevs((LPGUID) &GUID_DEVCLASS_PORTS, 0, 0,DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
pComboBox->ResetContent();
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
//LPTSTR buffer = NULL;
char buffer[2048];
DWORD buffersize =sizeof(buffer);
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
break;
}
char *str = strstr(buffer,"COM");
if(str != NULL){
char *end = strstr(str,")");
if(end) *end = 0;
pComboBox->AddString(str);
}
}
if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
{
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
if( pComboBox-> GetCount() == 0 )
{
// ::AfxMessageBox( "找不到串口!!! " );
}
else{
pComboBox->SetCurSel(0);
}
return 0;
}