2002-04-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / util / w32reg.c
blob362bcab8f86984ec05f024ebdb22fc08b24f529f
1 /* w32reg.c - MS-Windows Registry access
2 * Copyright (C) 1999 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
21 #include <config.h>
22 #ifdef __MINGW32__ /* This module is only used in this environment */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <windows.h>
29 #include "util.h"
30 #include "memory.h"
33 /****************
34 * Return a string from the Win32 Registry or NULL in case of
35 * error. Caller must release the return value. A NUKK for root
36 * is an alias fro HKEY_CURRENT_USER
37 * NOTE: The value is allocated with a plain malloc() - use free() and not
38 * the usual gcry_free()!!!
40 char *
41 read_w32_registry_string( const char *root, const char *dir, const char *name )
43 HKEY root_key, key_handle;
44 DWORD n1, nbytes;
45 char *result = NULL;
47 if( !root )
48 root_key = HKEY_CURRENT_USER;
49 else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
50 root_key = HKEY_CLASSES_ROOT;
51 else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
52 root_key = HKEY_CURRENT_USER;
53 else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
54 root_key = HKEY_LOCAL_MACHINE;
55 else if( !strcmp( root, "HKEY_USERS" ) )
56 root_key = HKEY_USERS;
57 else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
58 root_key = HKEY_PERFORMANCE_DATA;
59 else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
60 root_key = HKEY_CURRENT_CONFIG;
61 else
62 return NULL;
64 if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
65 return NULL; /* no need for a RegClose, so return direct */
67 nbytes = 1;
68 if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
69 goto leave;
70 result = malloc( (n1=nbytes+1) );
71 if( !result )
72 goto leave;
73 if( RegQueryValueEx( key_handle, name, 0, NULL, result, &n1 ) ) {
74 free(result); result = NULL;
75 goto leave;
77 result[nbytes] = 0; /* make sure it is really a string */
79 leave:
80 RegCloseKey( key_handle );
81 return result;
88 #endif /* __MINGW32__ */