枚举串口函数
void CEnumComDlg::EnumPort()
{
HKEY hKey;
LPCTSTR lpSubKey="HARDWARE\\DEVICEMAP\\SERIALCOMM\\";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, &hKey)!= ERROR_SUCCESS)
{
return ;
}
char szValueName[NAME_LEN];
BYTE szPortName[NAME_LEN];
LONG status;
DWORD dwIndex = 0;
DWORD dwSizeValueName=100;
DWORD dwSizeofPortName=100;
DWORD Type;
dwSizeValueName = NAME_LEN;
dwSizeofPortName = NAME_LEN;
do
{
status = RegEnumValue(hKey, dwIndex++, szValueName, &dwSizeValueName, NULL, &Type,
szPortName, &dwSizeofPortName);
if((status == ERROR_SUCCESS))
{
CString str;
str=(char *)szPortName;
//MessageBox(str);
m_cmbCom.AddString(str);
}
dwSizeValueName = NAME_LEN;
dwSizeofPortName = NAME_LEN;
} while((status!= ERROR_NO_MORE_ITEMS));
RegCloseKey(hKey);
}
调用即可,显示结果如下
完整源码请详见:https://download.csdn.net/download/qq_23565865/10794115