Debian Codegraph @

pTk/mTk/tclWin/tclWinReg.c

raw
1 /*
2 * tclWinReg.c --
3 *
4 * This file contains the implementation of the "registry" Tcl
5 * built-in command. This command is built as a dynamically
6 * loadable extension in a separate DLL.
7 *
8 * Copyright (c) 1997 by Sun Microsystems, Inc.
9 * Copyright (c) 1998-1999 by Scriptics Corporation.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclWinReg.c,v 1.21.2.3 2003/11/10 22:42:07 dgp Exp $
15 */
16
17 #include <tclPort.h>
18 #include <stdlib.h>
19
20 /*
21 * TCL_STORAGE_CLASS is set unconditionally to DLLEXPORT because the
22 * Registry_Init declaration is in the source file itself, which is only
23 * accessed when we are building a library.
24 */
25
26 #undef TCL_STORAGE_CLASS
27 #define TCL_STORAGE_CLASS DLLEXPORT
28
29 /*
30 * The following macros convert between different endian ints.
31 */
32
33 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
34 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
35
36 /*
37 * The following flag is used in OpenKeys to indicate that the specified
38 * key should be created if it doesn't currently exist.
39 */
40
41 #define REG_CREATE 1
42
43 /*
44 * The following tables contain the mapping from registry root names
45 * to the system predefined keys.
46 */
47
48 static CONST char *rootKeyNames[] = {
49 "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CLASSES_ROOT",
50 "HKEY_CURRENT_USER", "HKEY_CURRENT_CONFIG",
51 "HKEY_PERFORMANCE_DATA", "HKEY_DYN_DATA", NULL
52 };
53
54 static HKEY rootKeys[] = {
55 HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
56 HKEY_CURRENT_CONFIG, HKEY_PERFORMANCE_DATA, HKEY_DYN_DATA
57 };
58
59 /*
60 * The following table maps from registry types to strings. Note that
61 * the indices for this array are the same as the constants for the
62 * known registry types so we don't need a separate table to hold the
63 * mapping.
64 */
65
66 static CONST char *typeNames[] = {
67 "none", "sz", "expand_sz", "binary", "dword",
68 "dword_big_endian", "link", "multi_sz", "resource_list", NULL
69 };
70
71 static DWORD lastType = REG_RESOURCE_LIST;
72
73 /*
74 * The following structures allow us to select between the Unicode and ASCII
75 * interfaces at run time based on whether Unicode APIs are available. The
76 * Unicode APIs are preferable because they will handle characters outside
77 * of the current code page.
78 */
79
80 typedef struct RegWinProcs {
81 int useWide;
82
83 LONG (WINAPI *regConnectRegistryProc)(CONST TCHAR *, HKEY, PHKEY);
84 LONG (WINAPI *regCreateKeyExProc)(HKEY, CONST TCHAR *, DWORD, TCHAR *,
85 DWORD, REGSAM, SECURITY_ATTRIBUTES *, HKEY *, DWORD *);
86 LONG (WINAPI *regDeleteKeyProc)(HKEY, CONST TCHAR *);
87 LONG (WINAPI *regDeleteValueProc)(HKEY, CONST TCHAR *);
88 LONG (WINAPI *regEnumKeyProc)(HKEY, DWORD, TCHAR *, DWORD);
89 LONG (WINAPI *regEnumKeyExProc)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
90 TCHAR *, DWORD *, FILETIME *);
91 LONG (WINAPI *regEnumValueProc)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
92 DWORD *, BYTE *, DWORD *);
93 LONG (WINAPI *regOpenKeyExProc)(HKEY, CONST TCHAR *, DWORD, REGSAM,
94 HKEY *);
95 LONG (WINAPI *regQueryInfoKeyProc)(HKEY, TCHAR *, DWORD *, DWORD *,
96 DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *,
97 FILETIME *);
98 LONG (WINAPI *regQueryValueExProc)(HKEY, CONST TCHAR *, DWORD *, DWORD *,
99 BYTE *, DWORD *);
100 LONG (WINAPI *regSetValueExProc)(HKEY, CONST TCHAR *, DWORD, DWORD,
101 CONST BYTE*, DWORD);
102 } RegWinProcs;
103
104 static RegWinProcs *regWinProcs;
105
106 static RegWinProcs asciiProcs = {
107 0,
108
109 (LONG (WINAPI *)(CONST TCHAR *, HKEY, PHKEY)) RegConnectRegistryA,
110 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, TCHAR *,
111 DWORD, REGSAM, SECURITY_ATTRIBUTES *, HKEY *,
112 DWORD *)) RegCreateKeyExA,
113 (LONG (WINAPI *)(HKEY, CONST TCHAR *)) RegDeleteKeyA,
114 (LONG (WINAPI *)(HKEY, CONST TCHAR *)) RegDeleteValueA,
115 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD)) RegEnumKeyA,
116 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
117 TCHAR *, DWORD *, FILETIME *)) RegEnumKeyExA,
118 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
119 DWORD *, BYTE *, DWORD *)) RegEnumValueA,
120 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, REGSAM,
121 HKEY *)) RegOpenKeyExA,
122 (LONG (WINAPI *)(HKEY, TCHAR *, DWORD *, DWORD *,
123 DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *,
124 FILETIME *)) RegQueryInfoKeyA,
125 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD *, DWORD *,
126 BYTE *, DWORD *)) RegQueryValueExA,
127 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, DWORD,
128 CONST BYTE*, DWORD)) RegSetValueExA,
129 };
130
131 static RegWinProcs unicodeProcs = {
132 1,
133
134 (LONG (WINAPI *)(CONST TCHAR *, HKEY, PHKEY)) RegConnectRegistryW,
135 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, TCHAR *,
136 DWORD, REGSAM, SECURITY_ATTRIBUTES *, HKEY *,
137 DWORD *)) RegCreateKeyExW,
138 (LONG (WINAPI *)(HKEY, CONST TCHAR *)) RegDeleteKeyW,
139 (LONG (WINAPI *)(HKEY, CONST TCHAR *)) RegDeleteValueW,
140 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD)) RegEnumKeyW,
141 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
142 TCHAR *, DWORD *, FILETIME *)) RegEnumKeyExW,
143 (LONG (WINAPI *)(HKEY, DWORD, TCHAR *, DWORD *, DWORD *,
144 DWORD *, BYTE *, DWORD *)) RegEnumValueW,
145 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, REGSAM,
146 HKEY *)) RegOpenKeyExW,
147 (LONG (WINAPI *)(HKEY, TCHAR *, DWORD *, DWORD *,
148 DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *, DWORD *,
149 FILETIME *)) RegQueryInfoKeyW,
150 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD *, DWORD *,
151 BYTE *, DWORD *)) RegQueryValueExW,
152 (LONG (WINAPI *)(HKEY, CONST TCHAR *, DWORD, DWORD,
153 CONST BYTE*, DWORD)) RegSetValueExW,
154 };
155
156
157 /*
158 * Declarations for functions defined in this file.
159 */
160
161 static void AppendSystemError(Tcl_Interp *interp, DWORD error);
162 static int BroadcastValue(Tcl_Interp *interp, int objc,
163 Tcl_Obj * CONST objv[]);
164 static DWORD ConvertDWORD(DWORD type, DWORD value);
165 static int DeleteKey(Tcl_Interp *interp, Tcl_Obj *keyNameObj);
166 static int DeleteValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
167 Tcl_Obj *valueNameObj);
168 static int GetKeyNames(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
169 Tcl_Obj *patternObj);
170 static int GetType(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
171 Tcl_Obj *valueNameObj);
172 static int GetValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
173 Tcl_Obj *valueNameObj);
174 static int GetValueNames(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
175 Tcl_Obj *patternObj);
176 static int OpenKey(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
177 REGSAM mode, int flags, HKEY *keyPtr);
178 static DWORD OpenSubKey(char *hostName, HKEY rootKey,
179 char *keyName, REGSAM mode, int flags,
180 HKEY *keyPtr);
181 static int ParseKeyName(Tcl_Interp *interp, char *name,
182 char **hostNamePtr, HKEY *rootKeyPtr,
183 char **keyNamePtr);
184 static DWORD RecursiveDeleteKey(HKEY hStartKey,
185 CONST TCHAR * pKeyName);
186 static int RegistryObjCmd(ClientData clientData,
187 Tcl_Interp *interp, int objc,
188 Tcl_Obj * CONST objv[]);
189 static int SetValue(Tcl_Interp *interp, Tcl_Obj *keyNameObj,
190 Tcl_Obj *valueNameObj, Tcl_Obj *dataObj,
191 Tcl_Obj *typeObj);
192
193 EXTERN int Registry_Init(Tcl_Interp *interp);
194
195 /*
196 *----------------------------------------------------------------------
197 *
198 * Registry_Init --
199 *
200 * This procedure initializes the registry command.
201 *
202 * Results:
203 * A standard Tcl result.
204 *
205 * Side effects:
206 * None.
207 *
208 *----------------------------------------------------------------------
209 */
210
211 int
212 Registry_Init(
213 Tcl_Interp *interp)
214 {
215 if (!Tcl_InitStubs(interp, "8.0", 0)) {
216 return TCL_ERROR;
217 }
218
219 /*
220 * Determine if the unicode interfaces are available and select the
221 * appropriate registry function table.
222 */
223
224 if (TclWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
225 regWinProcs = &unicodeProcs;
226 } else {
227 regWinProcs = &asciiProcs;
228 }
229
230 Tcl_CreateObjCommand(interp, "registry", RegistryObjCmd, NULL, NULL);
231 return Tcl_PkgProvide(interp, "registry", "1.1.3");
232 }
233
234 /*
235 *----------------------------------------------------------------------
236 *
237 * RegistryObjCmd --
238 *
239 * This function implements the Tcl "registry" command.
240 *
241 * Results:
242 * A standard Tcl result.
243 *
244 * Side effects:
245 * None.
246 *
247 *----------------------------------------------------------------------
248 */
249
250 static int
251 RegistryObjCmd(
252 ClientData clientData, /* Not used. */
253 Tcl_Interp *interp, /* Current interpreter. */
254 int objc, /* Number of arguments. */
255 Tcl_Obj * CONST objv[]) /* Argument values. */
256 {
257 int index;
258 char *errString;
259
260 static CONST char *subcommands[] = {
261 "broadcast", "delete", "get", "keys", "set", "type", "values",
262 (char *) NULL
263 };
264 enum SubCmdIdx {
265 BroadcastIdx, DeleteIdx, GetIdx, KeysIdx, SetIdx, TypeIdx, ValuesIdx
266 };
267
268 if (objc < 2) {
269 Tcl_WrongNumArgs(interp, objc, objv, "option ?arg arg ...?");
270 return TCL_ERROR;
271 }
272
273 if (Tcl_GetIndexFromObj(interp, objv[1], subcommands, "option", 0, &index)
274 != TCL_OK) {
275 return TCL_ERROR;
276 }
277
278 switch (index) {
279 case BroadcastIdx: /* broadcast */
280 return BroadcastValue(interp, objc, objv);
281 break;
282 case DeleteIdx: /* delete */
283 if (objc == 3) {
284 return DeleteKey(interp, objv[2]);
285 } else if (objc == 4) {
286 return DeleteValue(interp, objv[2], objv[3]);
287 }
288 errString = "keyName ?valueName?";
289 break;
290 case GetIdx: /* get */
291 if (objc == 4) {
292 return GetValue(interp, objv[2], objv[3]);
293 }
294 errString = "keyName valueName";
295 break;
296 case KeysIdx: /* keys */
297 if (objc == 3) {
298 return GetKeyNames(interp, objv[2], NULL);
299 } else if (objc == 4) {
300 return GetKeyNames(interp, objv[2], objv[3]);
301 }
302 errString = "keyName ?pattern?";
303 break;
304 case SetIdx: /* set */
305 if (objc == 3) {
306 HKEY key;
307
308 /*
309 * Create the key and then close it immediately.
310 */
311
312 if (OpenKey(interp, objv[2], KEY_ALL_ACCESS, 1, &key)
313 != TCL_OK) {
314 return TCL_ERROR;
315 }
316 RegCloseKey(key);
317 return TCL_OK;
318 } else if (objc == 5 || objc == 6) {
319 Tcl_Obj *typeObj = (objc == 5) ? NULL : objv[5];
320 return SetValue(interp, objv[2], objv[3], objv[4], typeObj);
321 }
322 errString = "keyName ?valueName data ?type??";
323 break;
324 case TypeIdx: /* type */
325 if (objc == 4) {
326 return GetType(interp, objv[2], objv[3]);
327 }
328 errString = "keyName valueName";
329 break;
330 case ValuesIdx: /* values */
331 if (objc == 3) {
332 return GetValueNames(interp, objv[2], NULL);
333 } else if (objc == 4) {
334 return GetValueNames(interp, objv[2], objv[3]);
335 }
336 errString = "keyName ?pattern?";
337 break;
338 }
339 Tcl_WrongNumArgs(interp, 2, objv, errString);
340 return TCL_ERROR;
341 }
342
343 /*
344 *----------------------------------------------------------------------
345 *
346 * DeleteKey --
347 *
348 * This function deletes a registry key.
349 *
350 * Results:
351 * A standard Tcl result.
352 *
353 * Side effects:
354 * None.
355 *
356 *----------------------------------------------------------------------
357 */
358
359 static int
360 DeleteKey(
361 Tcl_Interp *interp, /* Current interpreter. */
362 Tcl_Obj *keyNameObj) /* Name of key to delete. */
363 {
364 char *tail, *buffer, *hostName, *keyName;
365 CONST char *nativeTail;
366 HKEY rootKey, subkey;
367 DWORD result;
368 int length;
369 Tcl_Obj *resultPtr;
370 Tcl_DString buf;
371
372 /*
373 * Find the parent of the key being deleted and open it.
374 */
375
376 keyName = Tcl_GetStringFromObj(keyNameObj, &length);
377 buffer = ckalloc((unsigned int) length + 1);
378 strcpy(buffer, keyName);
379
380 if (ParseKeyName(interp, buffer, &hostName, &rootKey, &keyName)
381 != TCL_OK) {
382 ckfree(buffer);
383 return TCL_ERROR;
384 }
385
386 resultPtr = Tcl_GetObjResult(interp);
387 if (*keyName == '\0') {
388 Tcl_AppendToObj(resultPtr, "bad key: cannot delete root keys", -1);
389 ckfree(buffer);
390 return TCL_ERROR;
391 }
392
393 tail = strrchr(keyName, '\\');
394 if (tail) {
395 *tail++ = '\0';
396 } else {
397 tail = keyName;
398 keyName = NULL;
399 }
400
401 result = OpenSubKey(hostName, rootKey, keyName,
402 KEY_ENUMERATE_SUB_KEYS | DELETE, 0, &subkey);
403 if (result != ERROR_SUCCESS) {
404 ckfree(buffer);
405 if (result == ERROR_FILE_NOT_FOUND) {
406 return TCL_OK;
407 } else {
408 Tcl_AppendToObj(resultPtr, "unable to delete key: ", -1);
409 AppendSystemError(interp, result);
410 return TCL_ERROR;
411 }
412 }
413
414 /*
415 * Now we recursively delete the key and everything below it.
416 */
417
418 nativeTail = Tcl_WinUtfToTChar(tail, -1, &buf);
419 result = RecursiveDeleteKey(subkey, nativeTail);
420 Tcl_DStringFree(&buf);
421
422 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
423 Tcl_AppendToObj(resultPtr, "unable to delete key: ", -1);
424 AppendSystemError(interp, result);
425 result = TCL_ERROR;
426 } else {
427 result = TCL_OK;
428 }
429
430 RegCloseKey(subkey);
431 ckfree(buffer);
432 return result;
433 }
434
435 /*
436 *----------------------------------------------------------------------
437 *
438 * DeleteValue --
439 *
440 * This function deletes a value from a registry key.
441 *
442 * Results:
443 * A standard Tcl result.
444 *
445 * Side effects:
446 * None.
447 *
448 *----------------------------------------------------------------------
449 */
450
451 static int
452 DeleteValue(
453 Tcl_Interp *interp, /* Current interpreter. */
454 Tcl_Obj *keyNameObj, /* Name of key. */
455 Tcl_Obj *valueNameObj) /* Name of value to delete. */
456 {
457 HKEY key;
458 char *valueName;
459 int length;
460 DWORD result;
461 Tcl_Obj *resultPtr;
462 Tcl_DString ds;
463
464 /*
465 * Attempt to open the key for deletion.
466 */
467
468 if (OpenKey(interp, keyNameObj, KEY_SET_VALUE, 0, &key)
469 != TCL_OK) {
470 return TCL_ERROR;
471 }
472
473 resultPtr = Tcl_GetObjResult(interp);
474 valueName = Tcl_GetStringFromObj(valueNameObj, &length);
475 Tcl_WinUtfToTChar(valueName, length, &ds);
476 result = (*regWinProcs->regDeleteValueProc)(key, Tcl_DStringValue(&ds));
477 Tcl_DStringFree(&ds);
478 if (result != ERROR_SUCCESS) {
479 Tcl_AppendStringsToObj(resultPtr, "unable to delete value \"",
480 Tcl_GetString(valueNameObj), "\" from key \"",
481 Tcl_GetString(keyNameObj), "\": ", NULL);
482 AppendSystemError(interp, result);
483 result = TCL_ERROR;
484 } else {
485 result = TCL_OK;
486 }
487 RegCloseKey(key);
488 return result;
489 }
490
491 /*
492 *----------------------------------------------------------------------
493 *
494 * GetKeyNames --
495 *
496 * This function enumerates the subkeys of a given key. If the
497 * optional pattern is supplied, then only keys that match the
498 * pattern will be returned.
499 *
500 * Results:
501 * Returns the list of subkeys in the result object of the
502 * interpreter, or an error message on failure.
503 *
504 * Side effects:
505 * None.
506 *
507 *----------------------------------------------------------------------
508 */
509
510 static int
511 GetKeyNames(
512 Tcl_Interp *interp, /* Current interpreter. */
513 Tcl_Obj *keyNameObj, /* Key to enumerate. */
514 Tcl_Obj *patternObj) /* Optional match pattern. */
515 {
516 HKEY key;
517 DWORD index;
518 char buffer[MAX_PATH+1], *pattern, *name;
519 Tcl_Obj *resultPtr;
520 int result = TCL_OK;
521 Tcl_DString ds;
522
523 /*
524 * Attempt to open the key for enumeration.
525 */
526
527 if (OpenKey(interp, keyNameObj, KEY_ENUMERATE_SUB_KEYS, 0, &key)
528 != TCL_OK) {
529 return TCL_ERROR;
530 }
531
532 if (patternObj) {
533 pattern = Tcl_GetString(patternObj);
534 } else {
535 pattern = NULL;
536 }
537
538 /*
539 * Enumerate over the subkeys until we get an error, indicating the
540 * end of the list.
541 */
542
543 resultPtr = Tcl_GetObjResult(interp);
544 for (index = 0; (*regWinProcs->regEnumKeyProc)(key, index, buffer,
545 MAX_PATH+1) == ERROR_SUCCESS; index++) {
546 Tcl_WinTCharToUtf((TCHAR *) buffer, -1, &ds);
547 name = Tcl_DStringValue(&ds);
548 if (pattern && !Tcl_StringMatch(name, pattern)) {
549 Tcl_DStringFree(&ds);
550 continue;
551 }
552 result = Tcl_ListObjAppendElement(interp, resultPtr,
553 Tcl_NewStringObj(name, Tcl_DStringLength(&ds)));
554 Tcl_DStringFree(&ds);
555 if (result != TCL_OK) {
556 break;
557 }
558 }
559
560 RegCloseKey(key);
561 return result;
562 }
563
564 /*
565 *----------------------------------------------------------------------
566 *
567 * GetType --
568 *
569 * This function gets the type of a given registry value and
570 * places it in the interpreter result.
571 *
572 * Results:
573 * Returns a normal Tcl result.
574 *
575 * Side effects:
576 * None.
577 *
578 *----------------------------------------------------------------------
579 */
580
581 static int
582 GetType(
583 Tcl_Interp *interp, /* Current interpreter. */
584 Tcl_Obj *keyNameObj, /* Name of key. */
585 Tcl_Obj *valueNameObj) /* Name of value to get. */
586 {
587 HKEY key;
588 Tcl_Obj *resultPtr;
589 DWORD result;
590 DWORD type;
591 Tcl_DString ds;
592 char *valueName;
593 CONST char *nativeValue;
594 int length;
595
596 /*
597 * Attempt to open the key for reading.
598 */
599
600 if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
601 != TCL_OK) {
602 return TCL_ERROR;
603 }
604
605 /*
606 * Get the type of the value.
607 */
608
609 resultPtr = Tcl_GetObjResult(interp);
610
611 valueName = Tcl_GetStringFromObj(valueNameObj, &length);
612 nativeValue = Tcl_WinUtfToTChar(valueName, length, &ds);
613 result = (*regWinProcs->regQueryValueExProc)(key, nativeValue, NULL, &type,
614 NULL, NULL);
615 Tcl_DStringFree(&ds);
616 RegCloseKey(key);
617
618 if (result != ERROR_SUCCESS) {
619 Tcl_AppendStringsToObj(resultPtr, "unable to get type of value \"",
620 Tcl_GetString(valueNameObj), "\" from key \"",
621 Tcl_GetString(keyNameObj), "\": ", NULL);
622 AppendSystemError(interp, result);
623 return TCL_ERROR;
624 }
625
626 /*
627 * Set the type into the result. Watch out for unknown types.
628 * If we don't know about the type, just use the numeric value.
629 */
630
631 if (type > lastType || type < 0) {
632 Tcl_SetIntObj(resultPtr, (int) type);
633 } else {
634 Tcl_SetStringObj(resultPtr, typeNames[type], -1);
635 }
636 return TCL_OK;
637 }
638
639 /*
640 *----------------------------------------------------------------------
641 *
642 * GetValue --
643 *
644 * This function gets the contents of a registry value and places
645 * a list containing the data and the type in the interpreter
646 * result.
647 *
648 * Results:
649 * Returns a normal Tcl result.
650 *
651 * Side effects:
652 * None.
653 *
654 *----------------------------------------------------------------------
655 */
656
657 static int
658 GetValue(
659 Tcl_Interp *interp, /* Current interpreter. */
660 Tcl_Obj *keyNameObj, /* Name of key. */
661 Tcl_Obj *valueNameObj) /* Name of value to get. */
662 {
663 HKEY key;
664 char *valueName;
665 CONST char *nativeValue;
666 DWORD result, length, type;
667 Tcl_Obj *resultPtr;
668 Tcl_DString data, buf;
669 int nameLen;
670
671 /*
672 * Attempt to open the key for reading.
673 */
674
675 if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
676 != TCL_OK) {
677 return TCL_ERROR;
678 }
679
680 /*
681 * Initialize a Dstring to maximum statically allocated size
682 * we could get one more byte by avoiding Tcl_DStringSetLength()
683 * and just setting length to TCL_DSTRING_STATIC_SIZE, but this
684 * should be safer if the implementation of Dstrings changes.
685 *
686 * This allows short values to be read from the registy in one call.
687 * Longer values need a second call with an expanded DString.
688 */
689
690 Tcl_DStringInit(&data);
691 length = TCL_DSTRING_STATIC_SIZE - 1;
692 Tcl_DStringSetLength(&data, (int) length);
693
694 resultPtr = Tcl_GetObjResult(interp);
695
696 valueName = Tcl_GetStringFromObj(valueNameObj, &nameLen);
697 nativeValue = Tcl_WinUtfToTChar(valueName, nameLen, &buf);
698
699 result = (*regWinProcs->regQueryValueExProc)(key, nativeValue, NULL, &type,
700 (BYTE *) Tcl_DStringValue(&data), &length);
701 while (result == ERROR_MORE_DATA) {
702 /*
703 * The Windows docs say that in this error case, we just need
704 * to expand our buffer and request more data.
705 * Required for HKEY_PERFORMANCE_DATA
706 */
707 length *= 2;
708 Tcl_DStringSetLength(&data, (int) length);
709 result = (*regWinProcs->regQueryValueExProc)(key, (char *) nativeValue,
710 NULL, &type, (BYTE *) Tcl_DStringValue(&data), &length);
711 }
712 Tcl_DStringFree(&buf);
713 RegCloseKey(key);
714 if (result != ERROR_SUCCESS) {
715 Tcl_AppendStringsToObj(resultPtr, "unable to get value \"",
716 Tcl_GetString(valueNameObj), "\" from key \"",
717 Tcl_GetString(keyNameObj), "\": ", NULL);
718 AppendSystemError(interp, result);
719 Tcl_DStringFree(&data);
720 return TCL_ERROR;
721 }
722
723 /*
724 * If the data is a 32-bit quantity, store it as an integer object. If it
725 * is a multi-string, store it as a list of strings. For null-terminated
726 * strings, append up the to first null. Otherwise, store it as a binary
727 * string.
728 */
729
730 if (type == REG_DWORD || type == REG_DWORD_BIG_ENDIAN) {
731 Tcl_SetIntObj(resultPtr, (int) ConvertDWORD(type,
732 *((DWORD*) Tcl_DStringValue(&data))));
733 } else if (type == REG_MULTI_SZ) {
734 char *p = Tcl_DStringValue(&data);
735 char *end = Tcl_DStringValue(&data) + length;
736
737 /*
738 * Multistrings are stored as an array of null-terminated strings,
739 * terminated by two null characters. Also do a bounds check in
740 * case we get bogus data.
741 */
742
743 while (p < end && ((regWinProcs->useWide)
744 ? *((Tcl_UniChar *)p) : *p) != 0) {
745 Tcl_WinTCharToUtf((TCHAR *) p, -1, &buf);
746 Tcl_ListObjAppendElement(interp, resultPtr,
747 Tcl_NewStringObj(Tcl_DStringValue(&buf),
748 Tcl_DStringLength(&buf)));
749 if (regWinProcs->useWide) {
750 while (*((Tcl_UniChar *)p)++ != 0) {}
751 } else {
752 while (*p++ != '\0') {}
753 }
754 Tcl_DStringFree(&buf);
755 }
756 } else if ((type == REG_SZ) || (type == REG_EXPAND_SZ)) {
757 Tcl_WinTCharToUtf((TCHAR *) Tcl_DStringValue(&data), -1, &buf);
758 Tcl_SetStringObj(resultPtr, Tcl_DStringValue(&buf),
759 Tcl_DStringLength(&buf));
760 Tcl_DStringFree(&buf);
761 } else {
762 /*
763 * Save binary data as a byte array.
764 */
765
766 Tcl_SetByteArrayObj(resultPtr, Tcl_DStringValue(&data), (int) length);
767 }
768 Tcl_DStringFree(&data);
769 return result;
770 }
771
772 /*
773 *----------------------------------------------------------------------
774 *
775 * GetValueNames --
776 *
777 * This function enumerates the values of the a given key. If
778 * the optional pattern is supplied, then only value names that
779 * match the pattern will be returned.
780 *
781 * Results:
782 * Returns the list of value names in the result object of the
783 * interpreter, or an error message on failure.
784 *
785 * Side effects:
786 * None.
787 *
788 *----------------------------------------------------------------------
789 */
790
791 static int
792 GetValueNames(
793 Tcl_Interp *interp, /* Current interpreter. */
794 Tcl_Obj *keyNameObj, /* Key to enumerate. */
795 Tcl_Obj *patternObj) /* Optional match pattern. */
796 {
797 HKEY key;
798 Tcl_Obj *resultPtr;
799 DWORD index, size, maxSize, result;
800 Tcl_DString buffer, ds;
801 char *pattern, *name;
802
803 /*
804 * Attempt to open the key for enumeration.
805 */
806
807 if (OpenKey(interp, keyNameObj, KEY_QUERY_VALUE, 0, &key)
808 != TCL_OK) {
809 return TCL_ERROR;
810 }
811
812 resultPtr = Tcl_GetObjResult(interp);
813
814 /*
815 * Query the key to determine the appropriate buffer size to hold the
816 * largest value name plus the terminating null.
817 */
818
819 result = (*regWinProcs->regQueryInfoKeyProc)(key, NULL, NULL, NULL, NULL,
820 NULL, NULL, &index, &maxSize, NULL, NULL, NULL);
821 if (result != ERROR_SUCCESS) {
822 Tcl_AppendStringsToObj(resultPtr, "unable to query key \"",
823 Tcl_GetString(keyNameObj), "\": ", NULL);
824 AppendSystemError(interp, result);
825 RegCloseKey(key);
826 result = TCL_ERROR;
827 goto done;
828 }
829 maxSize++;
830
831
832 Tcl_DStringInit(&buffer);
833 Tcl_DStringSetLength(&buffer,
834 (int) ((regWinProcs->useWide) ? maxSize*2 : maxSize));
835 index = 0;
836 result = TCL_OK;
837
838 if (patternObj) {
839 pattern = Tcl_GetString(patternObj);
840 } else {
841 pattern = NULL;
842 }
843
844 /*
845 * Enumerate the values under the given subkey until we get an error,
846 * indicating the end of the list. Note that we need to reset size
847 * after each iteration because RegEnumValue smashes the old value.
848 */
849
850 size = maxSize;
851 while ((*regWinProcs->regEnumValueProc)(key, index,
852 Tcl_DStringValue(&buffer), &size, NULL, NULL, NULL, NULL)
853 == ERROR_SUCCESS) {
854
855 if (regWinProcs->useWide) {
856 size *= 2;
857 }
858
859 Tcl_WinTCharToUtf((TCHAR *) Tcl_DStringValue(&buffer), (int) size, &ds);
860 name = Tcl_DStringValue(&ds);
861 if (!pattern || Tcl_StringMatch(name, pattern)) {
862 result = Tcl_ListObjAppendElement(interp, resultPtr,
863 Tcl_NewStringObj(name, Tcl_DStringLength(&ds)));
864 if (result != TCL_OK) {
865 Tcl_DStringFree(&ds);
866 break;
867 }
868 }
869 Tcl_DStringFree(&ds);
870
871 index++;
872 size = maxSize;
873 }
874 Tcl_DStringFree(&buffer);
875
876 done:
877 RegCloseKey(key);
878 return result;
879 }
880
881 /*
882 *----------------------------------------------------------------------
883 *
884 * OpenKey --
885 *
886 * This function opens the specified key. This function is a
887 * simple wrapper around ParseKeyName and OpenSubKey.
888 *
889 * Results:
890 * Returns the opened key in the keyPtr argument and a Tcl
891 * result code.
892 *
893 * Side effects:
894 * None.
895 *
896 *----------------------------------------------------------------------
897 */
898
899 static int
900 OpenKey(
901 Tcl_Interp *interp, /* Current interpreter. */
902 Tcl_Obj *keyNameObj, /* Key to open. */
903 REGSAM mode, /* Access mode. */
904 int flags, /* 0 or REG_CREATE. */
905 HKEY *keyPtr) /* Returned HKEY. */
906 {
907 char *keyName, *buffer, *hostName;
908 int length;
909 HKEY rootKey;
910 DWORD result;
911
912 keyName = Tcl_GetStringFromObj(keyNameObj, &length);
913 buffer = ckalloc((unsigned int) length + 1);
914 strcpy(buffer, keyName);
915
916 result = ParseKeyName(interp, buffer, &hostName, &rootKey, &keyName);
917 if (result == TCL_OK) {
918 result = OpenSubKey(hostName, rootKey, keyName, mode, flags, keyPtr);
919 if (result != ERROR_SUCCESS) {
920 Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
921 Tcl_AppendToObj(resultPtr, "unable to open key: ", -1);
922 AppendSystemError(interp, result);
923 result = TCL_ERROR;
924 } else {
925 result = TCL_OK;
926 }
927 }
928
929 ckfree(buffer);
930 return result;
931 }
932
933 /*
934 *----------------------------------------------------------------------
935 *
936 * OpenSubKey --
937 *
938 * This function opens a given subkey of a root key on the
939 * specified host.
940 *
941 * Results:
942 * Returns the opened key in the keyPtr and a Windows error code
943 * as the return value.
944 *
945 * Side effects:
946 * None.
947 *
948 *----------------------------------------------------------------------
949 */
950
951 static DWORD
952 OpenSubKey(
953 char *hostName, /* Host to access, or NULL for local. */
954 HKEY rootKey, /* Root registry key. */
955 char *keyName, /* Subkey name. */
956 REGSAM mode, /* Access mode. */
957 int flags, /* 0 or REG_CREATE. */
958 HKEY *keyPtr) /* Returned HKEY. */
959 {
960 DWORD result;
961 Tcl_DString buf;
962
963 /*
964 * Attempt to open the root key on a remote host if necessary.
965 */
966
967 if (hostName) {
968 hostName = (char *) Tcl_WinUtfToTChar(hostName, -1, &buf);
969 result = (*regWinProcs->regConnectRegistryProc)(hostName, rootKey,
970 &rootKey);
971 Tcl_DStringFree(&buf);
972 if (result != ERROR_SUCCESS) {
973 return result;
974 }
975 }
976
977 /*
978 * Now open the specified key with the requested permissions. Note
979 * that this key must be closed by the caller.
980 */
981
982 keyName = (char *) Tcl_WinUtfToTChar(keyName, -1, &buf);
983 if (flags & REG_CREATE) {
984 DWORD create;
985 result = (*regWinProcs->regCreateKeyExProc)(rootKey, keyName, 0, NULL,
986 REG_OPTION_NON_VOLATILE, mode, NULL, keyPtr, &create);
987 } else {
988 if (rootKey == HKEY_PERFORMANCE_DATA) {
989 /*
990 * Here we fudge it for this special root key.
991 * See MSDN for more info on HKEY_PERFORMANCE_DATA and
992 * the peculiarities surrounding it
993 */
994 *keyPtr = HKEY_PERFORMANCE_DATA;
995 result = ERROR_SUCCESS;
996 } else {
997 result = (*regWinProcs->regOpenKeyExProc)(rootKey, keyName, 0,
998 mode, keyPtr);
999 }
1000 }
1001 Tcl_DStringFree(&buf);
1002
1003 /*
1004 * Be sure to close the root key since we are done with it now.
1005 */
1006
1007 if (hostName) {
1008 RegCloseKey(rootKey);
1009 }
1010 return result;
1011 }
1012
1013 /*
1014 *----------------------------------------------------------------------
1015 *
1016 * ParseKeyName --
1017 *
1018 * This function parses a key name into the host, root, and subkey
1019 * parts.
1020 *
1021 * Results:
1022 * The pointers to the start of the host and subkey names are
1023 * returned in the hostNamePtr and keyNamePtr variables. The
1024 * specified root HKEY is returned in rootKeyPtr. Returns
1025 * a standard Tcl result.
1026 *
1027 *
1028 * Side effects:
1029 * Modifies the name string by inserting nulls.
1030 *
1031 *----------------------------------------------------------------------
1032 */
1033
1034 static int
1035 ParseKeyName(
1036 Tcl_Interp *interp, /* Current interpreter. */
1037 char *name,
1038 char **hostNamePtr,
1039 HKEY *rootKeyPtr,
1040 char **keyNamePtr)
1041 {
1042 char *rootName;
1043 int result, index;
1044 Tcl_Obj *rootObj, *resultPtr = Tcl_GetObjResult(interp);
1045
1046 /*
1047 * Split the key into host and root portions.
1048 */
1049
1050 *hostNamePtr = *keyNamePtr = rootName = NULL;
1051 if (name[0] == '\\') {
1052 if (name[1] == '\\') {
1053 *hostNamePtr = name;
1054 for (rootName = name+2; *rootName != '\0'; rootName++) {
1055 if (*rootName == '\\') {
1056 *rootName++ = '\0';
1057 break;
1058 }
1059 }
1060 }
1061 } else {
1062 rootName = name;
1063 }
1064 if (!rootName) {
1065 Tcl_AppendStringsToObj(resultPtr, "bad key \"", name,
1066 "\": must start with a valid root", NULL);
1067 return TCL_ERROR;
1068 }
1069
1070 /*
1071 * Split the root into root and subkey portions.
1072 */
1073
1074 for (*keyNamePtr = rootName; **keyNamePtr != '\0'; (*keyNamePtr)++) {
1075 if (**keyNamePtr == '\\') {
1076 **keyNamePtr = '\0';
1077 (*keyNamePtr)++;
1078 break;
1079 }
1080 }
1081
1082 /*
1083 * Look for a matching root name.
1084 */
1085
1086 rootObj = Tcl_NewStringObj(rootName, -1);
1087 result = Tcl_GetIndexFromObj(interp, rootObj, rootKeyNames, "root name",
1088 TCL_EXACT, &index);
1089 Tcl_DecrRefCount(rootObj);
1090 if (result != TCL_OK) {
1091 return TCL_ERROR;
1092 }
1093 *rootKeyPtr = rootKeys[index];
1094 return TCL_OK;
1095 }
1096
1097 /*
1098 *----------------------------------------------------------------------
1099 *
1100 * RecursiveDeleteKey --
1101 *
1102 * This function recursively deletes all the keys below a starting
1103 * key. Although Windows 95 does this automatically, we still need
1104 * to do this for Windows NT.
1105 *
1106 * Results:
1107 * Returns a Windows error code.
1108 *
1109 * Side effects:
1110 * Deletes all of the keys and values below the given key.
1111 *
1112 *----------------------------------------------------------------------
1113 */
1114
1115 static DWORD
1116 RecursiveDeleteKey(
1117 HKEY startKey, /* Parent of key to be deleted. */
1118 CONST char *keyName) /* Name of key to be deleted in external
1119 * encoding, not UTF. */
1120 {
1121 DWORD result, size, maxSize;
1122 Tcl_DString subkey;
1123 HKEY hKey;
1124
1125 /*
1126 * Do not allow NULL or empty key name.
1127 */
1128
1129 if (!keyName || *keyName == '\0') {
1130 return ERROR_BADKEY;
1131 }
1132
1133 result = (*regWinProcs->regOpenKeyExProc)(startKey, keyName, 0,
1134 KEY_ENUMERATE_SUB_KEYS | DELETE | KEY_QUERY_VALUE, &hKey);
1135 if (result != ERROR_SUCCESS) {
1136 return result;
1137 }
1138 result = (*regWinProcs->regQueryInfoKeyProc)(hKey, NULL, NULL, NULL, NULL,
1139 &maxSize, NULL, NULL, NULL, NULL, NULL, NULL);
1140 maxSize++;
1141 if (result != ERROR_SUCCESS) {
1142 return result;
1143 }
1144
1145 Tcl_DStringInit(&subkey);
1146 Tcl_DStringSetLength(&subkey,
1147 (int) ((regWinProcs->useWide) ? maxSize * 2 : maxSize));
1148
1149 while (result == ERROR_SUCCESS) {
1150 /*
1151 * Always get index 0 because key deletion changes ordering.
1152 */
1153
1154 size = maxSize;
1155 result=(*regWinProcs->regEnumKeyExProc)(hKey, 0,
1156 Tcl_DStringValue(&subkey), &size, NULL, NULL, NULL, NULL);
1157 if (result == ERROR_NO_MORE_ITEMS) {
1158 result = (*regWinProcs->regDeleteKeyProc)(startKey, keyName);
1159 break;
1160 } else if (result == ERROR_SUCCESS) {
1161 result = RecursiveDeleteKey(hKey, Tcl_DStringValue(&subkey));
1162 }
1163 }
1164 Tcl_DStringFree(&subkey);
1165 RegCloseKey(hKey);
1166 return result;
1167 }
1168
1169 /*
1170 *----------------------------------------------------------------------
1171 *
1172 * SetValue --
1173 *
1174 * This function sets the contents of a registry value. If
1175 * the key or value does not exist, it will be created. If it
1176 * does exist, then the data and type will be replaced.
1177 *
1178 * Results:
1179 * Returns a normal Tcl result.
1180 *
1181 * Side effects:
1182 * May create new keys or values.
1183 *
1184 *----------------------------------------------------------------------
1185 */
1186
1187 static int
1188 SetValue(
1189 Tcl_Interp *interp, /* Current interpreter. */
1190 Tcl_Obj *keyNameObj, /* Name of key. */
1191 Tcl_Obj *valueNameObj, /* Name of value to set. */
1192 Tcl_Obj *dataObj, /* Data to be written. */
1193 Tcl_Obj *typeObj) /* Type of data to be written. */
1194 {
1195 DWORD type, result;
1196 HKEY key;
1197 int length;
1198 char *valueName;
1199 Tcl_Obj *resultPtr;
1200 Tcl_DString nameBuf;
1201
1202 if (typeObj == NULL) {
1203 type = REG_SZ;
1204 } else if (Tcl_GetIndexFromObj(interp, typeObj, typeNames, "type",
1205 0, (int *) &type) != TCL_OK) {
1206 if (Tcl_GetIntFromObj(NULL, typeObj, (int*) &type) != TCL_OK) {
1207 return TCL_ERROR;
1208 }
1209 Tcl_ResetResult(interp);
1210 }
1211 if (OpenKey(interp, keyNameObj, KEY_ALL_ACCESS, 1, &key) != TCL_OK) {
1212 return TCL_ERROR;
1213 }
1214
1215 valueName = Tcl_GetStringFromObj(valueNameObj, &length);
1216 valueName = (char *) Tcl_WinUtfToTChar(valueName, length, &nameBuf);
1217 resultPtr = Tcl_GetObjResult(interp);
1218
1219 if (type == REG_DWORD || type == REG_DWORD_BIG_ENDIAN) {
1220 DWORD value;
1221 if (Tcl_GetIntFromObj(interp, dataObj, (int*) &value) != TCL_OK) {
1222 RegCloseKey(key);
1223 Tcl_DStringFree(&nameBuf);
1224 return TCL_ERROR;
1225 }
1226
1227 value = ConvertDWORD(type, value);
1228 result = (*regWinProcs->regSetValueExProc)(key, valueName, 0, type,
1229 (BYTE*) &value, sizeof(DWORD));
1230 } else if (type == REG_MULTI_SZ) {
1231 Tcl_DString data, buf;
1232 int objc, i;
1233 Tcl_Obj **objv;
1234
1235 if (Tcl_ListObjGetElements(interp, dataObj, &objc, &objv) != TCL_OK) {
1236 RegCloseKey(key);
1237 Tcl_DStringFree(&nameBuf);
1238 return TCL_ERROR;
1239 }
1240
1241 /*
1242 * Append the elements as null terminated strings. Note that
1243 * we must not assume the length of the string in case there are
1244 * embedded nulls, which aren't allowed in REG_MULTI_SZ values.
1245 */
1246
1247 Tcl_DStringInit(&data);
1248 for (i = 0; i < objc; i++) {
1249 Tcl_DStringAppend(&data, Tcl_GetString(objv[i]), -1);
1250
1251 /*
1252 * Add a null character to separate this value from the next.
1253 * We accomplish this by growing the string by one byte. Since the
1254 * DString always tacks on an extra null byte, the new byte will
1255 * already be set to null.
1256 */
1257
1258 Tcl_DStringSetLength(&data, Tcl_DStringLength(&data)+1);
1259 }
1260
1261 Tcl_WinUtfToTChar(Tcl_DStringValue(&data), Tcl_DStringLength(&data)+1,
1262 &buf);
1263 result = (*regWinProcs->regSetValueExProc)(key, valueName, 0, type,
1264 (BYTE *) Tcl_DStringValue(&buf),
1265 (DWORD) Tcl_DStringLength(&buf));
1266 Tcl_DStringFree(&data);
1267 Tcl_DStringFree(&buf);
1268 } else if (type == REG_SZ || type == REG_EXPAND_SZ) {
1269 Tcl_DString buf;
1270 char *data = Tcl_GetStringFromObj(dataObj, &length);
1271
1272 data = (char *) Tcl_WinUtfToTChar(data, length, &buf);
1273
1274 /*
1275 * Include the null in the length, padding if needed for Unicode.
1276 */
1277
1278 if (regWinProcs->useWide) {
1279 Tcl_DStringSetLength(&buf, Tcl_DStringLength(&buf)+1);
1280 }
1281 length = Tcl_DStringLength(&buf) + 1;
1282
1283 result = (*regWinProcs->regSetValueExProc)(key, valueName, 0, type,
1284 (BYTE*)data, (DWORD) length);
1285 Tcl_DStringFree(&buf);
1286 } else {
1287 char *data;
1288
1289 /*
1290 * Store binary data in the registry.
1291 */
1292
1293 data = Tcl_GetByteArrayFromObj(dataObj, &length);
1294 result = (*regWinProcs->regSetValueExProc)(key, valueName, 0, type,
1295 (BYTE *)data, (DWORD) length);
1296 }
1297 Tcl_DStringFree(&nameBuf);
1298 RegCloseKey(key);
1299 if (result != ERROR_SUCCESS) {
1300 Tcl_AppendToObj(resultPtr, "unable to set value: ", -1);
1301 AppendSystemError(interp, result);
1302 return TCL_ERROR;
1303 }
1304 return TCL_OK;
1305 }
1306
1307 /*
1308 *----------------------------------------------------------------------
1309 *
1310 * BroadcastValue --
1311 *
1312 * This function broadcasts a WM_SETTINGCHANGE message to indicate
1313 * to other programs that we have changed the contents of a registry
1314 * value.
1315 *
1316 * Results:
1317 * Returns a normal Tcl result.
1318 *
1319 * Side effects:
1320 * Will cause other programs to reload their system settings.
1321 *
1322 *----------------------------------------------------------------------
1323 */
1324
1325 static int
1326 BroadcastValue(
1327 Tcl_Interp *interp, /* Current interpreter. */
1328 int objc, /* Number of arguments. */
1329 Tcl_Obj * CONST objv[]) /* Argument values. */
1330 {
1331 DWORD result, sendResult;
1332 UINT timeout = 3000;
1333 int len;
1334 char *str;
1335 Tcl_Obj *objPtr;
1336
1337 if ((objc != 3) && (objc != 5)) {
1338 Tcl_WrongNumArgs(interp, 2, objv, "keyName ?-timeout millisecs?");
1339 return TCL_ERROR;
1340 }
1341
1342 if (objc > 3) {
1343 str = Tcl_GetStringFromObj(objv[3], &len);
1344 if ((len < 2) || (*str != '-') || strncmp(str, "-timeout", (size_t) len)) {
1345 Tcl_WrongNumArgs(interp, 2, objv, "keyName ?-timeout millisecs?");
1346 return TCL_ERROR;
1347 }
1348 if (Tcl_GetIntFromObj(interp, objv[4], (int *) &timeout) != TCL_OK) {
1349 return TCL_ERROR;
1350 }
1351 }
1352
1353 str = Tcl_GetStringFromObj(objv[2], &len);
1354 if (len == 0) {
1355 str = NULL;
1356 }
1357
1358 /*
1359 * Use the ignore the result.
1360 */
1361 result = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
1362 (WPARAM) 0, (LPARAM) str, SMTO_ABORTIFHUNG, timeout, &sendResult);
1363
1364 objPtr = Tcl_NewObj();
1365 Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewIntObj((int) result));
1366 Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewIntObj((int) sendResult));
1367 Tcl_SetObjResult(interp, objPtr);
1368
1369 return TCL_OK;
1370 }
1371
1372 /*
1373 *----------------------------------------------------------------------
1374 *
1375 * AppendSystemError --
1376 *
1377 * This routine formats a Windows system error message and places
1378 * it into the interpreter result.
1379 *
1380 * Results:
1381 * None.
1382 *
1383 * Side effects:
1384 * None.
1385 *
1386 *----------------------------------------------------------------------
1387 */
1388
1389 static void
1390 AppendSystemError(
1391 Tcl_Interp *interp, /* Current interpreter. */
1392 DWORD error) /* Result code from error. */
1393 {
1394 int length;
1395 WCHAR *wMsgPtr;
1396 char *msg;
1397 char id[TCL_INTEGER_SPACE], msgBuf[24 + TCL_INTEGER_SPACE];
1398 Tcl_DString ds;
1399 Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
1400
1401 length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
1402 | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error,
1403 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (WCHAR *) &wMsgPtr,
1404 0, NULL);
1405 if (length == 0) {
1406 char *msgPtr;
1407
1408 length = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
1409 | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, error,
1410 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &msgPtr,
1411 0, NULL);
1412 if (length > 0) {
1413 wMsgPtr = (WCHAR *) LocalAlloc(LPTR, (length + 1) * sizeof(WCHAR));
1414 MultiByteToWideChar(CP_ACP, 0, msgPtr, length + 1, wMsgPtr,
1415 length + 1);
1416 LocalFree(msgPtr);
1417 }
1418 }
1419 if (length == 0) {
1420 if (error == ERROR_CALL_NOT_IMPLEMENTED) {
1421 msg = "function not supported under Win32s";
1422 } else {
1423 sprintf(msgBuf, "unknown error: %ld", error);
1424 msg = msgBuf;
1425 }
1426 } else {
1427 Tcl_Encoding encoding;
1428
1429 encoding = Tcl_GetEncoding(NULL, "unicode");
1430 Tcl_ExternalToUtfDString(encoding, (char *) wMsgPtr, -1, &ds);
1431 Tcl_FreeEncoding(encoding);
1432 LocalFree(wMsgPtr);
1433
1434 msg = Tcl_DStringValue(&ds);
1435 length = Tcl_DStringLength(&ds);
1436
1437 /*
1438 * Trim the trailing CR/LF from the system message.
1439 */
1440 if (msg[length-1] == '\n') {
1441 msg[--length] = 0;
1442 }
1443 if (msg[length-1] == '\r') {
1444 msg[--length] = 0;
1445 }
1446 }
1447
1448 sprintf(id, "%ld", error);
1449 Tcl_SetErrorCode(interp, "WINDOWS", id, msg, (char *) NULL);
1450 Tcl_AppendToObj(resultPtr, msg, length);
1451
1452 if (length != 0) {
1453 Tcl_DStringFree(&ds);
1454 }
1455 }
1456
1457 /*
1458 *----------------------------------------------------------------------
1459 *
1460 * ConvertDWORD --
1461 *
1462 * This function determines whether a DWORD needs to be byte
1463 * swapped, and returns the appropriately swapped value.
1464 *
1465 * Results:
1466 * Returns a converted DWORD.
1467 *
1468 * Side effects:
1469 * None.
1470 *
1471 *----------------------------------------------------------------------
1472 */
1473
1474 static DWORD
1475 ConvertDWORD(
1476 DWORD type, /* Either REG_DWORD or REG_DWORD_BIG_ENDIAN */
1477 DWORD value) /* The value to be converted. */
1478 {
1479 DWORD order = 1;
1480 DWORD localType;
1481
1482 /*
1483 * Check to see if the low bit is in the first byte.
1484 */
1485
1486 localType = (*((char*)(&order)) == 1) ? REG_DWORD : REG_DWORD_BIG_ENDIAN;
1487 return (type != localType) ? SWAPLONG(value) : value;
1488 }