|
1 // Copyright (c) 2006-2008 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_util.h" |
|
6 |
|
7 #include <map> |
|
8 #include <sddl.h> |
|
9 |
|
10 #include "base/logging.h" |
|
11 #include "base/registry.h" |
|
12 #include "base/scoped_handle.h" |
|
13 #include "base/scoped_ptr.h" |
|
14 #include "base/singleton.h" |
|
15 #include "base/string_util.h" |
|
16 #include "base/tracked.h" |
|
17 |
|
18 namespace win_util { |
|
19 |
|
20 WinVersion GetWinVersion() { |
|
21 static bool checked_version = false; |
|
22 static WinVersion win_version = WINVERSION_PRE_2000; |
|
23 if (!checked_version) { |
|
24 OSVERSIONINFOEX version_info; |
|
25 version_info.dwOSVersionInfoSize = sizeof version_info; |
|
26 GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); |
|
27 if (version_info.dwMajorVersion == 5) { |
|
28 switch (version_info.dwMinorVersion) { |
|
29 case 0: |
|
30 win_version = WINVERSION_2000; |
|
31 break; |
|
32 case 1: |
|
33 win_version = WINVERSION_XP; |
|
34 break; |
|
35 case 2: |
|
36 default: |
|
37 win_version = WINVERSION_SERVER_2003; |
|
38 break; |
|
39 } |
|
40 } else if (version_info.dwMajorVersion == 6) { |
|
41 if (version_info.wProductType != VER_NT_WORKSTATION) { |
|
42 // 2008 is 6.0, and 2008 R2 is 6.1. |
|
43 win_version = WINVERSION_2008; |
|
44 } else { |
|
45 if (version_info.dwMinorVersion == 0) { |
|
46 win_version = WINVERSION_VISTA; |
|
47 } else { |
|
48 win_version = WINVERSION_WIN7; |
|
49 } |
|
50 } |
|
51 } else if (version_info.dwMajorVersion > 6) { |
|
52 win_version = WINVERSION_WIN7; |
|
53 } |
|
54 checked_version = true; |
|
55 } |
|
56 return win_version; |
|
57 } |
|
58 |
|
59 bool IsShiftPressed() { |
|
60 return (::GetKeyState(VK_SHIFT) & 0x80) == 0x80; |
|
61 } |
|
62 |
|
63 bool IsCtrlPressed() { |
|
64 return (::GetKeyState(VK_CONTROL) & 0x80) == 0x80; |
|
65 } |
|
66 |
|
67 bool IsAltPressed() { |
|
68 return (::GetKeyState(VK_MENU) & 0x80) == 0x80; |
|
69 } |
|
70 |
|
71 std::wstring FormatMessage(unsigned messageid) { |
|
72 wchar_t* string_buffer = NULL; |
|
73 unsigned string_length = ::FormatMessage( |
|
74 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
|
75 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, messageid, 0, |
|
76 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); |
|
77 |
|
78 std::wstring formatted_string; |
|
79 if (string_buffer) { |
|
80 formatted_string = string_buffer; |
|
81 LocalFree(reinterpret_cast<HLOCAL>(string_buffer)); |
|
82 } else { |
|
83 // The formating failed. simply convert the message value into a string. |
|
84 SStringPrintf(&formatted_string, L"message number %d", messageid); |
|
85 } |
|
86 return formatted_string; |
|
87 } |
|
88 |
|
89 std::wstring FormatLastWin32Error() { |
|
90 return FormatMessage(GetLastError()); |
|
91 } |
|
92 |
|
93 } // namespace win_util |
|
94 |
|
95 #ifdef _MSC_VER |
|
96 // |
|
97 // If the ASSERT below fails, please install Visual Studio 2005 Service Pack 1. |
|
98 // |
|
99 extern char VisualStudio2005ServicePack1Detection[10]; |
|
100 COMPILE_ASSERT(sizeof(&VisualStudio2005ServicePack1Detection) == sizeof(void*), |
|
101 VS2005SP1Detect); |
|
102 // |
|
103 // Chrome requires at least Service Pack 1 for Visual Studio 2005. |
|
104 // |
|
105 #endif // _MSC_VER |
|
106 |
|
107 #if 0 |
|
108 #error You must install the Windows 2008 or Vista Software Development Kit and \ |
|
109 set it as your default include path to build this library. You can grab it by \ |
|
110 searching for "download windows sdk 2008" in your favorite web search engine. \ |
|
111 Also make sure you register the SDK with Visual Studio, by selecting \ |
|
112 "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ |
|
113 menu (see Start - All Programs - Microsoft Windows SDK - \ |
|
114 Visual Studio Registration). |
|
115 #endif |