windows_version.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/win/windows_version.h"
6
7#include <windows.h>
8
9#include "base/logging.h"
10
11namespace base {
12namespace win {
13
14Version GetVersion() {
15  static bool checked_version = false;
16  static Version win_version = VERSION_PRE_2000;
17  if (!checked_version) {
18    OSVERSIONINFOEX version_info;
19    version_info.dwOSVersionInfoSize = sizeof version_info;
20    GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
21    if (version_info.dwMajorVersion == 5) {
22      switch (version_info.dwMinorVersion) {
23        case 0:
24          win_version = VERSION_2000;
25          break;
26        case 1:
27          win_version = VERSION_XP;
28          break;
29        case 2:
30        default:
31          win_version = VERSION_SERVER_2003;
32          break;
33      }
34    } else if (version_info.dwMajorVersion == 6) {
35      if (version_info.wProductType != VER_NT_WORKSTATION) {
36        // 2008 is 6.0, and 2008 R2 is 6.1.
37        win_version = VERSION_2008;
38      } else {
39        if (version_info.dwMinorVersion == 0) {
40          win_version = VERSION_VISTA;
41        } else {
42          win_version = VERSION_WIN7;
43        }
44      }
45    } else if (version_info.dwMajorVersion > 6) {
46      win_version = VERSION_WIN7;
47    }
48    checked_version = true;
49  }
50  return win_version;
51}
52
53void GetServicePackLevel(int* major, int* minor) {
54  DCHECK(major && minor);
55  static bool checked_version = false;
56  static int service_pack_major = -1;
57  static int service_pack_minor = -1;
58  if (!checked_version) {
59    OSVERSIONINFOEX version_info = {0};
60    version_info.dwOSVersionInfoSize = sizeof(version_info);
61    GetVersionEx(reinterpret_cast<OSVERSIONINFOW*>(&version_info));
62    service_pack_major = version_info.wServicePackMajor;
63    service_pack_minor = version_info.wServicePackMinor;
64    checked_version = true;
65  }
66
67  *major = service_pack_major;
68  *minor = service_pack_minor;
69}
70
71WindowsArchitecture GetWindowsArchitecture() {
72  SYSTEM_INFO system_info;
73  GetNativeSystemInfo(&system_info);
74  switch (system_info.wProcessorArchitecture) {
75    case PROCESSOR_ARCHITECTURE_INTEL: return X86_ARCHITECTURE;
76    case PROCESSOR_ARCHITECTURE_AMD64: return X64_ARCHITECTURE;
77    case PROCESSOR_ARCHITECTURE_IA64:  return IA64_ARCHITECTURE;
78    default:                           return OTHER_ARCHITECTURE;
79  }
80}
81
82WOW64Status GetWOW64Status() {
83  static WOW64Status wow64_status =
84      GetWOW64StatusForProcess(GetCurrentProcess());
85  return wow64_status;
86}
87
88WOW64Status GetWOW64StatusForProcess(HANDLE process_handle) {
89  typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL);
90  IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
91      GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process"));
92  if (!is_wow64_process)
93    return WOW64_DISABLED;
94  BOOL is_wow64 = FALSE;
95  if (!(*is_wow64_process)(process_handle, &is_wow64))
96    return WOW64_UNKNOWN;
97  return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
98}
99
100}  // namespace win
101}  // namespace base
102