RegistryUtil.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #include "stdafx.h"
  15. #include "RegistryUtil.h"
  16. using namespace std;
  17. #define SIZE 4096
  18. bool RegistryUtil::ReadRegistry(const wchar_t* key, const wchar_t* name, int* result)
  19. {
  20. wstring* strResult = new wstring();
  21. if(!ReadRegistry(key, name, strResult))
  22. {
  23. return false;
  24. }
  25. *result = stoi( strResult->c_str() );
  26. return true;
  27. }
  28. bool RegistryUtil::ReadRegistry(const wchar_t* key, const wchar_t* name, wstring* result)
  29. {
  30. HRESULT hResult;
  31. HKEY rootKey = NULL;
  32. hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CURRENT_USER, (LPCWSTR)key, NULL, KEY_READ, &rootKey));
  33. if(!SUCCEEDED(hResult))
  34. {
  35. return false;
  36. }
  37. wchar_t value[SIZE];
  38. DWORD value_length = SIZE;
  39. hResult = RegQueryValueEx(rootKey, (LPCWSTR)name, NULL, NULL, (LPBYTE)value, &value_length );
  40. if(!SUCCEEDED(hResult))
  41. {
  42. return false;
  43. }
  44. result->append(value);
  45. HRESULT hResult2 = RegCloseKey(rootKey);
  46. if (!SUCCEEDED(hResult2))
  47. {
  48. return false;
  49. }
  50. return true;
  51. }