/*
* @brief:宽字符串转换为string
*/
std::string wideCharToString(wchar_t* pWCStrKey)
{
int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);
char* pCStrKey = new char[pSize + 1];
WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);
pCStrKey[pSize] = '\0';
std::string str = pCStrKey;
delete[] pCStrKey;
return str;
}
/*
* @brief:读取可用Port并转换为string
*/
std::vector<std::string> getAvailableSerialPort()
{
HKEY hKey;
wchar_t portName[256], w_comName[256];
std::vector<std::string> comNameVec;
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey))
{
int count = 0;
DWORD dwLong, dwSize;
while (true)
{
dwLong = dwSize = sizeof(portName);
if (ERROR_NO_MORE_ITEMS == ::RegEnumValue(hKey, count, portName, &dwLong, NULL, NULL, (PUCHAR)w_comName, &dwSize))
{
break;
}
comNameVec.push_back(wideCharToString(w_comName));
qDebug() << wideCharToString(w_comName).c_str();
count++;
}
RegCloseKey(hKey);
}
else
{
qDebug() << "NO Serial Port";
}
return comNameVec;
}
通过查询注册表读取当前可用串口
最新推荐文章于 2024-07-12 00:37:47 发布