|
|
1.1 root 1: #ifndef STF_LITE
2: #include <windows.h>
3: #ifdef WIN32
4: #include <malloc.h>
5: #endif
6: #include "setupapi.h"
7: #include "msregdb.h"
8:
9: HANDLE hinstDLL = NULL;
10:
11: #ifdef WIN16
12: typedef LONG (APIENTRY *LPFNRCLK)(LONG);
13: typedef LONG (APIENTRY *LPFNRCRK)(LONG, LPSTR, LPSTR);
14: typedef LONG (APIENTRY *LPFNRDLK)(LONG, LPSTR);
15: typedef LONG (APIENTRY *LPFNROPK)(LONG, LPSTR, LPSTR);
16: typedef LONG (APIENTRY *LPFNRQRV)(LONG, LPSTR, LPSTR, LPSTR);
17: typedef LONG (APIENTRY *LPFNRSTV)(LONG, LPSTR, LONG, LPSTR, LONG);
18:
19: LPFNRCLK lpfnRegCloseKey;
20: LPFNRCRK lpfnRegCreateKey;
21: LPFNRDLK lpfnRegDeleteKey;
22: LPFNROPK lpfnRegOpenKey;
23: LPFNRQRV lpfnRegQueryValue;
24: LPFNRSTV lpfnRegSetValue;
25: #endif
26:
27: #ifdef WIN32
28: typedef LONG (APIENTRY *LPFNRCLK)(HKEY);
29: typedef LONG (APIENTRY *LPFNRCRK)(HKEY, LPCTSTR, PHKEY);
30: typedef LONG (APIENTRY *LPFNRDLK)(HKEY, LPCTSTR);
31: typedef LONG (APIENTRY *LPFNROPK)(HKEY, LPCTSTR, PHKEY);
32: typedef LONG (APIENTRY *LPFNRQRV)(HKEY, LPCTSTR, LPTSTR, PLONG);
33: typedef LONG (APIENTRY *LPFNRSTV)(HKEY, LPCTSTR, DWORD, LPTSTR, DWORD);
34: typedef LONG (APIENTRY *LPFNRCRKe)(HKEY, LPCTSTR, DWORD, LPTSTR, DWORD,
35: REGSAM, LPSECURITY_ATTRIBUTES, PHKEY, LPDWORD);
36: typedef LONG (APIENTRY *LPFNROPKe)(HKEY, LPCTSTR, DWORD, REGSAM, PHKEY);
37: typedef LONG (APIENTRY *LPFNRQRVe)(HKEY, LPTSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD);
38: typedef LONG (APIENTRY *LPFNRSTVe)(HKEY, LPCTSTR, DWORD, DWORD, LPBYTE, DWORD);
39: typedef LONG (APIENTRY *LPFNREK)(HKEY, DWORD, LPTSTR, LPDWORD);
40: typedef LONG (APIENTRY *LPFNRQIK)(HKEY, LPTSTR, LPDWORD, LPDWORD, LPDWORD,
41: LPDWORD, LPDWORD, LPDWORD, LPDWORD, LPDWORD, LPDWORD, PFILETIME);
42:
43: LPFNRCLK lpfnRegCloseKey;
44: LPFNRCRK lpfnRegCreateKey;
45: LPFNRDLK lpfnRegDeleteKey;
46: LPFNROPK lpfnRegOpenKey;
47: LPFNRQRV lpfnRegQueryValue;
48: LPFNRSTV lpfnRegSetValue;
49: LPFNRCRKe lpfnRegCreateKeyEx;
50: LPFNROPKe lpfnRegOpenKeyEx;
51: LPFNRQRVe lpfnRegQueryValueEx;
52: LPFNRSTVe lpfnRegSetValueEx;
53: LPFNREK lpfnRegEnumKey;
54: LPFNRQIK lpfnRegQueryInfoKey;
55: #endif
56:
57: // **************************************************************************
58: void CreateRegKey(LPSTR szKey)
59: {
60: HKEY hKey;
61: INT i;
62:
63: if ((*lpfnRegCreateKey)(HKEY_CLASSES_ROOT, szKey, &hKey)
64: > ERROR_SUCCESS)
65: {
66: i = EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKey", NULL, NULL);
67: #ifdef DEBUG
68: StfApiErr(saeFail, "CreateRegKey", szKey);
69: #endif // DEBUG
70: SetupError(STFERR);
71: }
72:
73: if ((*lpfnRegCloseKey)(hKey) > ERROR_SUCCESS)
74: {
75: i = EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKey", NULL, NULL);
76: #ifdef DEBUG
77: StfApiErr(saeFail, "CreateRegKey", szKey);
78: #endif // DEBUG
79: SetupError(STFERR);
80: }
81: }
82:
83:
84: // **************************************************************************
85: void CreateRegKeyValue(LPSTR szKey, LPSTR szValue)
86: {
87: if ((*lpfnRegSetValue)(HKEY_CLASSES_ROOT, (LPCTSTR)szKey, REG_SZ, szValue,
88: lstrlen(szKey)) > ERROR_SUCCESS)
89: {
90: EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKeyValue", NULL, NULL);
91: #ifdef DEBUG
92: StfApiErr(saeFail, "CreateRegKeyValue",SzCat2Str((LPSTR)szKey,", ",(LPSTR)szValue));
93: #endif // DEBUG
94: SetupError(STFERR);
95: }
96: }
97:
98:
99: // **************************************************************************
100: INT DoesRegKeyExist(LPSTR szKey)
101: {
102: HKEY hKey;
103:
104: if ((*lpfnRegOpenKey)(HKEY_CLASSES_ROOT, szKey, &hKey)
105: != ERROR_SUCCESS)
106: return(0);
107:
108: (*lpfnRegCloseKey)(hKey);
109:
110: return(1);
111: }
112:
113:
114: // **************************************************************************
115: void SetRegKeyValue(LPSTR szKey, LPSTR szValue)
116: {
117: if ((*lpfnRegSetValue)(HKEY_CLASSES_ROOT, szKey, REG_SZ, szValue,
118: lstrlen(szKey)) > ERROR_SUCCESS)
119: {
120: EercErrorHandler(GRC_API_FAILED, 1, "SetRegKeyValue", NULL, NULL);
121: #ifdef DEBUG
122: StfApiErr(saeFail, "SetRegKeyValue", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValue));
123: #endif // DEBUG
124: SetupError(STFERR);
125: }
126: }
127:
128:
129: // **************************************************************************
130: LPSTR GetRegKeyValue(LPSTR szKey, LPSTR szBuf, INT cbBuf)
131: {
132: LONG cb = cbBuf;
133:
134: if (szBuf != NULL && cbBuf > 0)
135: *szBuf = '\0';
136:
137: if (!DoesRegKeyExist(szKey))
138: return(szBuf);
139:
140: if ((*lpfnRegQueryValue)(HKEY_CLASSES_ROOT, szKey, szBuf, &cb)
141: != ERROR_SUCCESS)
142: {
143: EercErrorHandler(GRC_API_FAILED, 1, "SetRegKeyValue", NULL, NULL);
144: #ifdef DEBUG
145: StfApiErr(saeFail, "GetRegKeyValue", szKey);
146: #endif // DEBUG
147: SetupError(STFERR);
148: }
149:
150: if (cb > cbBuf)
151: {
152: DoMsgBox("Buffer Overflow", "MS-Setup Error", MB_ICONHAND+MB_OK);
153: SetupError(STFERR);
154: }
155: *(szBuf + cb) = '\0';
156:
157: return(szBuf);
158: }
159:
160:
161: // **************************************************************************
162: void DeleteRegKey(LPSTR szKey)
163: {
164: #ifdef WIN16
165: (*lpfnRegDeleteKey)(HKEY_CLASSES_ROOT, szKey);
166: #elif defined(WIN32)
167: if (DoesRegKeyExistEx(HKEY_CLASSES_ROOT, szKey))
168: {
169: //
170: // The Win32 "RegDeleteKey()" function will not delete a key
171: // that has existing subkeys (unlike the Win16 version).
172: // DeleteAllSubkeys is a recursive function that removes a
173: // complete "tree" of registry keys.
174: //
175: if (!DeleteAllSubKeys(HKEY_CLASSES_ROOT, (LPCTSTR)szKey))
176: {
177: EercErrorHandler(GRC_API_FAILED, 1, "DeleteAllSubKeys", NULL, NULL);
178: #ifdef DEBUG
179: StfApiErr(saeFail, "DeleteAllSubKeys", (LPSTR)szKey);
180: #endif // DEBUG
181: SetupError(STFERR);
182: }
183: }
184: #endif
185: }
186:
187:
188:
189: #ifdef WIN32
190:
191: //
192: // All the functions that follow are "extended" functions for
193: // Win32. They are not limited to the HKEY_CLASSES_ROOT key
194: // as the original Setup Toolkit functions are.
195: //
196:
197: BOOL DeleteAllSubKeys(HKEY hKey, LPCTSTR szKey)
198: {
199: HKEY hKey2;
200: TCHAR ClassStr[128];
201: LPTSTR szClass = ClassStr;
202: LPTSTR szSubKey;
203: LPTSTR szFullSubKey;
204: LONG lResult;
205: DWORD cchClass, dwNumSubKeys, cchMaxSubKey, cchMaxClass,
206: cValues, cchMaxValName, cbMaxValData, cbSecurityDescriptor;
207: DWORD i, cchName;
208: FILETIME ftLastWrite;
209:
210: if ((lResult = (*lpfnRegOpenKeyEx)(hKey, szKey,
211: 0, KEY_READ, &hKey2)) > ERROR_SUCCESS)
212: {
213: EercErrorHandler(GRC_API_FAILED, 1, "DeleteAllSubKeys:open", NULL, NULL);
214: #ifdef DEBUG
215: StfApiErr(saeFail, "DeleteAllSubKeys:open", (LPSTR)szKey);
216: #endif // DEBUG
217: return(FALSE);
218: }
219:
220: //
221: // First, see how many subkeys this key has.
222: //
223: cchClass=128;
224: if ((lResult = (*lpfnRegQueryInfoKey)(hKey2, szClass, &cchClass, NULL, &dwNumSubKeys,
225: &cchMaxSubKey, &cchMaxClass, &cValues, &cchMaxValName,
226: &cbMaxValData, &cbSecurityDescriptor, &ftLastWrite)) != ERROR_SUCCESS)
227: {
228: (*lpfnRegCloseKey)(hKey2);
229: return(FALSE);
230: }
231:
232: //
233: // if this key has no subkeys at all, we can remove
234: // it immediately and finish.
235: //
236: if (dwNumSubKeys == 0)
237: {
238: (*lpfnRegCloseKey)(hKey2);
239: lResult = (*lpfnRegDeleteKey)(hKey, szKey);
240: }
241: else
242: {
243: //
244: // This key has subkeys; we'll need to enumerate them
245: // and "zap" each one in turn.
246: //
247: // RegQueryInfoKey() has indicated what the maximum length
248: // of a subkey name will be for this particular key
249: // in "cchMaxSubKey", so we can allocate just what we
250: // need to hold the names.
251: //
252: szSubKey = (LPTSTR)malloc((cchMaxSubKey+1) * sizeof(TCHAR));
253: szFullSubKey = (LPTSTR)malloc((cchMaxSubKey + lstrlen(szKey) + 2)*sizeof(TCHAR));
254:
255: for (i = dwNumSubKeys; i > 0; i--)
256: {
257: cchName = cchMaxSubKey;
258: if ((lResult = (*lpfnRegEnumKey)(hKey2, i - 1,
259: szSubKey, &cchName)) == ERROR_SUCCESS)
260: {
261: //
262: // Construct the full hierarchical key name, and
263: // nuke it.
264: // (RegEnumKey just returns the name of the subkey)
265: //
266: wsprintf(szFullSubKey, "%s\\%s", szKey, szSubKey);
267: if (!DeleteAllSubKeys(hKey, szFullSubKey))
268: {
269: (*lpfnRegCloseKey)(hKey2);
270: free(szSubKey);
271: free(szFullSubKey);
272: return(FALSE);
273: }
274: }
275: else
276: {
277: if (lResult != ERROR_NO_MORE_ITEMS)
278: {
279: // Usually, you only see NO_MORE_ITEMS
280: // when you're counting up from 0 and
281: // gone past the end.
282: // Some other tragedy has befallen us.
283: //
284: EercErrorHandler(GRC_API_FAILED, 1, "DeleteAllSubKeys:enum", NULL, NULL);
285: #ifdef DEBUG
286: StfApiErr(saeFail, "DeleteAllSubKeys:Enum", (LPSTR)szKey);
287: #endif // DEBUG
288: (*lpfnRegCloseKey)(hKey2);
289: free(szSubKey);
290: free(szFullSubKey);
291: return(FALSE);
292: }
293: }
294: }
295: //
296: // Now that the count of subkeys is down to 0, we
297: // can delete ourselves.
298: //
299: (*lpfnRegCloseKey)(hKey2);
300: lResult = (*lpfnRegDeleteKey)(hKey, szKey);
301: free(szSubKey);
302: free(szFullSubKey);
303: }
304: return(TRUE);
305: }
306:
307: // **************************************************************************
308: void CreateRegKeyEx(HKEY hKey, LPCSTR szKey)
309: {
310: HKEY hKey2;
311: INT i;
312:
313: if ((*lpfnRegCreateKey)(hKey, szKey, &hKey2)
314: > ERROR_SUCCESS)
315: {
316: i = EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKeyEx", NULL, NULL);
317: #ifdef DEBUG
318: StfApiErr(saeFail, "CreateRegKeyEx", (LPSTR)szKey);
319: #endif // DEBUG
320: SetupError(STFERR);
321: }
322:
323: if ((*lpfnRegCloseKey)(hKey2) > ERROR_SUCCESS)
324: {
325: i = EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKeyEx", NULL, NULL);
326: #ifdef DEBUG
327: StfApiErr(saeFail, "CreateRegKeyEx", (LPSTR)szKey);
328: #endif // DEBUG
329: SetupError(STFERR);
330: }
331: }
332:
333:
334: // **************************************************************************
335: void CreateRegKeyValueEx(HKEY hKey, LPCTSTR szKey,
336: LPTSTR szValueName, DWORD dwType,
337: LPBYTE lpValueData, DWORD dwDataLen)
338: {
339: HKEY hKey2;
340:
341: if ((*lpfnRegOpenKeyEx)(hKey, szKey, 0, KEY_WRITE, &hKey2) > ERROR_SUCCESS)
342: {
343: EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKeyValueEx:open", NULL, NULL);
344: #ifdef DEBUG
345: StfApiErr(saeFail, "CreateRegKeyValueEx:open", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValueName));
346: #endif // DEBUG
347: SetupError(STFERR);
348: }
349:
350: if ((*lpfnRegSetValueEx)(hKey2, szValueName, 0, dwType, lpValueData,
351: dwDataLen) > ERROR_SUCCESS)
352: {
353: EercErrorHandler(GRC_API_FAILED, 1, "CreateRegKeyValueEx", NULL, NULL);
354: #ifdef DEBUG
355: StfApiErr(saeFail, "CreateRegKeyValueEx", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValueName));
356: #endif // DEBUG
357: SetupError(STFERR);
358: }
359: }
360:
361:
362: // **************************************************************************
363: INT DoesRegKeyExistEx(HKEY hKey, LPCSTR szKey)
364: {
365: HKEY hKey2;
366:
367: if ((*lpfnRegOpenKey)(hKey, szKey, &hKey2)
368: != ERROR_SUCCESS)
369: return(0);
370:
371: (*lpfnRegCloseKey)(hKey2);
372:
373: return(1);
374: }
375:
376:
377: // **************************************************************************
378: void SetRegKeyValueEx(HKEY hKey, LPCTSTR szKey,
379: LPTSTR szValueName, DWORD dwType,
380: LPBYTE lpValueData, DWORD dwDataLen)
381: {
382: HKEY hKey2;
383:
384: if ((*lpfnRegOpenKeyEx)(hKey, szKey, 0, KEY_WRITE, &hKey2) > ERROR_SUCCESS)
385: {
386: EercErrorHandler(GRC_API_FAILED, 1, "SetRegKeyValueEx:open", NULL, NULL);
387: #ifdef DEBUG
388: StfApiErr(saeFail, "SetRegKeyValueEx:open", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValueName));
389: #endif // DEBUG
390: SetupError(STFERR);
391: }
392:
393: if ((*lpfnRegSetValueEx)(hKey2, szValueName, 0, dwType, lpValueData,
394: dwDataLen) > ERROR_SUCCESS)
395: {
396: EercErrorHandler(GRC_API_FAILED, 1, "SetRegKeyValueEx", NULL, NULL);
397: #ifdef DEBUG
398: StfApiErr(saeFail, "SetRegKeyValueEx", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValueName));
399: #endif // DEBUG
400: SetupError(STFERR);
401: }
402: }
403:
404:
405: // **************************************************************************
406: LONG GetRegKeyValueEx(HKEY hKey, LPCTSTR szKey, LPTSTR szValueName, LPDWORD lpdwType, LPBYTE lpBuf, DWORD cbBuf)
407: {
408: HKEY hKey2;
409: DWORD cb = cbBuf;
410: LONG lRetVal;
411:
412: if (!DoesRegKeyExistEx(hKey, szKey))
413: return(ERROR_BADKEY);
414:
415: if ((*lpfnRegOpenKeyEx)(hKey, szKey, 0, KEY_READ, &hKey2) > ERROR_SUCCESS)
416: {
417: EercErrorHandler(GRC_API_FAILED, 1, "GetRegKeyValueEx", NULL, NULL);
418: #ifdef DEBUG
419: StfApiErr(saeFail, "GetRegKeyValueEx", SzCat2Str((LPSTR)szKey, ", ", (LPSTR)szValueName));
420: #endif // DEBUG
421: SetupError(STFERR);
422: }
423:
424: if ((*lpfnRegQueryValueEx)(hKey2, (LPTSTR)szValueName, NULL,
425: lpdwType, lpBuf, &cb)
426: != ERROR_SUCCESS)
427: {
428: EercErrorHandler(GRC_API_FAILED, 1, "SetRegKeyValueEx", NULL, NULL);
429: #ifdef DEBUG
430: StfApiErr(saeFail, "GetRegKeyValueEx", (LPSTR)szKey);
431: #endif // DEBUG
432: SetupError(STFERR);
433: }
434:
435: if (cb > cbBuf)
436: {
437: DoMsgBox("Buffer Overflow", "MS-Setup Error", MB_ICONHAND+MB_OK);
438: SetupError(STFERR);
439: lRetVal = ERROR_MORE_DATA;
440: }
441: else
442: lRetVal = ERROR_SUCCESS;
443:
444: return(lRetVal);
445: }
446:
447:
448: // **************************************************************************
449: void DeleteRegKeyEx(HKEY hKey, LPCTSTR szKey)
450: {
451: if (DoesRegKeyExistEx(hKey, szKey))
452: {
453: //
454: // DeleteAllSubkeys is a recursive function that removes a
455: // key tree from the bottom up. The Win32 "RegDeleteKey()"
456: // function will not delete a key that has existing subkeys.
457: //
458: if (!DeleteAllSubKeys(hKey, szKey))
459: {
460: EercErrorHandler(GRC_API_FAILED, 1, "DeleteAllSubKeys", NULL, NULL);
461: #ifdef DEBUG
462: StfApiErr(saeFail, "DeleteAllSubKeys", (LPSTR)szKey);
463: #endif // DEBUG
464: SetupError(STFERR);
465: }
466: }
467: }
468:
469: #endif
470:
471: // **************************************************************************
472: BOOL FInitRegDb(void)
473: {
474: #ifdef WIN16
475: if ((hinstDLL = LoadLibrary("SHELL.DLL")) == NULL)
476: return(FALSE);
477:
478: if (hinstDLL < 32)
479: {
480: hinstDLL = NULL;
481: return(FALSE);
482: }
483: #elif defined(WIN32)
484: if ((hinstDLL = LoadLibrary("ADVAPI32.DLL")) == NULL)
485: return(FALSE);
486: #endif
487:
488: lpfnRegCloseKey = (LPFNRCLK)GetProcAddress(hinstDLL, "RegCloseKey");
489: #ifdef UNICODE
490: lpfnRegCreateKey = (LPFNRCRK)GetProcAddress(hinstDLL, "RegCreateKeyW");
491: lpfnRegDeleteKey = (LPFNRDLK)GetProcAddress(hinstDLL, "RegDeleteKeyW");
492: lpfnRegOpenKey = (LPFNROPK)GetProcAddress(hinstDLL, "RegOpenKeyW");
493: lpfnRegQueryValue = (LPFNRQRV)GetProcAddress(hinstDLL, "RegQueryValueW");
494: lpfnRegSetValue = (LPFNRSTV)GetProcAddress(hinstDLL, "RegSetValueW");
495: #else
496: lpfnRegCreateKey = (LPFNRCRK)GetProcAddress(hinstDLL, "RegCreateKeyA");
497: lpfnRegDeleteKey = (LPFNRDLK)GetProcAddress(hinstDLL, "RegDeleteKeyA");
498: lpfnRegOpenKey = (LPFNROPK)GetProcAddress(hinstDLL, "RegOpenKeyA");
499: lpfnRegQueryValue = (LPFNRQRV)GetProcAddress(hinstDLL, "RegQueryValueA");
500: lpfnRegSetValue = (LPFNRSTV)GetProcAddress(hinstDLL, "RegSetValueA");
501: #endif
502:
503: if (lpfnRegCloseKey == NULL
504: || lpfnRegCreateKey == NULL
505: || lpfnRegDeleteKey == NULL
506: || lpfnRegOpenKey == NULL
507: || lpfnRegQueryValue == NULL
508: || lpfnRegSetValue == NULL)
509: {
510: FreeLibrary(hinstDLL);
511:
512: hinstDLL = NULL;
513: return(FALSE);
514: }
515:
516: #ifdef WIN32
517: #ifdef UNICODE
518: lpfnRegCreateKeyEx = (LPFNRCRKe)GetProcAddress(hinstDLL, "RegCreateKeyExW");
519: lpfnRegOpenKeyEx = (LPFNROPKe)GetProcAddress(hinstDLL, "RegOpenKeyExW");
520: lpfnRegQueryValueEx = (LPFNRQRVe)GetProcAddress(hinstDLL, "RegQueryValueExW");
521: lpfnRegSetValueEx = (LPFNRSTVe)GetProcAddress(hinstDLL, "RegSetValueExW");
522: lpfnRegEnumKey = (LPFNREK)GetProcAddress(hinstDLL, "RegEnumKeyW");
523: lpfnRegQueryInfoKey = (LPFNRQIK)GetProcAddress(hinstDLL, "RegQueryInfoKeyW");
524: #else
525: lpfnRegCreateKeyEx = (LPFNRCRKe)GetProcAddress(hinstDLL, "RegCreateKeyExA");
526: lpfnRegOpenKeyEx = (LPFNROPKe)GetProcAddress(hinstDLL, "RegOpenKeyExA");
527: lpfnRegQueryValueEx = (LPFNRQRVe)GetProcAddress(hinstDLL, "RegQueryValueExA");
528: lpfnRegSetValueEx = (LPFNRSTVe)GetProcAddress(hinstDLL, "RegSetValueExA");
529: lpfnRegEnumKey = (LPFNREK)GetProcAddress(hinstDLL, "RegEnumKeyA");
530: lpfnRegQueryInfoKey = (LPFNRQIK)GetProcAddress(hinstDLL, "RegQueryInfoKeyA");
531: #endif // UNICODE
532: #endif // WIN32
533:
534: return(TRUE);
535: }
536:
537:
538: // **************************************************************************
539: VOID TerminateRegDb(void)
540: {
541: if (hinstDLL != NULL)
542: {
543: FreeLibrary(hinstDLL);
544:
545: hinstDLL = NULL;
546: }
547: }
548:
549:
550: #endif /* !STF_LITE */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.