赞
踩
最后得到的是 端口号数字数组
摘抄自: https://blog.csdn.net/qq_37469992/article/details/82927956
void CBoard::getComPortName(std::vector<int> &PortName) { PortName.clear(); HKEY hKey; TCHAR portName[256]; TCHAR commName[256]; //打开串口注册表对应的键值 CString strKey = _T("Hardware\\DeviceMap\\SerialComm"); if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, NULL, KEY_READ, &hKey)) { int i = 0; int mm = 0; DWORD dwLong, dwSize; while (TRUE) { dwLong = dwSize = sizeof(portName); //枚举串口 if (ERROR_NO_MORE_ITEMS == RegEnumValue(hKey, i, portName, &dwLong, NULL, NULL, (LPBYTE)commName, &dwSize)) { break; } //commName = "COM7" int num = _wtoi(&commName[3]); PortName.push_back(num); i++; } //关闭注册表 RegCloseKey(hKey); } else { MessageBox(NULL, _T("您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项"), _T("警告"), MB_OK); } //返回串口号 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。