//
// Created by Mr.V on 2024/1/20.
//

#include "ComPortTool.h"

ComPortTool::ComPortTool(){}

ComPortTool::~ComPortTool(){}

//得到所有的串口号
std::vector<std::string> ComPortTool::getComPort()
{
    HKEY hKey;
    char portName[256], commName[256];
    std::vector<std::string> comName;
    //打开串口注册表对应的键值
    if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm", 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, (PUCHAR)commName, &dwSize))
            {
                break;
            }
            comName.push_back(commName);
            i++;
        }
        //关闭注册表
        RegCloseKey(hKey);
    }
    else
    {
        MessageBox(NULL,"您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项","警告",MB_OK);
    }
    //返回串口号
    return comName;
}