2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
12 #include "osregistry.h"
14 // NEVER ADD ANY OTHER INCLUDES HERE - YOU'LL LIKELY BREAK THE LAUNCHER!!!!!!!!!!!!!
16 // ------------------------------------------------------------------------------------------------------------
17 // REGISTRY DEFINES/VARS
20 static char szCompanyName[128];
21 static char szAppName[128];
22 static char szAppVersion[128];
24 char *Osreg_company_name = "Volition";
25 char *Osreg_class_name = "Freespace2Class";
27 char *Osreg_app_name = "FreeSpace2Demo";
28 char *Osreg_title = "Freespace 2 Demo";
29 #elif defined(OEM_BUILD)
30 char *Osreg_app_name = "FreeSpace2OEM";
31 char *Osreg_title = "Freespace 2 OEM";
33 char *Osreg_app_name = "FreeSpace2";
34 char *Osreg_title = "Freespace 2";
37 int Os_reg_inited = 0;
40 // ------------------------------------------------------------------------------------------------------------
44 // os registry functions -------------------------------------------------------------
46 // initialize the registry. setup default keys to use
47 void os_init_registry_stuff(char *company, char *app, char *version)
50 strcpy( szCompanyName, company );
52 strcpy( szCompanyName, Osreg_company_name);
56 strcpy( szAppName, app );
58 strcpy( szAppName, Osreg_app_name);
62 strcpy( szAppVersion, version);
64 strcpy( szAppVersion, "1.0");
70 // Removes a value from to the INI file. Passing
71 // name=NULL will delete the section.
72 void os_config_remove( char *section, char *name )
84 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
86 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
94 lResult = RegDeleteKey( HKEY_LOCAL_MACHINE, keyname );
95 if ( lResult != ERROR_SUCCESS ) {
99 lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, // Where to add it
100 keyname, // name of key
101 NULL, // DWORD reserved
103 REG_OPTION_NON_VOLATILE, // Save to disk
104 KEY_ALL_ACCESS, // Allows all changes
105 NULL, // Default security attributes
106 &hKey, // Location to store key
107 &dwDisposition ); // Location to store status of key
109 if ( lResult != ERROR_SUCCESS ) {
113 lResult = RegDeleteValue( hKey, name );
114 if ( lResult != ERROR_SUCCESS ) {
124 // Writes a string to the INI file. If value is NULL,
125 // removes the string. Writing a NULL value to a NULL name will delete
127 void os_config_write_string( char *section, char *name, char *value )
139 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
141 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
144 lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, // Where to add it
145 keyname, // name of key
146 NULL, // DWORD reserved
148 REG_OPTION_NON_VOLATILE, // Save to disk
149 KEY_ALL_ACCESS, // Allows all changes
150 NULL, // Default security attributes
151 &hKey, // Location to store key
152 &dwDisposition ); // Location to store status of key
154 if ( lResult != ERROR_SUCCESS ) {
162 lResult = RegSetValueEx( hKey, // Handle to key
163 name, // The values name
164 NULL, // DWORD reserved
165 REG_SZ, // null terminated string
166 (CONST BYTE *)value, // value to set
167 strlen(value) + 1 ); // How many bytes to set
169 if ( lResult != ERROR_SUCCESS ) {
179 // same as previous function except we don't use the application name to build up the keyname
180 void os_config_write_string2( char *section, char *name, char *value )
192 sprintf( keyname, "Software\\%s\\%s", szCompanyName, section );
194 sprintf( keyname, "Software\\%s", szCompanyName );
197 lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, // Where to add it
198 keyname, // name of key
199 NULL, // DWORD reserved
201 REG_OPTION_NON_VOLATILE, // Save to disk
202 KEY_ALL_ACCESS, // Allows all changes
203 NULL, // Default security attributes
204 &hKey, // Location to store key
205 &dwDisposition ); // Location to store status of key
207 if ( lResult != ERROR_SUCCESS ) {
215 lResult = RegSetValueEx( hKey, // Handle to key
216 name, // The values name
217 NULL, // DWORD reserved
218 REG_SZ, // null terminated string
219 (CONST BYTE *)value, // value to set
220 strlen(value) + 1 ); // How many bytes to set
222 if ( lResult != ERROR_SUCCESS ) {
232 // Writes an unsigned int to the INI file.
233 void os_config_write_uint( char *section, char *name, uint value )
245 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
247 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
250 lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE, // Where to add it
251 keyname, // name of key
252 NULL, // DWORD reserved
254 REG_OPTION_NON_VOLATILE, // Save to disk
255 KEY_ALL_ACCESS, // Allows all changes
256 NULL, // Default security attributes
257 &hKey, // Location to store key
258 &dwDisposition ); // Location to store status of key
260 if ( lResult != ERROR_SUCCESS ) {
268 lResult = RegSetValueEx( hKey, // Handle to key
269 name, // The values name
270 NULL, // DWORD reserved
271 REG_DWORD, // null terminated string
272 (CONST BYTE *)&value, // value to set
273 4 ); // How many bytes to set
275 if ( lResult != ERROR_SUCCESS ) {
286 // Reads a string from the INI file. If default is passed,
287 // and the string isn't found, returns ptr to default otherwise
288 // returns NULL; Copy the return value somewhere before
289 // calling os_read_string again, because it might reuse the
291 static char tmp_string_data[1024];
292 char * os_config_read_string( char *section, char *name, char *default_value )
304 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
306 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
309 lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, // Where it is
310 keyname, // name of key
311 NULL, // DWORD reserved
312 KEY_QUERY_VALUE, // Allows all changes
313 &hKey ); // Location to store key
315 if ( lResult != ERROR_SUCCESS ) {
324 lResult = RegQueryValueEx( hKey, // Handle to key
325 name, // The values name
326 NULL, // DWORD reserved
327 &dwType, // What kind it is
328 (ubyte *)&tmp_string_data, // value to set
329 &dwLen ); // How many bytes to set
331 if ( lResult != ERROR_SUCCESS ) {
335 default_value = tmp_string_data;
341 return default_value;
344 // same as previous function except we don't use the application name to build up the keyname
345 char * os_config_read_string2( char *section, char *name, char *default_value )
357 sprintf( keyname, "Software\\%s\\%s", szCompanyName, section );
359 sprintf( keyname, "Software\\%s", szCompanyName );
362 lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, // Where it is
363 keyname, // name of key
364 NULL, // DWORD reserved
365 KEY_QUERY_VALUE, // Allows all changes
366 &hKey ); // Location to store key
368 if ( lResult != ERROR_SUCCESS ) {
377 lResult = RegQueryValueEx( hKey, // Handle to key
378 name, // The values name
379 NULL, // DWORD reserved
380 &dwType, // What kind it is
381 (ubyte *)&tmp_string_data, // value to set
382 &dwLen ); // How many bytes to set
384 if ( lResult != ERROR_SUCCESS ) {
388 default_value = tmp_string_data;
394 return default_value;
397 // Reads a string from the INI file. Default_value must
398 // be passed, and if 'name' isn't found, then returns default_value
399 uint os_config_read_uint( char *section, char *name, uint default_value )
412 sprintf( keyname, "Software\\%s\\%s\\%s", szCompanyName, szAppName, section );
414 sprintf( keyname, "Software\\%s\\%s", szCompanyName, szAppName );
417 lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, // Where it is
418 keyname, // name of key
419 NULL, // DWORD reserved
420 KEY_QUERY_VALUE, // Allows all changes
421 &hKey ); // Location to store key
423 if ( lResult != ERROR_SUCCESS ) {
432 lResult = RegQueryValueEx( hKey, // Handle to key
433 name, // The values name
434 NULL, // DWORD reserved
435 &dwType, // What kind it is
436 (ubyte *)&tmp_val, // value to set
437 &dwLen ); // How many bytes to set
439 if ( lResult != ERROR_SUCCESS ) {
443 default_value = tmp_val;
449 return default_value;
452 // uses Ex versions of Windows registry functions
453 static char tmp_string_data_ex[1024];
454 char * os_config_read_string_ex( char *keyname, char *name, char *default_value )
460 lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, // Where it is
461 keyname, // name of key
462 NULL, // DWORD reserved
463 KEY_QUERY_VALUE, // Allows all changes
464 &hKey ); // Location to store key
466 if ( lResult != ERROR_SUCCESS ) {
467 //mprintf(( "Error opening registry key '%s'\n", keyname ));
472 //mprintf(( "No variable name passed\n" ));
477 lResult = RegQueryValueEx( hKey, // Handle to key
478 name, // The values name
479 NULL, // DWORD reserved
480 &dwType, // What kind it is
481 (ubyte *)&tmp_string_data_ex, // value to set
482 &dwLen ); // How many bytes to set
484 if ( lResult != ERROR_SUCCESS ) {
485 //mprintf(( "Error reading registry key '%s'\n", name ));
489 default_value = tmp_string_data_ex;
495 return default_value;