1 /* Copyright (C) 2001, Ghostgum Software Pty Ltd. All rights reserved.
2
3 This software is provided AS-IS with no warranty, either express or
4 implied.
5
6 This software is distributed under license and may not be copied,
7 modified or distributed except as expressly authorized under the terms
8 of the license contained in the file LICENSE in this distribution.
9
10 For more information about licensing, please refer to
11 http://www.ghostscript.com/licensing/. For information on
12 commercial licensing, go to http://www.artifex.com/licensing/ or
13 contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 San Rafael, CA 94903, U.S.A., +1(415)492-9861.
15 */
16
17 /* $Id: dwreg.c,v 1.3 2002/02/21 22:24:51 giles Exp $ */
18 /* MS Windows registry values */
19
20 #include <windows.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for getenv */
23 #include <string.h>
24 #include "gscdefs.h" /* for gs_productfamily and gs_revision */
25
26 /* We store registry named values under the key
27 * "Software\\AFPL Ghostscript"
28 * where "AFPL Ghostscript" is actually gs_productfamily.
29 * Either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER will be used.
30 */
31 int
win_registry_key(char * buf,int len)32 win_registry_key(char *buf, int len)
33 {
34 const char *software = "Software";
35 if (strlen(software) + 1 + strlen(gs_productfamily) >= len)
36 return -1;
37
38 strcpy(buf, software);
39 strcat(buf, "\\");
40 strcat(buf, gs_productfamily);
41 return 0;
42 }
43
44 /*
45 * Get a named registry value from HKCU.
46 * name, ptr, plen and return values are the same as in gp_getenv();
47 */
48 int
win_get_reg_value(const char * name,char * ptr,int * plen)49 win_get_reg_value(const char *name, char *ptr, int *plen)
50 {
51 HKEY hkey;
52 DWORD cbData, keytype;
53 BYTE b;
54 LONG rc;
55 BYTE *bptr = (BYTE *)ptr;
56 char key[256];
57
58 win_registry_key(key, sizeof(key));
59 if (RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_READ, &hkey)
60 == ERROR_SUCCESS) {
61 keytype = REG_SZ;
62 cbData = *plen;
63 if (bptr == (char *)NULL)
64 bptr = &b; /* Registry API won't return ERROR_MORE_DATA */
65 /* if ptr is NULL */
66 rc = RegQueryValueEx(hkey, (char *)name, 0, &keytype, bptr, &cbData);
67 RegCloseKey(hkey);
68 if (rc == ERROR_SUCCESS) {
69 *plen = cbData;
70 return 0; /* found environment variable and copied it */
71 } else if (rc == ERROR_MORE_DATA) {
72 /* buffer wasn't large enough */
73 *plen = cbData;
74 return -1;
75 }
76 }
77 return 1; /* not found */
78 }
79
80 /*
81 * Set a named registry value under HKCU.
82 * name = name of named value
83 * str = value of named value
84 * Returns 0 on success.
85 */
86 int
win_set_reg_value(const char * name,const char * value)87 win_set_reg_value(const char *name, const char *value)
88 {
89 HKEY hkey;
90 LONG rc;
91 char key[256];
92 DWORD dwDisposition;
93
94 win_registry_key(key, sizeof(key));
95 rc = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_WRITE, &hkey);
96 if (rc != ERROR_SUCCESS)
97 rc = RegCreateKeyEx(HKEY_CURRENT_USER, key, 0, "", 0,
98 KEY_ALL_ACCESS, NULL, &hkey, &dwDisposition);
99 if (rc == ERROR_SUCCESS) {
100 rc = RegSetValueEx(hkey, name, 0, REG_SZ,
101 (CONST BYTE *)value, strlen(value)+1);
102 RegCloseKey(hkey);
103 }
104
105 return rc == ERROR_SUCCESS ? 0 : -1;
106 }
107
108