/ tools / BugReportTool / BugReportTool / ReportMonitorInfo.cpp
ReportMonitorInfo.cpp
 1  #pragma once
 2  #include "ReportMonitorInfo.h"
 3  #include <Windows.h>
 4  #include <filesystem>
 5  #include "../../../src/common/utils/winapi_error.h"
 6  using namespace std;
 7  
 8  namespace
 9  {
10      int BuildMonitorInfoReport(std::wostream& os)
11      {
12          struct capture
13          {
14              std::wostream* os = nullptr;
15          };
16  
17          auto callback = [](HMONITOR monitor, HDC, RECT*, LPARAM prm) -> BOOL {
18              std::wostream& os = *(reinterpret_cast<capture*>(prm))->os;
19              MONITORINFOEX mi;
20              mi.cbSize = sizeof(mi);
21  
22              if (GetMonitorInfoW(monitor, &mi))
23              {
24                  os << "GetMonitorInfo OK\n";
25                  DISPLAY_DEVICE displayDevice = { sizeof(displayDevice) };
26  
27                  DWORD i = 0;
28                  while (EnumDisplayDevicesW(mi.szDevice, i++, &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME))
29                  {
30                      const bool active = displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE;
31                      const bool mirroring = displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER;
32                      os << "EnumDisplayDevices OK:\n"
33                          << "\tMirroring = " << mirroring << '\n'
34                          << "\tActive = " << active << '\n'
35                          << "\tDeviceID = " << displayDevice.DeviceID << '\n'
36                          << "\tDeviceKey = " << displayDevice.DeviceKey << '\n'
37                          << "\tDeviceName = " << displayDevice.DeviceName << '\n'
38                          << "\tDeviceString = " << displayDevice.DeviceString << '\n';
39                  }
40              }
41              else
42              {
43                  auto message = get_last_error_message(GetLastError());
44                  os << "GetMonitorInfo FAILED: " << (message.has_value() ? message.value() : L"") << '\n';
45              }
46              return TRUE;
47          };
48  
49          capture c;
50          c.os = &os;
51          if (EnumDisplayMonitors(nullptr, nullptr, callback, reinterpret_cast<LPARAM>(& c)))
52          {
53              os << "EnumDisplayMonitors OK\n";
54          }
55          else
56          {
57              auto message = get_last_error_message(GetLastError());
58              os << "EnumDisplayMonitors FAILED: " << (message.has_value() ? message.value() : L"") << '\n';
59          }
60          return 0;
61      }
62  }
63  
64  void ReportMonitorInfo(const filesystem::path& tmpDir)
65  {
66      auto monitorReportPath = tmpDir;
67      monitorReportPath.append("monitor-report-info.txt");
68  
69      try
70      {
71          wofstream monitorReport(monitorReportPath);
72          monitorReport << "GetSystemMetrics = " << GetSystemMetrics(SM_CMONITORS) << '\n';
73          BuildMonitorInfoReport(monitorReport);
74      }
75      catch (std::exception& ex)
76      {
77          printf("Failed to report monitor info. %s\n", ex.what());
78      }
79      catch (...)
80      {
81          printf("Failed to report monitor info\n");
82      }
83  }