您的位置:首页 > 编程语言 > C语言/C++

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: