1 /* w32-reg.c - MS-Windows Registry access
2 * Copyright (C) 1999, 2002, 2007 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_W32_SYSTEM
22 /* This module is only used in this environment */
30 #include "libjnlib-config.h"
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
;
59 /* Return a string from the Win32 Registry or NULL in case of error.
60 Caller must release the return value. A NULL for root is an alias
61 for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
63 read_w32_registry_string (const char *root
, const char *dir
, const char *name
)
65 HKEY root_key
, key_handle
;
66 DWORD n1
, nbytes
, type
;
69 if ( !(root_key
= get_root_key(root
) ) )
72 if ( RegOpenKeyEx( root_key
, dir
, 0, KEY_READ
, &key_handle
) )
75 return NULL
; /* No need for a RegClose, so return immediately. */
76 /* It seems to be common practise to fall back to HKLM. */
77 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE
, dir
, 0, KEY_READ
, &key_handle
) )
78 return NULL
; /* Still no need for a RegClose. */
82 if (RegQueryValueEx( key_handle
, name
, 0, NULL
, NULL
, &nbytes
) )
84 result
= jnlib_malloc ((n1
=nbytes
+1));
87 if (RegQueryValueEx( key_handle
, name
, 0, &type
, result
, &n1
))
93 result
[nbytes
] = 0; /* Make sure it is a string. */
94 if (type
== REG_EXPAND_SZ
&& strchr (result
, '%'))
99 tmp
= jnlib_malloc (n1
+1);
102 nbytes
= ExpandEnvironmentStrings (result
, tmp
, n1
);
103 if (nbytes
&& nbytes
> n1
)
107 tmp
= jnlib_malloc (n1
+ 1);
110 nbytes
= ExpandEnvironmentStrings (result
, tmp
, n1
);
111 if (nbytes
&& nbytes
> n1
)
113 /* Oops - truncated, better don't expand at all. */
123 /* Okay, reduce the length. */
126 result
= jnlib_malloc (strlen (tmp
)+1);
131 strcpy (result
, tmp
);
137 /* Error - don't expand. */
143 RegCloseKey (key_handle
);
149 write_w32_registry_string (const char *root
, const char *dir
,
150 const char *name
, const char *value
)
152 HKEY root_key
, reg_key
;
154 if ( !(root_key
= get_root_key(root
) ) )
157 if ( RegOpenKeyEx( root_key
, dir
, 0, KEY_WRITE
, ®_key
)
161 if ( RegSetValueEx (reg_key
, name
, 0, REG_SZ
, (BYTE
*)value
,
162 strlen( value
) ) != ERROR_SUCCESS
)
164 if ( RegCreateKey( root_key
, name
, ®_key
) != ERROR_SUCCESS
)
166 RegCloseKey(reg_key
);
169 if ( RegSetValueEx (reg_key
, name
, 0, REG_SZ
, (BYTE
*)value
,
170 strlen( value
) ) != ERROR_SUCCESS
)
172 RegCloseKey(reg_key
);
177 RegCloseKey (reg_key
);
182 #endif /*HAVE_W32_SYSTEM*/