vc枚举所有可用的端口号

代码如下:
void CMainFrame::FindComPort()//查找所有可用的端口号
{
	HKEY   hKey;

	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
	{
		TCHAR		szPortName[256], szComName[256];
		DWORD		dwLong, dwSize;
		int			nCount	= 0, nIndex = 0;

		m_pComboBoxSerialPortNumber->RemoveAllItems();		

		while(true)
		{
			dwLong	= dwSize	= 256;
			if(RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS)
				break;

			m_pComboBoxSerialPortNumber->AddSortedItem(szComName, nCount);
			nCount++;
		}

		RegCloseKey(hKey);

		m_pComboBoxSerialPortNumber->AddItem(_T("刷新"), 0);
		m_pComboBoxSerialPortNumber->SelectItem(0);
		
		nIndex = m_pComboBoxSerialPortNumber->GetCurSel();
		CString t_strPort;
		t_strPort = m_pComboBoxSerialPortNumber->GetItem(nIndex);

		if (t_strPort != _T("刷新"))
		{
			nIndex = t_strPort.Find('M');
			t_strPort.Delete(0, nIndex+1);
			m_nCOM = atoi((LPCTSTR)t_strPort);
		}
	}
}

你可能感兴趣的:(windows,mfc,VC,枚举端口号)