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
#if defined (__MINGW32__) || defined (__CYGWIN32__)
23
/* This module is only used in this environment */
34
get_root_key(const char *root)
39
root_key = HKEY_CURRENT_USER;
40
else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
41
root_key = HKEY_CLASSES_ROOT;
42
else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
43
root_key = HKEY_CURRENT_USER;
44
else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
45
root_key = HKEY_LOCAL_MACHINE;
46
else if( !strcmp( root, "HKEY_USERS" ) )
47
root_key = HKEY_USERS;
48
else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
49
root_key = HKEY_PERFORMANCE_DATA;
50
else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
51
root_key = HKEY_CURRENT_CONFIG;
60
* Return a string from the Win32 Registry or NULL in case of
61
* error. Caller must release the return value. A NULL for root
62
* is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
63
* NOTE: The value is allocated with a plain malloc() - use free() and not
64
* the usual m_free()!!!
67
read_w32_registry_string( const char *root, const char *dir, const char *name )
69
HKEY root_key, key_handle;
70
DWORD n1, nbytes, type;
73
if ( !(root_key = get_root_key(root) ) )
76
if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
79
return NULL; /* no need for a RegClose, so return direct */
80
/* It seems to be common practise to fall back to HLM. */
81
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
82
return NULL; /* still no need for a RegClose, so return direct */
86
if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
88
result = malloc( (n1=nbytes+1) );
91
if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
92
free(result); result = NULL;
95
result[nbytes] = 0; /* make sure it is really a string */
96
if (type == REG_EXPAND_SZ && strchr (result, '%')) {
103
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
104
if (nbytes && nbytes > n1) {
106
tmp = malloc (n1 + 1);
109
nbytes = ExpandEnvironmentStrings (result, tmp, n1);
110
if (nbytes && nbytes > n1) {
111
free (tmp); /* oops - truncated, better don't expand at all */
118
else if (nbytes) { /* okay, reduce the length */
121
result = malloc (strlen (tmp)+1);
125
strcpy (result, tmp);
129
else { /* error - don't expand */
135
RegCloseKey( key_handle );
141
write_w32_registry_string(const char *root, const char *dir,
142
const char *name, const char *value)
144
HKEY root_key, reg_key;
146
if ( !(root_key = get_root_key(root) ) )
149
if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key )
153
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
154
strlen( value ) ) != ERROR_SUCCESS ) {
155
if ( RegCreateKey( root_key, name, ®_key ) != ERROR_SUCCESS ) {
156
RegCloseKey(reg_key);
159
if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
160
strlen( value ) ) != ERROR_SUCCESS ) {
161
RegCloseKey(reg_key);
166
RegCloseKey( reg_key );
171
#endif /* __MINGW32__ || __CYGWIN32__ */