CComHelper.cpp 917 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "../pch/pch.h"
  2. #include "CComHelper.h"
  3. CComHelper::CComHelper()
  4. {
  5. }
  6. CComHelper::~CComHelper()
  7. {
  8. }
  9. std::vector<std::wstring> CComHelper::getComPort()
  10. {
  11. std::vector<std::wstring> comVector;
  12. HKEY hKey;
  13. TCHAR portName[256], commName[256];
  14. // 打开串口注册表对应的键值
  15. if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Hardware\\DeviceMap\\SerialComm", NULL, KEY_READ, &hKey))
  16. {
  17. int i = 0;
  18. int mm = 0;
  19. DWORD dwLong, dwSize;
  20. while (TRUE)
  21. {
  22. dwLong = dwSize = sizeof(portName);
  23. // 枚举串口
  24. if (ERROR_NO_MORE_ITEMS == ::RegEnumValue(hKey, i, portName, &dwLong, NULL, NULL, (PUCHAR)commName, &dwSize))
  25. {
  26. break;
  27. }
  28. comVector.push_back(commName);
  29. i++;
  30. }
  31. // 关闭注册表
  32. RegCloseKey(hKey);
  33. }
  34. else
  35. {
  36. MessageBox(NULL, L"您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项", L"警告", MB_OK);
  37. }
  38. // 返回串口号
  39. return comVector;
  40. }