VC++ 枚举串口列表
2012-10-29 17:36
281 查看
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;
}
{
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;
}
相关文章推荐
- VC 枚举电脑串口
- VC 枚举串口四法
- VC 枚举串口四法
- VC得到可用的串口列表
- VC枚举电脑上可用串口
- VC中的一段枚举串口代码
- VC枚举串口端口应用
- (转)VC得到可用的串口列表
- VC++通过注册表枚举串口
- vc 枚举串口
- VC枚举计算机串口资料(非注册表方法)
- 通过查询注册表,枚举所有串口,并加载到下拉列表中
- VC中ODBC数据库技术应用源程序详解之一(串口数据保存相关)
- 反射的项目实战,将枚举转换成列表的办法
- VC环境下,串口编程方法
- VC使用WMI获取活动的网卡列表
- 四种方法实现VC枚举系统当前进程(转)
- 枚举可用串口号的动态链接库文件
- 深入浅出VC++串口编程之基于Win32 API
- VC++获取串口设备信息,CPU ID,硬盘ID,网卡MAC等机器硬件信息,用于加密