michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/win_util.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/registry.h" michael@0: #include "base/scoped_handle.h" michael@0: #include "base/scoped_ptr.h" michael@0: #include "base/singleton.h" michael@0: #include "base/string_util.h" michael@0: #include "base/tracked.h" michael@0: michael@0: namespace win_util { michael@0: michael@0: WinVersion GetWinVersion() { michael@0: static bool checked_version = false; michael@0: static WinVersion win_version = WINVERSION_PRE_2000; michael@0: if (!checked_version) { michael@0: OSVERSIONINFOEX version_info; michael@0: version_info.dwOSVersionInfoSize = sizeof version_info; michael@0: GetVersionEx(reinterpret_cast(&version_info)); michael@0: if (version_info.dwMajorVersion == 5) { michael@0: switch (version_info.dwMinorVersion) { michael@0: case 0: michael@0: win_version = WINVERSION_2000; michael@0: break; michael@0: case 1: michael@0: win_version = WINVERSION_XP; michael@0: break; michael@0: case 2: michael@0: default: michael@0: win_version = WINVERSION_SERVER_2003; michael@0: break; michael@0: } michael@0: } else if (version_info.dwMajorVersion == 6) { michael@0: if (version_info.wProductType != VER_NT_WORKSTATION) { michael@0: // 2008 is 6.0, and 2008 R2 is 6.1. michael@0: win_version = WINVERSION_2008; michael@0: } else { michael@0: if (version_info.dwMinorVersion == 0) { michael@0: win_version = WINVERSION_VISTA; michael@0: } else { michael@0: win_version = WINVERSION_WIN7; michael@0: } michael@0: } michael@0: } else if (version_info.dwMajorVersion > 6) { michael@0: win_version = WINVERSION_WIN7; michael@0: } michael@0: checked_version = true; michael@0: } michael@0: return win_version; michael@0: } michael@0: michael@0: bool IsShiftPressed() { michael@0: return (::GetKeyState(VK_SHIFT) & 0x80) == 0x80; michael@0: } michael@0: michael@0: bool IsCtrlPressed() { michael@0: return (::GetKeyState(VK_CONTROL) & 0x80) == 0x80; michael@0: } michael@0: michael@0: bool IsAltPressed() { michael@0: return (::GetKeyState(VK_MENU) & 0x80) == 0x80; michael@0: } michael@0: michael@0: std::wstring FormatMessage(unsigned messageid) { michael@0: wchar_t* string_buffer = NULL; michael@0: unsigned string_length = ::FormatMessage( michael@0: FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | michael@0: FORMAT_MESSAGE_IGNORE_INSERTS, NULL, messageid, 0, michael@0: reinterpret_cast(&string_buffer), 0, NULL); michael@0: michael@0: std::wstring formatted_string; michael@0: if (string_buffer) { michael@0: formatted_string = string_buffer; michael@0: LocalFree(reinterpret_cast(string_buffer)); michael@0: } else { michael@0: // The formating failed. simply convert the message value into a string. michael@0: SStringPrintf(&formatted_string, L"message number %d", messageid); michael@0: } michael@0: return formatted_string; michael@0: } michael@0: michael@0: std::wstring FormatLastWin32Error() { michael@0: return FormatMessage(GetLastError()); michael@0: } michael@0: michael@0: } // namespace win_util michael@0: michael@0: #ifdef _MSC_VER michael@0: // michael@0: // If the ASSERT below fails, please install Visual Studio 2005 Service Pack 1. michael@0: // michael@0: extern char VisualStudio2005ServicePack1Detection[10]; michael@0: COMPILE_ASSERT(sizeof(&VisualStudio2005ServicePack1Detection) == sizeof(void*), michael@0: VS2005SP1Detect); michael@0: // michael@0: // Chrome requires at least Service Pack 1 for Visual Studio 2005. michael@0: // michael@0: #endif // _MSC_VER michael@0: michael@0: #if 0 michael@0: #error You must install the Windows 2008 or Vista Software Development Kit and \ michael@0: set it as your default include path to build this library. You can grab it by \ michael@0: searching for "download windows sdk 2008" in your favorite web search engine. \ michael@0: Also make sure you register the SDK with Visual Studio, by selecting \ michael@0: "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ michael@0: menu (see Start - All Programs - Microsoft Windows SDK - \ michael@0: Visual Studio Registration). michael@0: #endif