Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: base/win_util.cc

Issue 6019007: Remove win_util::FormatMessage and FormatLastWin32Error. These were only used... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/win_util.h ('k') | base/win_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/win_util.h" 5 #include "base/win_util.h"
6 6
7 #include <aclapi.h> 7 #include <aclapi.h>
8 #include <shobjidl.h> // Must be before propkey. 8 #include <shobjidl.h> // Must be before propkey.
9 #include <propkey.h> 9 #include <propkey.h>
10 #include <propvarutil.h> 10 #include <propvarutil.h>
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 if (!::ConvertSidToStringSid(user->User.Sid, &sid_string)) 63 if (!::ConvertSidToStringSid(user->User.Sid, &sid_string))
64 return false; 64 return false;
65 65
66 *user_sid = sid_string; 66 *user_sid = sid_string;
67 67
68 ::LocalFree(sid_string); 68 ::LocalFree(sid_string);
69 69
70 return true; 70 return true;
71 } 71 }
72 72
73 #pragma warning(push)
74 #pragma warning(disable:4312 4244)
75 WNDPROC SetWindowProc(HWND hwnd, WNDPROC proc) {
76 // The reason we don't return the SetwindowLongPtr() value is that it returns
77 // the orignal window procedure and not the current one. I don't know if it is
78 // a bug or an intended feature.
79 WNDPROC oldwindow_proc =
80 reinterpret_cast<WNDPROC>(GetWindowLongPtr(hwnd, GWLP_WNDPROC));
81 SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(proc));
82 return oldwindow_proc;
83 }
84
85 void* SetWindowUserData(HWND hwnd, void* user_data) {
86 return
87 reinterpret_cast<void*>(SetWindowLongPtr(hwnd, GWLP_USERDATA,
88 reinterpret_cast<LONG_PTR>(user_data)));
89 }
90
91 void* GetWindowUserData(HWND hwnd) {
92 return reinterpret_cast<void*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
93 }
94
95 #pragma warning(pop)
96
97 bool IsShiftPressed() { 73 bool IsShiftPressed() {
98 return (::GetKeyState(VK_SHIFT) & 0x8000) == 0x8000; 74 return (::GetKeyState(VK_SHIFT) & 0x8000) == 0x8000;
99 } 75 }
100 76
101 bool IsCtrlPressed() { 77 bool IsCtrlPressed() {
102 return (::GetKeyState(VK_CONTROL) & 0x8000) == 0x8000; 78 return (::GetKeyState(VK_CONTROL) & 0x8000) == 0x8000;
103 } 79 }
104 80
105 bool IsAltPressed() { 81 bool IsAltPressed() {
106 return (::GetKeyState(VK_MENU) & 0x8000) == 0x8000; 82 return (::GetKeyState(VK_MENU) & 0x8000) == 0x8000;
107 } 83 }
108 84
109 std::wstring GetClassName(HWND window) {
110 // GetClassNameW will return a truncated result (properly null terminated) if
111 // the given buffer is not large enough. So, it is not possible to determine
112 // that we got the entire class name if the result is exactly equal to the
113 // size of the buffer minus one.
114 DWORD buffer_size = MAX_PATH;
115 while (true) {
116 std::wstring output;
117 DWORD size_ret =
118 GetClassNameW(window, WriteInto(&output, buffer_size), buffer_size);
119 if (size_ret == 0)
120 break;
121 if (size_ret < (buffer_size - 1)) {
122 output.resize(size_ret);
123 return output;
124 }
125 buffer_size *= 2;
126 }
127 return std::wstring(); // error
128 }
129
130 bool UserAccountControlIsEnabled() { 85 bool UserAccountControlIsEnabled() {
131 // This can be slow if Windows ends up going to disk. Should watch this key 86 // This can be slow if Windows ends up going to disk. Should watch this key
132 // for changes and only read it once, preferably on the file thread. 87 // for changes and only read it once, preferably on the file thread.
133 // http://code.google.com/p/chromium/issues/detail?id=61644 88 // http://code.google.com/p/chromium/issues/detail?id=61644
134 base::ThreadRestrictions::ScopedAllowIO allow_io; 89 base::ThreadRestrictions::ScopedAllowIO allow_io;
135 90
136 base::win::RegKey key(HKEY_LOCAL_MACHINE, 91 base::win::RegKey key(HKEY_LOCAL_MACHINE,
137 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 92 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
138 KEY_READ); 93 KEY_READ);
139 DWORD uac_enabled; 94 DWORD uac_enabled;
140 if (!key.ReadValueDW(L"EnableLUA", &uac_enabled)) 95 if (!key.ReadValueDW(L"EnableLUA", &uac_enabled))
141 return true; 96 return true;
142 // Users can set the EnableLUA value to something arbitrary, like 2, which 97 // Users can set the EnableLUA value to something arbitrary, like 2, which
143 // Vista will treat as UAC enabled, so we make sure it is not set to 0. 98 // Vista will treat as UAC enabled, so we make sure it is not set to 0.
144 return (uac_enabled != 0); 99 return (uac_enabled != 0);
145 } 100 }
146 101
147 std::wstring FormatMessage(unsigned messageid) {
148 wchar_t* string_buffer = NULL;
149 unsigned string_length = ::FormatMessage(
150 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
151 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, messageid, 0,
152 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL);
153
154 std::wstring formatted_string;
155 if (string_buffer) {
156 formatted_string = string_buffer;
157 LocalFree(reinterpret_cast<HLOCAL>(string_buffer));
158 } else {
159 // The formating failed. simply convert the message value into a string.
160 base::SStringPrintf(&formatted_string, L"message number %d", messageid);
161 }
162 return formatted_string;
163 }
164
165 std::wstring FormatLastWin32Error() {
166 return FormatMessage(GetLastError());
167 }
168
169 bool SetAppIdForPropertyStore(IPropertyStore* property_store, 102 bool SetAppIdForPropertyStore(IPropertyStore* property_store,
170 const wchar_t* app_id) { 103 const wchar_t* app_id) {
171 DCHECK(property_store); 104 DCHECK(property_store);
172 105
173 // App id should be less than 128 chars and contain no space. And recommended 106 // App id should be less than 128 chars and contain no space. And recommended
174 // format is CompanyName.ProductName[.SubProduct.ProductNumber]. 107 // format is CompanyName.ProductName[.SubProduct.ProductNumber].
175 // See http://msdn.microsoft.com/en-us/library/dd378459%28VS.85%29.aspx 108 // See http://msdn.microsoft.com/en-us/library/dd378459%28VS.85%29.aspx
176 DCHECK(lstrlen(app_id) < 128 && wcschr(app_id, L' ') == NULL); 109 DCHECK(lstrlen(app_id) < 128 && wcschr(app_id, L' ') == NULL);
177 110
178 PROPVARIANT property_value; 111 PROPVARIANT property_value;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 151
219 #ifndef COPY_FILE_COPY_SYMLINK 152 #ifndef COPY_FILE_COPY_SYMLINK
220 #error You must install the Windows 2008 or Vista Software Development Kit and \ 153 #error You must install the Windows 2008 or Vista Software Development Kit and \
221 set it as your default include path to build this library. You can grab it by \ 154 set it as your default include path to build this library. You can grab it by \
222 searching for "download windows sdk 2008" in your favorite web search engine. \ 155 searching for "download windows sdk 2008" in your favorite web search engine. \
223 Also make sure you register the SDK with Visual Studio, by selecting \ 156 Also make sure you register the SDK with Visual Studio, by selecting \
224 "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \ 157 "Integrate Windows SDK with Visual Studio 2005" from the Windows SDK \
225 menu (see Start - All Programs - Microsoft Windows SDK - \ 158 menu (see Start - All Programs - Microsoft Windows SDK - \
226 Visual Studio Registration). 159 Visual Studio Registration).
227 #endif 160 #endif
OLDNEW
« no previous file with comments | « base/win_util.h ('k') | base/win_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698