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 #ifndef BASE_WIN_UTIL_H__ 6 #define BASE_WIN_UTIL_H__ 7 8 #include <windows.h> 9 #include <aclapi.h> 10 #include <shlobj.h> 11 12 #include <string> 13 14 #include "base/keyboard_codes.h" 15 16 namespace win_util { 17 18 // NOTE: Keep these in order so callers can do things like 19 // "if (GetWinVersion() > WINVERSION_2000) ...". It's OK to change the values, 20 // though. 21 enum WinVersion { 22 WINVERSION_PRE_2000 = 0, // Not supported 23 WINVERSION_2000 = 1, 24 WINVERSION_XP = 2, 25 WINVERSION_SERVER_2003 = 3, 26 WINVERSION_VISTA = 4, 27 WINVERSION_2008 = 5, 28 WINVERSION_WIN7 = 6, 29 }; 30 31 // Property key for System.AppUserModel.ID. 32 // <http://msdn.microsoft.com/en-us/library/dd391569(VS.85).aspx> 33 // TODO(xiyuan): Remove this once we compile with Win7 SDK. 34 extern const PROPERTYKEY kPKEYAppUserModelID; 35 36 void GetNonClientMetrics(NONCLIENTMETRICS* metrics); 37 38 // Returns the running version of Windows. 39 WinVersion GetWinVersion(); 40 41 // Returns the major and minor version of the service pack installed. 42 void GetServicePackLevel(int* major, int* minor); 43 44 // Adds an ACE in the DACL of the object referenced by handle. The ACE is 45 // granting |access| to the user |known_sid|. 46 // If |known_sid| is WinSelfSid, the sid of the current user will be added to 47 // the DACL. 48 bool AddAccessToKernelObject(HANDLE handle, WELL_KNOWN_SID_TYPE known_sid, 49 ACCESS_MASK access); 50 51 // Returns the string representing the current user sid. 52 bool GetUserSidString(std::wstring* user_sid); 53 54 // Creates a security descriptor with a DACL that has one ace giving full 55 // access to the current logon session. 56 // The security descriptor returned must be freed using LocalFree. 57 // The function returns true if it succeeds, false otherwise. 58 bool GetLogonSessionOnlyDACL(SECURITY_DESCRIPTOR** security_descriptor); 59 60 // Useful for subclassing a HWND. Returns the previous window procedure. 61 WNDPROC SetWindowProc(HWND hwnd, WNDPROC wndproc); 62 63 // Returns true if the existing window procedure is the same as |subclass_proc|. 64 bool IsSubclassed(HWND window, WNDPROC subclass_proc); 65 66 // Subclasses a window, replacing its existing window procedure with the 67 // specified one. Returns true if the current window procedure was replaced, 68 // false if the window has already been subclassed with the specified 69 // subclass procedure. 70 bool Subclass(HWND window, WNDPROC subclass_proc); 71 72 // Unsubclasses a window subclassed using Subclass. Returns true if 73 // the window was subclassed with the specified |subclass_proc| and the window 74 // was successfully unsubclassed, false if the window's window procedure is not 75 // |subclass_proc|. 76 bool Unsubclass(HWND window, WNDPROC subclass_proc); 77 78 // Retrieves the original WNDPROC of a window subclassed using 79 // SubclassWindow. 80 WNDPROC GetSuperclassWNDPROC(HWND window); 81 82 // Pointer-friendly wrappers around Get/SetWindowLong(..., GWLP_USERDATA, ...) 83 // Returns the previously set value. 84 void* SetWindowUserData(HWND hwnd, void* user_data); 85 void* GetWindowUserData(HWND hwnd); 86 87 // Returns true if the shift key is currently pressed. 88 bool IsShiftPressed(); 89 90 // Returns true if the ctrl key is currently pressed. 91 bool IsCtrlPressed(); 92 93 // Returns true if the alt key is currently pressed. 94 bool IsAltPressed(); 95 96 // A version of the GetClassNameW API that returns the class name in an 97 // std::wstring. An empty result indicates a failure to get the class name. 98 std::wstring GetClassName(HWND window); 99 100 // Returns false if user account control (UAC) has been disabled with the 101 // EnableLUA registry flag. Returns true if user account control is enabled. 102 // NOTE: The EnableLUA registry flag, which is ignored on Windows XP 103 // machines, might still exist and be set to 0 (UAC disabled), in which case 104 // this function will return false. You should therefore check this flag only 105 // if the OS is Vista. 106 bool UserAccountControlIsEnabled(); 107 108 // Use the Win32 API FormatMessage() function to generate a string, using 109 // Windows's default Message Compiled resources; ignoring the inserts. 110 std::wstring FormatMessage(unsigned messageid); 111 112 // Uses the last Win32 error to generate a human readable message string. 113 std::wstring FormatLastWin32Error(); 114 115 // Methods to convert base::KeyboardCode/Windows virtual key type methods. 116 WORD KeyboardCodeToWin(base::KeyboardCode keycode); 117 base::KeyboardCode WinToKeyboardCode(WORD keycode); 118 119 // Sets the application id in given IPropertyStore. The function is intended 120 // for tagging application/chromium shortcut, browser window and jump list for 121 // Win7. 122 bool SetAppIdForPropertyStore(IPropertyStore* property_store, 123 const wchar_t* app_id); 124 125 } // namespace win_util 126 127 #endif // BASE_WIN_UTIL_H__ 128