Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wversion.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
25 
26 #include "lib/sysdep/os/win/win.h"
28 
30 
31 
32 static wchar_t windowsVersionString[20];
33 static size_t windowsVersion; // see WVERSION_*
34 
35 
36 const wchar_t* wversion_Family()
37 {
38  ENSURE(windowsVersion != 0);
39  switch(windowsVersion)
40  {
41  case WVERSION_2K:
42  return L"Win2k";
43  case WVERSION_XP:
44  return L"WinXP";
45  case WVERSION_XP64:
46  return L"WinXP64";
47  case WVERSION_VISTA:
48  return L"Vista";
49  case WVERSION_7:
50  return L"Win7";
51  case WVERSION_8:
52  return L"Win8";
53  default:
54  return L"Windows";
55  }
56 }
57 
58 
59 const wchar_t* wversion_String()
60 {
61  ENSURE(windowsVersionString[0] != '\0');
62  return windowsVersionString;
63 }
64 
66 {
67  ENSURE(windowsVersion != 0);
68  return windowsVersion;
69 }
70 
71 
73 {
74  // note: don't use GetVersion[Ex] because it gives the version of the
75  // emulated OS when running an app with compatibility shims enabled.
76  HKEY hKey;
77  if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
78  {
79  DWORD size = sizeof(windowsVersionString);
80  (void)RegQueryValueExW(hKey, L"CurrentVersion", 0, 0, (LPBYTE)windowsVersionString, &size);
81 
82  unsigned major = 0, minor = 0;
83  // ICC 11.1.082 generates incorrect code for the following:
84  // const int ret = swscanf_s(windowsVersionString, L"%u.%u", &major, &minor);
85  std::wstringstream ss(windowsVersionString);
86  ss >> major;
87  wchar_t dot;
88  ss >> dot;
89  ENSURE(dot == '.');
90  ss >> minor;
91 
92  ENSURE(4 <= major && major <= 0xFF);
93  ENSURE(minor <= 0xFF);
94  windowsVersion = (major << 8) | minor;
95 
96  RegCloseKey(hKey);
97  }
98  else
100 
101  return INFO::OK;
102 }
const size_t WVERSION_XP
Definition: wversion.h:33
const Status LOGIC
Definition: status.h:409
const Status OK
Definition: status.h:386
static wchar_t windowsVersionString[20]
Definition: wversion.cpp:32
const size_t WVERSION_7
Definition: wversion.h:36
size_t wversion_Number()
Definition: wversion.cpp:65
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
#define WINIT_REGISTER_EARLY_INIT(func)
Definition: winit.h:140
const wchar_t * wversion_Family()
Definition: wversion.cpp:36
unsigned long DWORD
Definition: wgl.h:56
i64 Status
Error handling system.
Definition: status.h:171
const wchar_t * wversion_String()
Definition: wversion.cpp:59
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:331
const size_t WVERSION_XP64
Definition: wversion.h:34
const size_t WVERSION_8
Definition: wversion.h:37
static size_t windowsVersion
Definition: wversion.cpp:33
const size_t WVERSION_2K
Definition: wversion.h:32
const size_t WVERSION_VISTA
Definition: wversion.h:35
static Status wversion_Init()
Definition: wversion.cpp:72