registry.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Westwood Library *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/registry.cpp $*
  25. * *
  26. * $Author:: Patrick $*
  27. * *
  28. * $Modtime:: 8/16/01 11:40a $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "registry.h"
  35. #include <assert.h>
  36. #include <windows.h>
  37. //#include "wwdebug.h"
  38. /*
  39. **
  40. */
  41. RegistryClass::RegistryClass( const char * sub_key ) :
  42. IsValid( false )
  43. {
  44. DWORD disposition;
  45. HKEY key;
  46. assert( sizeof(HKEY) == sizeof(int) );
  47. if (::RegCreateKeyEx( HKEY_LOCAL_MACHINE, sub_key,
  48. 0, NULL, 0, KEY_ALL_ACCESS, NULL,
  49. &key, &disposition ) == ERROR_SUCCESS) {
  50. IsValid = true;
  51. Key = (int)key;
  52. }
  53. }
  54. RegistryClass::~RegistryClass( void )
  55. {
  56. if ( IsValid ) {
  57. if (::RegCloseKey( (HKEY)Key ) != ERROR_SUCCESS) { // Close the reg key
  58. }
  59. IsValid = false;
  60. }
  61. }
  62. int RegistryClass::Get_Int( const char * name, int def_value )
  63. {
  64. assert( IsValid );
  65. DWORD type, data = 0, data_len = sizeof( data );
  66. if (( ::RegQueryValueEx( (HKEY)Key, name, NULL, &type, (LPBYTE)&data, &data_len ) ==
  67. ERROR_SUCCESS ) && ( type == REG_DWORD )) {
  68. } else {
  69. data = def_value;
  70. }
  71. return data;
  72. }
  73. void RegistryClass::Set_Int( const char * name, int value )
  74. {
  75. assert( IsValid );
  76. if (::RegSetValueEx( (HKEY)Key, name, 0, REG_DWORD, (LPBYTE)&value, sizeof( DWORD ) ) !=
  77. ERROR_SUCCESS) {
  78. }
  79. }
  80. bool RegistryClass::Get_Bool( const char * name, bool def_value )
  81. {
  82. return (Get_Int( name, def_value ) != 0);
  83. }
  84. void RegistryClass::Set_Bool( const char * name, bool value )
  85. {
  86. Set_Int( name, value ? 1 : 0 );
  87. }
  88. float RegistryClass::Get_Float( const char * name, float def_value )
  89. {
  90. assert( IsValid );
  91. float data = 0;
  92. DWORD type, data_len = sizeof( data );
  93. if (( ::RegQueryValueEx( (HKEY)Key, name, NULL, &type, (LPBYTE)&data, &data_len ) ==
  94. ERROR_SUCCESS ) && ( type == REG_DWORD )) {
  95. } else {
  96. data = def_value;
  97. }
  98. return data;
  99. }
  100. void RegistryClass::Set_Float( const char * name, float value )
  101. {
  102. assert( IsValid );
  103. if (::RegSetValueEx( (HKEY)Key, name, 0, REG_DWORD, (LPBYTE)&value, sizeof( DWORD ) ) !=
  104. ERROR_SUCCESS) {
  105. }
  106. }
  107. int RegistryClass::Get_Bin_Size( const char * name )
  108. {
  109. assert( IsValid );
  110. unsigned long size = 0;
  111. ::RegQueryValueEx( (HKEY)Key, name, NULL, NULL, NULL, &size );
  112. return size;
  113. }
  114. void RegistryClass::Get_Bin( const char * name, void *buffer, int buffer_size )
  115. {
  116. assert( IsValid );
  117. assert( buffer != NULL );
  118. assert( buffer_size > 0 );
  119. unsigned long size = buffer_size;
  120. ::RegQueryValueEx( (HKEY)Key, name, NULL, NULL, (LPBYTE)buffer, &size );
  121. return ;
  122. }
  123. void RegistryClass::Set_Bin( const char * name, const void *buffer, int buffer_size )
  124. {
  125. assert( IsValid );
  126. assert( buffer != NULL );
  127. assert( buffer_size > 0 );
  128. ::RegSetValueEx( (HKEY)Key, name, 0, REG_BINARY, (LPBYTE)buffer, buffer_size );
  129. return ;
  130. }
  131. void RegistryClass::Get_String( const char * name, StringClass &string, const char *default_string )
  132. {
  133. assert( IsValid );
  134. string = (default_string == NULL) ? "" : default_string;
  135. //
  136. // Get the size of the entry
  137. //
  138. DWORD data_size = 0;
  139. DWORD type = 0;
  140. LONG result = ::RegQueryValueEx ((HKEY)Key, name, NULL, &type, NULL, &data_size);
  141. if (result == ERROR_SUCCESS && type == REG_SZ) {
  142. //
  143. // Read the entry from the registry
  144. //
  145. ::RegQueryValueEx ((HKEY)Key, name, NULL, &type,
  146. (LPBYTE)string.Get_Buffer (data_size), &data_size);
  147. }
  148. return ;
  149. }
  150. char *RegistryClass::Get_String( const char * name, char *value, int value_size,
  151. const char * default_string )
  152. {
  153. assert( IsValid );
  154. DWORD type = 0;
  155. if (( ::RegQueryValueEx( (HKEY)Key, name, NULL, &type, (LPBYTE)value, (DWORD*)&value_size ) ==
  156. ERROR_SUCCESS ) && ( type == REG_SZ )) {
  157. } else {
  158. //*value = 0;
  159. //value = (char *) default_string;
  160. if (default_string == NULL) {
  161. *value = 0;
  162. } else {
  163. assert(strlen(default_string) < (unsigned int) value_size);
  164. strcpy(value, default_string);
  165. }
  166. }
  167. return value;
  168. }
  169. void RegistryClass::Set_String( const char * name, const char *value )
  170. {
  171. assert( IsValid );
  172. int size = strlen( value ) + 1; // must include NULL
  173. if (::RegSetValueEx( (HKEY)Key, name, 0, REG_SZ, (LPBYTE)value, size ) !=
  174. ERROR_SUCCESS ) {
  175. }
  176. }
  177. void RegistryClass::Get_Value_List( DynamicVectorClass<StringClass> &list )
  178. {
  179. char value_name[128];
  180. //
  181. // Simply enumerate all the values in this key
  182. //
  183. int index = 0;
  184. unsigned long sizeof_name = sizeof (value_name);
  185. while (::RegEnumValue ((HKEY)Key, index ++,
  186. value_name, &sizeof_name, 0, NULL, NULL, NULL) == ERROR_SUCCESS)
  187. {
  188. sizeof_name = sizeof (value_name);
  189. //
  190. // Add this value name to the list
  191. //
  192. list.Add( value_name );
  193. }
  194. return ;
  195. }
  196. void RegistryClass::Delete_Value( const char * name)
  197. {
  198. ::RegDeleteValue( (HKEY)Key, name );
  199. return ;
  200. }
  201. void RegistryClass::Deleta_All_Values( void )
  202. {
  203. //
  204. // Build a list of the values in this key
  205. //
  206. DynamicVectorClass<StringClass> value_list;
  207. Get_Value_List (value_list);
  208. //
  209. // Loop over and delete each value
  210. //
  211. for (int index = 0; index < value_list.Count (); index ++) {
  212. Delete_Value( value_list[index] );
  213. }
  214. return ;
  215. }
  216. void RegistryClass::Get_String( const WCHAR * name, WideStringClass &string, const WCHAR *default_string )
  217. {
  218. assert( IsValid );
  219. string = (default_string == NULL) ? L"" : default_string;
  220. //
  221. // Get the size of the entry
  222. //
  223. DWORD data_size = 0;
  224. DWORD type = 0;
  225. LONG result = ::RegQueryValueExW ((HKEY)Key, name, NULL, &type, NULL, &data_size);
  226. if (result == ERROR_SUCCESS && type == REG_SZ) {
  227. //
  228. // Read the entry from the registry
  229. //
  230. ::RegQueryValueExW ((HKEY)Key, name, NULL, &type,
  231. (LPBYTE)string.Get_Buffer ((data_size / 2) + 1), &data_size);
  232. }
  233. return ;
  234. }
  235. void RegistryClass::Set_String( const WCHAR * name, const WCHAR *value )
  236. {
  237. assert( IsValid );
  238. //
  239. // Determine the size
  240. //
  241. int size = wcslen( value ) + 1;
  242. size = size * 2;
  243. //
  244. // Set the registry key
  245. //
  246. ::RegSetValueExW ( (HKEY)Key, name, 0, REG_SZ, (LPBYTE)value, size );
  247. return ;
  248. }