1
/* w32reg.c - MS-Windows Registry access
2
* Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22
#ifdef HAVE_W32_SYSTEM
23
/* This module is only used in this environment */
35
get_root_key(const char *root)
40
root_key = HKEY_CURRENT_USER;
41
else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
42
root_key = HKEY_CLASSES_ROOT;
43
else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
44
root_key = HKEY_CURRENT_USER;
45
else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
46
root_key = HKEY_LOCAL_MACHINE;
47
else if( !strcmp( root, "HKEY_USERS" ) )
48
root_key = HKEY_USERS;
49
else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
50
root_key = HKEY_PERFORMANCE_DATA;
51
else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
52
root_key = HKEY_CURRENT_CONFIG;
61
* Return a string from the Win32 Registry or NULL in case of
62
* error. Caller must release the return value. A NULL for root
63
* is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
64
* NOTE: The value is allocated with a plain malloc() - use free() and not
65
* the usual m_free()!!!
68
read_w32_registry_string( const char *root, const char *dir, const char *name )
70
HKEY root_key, key_handle;
71
DWORD n1, nbytes, type;
74
if ( !(root_key = get_root_key(root) ) )
77
if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
80
return NULL; /* no need for a RegClose, so return direct */
81
/* It seems to be common practise to fall back to HLM. */
82
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
83
return NULL; /* still no need for a RegClose, so return direct */
87
if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
89
result = malloc( (n1=nbytes+1) );
92
if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
93
free(result); result = NULL;
96
result[nbytes] = 0; /* make sure it is really a string */
97
if (type == REG_EXPAND_SZ && strchr (result, '%')) {
104
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
105
if (nbytes && nbytes > n1) {
108
tmp = malloc (n1 + 1);
111
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
112
if (nbytes && nbytes > n1) {
113
free (tmp); /* oops - truncated, better don't expand at all */
120
else if (nbytes) { /* okay, reduce the length */
123
result = malloc (strlen (tmp)+1);
127
strcpy (result, tmp);
131
else { /* error - don't expand */
137
RegCloseKey( key_handle );
143
write_w32_registry_string(const char *root, const char *dir,
144
const char *name, const char *value)
146
HKEY root_key, reg_key;
148
if ( !(root_key = get_root_key(root) ) )
151
if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key )
155
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
156
strlen( value ) ) != ERROR_SUCCESS ) {
157
if ( RegCreateKey( root_key, name, ®_key ) != ERROR_SUCCESS ) {
158
RegCloseKey(reg_key);
161
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
162
strlen( value ) ) != ERROR_SUCCESS ) {
163
RegCloseKey(reg_key);
168
RegCloseKey( reg_key );
173
#endif /*HAVE_W32_SYSTEM*/