summaryrefslogtreecommitdiff
path: root/src/envwindows.h
blob: c23f99f43d9ecbad1a0dc7245ee2434fc0eb3ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <QString>
#include <optional>

namespace env
{

// a variety of information on windows
//
class WindowsInfo
{
public:
  struct Version
  {
    DWORD major=0, minor=0, build=0;

    QString toString() const
    {
      return QString("%1.%2.%3").arg(major).arg(minor).arg(build);
    }

    friend bool operator==(const Version& a, const Version& b)
    {
      return
        a.major == b.major &&
        a.minor == b.minor &&
        a.build == b.build;
    }

    friend bool operator!=(const Version& a, const Version& b)
    {
      return !(a == b);
    }
  };

  struct Release
  {
    // the BuildLab entry from the registry, may be empty
    QString buildLab;

    // product name such as "Windows 10 Pro", may not be in English, may be
    // empty
    QString productName;

    // release ID such as 1809, may be mepty
    QString ID;

    // some sub-build number, undocumented, may be empty
    DWORD UBR;

    Release()
      : UBR(0)
    {
    }
  };


  WindowsInfo();

  // tries to guess whether this process is running in compatibility mode
  //
  bool compatibilityMode() const;

  // returns the Windows version, may not correspond to the actual version
  // if the process is running in compatibility mode
  //
  const Version& reportedVersion() const;

  // tries to guess the real Windows version that's running, can be empty
  //
  const Version& realVersion() const;

  // various information about the current release
  //
  const Release& release() const;

  // whether this process is running as administrator, may be empty if the
  // information is not available
  std::optional<bool> isElevated() const;

  // returns a string with all the above information on one line
  //
  QString toString() const;

private:
  Version m_reported, m_real;
  Release m_release;
  std::optional<bool> m_elevated;

  // uses RtlGetVersion() to get the version number as reported by Windows
  //
  Version getReportedVersion(HINSTANCE ntdll) const;

  // uses RtlGetNtVersionNumbers() to get the real version number
  //
  Version getRealVersion(HINSTANCE ntdll) const;

  // gets various information from the registry
  //
  Release getRelease() const;

  // gets whether the process is elevated
  //
  std::optional<bool> getElevated() const;
};

} // namespace