registry.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Registry.cpp
  19. // Simple interface for storing/retreiving registry values
  20. // Author: Matthew D. Campbell, December 2001
  21. #include <string>
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #include "Registry.h"
  25. bool getStringFromRegistry(HKEY root, std::string path, std::string key, std::string& val)
  26. {
  27. HKEY handle;
  28. unsigned char buffer[256];
  29. unsigned long size = 256;
  30. unsigned long type;
  31. int returnValue;
  32. if ((returnValue = RegOpenKeyEx( root, path.c_str(), 0, KEY_ALL_ACCESS, &handle )) == ERROR_SUCCESS)
  33. {
  34. returnValue = RegQueryValueEx(handle, key.c_str(), NULL, &type, (unsigned char *) &buffer, &size);
  35. RegCloseKey( handle );
  36. }
  37. if (returnValue == ERROR_SUCCESS)
  38. {
  39. val = (char *)buffer;
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool getUnsignedIntFromRegistry(HKEY root, std::string path, std::string key, unsigned int& val)
  45. {
  46. HKEY handle;
  47. unsigned long buffer;
  48. unsigned long size = sizeof(buffer);
  49. unsigned long type;
  50. int returnValue;
  51. if ((returnValue = RegOpenKeyEx( root, path.c_str(), 0, KEY_ALL_ACCESS, &handle )) == ERROR_SUCCESS)
  52. {
  53. returnValue = RegQueryValueEx(handle, key.c_str(), NULL, &type, (unsigned char *) &buffer, &size);
  54. RegCloseKey( handle );
  55. }
  56. if (returnValue == ERROR_SUCCESS)
  57. {
  58. val = buffer;
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool setStringInRegistry( HKEY root, std::string path, std::string key, std::string val)
  64. {
  65. HKEY handle;
  66. unsigned long type;
  67. unsigned long returnValue;
  68. int size;
  69. if ((returnValue = RegCreateKeyEx( root, path.c_str(), 0, "REG_NONE", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &handle, NULL )) == ERROR_SUCCESS)
  70. {
  71. type = REG_SZ;
  72. size = val.length()+1;
  73. returnValue = RegSetValueEx(handle, key.c_str(), 0, type, (unsigned char *)val.c_str(), size);
  74. RegCloseKey( handle );
  75. }
  76. return (returnValue == ERROR_SUCCESS);
  77. }
  78. bool setUnsignedIntInRegistry( HKEY root, std::string path, std::string key, unsigned int val)
  79. {
  80. HKEY handle;
  81. unsigned long type;
  82. unsigned long returnValue;
  83. int size;
  84. if ((returnValue = RegCreateKeyEx( root, path.c_str(), 0, "REG_NONE", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &handle, NULL )) == ERROR_SUCCESS)
  85. {
  86. type = REG_DWORD;
  87. size = 4;
  88. returnValue = RegSetValueEx(handle, key.c_str(), 0, type, (unsigned char *)&val, size);
  89. RegCloseKey( handle );
  90. }
  91. return (returnValue == ERROR_SUCCESS);
  92. }
  93. bool GetStringFromRegistry(std::string path, std::string key, std::string& val)
  94. {
  95. std::string fullPath = "SOFTWARE\\Electronic Arts\\EA Games\\Generals";
  96. fullPath.append(path);
  97. if (getStringFromRegistry(HKEY_LOCAL_MACHINE, fullPath.c_str(), key.c_str(), val))
  98. {
  99. return true;
  100. }
  101. return getStringFromRegistry(HKEY_CURRENT_USER, fullPath.c_str(), key.c_str(), val);
  102. }
  103. bool GetUnsignedIntFromRegistry(std::string path, std::string key, unsigned int& val)
  104. {
  105. std::string fullPath = "SOFTWARE\\Electronic Arts\\EA Games\\Generals";
  106. fullPath.append(path);
  107. if (getUnsignedIntFromRegistry(HKEY_LOCAL_MACHINE, fullPath.c_str(), key.c_str(), val))
  108. {
  109. return true;
  110. }
  111. return getUnsignedIntFromRegistry(HKEY_CURRENT_USER, fullPath.c_str(), key.c_str(), val);
  112. }