Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / afsapplib / settings.cpp
blobfbf4723a17f2b9acac28988837bcaa7b3ba0f587
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
15 #include <windows.h>
16 #include <winerror.h>
17 #include <WINNT/TaLocale.h>
18 #include <WINNT/settings.h>
22 * ROUTINES ___________________________________________________________________
26 BOOL RestoreSettings (HKEY hkParent,
27 LPCTSTR pszBase,
28 LPCTSTR pszValue,
29 PVOID pStructure,
30 size_t cbStructure,
31 WORD wVerExpected)
33 BOOL rc = FALSE;
35 size_t cbStored;
36 if ((cbStored = GetRegValueSize (hkParent, pszBase, pszValue)) != 0)
38 if (cbStored >= sizeof(WORD)+cbStructure)
40 PVOID pStructureFinal;
41 if ((pStructureFinal = (PVOID)Allocate (cbStored)) != NULL)
43 if (GetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, cbStored))
45 WORD wVerStored = *(LPWORD)pStructureFinal;
47 if ( (HIBYTE(wVerStored) == HIBYTE(wVerExpected)) &&
48 (LOBYTE(wVerStored) >= LOBYTE(wVerExpected)) )
50 memcpy (pStructure, &((LPBYTE)pStructureFinal)[ sizeof(WORD) ], cbStructure);
51 rc = TRUE;
55 Free (pStructureFinal);
60 return rc;
64 BOOL StoreSettings (HKEY hkParent,
65 LPCTSTR pszBase,
66 LPCTSTR pszValue,
67 PVOID pStructure,
68 size_t cbStructure,
69 WORD wVersion)
71 BOOL rc = FALSE;
73 PVOID pStructureFinal;
74 if ((pStructureFinal = (PVOID)Allocate (sizeof(WORD) + cbStructure)) != NULL)
76 *(LPWORD)pStructureFinal = wVersion;
77 memcpy (&((LPBYTE)pStructureFinal)[ sizeof(WORD) ], pStructure, cbStructure);
79 rc = SetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, sizeof(WORD) + cbStructure);
81 Free (pStructureFinal);
84 return rc;
88 void EraseSettings (HKEY hkParent, LPCTSTR pszBase, LPCTSTR pszValue)
90 HKEY hk;
91 if (RegOpenKey (hkParent, pszBase, &hk) == 0)
93 RegDeleteValue (hk, pszValue);
94 RegCloseKey (hk);
99 BOOL GetBinaryRegValue (HKEY hk,
100 LPCTSTR pszBase,
101 LPCTSTR pszValue,
102 PVOID pData,
103 size_t cbData)
105 BOOL rc = FALSE;
107 HKEY hkFinal;
108 if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
110 DWORD dwType;
111 DWORD dwSize = (DWORD)cbData;
113 if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, (LPBYTE)pData, &dwSize) == ERROR_SUCCESS)
114 rc = TRUE;
116 RegCloseKey (hk);
119 return rc;
123 size_t GetRegValueSize (HKEY hk,
124 LPCTSTR pszBase,
125 LPCTSTR pszValue)
127 size_t cb = 0;
129 HKEY hkFinal;
130 if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
132 DWORD dwType;
133 DWORD dwSize = 0;
135 if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
137 cb = (size_t)dwSize;
140 RegCloseKey (hk);
143 return cb;
147 BOOL SetBinaryRegValue (HKEY hk,
148 LPCTSTR pszBase,
149 LPCTSTR pszValue,
150 PVOID pData,
151 size_t cbData)
153 BOOL rc = FALSE;
155 HKEY hkFinal;
156 if (RegCreateKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
158 DWORD dwSize = (DWORD)cbData;
160 if (RegSetValueEx (hkFinal, pszValue, NULL, REG_BINARY, (LPBYTE)pData, dwSize) == ERROR_SUCCESS)
161 rc = TRUE;
163 RegCloseKey (hk);
166 return rc;
170 BOOL GetMultiStringRegValue (HKEY hk,
171 LPCTSTR pszBase,
172 LPCTSTR pszValue,
173 LPTSTR *pmszData)
175 BOOL rc = FALSE;
177 HKEY hkFinal;
178 if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
180 DWORD dwSize = 0;
181 DWORD dwType = 0;
182 RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, NULL, &dwSize);
184 if ((dwType == REG_MULTI_SZ) && (dwSize != 0))
186 *pmszData = AllocateString (dwSize);
188 if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, (LPBYTE)*pmszData, &dwSize) == ERROR_SUCCESS)
189 rc = TRUE;
190 else
192 FreeString (*pmszData);
193 *pmszData = NULL;
197 RegCloseKey (hk);
200 return rc;
204 BOOL SetMultiStringRegValue (HKEY hk,
205 LPCTSTR pszBase,
206 LPCTSTR pszValue,
207 LPCTSTR mszData)
209 BOOL rc = FALSE;
211 HKEY hkFinal;
212 if (RegCreateKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
214 DWORD dwSize = sizeof(TCHAR);
215 for (LPCTSTR psz = mszData; psz && *psz; psz += 1+lstrlen(psz))
216 dwSize += sizeof(TCHAR) * (1+lstrlen(psz));
218 if (RegSetValueEx (hkFinal, pszValue, NULL, REG_MULTI_SZ, (LPBYTE)mszData, dwSize) == ERROR_SUCCESS)
219 rc = TRUE;
221 RegCloseKey (hk);
224 return rc;
228 // Under Windows NT, RegDeleteKey() is not recursive--under Windows 95,
229 // it is. Sigh. This routine works recursively on either OS.
231 BOOL RegDeltreeKey (HKEY hk, LPTSTR pszKey)
233 HKEY hkSub;
234 if (RegOpenKey (hk, pszKey, &hkSub) == 0)
236 TCHAR szFound[ MAX_PATH ];
237 while (RegEnumKey (hkSub, 0, szFound, MAX_PATH) == 0)
239 if (!RegDeltreeKey (hkSub, szFound))
241 RegCloseKey (hkSub);
242 return FALSE;
246 RegCloseKey (hkSub);
249 if (RegDeleteKey (hk, pszKey) != 0)
250 return FALSE;
252 return TRUE;