You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1119 lines
33 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2003-2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Registry configuration wrapers class implementation
  28. //
  29. // Change made by S. Ganesh - ganesh@google.com:
  30. // Use SHQueryValueEx instead of RegQueryValueEx throughout.
  31. // A call to the SHLWAPI function is essentially a call to the standard
  32. // function but with post-processing:
  33. // * to fix REG_SZ or REG_EXPAND_SZ data that is not properly null-terminated;
  34. // * to expand REG_EXPAND_SZ data.
  35. #include "talk/base/win32regkey.h"
  36. #include <shlwapi.h>
  37. #include "talk/base/common.h"
  38. #include "talk/base/logging.h"
  39. #include "talk/base/scoped_ptr.h"
  40. namespace talk_base {
  41. RegKey::RegKey() {
  42. h_key_ = NULL;
  43. }
  44. RegKey::~RegKey() {
  45. Close();
  46. }
  47. HRESULT RegKey::Create(HKEY parent_key, const wchar_t* key_name) {
  48. return Create(parent_key,
  49. key_name,
  50. REG_NONE,
  51. REG_OPTION_NON_VOLATILE,
  52. KEY_ALL_ACCESS,
  53. NULL,
  54. NULL);
  55. }
  56. HRESULT RegKey::Open(HKEY parent_key, const wchar_t* key_name) {
  57. return Open(parent_key, key_name, KEY_ALL_ACCESS);
  58. }
  59. bool RegKey::HasValue(const TCHAR* value_name) const {
  60. return (ERROR_SUCCESS == ::RegQueryValueEx(h_key_, value_name, NULL,
  61. NULL, NULL, NULL));
  62. }
  63. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  64. const wchar_t* value_name,
  65. DWORD value) {
  66. ASSERT(full_key_name != NULL);
  67. return SetValueStaticHelper(full_key_name, value_name, REG_DWORD, &value);
  68. }
  69. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  70. const wchar_t* value_name,
  71. DWORD64 value) {
  72. ASSERT(full_key_name != NULL);
  73. return SetValueStaticHelper(full_key_name, value_name, REG_QWORD, &value);
  74. }
  75. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  76. const wchar_t* value_name,
  77. float value) {
  78. ASSERT(full_key_name != NULL);
  79. return SetValueStaticHelper(full_key_name, value_name,
  80. REG_BINARY, &value, sizeof(value));
  81. }
  82. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  83. const wchar_t* value_name,
  84. double value) {
  85. ASSERT(full_key_name != NULL);
  86. return SetValueStaticHelper(full_key_name, value_name,
  87. REG_BINARY, &value, sizeof(value));
  88. }
  89. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  90. const wchar_t* value_name,
  91. const TCHAR* value) {
  92. ASSERT(full_key_name != NULL);
  93. ASSERT(value != NULL);
  94. return SetValueStaticHelper(full_key_name, value_name,
  95. REG_SZ, const_cast<wchar_t*>(value));
  96. }
  97. HRESULT RegKey::SetValue(const wchar_t* full_key_name,
  98. const wchar_t* value_name,
  99. const uint8* value,
  100. DWORD byte_count) {
  101. ASSERT(full_key_name != NULL);
  102. return SetValueStaticHelper(full_key_name, value_name, REG_BINARY,
  103. const_cast<uint8*>(value), byte_count);
  104. }
  105. HRESULT RegKey::SetValueMultiSZ(const wchar_t* full_key_name,
  106. const wchar_t* value_name,
  107. const uint8* value,
  108. DWORD byte_count) {
  109. ASSERT(full_key_name != NULL);
  110. return SetValueStaticHelper(full_key_name, value_name, REG_MULTI_SZ,
  111. const_cast<uint8*>(value), byte_count);
  112. }
  113. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  114. const wchar_t* value_name,
  115. DWORD* value) {
  116. ASSERT(full_key_name != NULL);
  117. ASSERT(value != NULL);
  118. return GetValueStaticHelper(full_key_name, value_name, REG_DWORD, value);
  119. }
  120. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  121. const wchar_t* value_name,
  122. DWORD64* value) {
  123. ASSERT(full_key_name != NULL);
  124. ASSERT(value != NULL);
  125. return GetValueStaticHelper(full_key_name, value_name, REG_QWORD, value);
  126. }
  127. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  128. const wchar_t* value_name,
  129. float* value) {
  130. ASSERT(value != NULL);
  131. ASSERT(full_key_name != NULL);
  132. DWORD byte_count = 0;
  133. scoped_array<byte> buffer;
  134. HRESULT hr = GetValueStaticHelper(full_key_name, value_name,
  135. REG_BINARY, buffer.accept(), &byte_count);
  136. if (SUCCEEDED(hr)) {
  137. ASSERT(byte_count == sizeof(*value));
  138. if (byte_count == sizeof(*value)) {
  139. *value = *reinterpret_cast<float*>(buffer.get());
  140. }
  141. }
  142. return hr;
  143. }
  144. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  145. const wchar_t* value_name,
  146. double* value) {
  147. ASSERT(value != NULL);
  148. ASSERT(full_key_name != NULL);
  149. DWORD byte_count = 0;
  150. scoped_array<byte> buffer;
  151. HRESULT hr = GetValueStaticHelper(full_key_name, value_name,
  152. REG_BINARY, buffer.accept(), &byte_count);
  153. if (SUCCEEDED(hr)) {
  154. ASSERT(byte_count == sizeof(*value));
  155. if (byte_count == sizeof(*value)) {
  156. *value = *reinterpret_cast<double*>(buffer.get());
  157. }
  158. }
  159. return hr;
  160. }
  161. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  162. const wchar_t* value_name,
  163. wchar_t** value) {
  164. ASSERT(full_key_name != NULL);
  165. ASSERT(value != NULL);
  166. return GetValueStaticHelper(full_key_name, value_name, REG_SZ, value);
  167. }
  168. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  169. const wchar_t* value_name,
  170. std::wstring* value) {
  171. ASSERT(full_key_name != NULL);
  172. ASSERT(value != NULL);
  173. scoped_array<wchar_t> buffer;
  174. HRESULT hr = RegKey::GetValue(full_key_name, value_name, buffer.accept());
  175. if (SUCCEEDED(hr)) {
  176. value->assign(buffer.get());
  177. }
  178. return hr;
  179. }
  180. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  181. const wchar_t* value_name,
  182. std::vector<std::wstring>* value) {
  183. ASSERT(full_key_name != NULL);
  184. ASSERT(value != NULL);
  185. return GetValueStaticHelper(full_key_name, value_name, REG_MULTI_SZ, value);
  186. }
  187. HRESULT RegKey::GetValue(const wchar_t* full_key_name,
  188. const wchar_t* value_name,
  189. uint8** value,
  190. DWORD* byte_count) {
  191. ASSERT(full_key_name != NULL);
  192. ASSERT(value != NULL);
  193. ASSERT(byte_count != NULL);
  194. return GetValueStaticHelper(full_key_name, value_name,
  195. REG_BINARY, value, byte_count);
  196. }
  197. HRESULT RegKey::DeleteSubKey(const wchar_t* key_name) {
  198. ASSERT(key_name != NULL);
  199. ASSERT(h_key_ != NULL);
  200. LONG res = ::RegDeleteKey(h_key_, key_name);
  201. HRESULT hr = HRESULT_FROM_WIN32(res);
  202. if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
  203. hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) {
  204. hr = S_FALSE;
  205. }
  206. return hr;
  207. }
  208. HRESULT RegKey::DeleteValue(const wchar_t* value_name) {
  209. ASSERT(h_key_ != NULL);
  210. LONG res = ::RegDeleteValue(h_key_, value_name);
  211. HRESULT hr = HRESULT_FROM_WIN32(res);
  212. if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
  213. hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) {
  214. hr = S_FALSE;
  215. }
  216. return hr;
  217. }
  218. HRESULT RegKey::Close() {
  219. HRESULT hr = S_OK;
  220. if (h_key_ != NULL) {
  221. LONG res = ::RegCloseKey(h_key_);
  222. hr = HRESULT_FROM_WIN32(res);
  223. h_key_ = NULL;
  224. }
  225. return hr;
  226. }
  227. HRESULT RegKey::Create(HKEY parent_key,
  228. const wchar_t* key_name,
  229. wchar_t* lpszClass,
  230. DWORD options,
  231. REGSAM sam_desired,
  232. LPSECURITY_ATTRIBUTES lpSecAttr,
  233. LPDWORD lpdwDisposition) {
  234. ASSERT(key_name != NULL);
  235. ASSERT(parent_key != NULL);
  236. DWORD dw = 0;
  237. HKEY h_key = NULL;
  238. LONG res = ::RegCreateKeyEx(parent_key, key_name, 0, lpszClass, options,
  239. sam_desired, lpSecAttr, &h_key, &dw);
  240. HRESULT hr = HRESULT_FROM_WIN32(res);
  241. if (lpdwDisposition) {
  242. *lpdwDisposition = dw;
  243. }
  244. // we have to close the currently opened key
  245. // before replacing it with the new one
  246. if (hr == S_OK) {
  247. hr = Close();
  248. ASSERT(hr == S_OK);
  249. h_key_ = h_key;
  250. }
  251. return hr;
  252. }
  253. HRESULT RegKey::Open(HKEY parent_key,
  254. const wchar_t* key_name,
  255. REGSAM sam_desired) {
  256. ASSERT(key_name != NULL);
  257. ASSERT(parent_key != NULL);
  258. HKEY h_key = NULL;
  259. LONG res = ::RegOpenKeyEx(parent_key, key_name, 0, sam_desired, &h_key);
  260. HRESULT hr = HRESULT_FROM_WIN32(res);
  261. // we have to close the currently opened key
  262. // before replacing it with the new one
  263. if (hr == S_OK) {
  264. // close the currently opened key if any
  265. hr = Close();
  266. ASSERT(hr == S_OK);
  267. h_key_ = h_key;
  268. }
  269. return hr;
  270. }
  271. // save the key and all of its subkeys and values to a file
  272. HRESULT RegKey::Save(const wchar_t* full_key_name, const wchar_t* file_name) {
  273. ASSERT(full_key_name != NULL);
  274. ASSERT(file_name != NULL);
  275. std::wstring key_name(full_key_name);
  276. HKEY h_key = GetRootKeyInfo(&key_name);
  277. if (!h_key) {
  278. return E_FAIL;
  279. }
  280. RegKey key;
  281. HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ);
  282. if (FAILED(hr)) {
  283. return hr;
  284. }
  285. AdjustCurrentProcessPrivilege(SE_BACKUP_NAME, true);
  286. LONG res = ::RegSaveKey(key.h_key_, file_name, NULL);
  287. AdjustCurrentProcessPrivilege(SE_BACKUP_NAME, false);
  288. return HRESULT_FROM_WIN32(res);
  289. }
  290. // restore the key and all of its subkeys and values which are saved into a file
  291. HRESULT RegKey::Restore(const wchar_t* full_key_name,
  292. const wchar_t* file_name) {
  293. ASSERT(full_key_name != NULL);
  294. ASSERT(file_name != NULL);
  295. std::wstring key_name(full_key_name);
  296. HKEY h_key = GetRootKeyInfo(&key_name);
  297. if (!h_key) {
  298. return E_FAIL;
  299. }
  300. RegKey key;
  301. HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_WRITE);
  302. if (FAILED(hr)) {
  303. return hr;
  304. }
  305. AdjustCurrentProcessPrivilege(SE_RESTORE_NAME, true);
  306. LONG res = ::RegRestoreKey(key.h_key_, file_name, REG_FORCE_RESTORE);
  307. AdjustCurrentProcessPrivilege(SE_RESTORE_NAME, false);
  308. return HRESULT_FROM_WIN32(res);
  309. }
  310. // check if the current key has the specified subkey
  311. bool RegKey::HasSubkey(const wchar_t* key_name) const {
  312. ASSERT(key_name != NULL);
  313. RegKey key;
  314. HRESULT hr = key.Open(h_key_, key_name, KEY_READ);
  315. key.Close();
  316. return hr == S_OK;
  317. }
  318. // static flush key
  319. HRESULT RegKey::FlushKey(const wchar_t* full_key_name) {
  320. ASSERT(full_key_name != NULL);
  321. HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  322. // get the root HKEY
  323. std::wstring key_name(full_key_name);
  324. HKEY h_key = GetRootKeyInfo(&key_name);
  325. if (h_key != NULL) {
  326. LONG res = ::RegFlushKey(h_key);
  327. hr = HRESULT_FROM_WIN32(res);
  328. }
  329. return hr;
  330. }
  331. // static SET helper
  332. HRESULT RegKey::SetValueStaticHelper(const wchar_t* full_key_name,
  333. const wchar_t* value_name,
  334. DWORD type,
  335. LPVOID value,
  336. DWORD byte_count) {
  337. ASSERT(full_key_name != NULL);
  338. HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  339. // get the root HKEY
  340. std::wstring key_name(full_key_name);
  341. HKEY h_key = GetRootKeyInfo(&key_name);
  342. if (h_key != NULL) {
  343. RegKey key;
  344. hr = key.Create(h_key, key_name.c_str());
  345. if (hr == S_OK) {
  346. switch (type) {
  347. case REG_DWORD:
  348. hr = key.SetValue(value_name, *(static_cast<DWORD*>(value)));
  349. break;
  350. case REG_QWORD:
  351. hr = key.SetValue(value_name, *(static_cast<DWORD64*>(value)));
  352. break;
  353. case REG_SZ:
  354. hr = key.SetValue(value_name, static_cast<const wchar_t*>(value));
  355. break;
  356. case REG_BINARY:
  357. hr = key.SetValue(value_name, static_cast<const uint8*>(value),
  358. byte_count);
  359. break;
  360. case REG_MULTI_SZ:
  361. hr = key.SetValue(value_name, static_cast<const uint8*>(value),
  362. byte_count, type);
  363. break;
  364. default:
  365. ASSERT(false);
  366. hr = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH);
  367. break;
  368. }
  369. // close the key after writing
  370. HRESULT temp_hr = key.Close();
  371. if (hr == S_OK) {
  372. hr = temp_hr;
  373. }
  374. }
  375. }
  376. return hr;
  377. }
  378. // static GET helper
  379. HRESULT RegKey::GetValueStaticHelper(const wchar_t* full_key_name,
  380. const wchar_t* value_name,
  381. DWORD type,
  382. LPVOID value,
  383. DWORD* byte_count) {
  384. ASSERT(full_key_name != NULL);
  385. HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  386. // get the root HKEY
  387. std::wstring key_name(full_key_name);
  388. HKEY h_key = GetRootKeyInfo(&key_name);
  389. if (h_key != NULL) {
  390. RegKey key;
  391. hr = key.Open(h_key, key_name.c_str(), KEY_READ);
  392. if (hr == S_OK) {
  393. switch (type) {
  394. case REG_DWORD:
  395. hr = key.GetValue(value_name, reinterpret_cast<DWORD*>(value));
  396. break;
  397. case REG_QWORD:
  398. hr = key.GetValue(value_name, reinterpret_cast<DWORD64*>(value));
  399. break;
  400. case REG_SZ:
  401. hr = key.GetValue(value_name, reinterpret_cast<wchar_t**>(value));
  402. break;
  403. case REG_MULTI_SZ:
  404. hr = key.GetValue(value_name, reinterpret_cast<
  405. std::vector<std::wstring>*>(value));
  406. break;
  407. case REG_BINARY:
  408. hr = key.GetValue(value_name, reinterpret_cast<uint8**>(value),
  409. byte_count);
  410. break;
  411. default:
  412. ASSERT(false);
  413. hr = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH);
  414. break;
  415. }
  416. // close the key after writing
  417. HRESULT temp_hr = key.Close();
  418. if (hr == S_OK) {
  419. hr = temp_hr;
  420. }
  421. }
  422. }
  423. return hr;
  424. }
  425. // GET helper
  426. HRESULT RegKey::GetValueHelper(const wchar_t* value_name,
  427. DWORD* type,
  428. uint8** value,
  429. DWORD* byte_count) const {
  430. ASSERT(byte_count != NULL);
  431. ASSERT(value != NULL);
  432. ASSERT(type != NULL);
  433. // init return buffer
  434. *value = NULL;
  435. // get the size of the return data buffer
  436. LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, type, NULL, byte_count);
  437. HRESULT hr = HRESULT_FROM_WIN32(res);
  438. if (hr == S_OK) {
  439. // if the value length is 0, nothing to do
  440. if (*byte_count != 0) {
  441. // allocate the buffer
  442. *value = new byte[*byte_count];
  443. ASSERT(*value != NULL);
  444. // make the call again to get the data
  445. res = ::SHQueryValueEx(h_key_, value_name, NULL,
  446. type, *value, byte_count);
  447. hr = HRESULT_FROM_WIN32(res);
  448. ASSERT(hr == S_OK);
  449. }
  450. }
  451. return hr;
  452. }
  453. // Int32 Get
  454. HRESULT RegKey::GetValue(const wchar_t* value_name, DWORD* value) const {
  455. ASSERT(value != NULL);
  456. DWORD type = 0;
  457. DWORD byte_count = sizeof(DWORD);
  458. LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
  459. value, &byte_count);
  460. HRESULT hr = HRESULT_FROM_WIN32(res);
  461. ASSERT((hr != S_OK) || (type == REG_DWORD));
  462. ASSERT((hr != S_OK) || (byte_count == sizeof(DWORD)));
  463. return hr;
  464. }
  465. // Int64 Get
  466. HRESULT RegKey::GetValue(const wchar_t* value_name, DWORD64* value) const {
  467. ASSERT(value != NULL);
  468. DWORD type = 0;
  469. DWORD byte_count = sizeof(DWORD64);
  470. LONG res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
  471. value, &byte_count);
  472. HRESULT hr = HRESULT_FROM_WIN32(res);
  473. ASSERT((hr != S_OK) || (type == REG_QWORD));
  474. ASSERT((hr != S_OK) || (byte_count == sizeof(DWORD64)));
  475. return hr;
  476. }
  477. // String Get
  478. HRESULT RegKey::GetValue(const wchar_t* value_name, wchar_t** value) const {
  479. ASSERT(value != NULL);
  480. DWORD byte_count = 0;
  481. DWORD type = 0;
  482. // first get the size of the string buffer
  483. LONG res = ::SHQueryValueEx(h_key_, value_name, NULL,
  484. &type, NULL, &byte_count);
  485. HRESULT hr = HRESULT_FROM_WIN32(res);
  486. if (hr == S_OK) {
  487. // allocate room for the string and a terminating \0
  488. *value = new wchar_t[(byte_count / sizeof(wchar_t)) + 1];
  489. if ((*value) != NULL) {
  490. if (byte_count != 0) {
  491. // make the call again
  492. res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
  493. *value, &byte_count);
  494. hr = HRESULT_FROM_WIN32(res);
  495. } else {
  496. (*value)[0] = L'\0';
  497. }
  498. ASSERT((hr != S_OK) || (type == REG_SZ) ||
  499. (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ));
  500. } else {
  501. hr = E_OUTOFMEMORY;
  502. }
  503. }
  504. return hr;
  505. }
  506. // get a string value
  507. HRESULT RegKey::GetValue(const wchar_t* value_name, std::wstring* value) const {
  508. ASSERT(value != NULL);
  509. DWORD byte_count = 0;
  510. DWORD type = 0;
  511. // first get the size of the string buffer
  512. LONG res = ::SHQueryValueEx(h_key_, value_name, NULL,
  513. &type, NULL, &byte_count);
  514. HRESULT hr = HRESULT_FROM_WIN32(res);
  515. if (hr == S_OK) {
  516. if (byte_count != 0) {
  517. // Allocate some memory and make the call again
  518. value->resize(byte_count / sizeof(wchar_t) + 1);
  519. res = ::SHQueryValueEx(h_key_, value_name, NULL, &type,
  520. &value->at(0), &byte_count);
  521. hr = HRESULT_FROM_WIN32(res);
  522. value->resize(wcslen(value->data()));
  523. } else {
  524. value->clear();
  525. }
  526. ASSERT((hr != S_OK) || (type == REG_SZ) ||
  527. (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ));
  528. }
  529. return hr;
  530. }
  531. // convert REG_MULTI_SZ bytes to string array
  532. HRESULT RegKey::MultiSZBytesToStringArray(const uint8* buffer,
  533. DWORD byte_count,
  534. std::vector<std::wstring>* value) {
  535. ASSERT(buffer != NULL);
  536. ASSERT(value != NULL);
  537. const wchar_t* data = reinterpret_cast<const wchar_t*>(buffer);
  538. DWORD data_len = byte_count / sizeof(wchar_t);
  539. value->clear();
  540. if (data_len > 1) {
  541. // must be terminated by two null characters
  542. if (data[data_len - 1] != 0 || data[data_len - 2] != 0) {
  543. return E_INVALIDARG;
  544. }
  545. // put null-terminated strings into arrays
  546. while (*data) {
  547. std::wstring str(data);
  548. value->push_back(str);
  549. data += str.length() + 1;
  550. }
  551. }
  552. return S_OK;
  553. }
  554. // get a std::vector<std::wstring> value from REG_MULTI_SZ type
  555. HRESULT RegKey::GetValue(const wchar_t* value_name,
  556. std::vector<std::wstring>* value) const {
  557. ASSERT(value != NULL);
  558. DWORD byte_count = 0;
  559. DWORD type = 0;
  560. uint8* buffer = 0;
  561. // first get the size of the buffer
  562. HRESULT hr = GetValueHelper(value_name, &type, &buffer, &byte_count);
  563. ASSERT((hr != S_OK) || (type == REG_MULTI_SZ));
  564. if (SUCCEEDED(hr)) {
  565. hr = MultiSZBytesToStringArray(buffer, byte_count, value);
  566. }
  567. return hr;
  568. }
  569. // Binary data Get
  570. HRESULT RegKey::GetValue(const wchar_t* value_name,
  571. uint8** value,
  572. DWORD* byte_count) const {
  573. ASSERT(byte_count != NULL);
  574. ASSERT(value != NULL);
  575. DWORD type = 0;
  576. HRESULT hr = GetValueHelper(value_name, &type, value, byte_count);
  577. ASSERT((hr != S_OK) || (type == REG_MULTI_SZ) || (type == REG_BINARY));
  578. return hr;
  579. }
  580. // Raw data get
  581. HRESULT RegKey::GetValue(const wchar_t* value_name,
  582. uint8** value,
  583. DWORD* byte_count,
  584. DWORD*type) const {
  585. ASSERT(type != NULL);
  586. ASSERT(byte_count != NULL);
  587. ASSERT(value != NULL);
  588. return GetValueHelper(value_name, type, value, byte_count);
  589. }
  590. // Int32 set
  591. HRESULT RegKey::SetValue(const wchar_t* value_name, DWORD value) const {
  592. ASSERT(h_key_ != NULL);
  593. LONG res = ::RegSetValueEx(h_key_, value_name, NULL, REG_DWORD,
  594. reinterpret_cast<const uint8*>(&value),
  595. sizeof(DWORD));
  596. return HRESULT_FROM_WIN32(res);
  597. }
  598. // Int64 set
  599. HRESULT RegKey::SetValue(const wchar_t* value_name, DWORD64 value) const {
  600. ASSERT(h_key_ != NULL);
  601. LONG res = ::RegSetValueEx(h_key_, value_name, NULL, REG_QWORD,
  602. reinterpret_cast<const uint8*>(&value),
  603. sizeof(DWORD64));
  604. return HRESULT_FROM_WIN32(res);
  605. }
  606. // String set
  607. HRESULT RegKey::SetValue(const wchar_t* value_name,
  608. const wchar_t* value) const {
  609. ASSERT(value != NULL);
  610. ASSERT(h_key_ != NULL);
  611. LONG res = ::RegSetValueEx(h_key_, value_name, NULL, REG_SZ,
  612. reinterpret_cast<const uint8*>(value),
  613. (lstrlen(value) + 1) * sizeof(wchar_t));
  614. return HRESULT_FROM_WIN32(res);
  615. }
  616. // Binary data set
  617. HRESULT RegKey::SetValue(const wchar_t* value_name,
  618. const uint8* value,
  619. DWORD byte_count) const {
  620. ASSERT(h_key_ != NULL);
  621. // special case - if 'value' is NULL make sure byte_count is zero
  622. if (value == NULL) {
  623. byte_count = 0;
  624. }
  625. LONG res = ::RegSetValueEx(h_key_, value_name, NULL,
  626. REG_BINARY, value, byte_count);
  627. return HRESULT_FROM_WIN32(res);
  628. }
  629. // Raw data set
  630. HRESULT RegKey::SetValue(const wchar_t* value_name,
  631. const uint8* value,
  632. DWORD byte_count,
  633. DWORD type) const {
  634. ASSERT(value != NULL);
  635. ASSERT(h_key_ != NULL);
  636. LONG res = ::RegSetValueEx(h_key_, value_name, NULL, type, value, byte_count);
  637. return HRESULT_FROM_WIN32(res);
  638. }
  639. bool RegKey::HasKey(const wchar_t* full_key_name) {
  640. ASSERT(full_key_name != NULL);
  641. // get the root HKEY
  642. std::wstring key_name(full_key_name);
  643. HKEY h_key = GetRootKeyInfo(&key_name);
  644. if (h_key != NULL) {
  645. RegKey key;
  646. HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ);
  647. key.Close();
  648. return S_OK == hr;
  649. }
  650. return false;
  651. }
  652. // static version of HasValue
  653. bool RegKey::HasValue(const wchar_t* full_key_name, const wchar_t* value_name) {
  654. ASSERT(full_key_name != NULL);
  655. bool has_value = false;
  656. // get the root HKEY
  657. std::wstring key_name(full_key_name);
  658. HKEY h_key = GetRootKeyInfo(&key_name);
  659. if (h_key != NULL) {
  660. RegKey key;
  661. if (key.Open(h_key, key_name.c_str(), KEY_READ) == S_OK) {
  662. has_value = key.HasValue(value_name);
  663. key.Close();
  664. }
  665. }
  666. return has_value;
  667. }
  668. HRESULT RegKey::GetValueType(const wchar_t* full_key_name,
  669. const wchar_t* value_name,
  670. DWORD* value_type) {
  671. ASSERT(full_key_name != NULL);
  672. ASSERT(value_type != NULL);
  673. *value_type = REG_NONE;
  674. std::wstring key_name(full_key_name);
  675. HKEY h_key = GetRootKeyInfo(&key_name);
  676. RegKey key;
  677. HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ);
  678. if (SUCCEEDED(hr)) {
  679. LONG res = ::SHQueryValueEx(key.h_key_, value_name, NULL, value_type,
  680. NULL, NULL);
  681. if (res != ERROR_SUCCESS) {
  682. hr = HRESULT_FROM_WIN32(res);
  683. }
  684. }
  685. return hr;
  686. }
  687. HRESULT RegKey::DeleteKey(const wchar_t* full_key_name) {
  688. ASSERT(full_key_name != NULL);
  689. return DeleteKey(full_key_name, true);
  690. }
  691. HRESULT RegKey::DeleteKey(const wchar_t* full_key_name, bool recursively) {
  692. ASSERT(full_key_name != NULL);
  693. // need to open the parent key first
  694. // get the root HKEY
  695. std::wstring key_name(full_key_name);
  696. HKEY h_key = GetRootKeyInfo(&key_name);
  697. // get the parent key
  698. std::wstring parent_key(GetParentKeyInfo(&key_name));
  699. RegKey key;
  700. HRESULT hr = key.Open(h_key, parent_key.c_str());
  701. if (hr == S_OK) {
  702. hr = recursively ? key.RecurseDeleteSubKey(key_name.c_str())
  703. : key.DeleteSubKey(key_name.c_str());
  704. } else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
  705. hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) {
  706. hr = S_FALSE;
  707. }
  708. key.Close();
  709. return hr;
  710. }
  711. HRESULT RegKey::DeleteValue(const wchar_t* full_key_name,
  712. const wchar_t* value_name) {
  713. ASSERT(full_key_name != NULL);
  714. HRESULT hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  715. // get the root HKEY
  716. std::wstring key_name(full_key_name);
  717. HKEY h_key = GetRootKeyInfo(&key_name);
  718. if (h_key != NULL) {
  719. RegKey key;
  720. hr = key.Open(h_key, key_name.c_str());
  721. if (hr == S_OK) {
  722. hr = key.DeleteValue(value_name);
  723. key.Close();
  724. }
  725. }
  726. return hr;
  727. }
  728. HRESULT RegKey::RecurseDeleteSubKey(const wchar_t* key_name) {
  729. ASSERT(key_name != NULL);
  730. RegKey key;
  731. HRESULT hr = key.Open(h_key_, key_name);
  732. if (hr == S_OK) {
  733. // enumerate all subkeys of this key and recursivelly delete them
  734. FILETIME time = {0};
  735. wchar_t key_name_buf[kMaxKeyNameChars] = {0};
  736. DWORD key_name_buf_size = kMaxKeyNameChars;
  737. while (hr == S_OK &&
  738. ::RegEnumKeyEx(key.h_key_, 0, key_name_buf, &key_name_buf_size,
  739. NULL, NULL, NULL, &time) == ERROR_SUCCESS) {
  740. hr = key.RecurseDeleteSubKey(key_name_buf);
  741. // restore the buffer size
  742. key_name_buf_size = kMaxKeyNameChars;
  743. }
  744. // close the top key
  745. key.Close();
  746. }
  747. if (hr == S_OK) {
  748. // the key has no more children keys
  749. // delete the key and all of its values
  750. hr = DeleteSubKey(key_name);
  751. }
  752. return hr;
  753. }
  754. HKEY RegKey::GetRootKeyInfo(std::wstring* full_key_name) {
  755. ASSERT(full_key_name != NULL);
  756. HKEY h_key = NULL;
  757. // get the root HKEY
  758. size_t index = full_key_name->find(L'\\');
  759. std::wstring root_key;
  760. if (index == -1) {
  761. root_key = *full_key_name;
  762. *full_key_name = L"";
  763. } else {
  764. root_key = full_key_name->substr(0, index);
  765. *full_key_name = full_key_name->substr(index + 1,
  766. full_key_name->length() - index - 1);
  767. }
  768. for (std::wstring::iterator iter = root_key.begin();
  769. iter != root_key.end(); ++iter) {
  770. *iter = toupper(*iter);
  771. }
  772. if (!root_key.compare(L"HKLM") ||
  773. !root_key.compare(L"HKEY_LOCAL_MACHINE")) {
  774. h_key = HKEY_LOCAL_MACHINE;
  775. } else if (!root_key.compare(L"HKCU") ||
  776. !root_key.compare(L"HKEY_CURRENT_USER")) {
  777. h_key = HKEY_CURRENT_USER;
  778. } else if (!root_key.compare(L"HKU") ||
  779. !root_key.compare(L"HKEY_USERS")) {
  780. h_key = HKEY_USERS;
  781. } else if (!root_key.compare(L"HKCR") ||
  782. !root_key.compare(L"HKEY_CLASSES_ROOT")) {
  783. h_key = HKEY_CLASSES_ROOT;
  784. }
  785. return h_key;
  786. }
  787. // Returns true if this key name is 'safe' for deletion
  788. // (doesn't specify a key root)
  789. bool RegKey::SafeKeyNameForDeletion(const wchar_t* key_name) {
  790. ASSERT(key_name != NULL);
  791. std::wstring key(key_name);
  792. HKEY root_key = GetRootKeyInfo(&key);
  793. if (!root_key) {
  794. key = key_name;
  795. }
  796. if (key.empty()) {
  797. return false;
  798. }
  799. bool found_subkey = false, backslash_found = false;
  800. for (size_t i = 0 ; i < key.length() ; ++i) {
  801. if (key[i] == L'\\') {
  802. backslash_found = true;
  803. } else if (backslash_found) {
  804. found_subkey = true;
  805. break;
  806. }
  807. }
  808. return (root_key == HKEY_USERS) ? found_subkey : true;
  809. }
  810. std::wstring RegKey::GetParentKeyInfo(std::wstring* key_name) {
  811. ASSERT(key_name != NULL);
  812. // get the parent key
  813. size_t index = key_name->rfind(L'\\');
  814. std::wstring parent_key;
  815. if (index == -1) {
  816. parent_key = L"";
  817. } else {
  818. parent_key = key_name->substr(0, index);
  819. *key_name = key_name->substr(index + 1, key_name->length() - index - 1);
  820. }
  821. return parent_key;
  822. }
  823. // get the number of values for this key
  824. uint32 RegKey::GetValueCount() {
  825. DWORD num_values = 0;
  826. LONG res = ::RegQueryInfoKey(
  827. h_key_, // key handle
  828. NULL, // buffer for class name
  829. NULL, // size of class string
  830. NULL, // reserved
  831. NULL, // number of subkeys
  832. NULL, // longest subkey size
  833. NULL, // longest class string
  834. &num_values, // number of values for this key
  835. NULL, // longest value name
  836. NULL, // longest value data
  837. NULL, // security descriptor
  838. NULL); // last write time
  839. ASSERT(res == ERROR_SUCCESS);
  840. return num_values;
  841. }
  842. // Enumerators for the value_names for this key
  843. // Called to get the value name for the given value name index
  844. // Use GetValueCount() to get the total value_name count for this key
  845. // Returns failure if no key at the specified index
  846. HRESULT RegKey::GetValueNameAt(int index, std::wstring* value_name,
  847. DWORD* type) {
  848. ASSERT(value_name != NULL);
  849. LONG res = ERROR_SUCCESS;
  850. wchar_t value_name_buf[kMaxValueNameChars] = {0};
  851. DWORD value_name_buf_size = kMaxValueNameChars;
  852. res = ::RegEnumValue(h_key_, index, value_name_buf, &value_name_buf_size,
  853. NULL, type, NULL, NULL);
  854. if (res == ERROR_SUCCESS) {
  855. value_name->assign(value_name_buf);
  856. }
  857. return HRESULT_FROM_WIN32(res);
  858. }
  859. uint32 RegKey::GetSubkeyCount() {
  860. // number of values for key
  861. DWORD num_subkeys = 0;
  862. LONG res = ::RegQueryInfoKey(
  863. h_key_, // key handle
  864. NULL, // buffer for class name
  865. NULL, // size of class string
  866. NULL, // reserved
  867. &num_subkeys, // number of subkeys
  868. NULL, // longest subkey size
  869. NULL, // longest class string
  870. NULL, // number of values for this key
  871. NULL, // longest value name
  872. NULL, // longest value data
  873. NULL, // security descriptor
  874. NULL); // last write time
  875. ASSERT(res == ERROR_SUCCESS);
  876. return num_subkeys;
  877. }
  878. HRESULT RegKey::GetSubkeyNameAt(int index, std::wstring* key_name) {
  879. ASSERT(key_name != NULL);
  880. LONG res = ERROR_SUCCESS;
  881. wchar_t key_name_buf[kMaxKeyNameChars] = {0};
  882. DWORD key_name_buf_size = kMaxKeyNameChars;
  883. res = ::RegEnumKeyEx(h_key_, index, key_name_buf, &key_name_buf_size,
  884. NULL, NULL, NULL, NULL);
  885. if (res == ERROR_SUCCESS) {
  886. key_name->assign(key_name_buf);
  887. }
  888. return HRESULT_FROM_WIN32(res);
  889. }
  890. // Is the key empty: having no sub-keys and values
  891. bool RegKey::IsKeyEmpty(const wchar_t* full_key_name) {
  892. ASSERT(full_key_name != NULL);
  893. bool is_empty = true;
  894. // Get the root HKEY
  895. std::wstring key_name(full_key_name);
  896. HKEY h_key = GetRootKeyInfo(&key_name);
  897. // Open the key to check
  898. if (h_key != NULL) {
  899. RegKey key;
  900. HRESULT hr = key.Open(h_key, key_name.c_str(), KEY_READ);
  901. if (SUCCEEDED(hr)) {
  902. is_empty = key.GetSubkeyCount() == 0 && key.GetValueCount() == 0;
  903. key.Close();
  904. }
  905. }
  906. return is_empty;
  907. }
  908. bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable) {
  909. ASSERT(privilege != NULL);
  910. bool ret = false;
  911. HANDLE token;
  912. if (::OpenProcessToken(::GetCurrentProcess(),
  913. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
  914. LUID luid;
  915. memset(&luid, 0, sizeof(luid));
  916. if (::LookupPrivilegeValue(NULL, privilege, &luid)) {
  917. TOKEN_PRIVILEGES privs;
  918. privs.PrivilegeCount = 1;
  919. privs.Privileges[0].Luid = luid;
  920. privs.Privileges[0].Attributes = to_enable ? SE_PRIVILEGE_ENABLED : 0;
  921. if (::AdjustTokenPrivileges(token, FALSE, &privs, 0, NULL, 0)) {
  922. ret = true;
  923. } else {
  924. LOG_GLE(LS_ERROR) << "AdjustTokenPrivileges failed";
  925. }
  926. } else {
  927. LOG_GLE(LS_ERROR) << "LookupPrivilegeValue failed";
  928. }
  929. CloseHandle(token);
  930. } else {
  931. LOG_GLE(LS_ERROR) << "OpenProcessToken(GetCurrentProcess) failed";
  932. }
  933. return ret;
  934. }
  935. } // namespace talk_base