reg_key.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2006 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "reg_key.h"
  17. #include <vector>
  18. using namespace std;
  19. Creg_key::Creg_key(HKEY key, const string& name, REGSAM sam_desired)
  20. {
  21. m_h = NULL;
  22. if (open(key, name, sam_desired) != ERROR_SUCCESS)
  23. throw exception();
  24. }
  25. Creg_key::Creg_key(const Creg_key& key, const string& name, REGSAM sam_desired)
  26. {
  27. m_h = NULL;
  28. if (open(key.m_h, name, sam_desired) != ERROR_SUCCESS)
  29. throw exception();
  30. }
  31. Creg_key::~Creg_key()
  32. {
  33. close();
  34. }
  35. LONG Creg_key::create(HKEY key, const string& name)
  36. {
  37. close();
  38. return RegCreateKeyExA(key, name.c_str(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &m_h, NULL);
  39. }
  40. LONG Creg_key::open(HKEY key, const string& name, REGSAM sam_desired)
  41. {
  42. close();
  43. return RegOpenKeyExA(key, name.c_str(), 0, sam_desired, &m_h);
  44. };
  45. LONG Creg_key::open(const Creg_key& key, const string& name, REGSAM sam_desired)
  46. {
  47. return open(key.m_h, name, sam_desired);
  48. };
  49. LONG Creg_key::close()
  50. {
  51. if (!m_h)
  52. return ERROR_SUCCESS;
  53. LONG result = RegCloseKey(m_h);
  54. m_h = NULL;
  55. return result;
  56. }
  57. LONG Creg_key::query_value(const string& name, string& value)
  58. {
  59. DWORD cb_d = 0;
  60. LONG result = RegQueryValueExA(m_h, name.c_str(), NULL, NULL, NULL, &cb_d);
  61. if (result != ERROR_SUCCESS)
  62. return result;
  63. if (!cb_d)
  64. {
  65. value.erase();
  66. return result;
  67. }
  68. vector<BYTE> d(cb_d);
  69. result = RegQueryValueExA(m_h, name.c_str(), NULL, NULL, &d.front(), &cb_d);
  70. if (result == ERROR_SUCCESS)
  71. {
  72. if (cb_d)
  73. value.assign(reinterpret_cast<char*>(&d.front()), cb_d - 1);
  74. else
  75. value.erase();
  76. }
  77. return result;
  78. }
  79. string Creg_key::query_value(const string& name)
  80. {
  81. string d;
  82. if (query_value(name, d) != ERROR_SUCCESS)
  83. throw exception();
  84. return d;
  85. }
  86. LONG Creg_key::set_value(const string& name, const string& value)
  87. {
  88. return RegSetValueExA(m_h, name.c_str(), 0, REG_SZ, reinterpret_cast<const BYTE*>(value.c_str()), value.size());
  89. }