Debian Codegraph @

win/tclWinDde.c

raw
1 /*
2 * tclWinDde.c --
3 *
4 * This file provides functions that implement the "send" command,
5 * allowing commands to be passed from interpreter to interpreter.
6 *
7 * Copyright (c) 1997 by Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #undef STATIC_BUILD
14 #ifndef USE_TCL_STUBS
15 # define USE_TCL_STUBS
16 #endif
17 #include "tclInt.h"
18 #include <dde.h>
19 #include <ddeml.h>
20 #include <tchar.h>
21
22 #if defined (__clang__) && (__clang_major__ > 20)
23 #pragma clang diagnostic ignored "-Wc++-keyword"
24 #endif
25
26 #if !defined(NDEBUG)
27 /* test POKE server Implemented for debug mode only */
28 # undef CBF_FAIL_POKES
29 # define CBF_FAIL_POKES 0
30 #endif
31
32 /*
33 * The following structure is used to keep track of the interpreters
34 * registered by this process.
35 */
36
37 typedef struct RegisteredInterp {
38 struct RegisteredInterp *nextPtr;
39 /* The next interp this application knows
40 * about. */
41 WCHAR *name; /* Interpreter's name (malloc-ed). */
42 Tcl_Obj *handlerPtr; /* The server handler command */
43 Tcl_Interp *interp; /* The interpreter attached to this name. */
44 } RegisteredInterp;
45
46 /*
47 * Used to keep track of conversations.
48 */
49
50 typedef struct Conversation {
51 struct Conversation *nextPtr;
52 /* The next conversation in the list. */
53 RegisteredInterp *riPtr; /* The info we know about the conversation. */
54 HCONV hConv; /* The DDE handle for this conversation. */
55 Tcl_Obj *returnPackagePtr; /* The result package for this conversation. */
56 } Conversation;
57
58 typedef struct {
59 Tcl_Interp *interp;
60 int result;
61 ATOM service;
62 ATOM topic;
63 HWND hwnd;
64 } DdeEnumServices;
65
66 typedef struct {
67 Conversation *currentConversations;
68 /* A list of conversations currently being
69 * processed. */
70 RegisteredInterp *interpListPtr;
71 /* List of all interpreters registered in the
72 * current process. */
73 } ThreadSpecificData;
74 static Tcl_ThreadDataKey dataKey;
75
76 /*
77 * The following variables cannot be placed in thread-local storage. The Mutex
78 * ddeMutex guards access to the ddeInstance.
79 */
80
81 static HSZ ddeServiceGlobal = 0;
82 static DWORD ddeInstance; /* The application instance handle given to us
83 * by DdeInitialize. */
84 static int ddeIsServer = 0;
85
86 #define TCL_DDE_VERSION "1.4.6"
87 #define TCL_DDE_PACKAGE_NAME "dde"
88 #define TCL_DDE_SERVICE_NAME L"TclEval"
89 #define TCL_DDE_EXECUTE_RESULT L"$TCLEVAL$EXECUTE$RESULT"
90
91 #define DDE_FLAG_ASYNC 1
92 #define DDE_FLAG_BINARY 2
93 #define DDE_FLAG_FORCE 4
94
95 TCL_DECLARE_MUTEX(ddeMutex)
96
97 #if TCL_MAJOR_VERSION < 9
98 # if TCL_UTF_MAX > 3
99 # define Tcl_WCharToUtfDString(a,b,c) Tcl_WinTCharToUtf((TCHAR *)(a),(b)*sizeof(WCHAR),c)
100 # define Tcl_UtfToWCharDString(a,b,c) (WCHAR *)Tcl_WinUtfToTChar(a,b,c)
101 # else
102 # define Tcl_WCharToUtfDString(a,b,c) Tcl_UniCharToUtfDString((Tcl_UniChar *)(a),b,c)
103 # define Tcl_UtfToWCharDString(a,b,c) (WCHAR *)Tcl_UtfToUniCharDString(a,b,c)
104 # endif
105 #ifndef Tcl_Size
106 # define Tcl_Size int
107 #endif
108 #ifndef Tcl_CreateObjCommand2
109 # define Tcl_CreateObjCommand2 Tcl_CreateObjCommand
110 #endif
111 #endif
112
113 /*
114 * Declarations for functions defined in this file.
115 */
116
117 static LRESULT CALLBACK DdeClientWindowProc(HWND hwnd, UINT uMsg,
118 WPARAM wParam, LPARAM lParam);
119 static int DdeCreateClient(DdeEnumServices *es);
120 static BOOL CALLBACK DdeEnumWindowsCallback(HWND hwndTarget,
121 LPARAM lParam);
122 static void DdeExitProc(void *clientData);
123 static int DdeGetServicesList(Tcl_Interp *interp,
124 const WCHAR *serviceName, const WCHAR *topicName);
125 static HDDEDATA CALLBACK DdeServerProc(UINT uType, UINT uFmt, HCONV hConv,
126 HSZ ddeTopic, HSZ ddeItem, HDDEDATA hData,
127 DWORD dwData1, DWORD dwData2);
128 static LRESULT DdeServicesOnAck(HWND hwnd, WPARAM wParam,
129 LPARAM lParam);
130 static void DeleteProc(void *clientData);
131 static Tcl_Obj * ExecuteRemoteObject(RegisteredInterp *riPtr,
132 Tcl_Obj *ddeObjectPtr);
133 static int MakeDdeConnection(Tcl_Interp *interp,
134 const WCHAR *name, HCONV *ddeConvPtr);
135 static void SetDdeError(Tcl_Interp *interp);
136 static int DdeObjCmd(void *clientData,
137 Tcl_Interp *interp, Tcl_Size objc,
138 Tcl_Obj *const objv[]);
139
140 #ifdef __cplusplus
141 extern "C" {
142 #endif
143 DLLEXPORT int Dde_Init(Tcl_Interp *interp);
144 DLLEXPORT int Dde_SafeInit(Tcl_Interp *interp);
145 #if TCL_MAJOR_VERSION < 9
146 /* With those additional entries, "load tcldde14.dll" works without 3th argument */
147 DLLEXPORT int Tcldde_Init(Tcl_Interp *interp);
148 DLLEXPORT int Tcldde_SafeInit(Tcl_Interp *interp);
149 #endif
150 #ifdef __cplusplus
151 }
152 #endif
153
154 /*
155 *----------------------------------------------------------------------
156 *
157 * Dde_Init --
158 *
159 * This function initializes the dde command.
160 *
161 * Results:
162 * A standard Tcl result.
163 *
164 * Side effects:
165 * None.
166 *
167 *----------------------------------------------------------------------
168 */
169
170 int
171 Dde_Init(
172 Tcl_Interp *interp)
173 {
174 if (!Tcl_InitStubs(interp, "8.5-", 0)) {
175 return TCL_ERROR;
176 }
177
178 Tcl_CreateObjCommand2(interp, "dde", DdeObjCmd, NULL, NULL);
179 Tcl_CreateExitHandler(DdeExitProc, NULL);
180 return Tcl_PkgProvideEx(interp, TCL_DDE_PACKAGE_NAME, TCL_DDE_VERSION, NULL);
181 }
182 #if TCL_MAJOR_VERSION < 9
183 int
184 Tcldde_Init(
185 Tcl_Interp *interp)
186 {
187 return Dde_Init(interp);
188 }
189 #endif
190
191 /*
192 *----------------------------------------------------------------------
193 *
194 * Dde_SafeInit --
195 *
196 * This function initializes the dde command within a safe interp
197 *
198 * Results:
199 * A standard Tcl result.
200 *
201 * Side effects:
202 * None.
203 *
204 *----------------------------------------------------------------------
205 */
206
207 int
208 Dde_SafeInit(
209 Tcl_Interp *interp)
210 {
211 int result = Dde_Init(interp);
212 if (result == TCL_OK) {
213 Tcl_HideCommand(interp, "dde", "dde");
214 }
215 return result;
216 }
217 #if TCL_MAJOR_VERSION < 9
218 int
219 Tcldde_SafeInit(
220 Tcl_Interp *interp)
221 {
222 return Dde_SafeInit(interp);
223 }
224 #endif
225
226 /*
227 *----------------------------------------------------------------------
228 *
229 * Initialize --
230 *
231 * Initialize the global DDE instance.
232 *
233 * Results:
234 * None.
235 *
236 * Side effects:
237 * Registers the DDE server proc.
238 *
239 *----------------------------------------------------------------------
240 */
241
242 static void
243 Initialize(void)
244 {
245 int nameFound = 0;
246 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
247
248 /*
249 * See if the application is already registered; if so, remove its current
250 * name from the registry. The deletion of the command will take care of
251 * disposing of this entry.
252 */
253
254 if (tsdPtr->interpListPtr != NULL) {
255 nameFound = 1;
256 }
257
258 /*
259 * Make sure that the DDE server is there. This is done only once, add an
260 * exit handler tear it down.
261 */
262
263 if (ddeInstance == 0) {
264 Tcl_MutexLock(&ddeMutex);
265 if (ddeInstance == 0) {
266 if (DdeInitializeW(&ddeInstance, (PFNCALLBACK)(void *)DdeServerProc,
267 CBF_SKIP_REGISTRATIONS | CBF_SKIP_UNREGISTRATIONS
268 | CBF_FAIL_POKES, 0) != DMLERR_NO_ERROR) {
269 ddeInstance = 0;
270 }
271 }
272 Tcl_MutexUnlock(&ddeMutex);
273 }
274 if ((ddeServiceGlobal == 0) && (nameFound != 0)) {
275 Tcl_MutexLock(&ddeMutex);
276 if ((ddeServiceGlobal == 0) && (nameFound != 0)) {
277 ddeIsServer = 1;
278 Tcl_CreateExitHandler(DdeExitProc, NULL);
279 ddeServiceGlobal = DdeCreateStringHandleW(ddeInstance,
280 TCL_DDE_SERVICE_NAME, CP_WINUNICODE);
281 DdeNameService(ddeInstance, ddeServiceGlobal, 0L, DNS_REGISTER);
282 } else {
283 ddeIsServer = 0;
284 }
285 Tcl_MutexUnlock(&ddeMutex);
286 }
287 }
288
289 /*
290 *----------------------------------------------------------------------
291 *
292 * DdeSetServerName --
293 *
294 * This function is called to associate an ASCII name with a Dde server.
295 * If the interpreter has already been named, the name replaces the old
296 * one.
297 *
298 * Results:
299 * The return value is the name actually given to the interp. This will
300 * normally be the same as name, but if name was already in use for a Dde
301 * Server then a name of the form "name #2" will be chosen, with a high
302 * enough number to make the name unique.
303 *
304 * Side effects:
305 * Registration info is saved, thereby allowing the "send" command to be
306 * used later to invoke commands in the application. In addition, the
307 * "send" command is created in the application's interpreter. The
308 * registration will be removed automatically if the interpreter is
309 * deleted or the "send" command is removed.
310 *
311 *----------------------------------------------------------------------
312 */
313
314 static const WCHAR *
315 DdeSetServerName(
316 Tcl_Interp *interp,
317 const WCHAR *name, /* The name that will be used to refer to the
318 * interpreter in later "send" commands. Must
319 * be globally unique. */
320 int flags, /* DDE_FLAG_FORCE or 0 */
321 Tcl_Obj *handlerPtr) /* Name of the optional proc/command to handle
322 * incoming Dde eval's */
323 {
324 int suffix;
325 RegisteredInterp *riPtr, *prevPtr;
326 Tcl_DString dString;
327 const WCHAR *actualName;
328 Tcl_Obj *srvListPtr = NULL, **srvPtrPtr = NULL;
329 Tcl_Size n, srvCount = 0, offset;
330 int lastSuffix, r = TCL_OK;
331 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
332
333 /*
334 * See if the application is already registered; if so, remove its current
335 * name from the registry. The deletion of the command will take care of
336 * disposing of this entry.
337 */
338
339 for (riPtr = tsdPtr->interpListPtr, prevPtr = NULL; riPtr != NULL;
340 prevPtr = riPtr, riPtr = riPtr->nextPtr) {
341 if (riPtr->interp == interp) {
342 if (name != NULL) {
343 if (prevPtr == NULL) {
344 tsdPtr->interpListPtr = tsdPtr->interpListPtr->nextPtr;
345 } else {
346 prevPtr->nextPtr = riPtr->nextPtr;
347 }
348 break;
349 } else {
350 /*
351 * The name was NULL, so the caller is asking for the name of
352 * the current interp.
353 */
354
355 return riPtr->name;
356 }
357 }
358 }
359
360 if (name == NULL) {
361 /*
362 * The name was NULL, so the caller is asking for the name of the
363 * current interp, but it doesn't have a name.
364 */
365
366 return L"";
367 }
368
369 /*
370 * Get the list of currently registered Tcl interpreters by calling the
371 * internal implementation of the 'dde services' command.
372 */
373
374 Tcl_DStringInit(&dString);
375 actualName = name;
376
377 if (!(flags & DDE_FLAG_FORCE)) {
378 r = DdeGetServicesList(interp, TCL_DDE_SERVICE_NAME, NULL);
379 if (r == TCL_OK) {
380 srvListPtr = Tcl_GetObjResult(interp);
381 }
382 if (r == TCL_OK) {
383 r = Tcl_ListObjGetElements(interp, srvListPtr, &srvCount,
384 &srvPtrPtr);
385 }
386 if (r != TCL_OK) {
387 Tcl_DStringInit(&dString);
388 OutputDebugStringW(Tcl_UtfToWCharDString(Tcl_GetString(Tcl_GetObjResult(interp)), -1, &dString));
389 Tcl_DStringFree(&dString);
390 return NULL;
391 }
392
393 /*
394 * Pick a name to use for the application. Use "name" if it's not
395 * already in use. Otherwise add a suffix such as " #2", trying larger
396 * and larger numbers until we eventually find one that is unique.
397 */
398
399 offset = lastSuffix = 0;
400 suffix = 1;
401
402 while (suffix != lastSuffix) {
403 lastSuffix = suffix;
404 if (suffix > 1) {
405 if (suffix == 2) {
406 Tcl_DStringAppend(&dString, (char *)name, wcslen(name) * sizeof(WCHAR));
407 Tcl_DStringAppend(&dString, (char *)L" #", 2 * sizeof(WCHAR));
408 offset = Tcl_DStringLength(&dString);
409 Tcl_DStringSetLength(&dString, offset + sizeof(WCHAR) * TCL_INTEGER_SPACE);
410 actualName = (WCHAR *) Tcl_DStringValue(&dString);
411 }
412 _snwprintf((WCHAR *) (Tcl_DStringValue(&dString) + offset),
413 TCL_INTEGER_SPACE, L"%d", suffix);
414 }
415
416 /*
417 * See if the name is already in use, if so increment suffix.
418 */
419
420 for (n = 0; n < srvCount; ++n) {
421 Tcl_Obj* namePtr;
422 Tcl_DString ds;
423
424 Tcl_ListObjIndex(interp, srvPtrPtr[n], 1, &namePtr);
425 Tcl_DStringInit(&ds);
426 Tcl_UtfToWCharDString(Tcl_GetString(namePtr), -1, &ds);
427 if (wcscmp(actualName, (WCHAR *)Tcl_DStringValue(&ds)) == 0) {
428 suffix++;
429 Tcl_DStringFree(&ds);
430 break;
431 }
432 Tcl_DStringFree(&ds);
433 }
434 }
435 }
436
437 /*
438 * We have found a unique name. Now add it to the registry.
439 */
440
441 riPtr = (RegisteredInterp *) Tcl_Alloc(sizeof(RegisteredInterp));
442 riPtr->interp = interp;
443 riPtr->name = (WCHAR *) Tcl_Alloc((wcslen(actualName) + 1) * sizeof(WCHAR));
444 riPtr->nextPtr = tsdPtr->interpListPtr;
445 riPtr->handlerPtr = handlerPtr;
446 if (riPtr->handlerPtr != NULL) {
447 Tcl_IncrRefCount(riPtr->handlerPtr);
448 }
449 tsdPtr->interpListPtr = riPtr;
450 wcscpy(riPtr->name, actualName);
451
452 if (Tcl_IsSafe(interp)) {
453 Tcl_ExposeCommand(interp, "dde", "dde");
454 }
455
456 Tcl_CreateObjCommand2(interp, "dde", DdeObjCmd,
457 riPtr, DeleteProc);
458 if (Tcl_IsSafe(interp)) {
459 Tcl_HideCommand(interp, "dde", "dde");
460 }
461 Tcl_DStringFree(&dString);
462
463 /*
464 * Re-initialize with the new name.
465 */
466
467 Initialize();
468
469 return riPtr->name;
470 }
471
472 /*
473 *----------------------------------------------------------------------
474 *
475 * DdeGetRegistrationPtr
476 *
477 * Retrieve the registration info for an interpreter.
478 *
479 * Results:
480 * Returns a pointer to the registration structure or NULL
481 *
482 * Side effects:
483 * None
484 *
485 *----------------------------------------------------------------------
486 */
487
488 static RegisteredInterp *
489 DdeGetRegistrationPtr(
490 Tcl_Interp *interp)
491 {
492 RegisteredInterp *riPtr;
493 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
494
495 for (riPtr = tsdPtr->interpListPtr; riPtr != NULL;
496 riPtr = riPtr->nextPtr) {
497 if (riPtr->interp == interp) {
498 break;
499 }
500 }
501 return riPtr;
502 }
503
504 /*
505 *----------------------------------------------------------------------
506 *
507 * DeleteProc
508 *
509 * This function is called when the command "dde" is destroyed.
510 *
511 * Results:
512 * none
513 *
514 * Side effects:
515 * The interpreter given by riPtr is unregistered.
516 *
517 *----------------------------------------------------------------------
518 */
519
520 static void
521 DeleteProc(
522 void *clientData) /* The interp we are deleting. */
523 {
524 RegisteredInterp *riPtr = (RegisteredInterp *) clientData;
525 RegisteredInterp *searchPtr, *prevPtr;
526 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
527
528 for (searchPtr = tsdPtr->interpListPtr, prevPtr = NULL;
529 (searchPtr != NULL) && (searchPtr != riPtr);
530 prevPtr = searchPtr, searchPtr = searchPtr->nextPtr) {
531 /*
532 * Empty loop body.
533 */
534 }
535
536 if (searchPtr != NULL) {
537 if (prevPtr == NULL) {
538 tsdPtr->interpListPtr = tsdPtr->interpListPtr->nextPtr;
539 } else {
540 prevPtr->nextPtr = searchPtr->nextPtr;
541 }
542 }
543 Tcl_Free((char *) riPtr->name);
544 if (riPtr->handlerPtr) {
545 Tcl_DecrRefCount(riPtr->handlerPtr);
546 }
547 Tcl_EventuallyFree(clientData, TCL_DYNAMIC);
548 }
549
550 /*
551 *----------------------------------------------------------------------
552 *
553 * ExecuteRemoteObject --
554 *
555 * Takes the package delivered by DDE and executes it in the server's
556 * interpreter.
557 *
558 * Results:
559 * A list Tcl_Obj * that describes what happened. The first element is
560 * the numerical return code (TCL_ERROR, etc.). The second element is the
561 * result of the script. If the return result was TCL_ERROR, then the
562 * third element will be the value of the global "errorCode", and the
563 * fourth will be the value of the global "errorInfo". The return result
564 * will have a refCount of 0.
565 *
566 * Side effects:
567 * A Tcl script is run, which can cause all kinds of other things to
568 * happen.
569 *
570 *----------------------------------------------------------------------
571 */
572
573 static Tcl_Obj *
574 ExecuteRemoteObject(
575 RegisteredInterp *riPtr, /* Info about this server. */
576 Tcl_Obj *ddeObjectPtr) /* The object to execute. */
577 {
578 Tcl_Obj *returnPackagePtr;
579 int result = TCL_OK;
580
581 if ((riPtr->handlerPtr == NULL) && Tcl_IsSafe(riPtr->interp)) {
582 Tcl_SetObjResult(riPtr->interp, Tcl_NewStringObj("permission denied: "
583 "a handler procedure must be defined for use in a safe "
584 "interp", -1));
585 Tcl_SetErrorCode(riPtr->interp, "TCL", "DDE", "SECURITY_CHECK", (char *)NULL);
586 result = TCL_ERROR;
587 }
588
589 if (riPtr->handlerPtr != NULL) {
590 /*
591 * Add the dde request data to the handler proc list.
592 */
593
594 Tcl_Obj *cmdPtr = Tcl_DuplicateObj(riPtr->handlerPtr);
595
596 result = Tcl_ListObjAppendElement(riPtr->interp, cmdPtr,
597 ddeObjectPtr);
598 if (result == TCL_OK) {
599 ddeObjectPtr = cmdPtr;
600 }
601 }
602
603 if (result == TCL_OK) {
604 result = Tcl_EvalObjEx(riPtr->interp, ddeObjectPtr, TCL_EVAL_GLOBAL);
605 }
606
607 returnPackagePtr = Tcl_NewListObj(0, NULL);
608
609 Tcl_ListObjAppendElement(NULL, returnPackagePtr,
610 Tcl_NewIntObj(result));
611 Tcl_ListObjAppendElement(NULL, returnPackagePtr,
612 Tcl_GetObjResult(riPtr->interp));
613
614 if (result == TCL_ERROR) {
615 Tcl_Obj *errorObjPtr = Tcl_GetVar2Ex(riPtr->interp, "errorCode", NULL,
616 TCL_GLOBAL_ONLY);
617 if (errorObjPtr) {
618 Tcl_ListObjAppendElement(NULL, returnPackagePtr, errorObjPtr);
619 }
620 errorObjPtr = Tcl_GetVar2Ex(riPtr->interp, "errorInfo", NULL,
621 TCL_GLOBAL_ONLY);
622 if (errorObjPtr) {
623 Tcl_ListObjAppendElement(NULL, returnPackagePtr, errorObjPtr);
624 }
625 }
626
627 return returnPackagePtr;
628 }
629
630 /*
631 *----------------------------------------------------------------------
632 *
633 * DdeServerProc --
634 *
635 * Handles all transactions for this server. Can handle execute, request,
636 * and connect protocols. Dde will call this routine when a client
637 * attempts to run a dde command using this server.
638 *
639 * Results:
640 * A DDE Handle with the result of the dde command.
641 *
642 * Side effects:
643 * Depending on which command is executed, arbitrary Tcl scripts can be
644 * run.
645 *
646 *----------------------------------------------------------------------
647 */
648
649 static HDDEDATA CALLBACK
650 DdeServerProc(
651 UINT uType, /* The type of DDE transaction we are
652 * performing. */
653 UINT uFmt, /* The format that data is sent or received */
654 HCONV hConv, /* The conversation associated with the
655 * current transaction. */
656 HSZ ddeTopic, HSZ ddeItem, /* String handles. Transaction-type
657 * dependent. */
658 HDDEDATA hData, /* DDE data. Transaction-type dependent. */
659 DWORD unused1, DWORD unused2)
660 /* Transaction-dependent data. */
661 {
662 Tcl_DString dString;
663 Tcl_Size len;
664 DWORD dlen;
665 WCHAR *utilString;
666 Tcl_Obj *ddeObjectPtr;
667 HDDEDATA ddeReturn = NULL;
668 RegisteredInterp *riPtr;
669 Conversation *convPtr, *prevConvPtr;
670 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
671 (void)unused1;
672 (void)unused2;
673
674 switch(uType) {
675 case XTYP_CONNECT:
676 /*
677 * Dde is trying to initialize a conversation with us. Check and make
678 * sure we have a valid topic.
679 */
680
681 len = DdeQueryStringW(ddeInstance, ddeTopic, NULL, 0, CP_WINUNICODE);
682 Tcl_DStringInit(&dString);
683 Tcl_DStringSetLength(&dString, (len + 1) * sizeof(WCHAR) - 1);
684 utilString = (WCHAR *) Tcl_DStringValue(&dString);
685 DdeQueryStringW(ddeInstance, ddeTopic, utilString, (DWORD) len + 1,
686 CP_WINUNICODE);
687
688 for (riPtr = tsdPtr->interpListPtr; riPtr != NULL;
689 riPtr = riPtr->nextPtr) {
690 if (_wcsicmp(utilString, riPtr->name) == 0) {
691 Tcl_DStringFree(&dString);
692 return (HDDEDATA) TRUE;
693 }
694 }
695
696 Tcl_DStringFree(&dString);
697 return (HDDEDATA) FALSE;
698
699 case XTYP_CONNECT_CONFIRM:
700 /*
701 * Dde has decided that we can connect, so it gives us a conversation
702 * handle. We need to keep track of it so we know which execution
703 * result to return in an XTYP_REQUEST.
704 */
705
706 len = DdeQueryStringW(ddeInstance, ddeTopic, NULL, 0, CP_WINUNICODE);
707 Tcl_DStringInit(&dString);
708 Tcl_DStringSetLength(&dString, (len + 1) * sizeof(WCHAR) - 1);
709 utilString = (WCHAR *) Tcl_DStringValue(&dString);
710 DdeQueryStringW(ddeInstance, ddeTopic, utilString, (DWORD) len + 1,
711 CP_WINUNICODE);
712 for (riPtr = tsdPtr->interpListPtr; riPtr != NULL;
713 riPtr = riPtr->nextPtr) {
714 if (_wcsicmp(riPtr->name, utilString) == 0) {
715 convPtr = (Conversation *) Tcl_Alloc(sizeof(Conversation));
716 convPtr->nextPtr = tsdPtr->currentConversations;
717 convPtr->returnPackagePtr = NULL;
718 convPtr->hConv = hConv;
719 convPtr->riPtr = riPtr;
720 tsdPtr->currentConversations = convPtr;
721 break;
722 }
723 }
724 Tcl_DStringFree(&dString);
725 return (HDDEDATA) TRUE;
726
727 case XTYP_DISCONNECT:
728 /*
729 * The client has disconnected from our server. Forget this
730 * conversation.
731 */
732
733 for (convPtr = tsdPtr->currentConversations, prevConvPtr = NULL;
734 convPtr != NULL;
735 prevConvPtr = convPtr, convPtr = convPtr->nextPtr) {
736 if (hConv == convPtr->hConv) {
737 if (prevConvPtr == NULL) {
738 tsdPtr->currentConversations = convPtr->nextPtr;
739 } else {
740 prevConvPtr->nextPtr = convPtr->nextPtr;
741 }
742 if (convPtr->returnPackagePtr != NULL) {
743 Tcl_DecrRefCount(convPtr->returnPackagePtr);
744 }
745 Tcl_Free((char *) convPtr);
746 break;
747 }
748 }
749 return (HDDEDATA) TRUE;
750
751 case XTYP_REQUEST:
752 /*
753 * This could be either a request for a value of a Tcl variable, or it
754 * could be the send command requesting the results of the last
755 * execute.
756 */
757
758 if ((uFmt != CF_TEXT) && (uFmt != CF_UNICODETEXT)) {
759 return (HDDEDATA) FALSE;
760 }
761
762 ddeReturn = (HDDEDATA) FALSE;
763 for (convPtr = tsdPtr->currentConversations; (convPtr != NULL)
764 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
765 /*
766 * Empty loop body.
767 */
768 }
769
770 if (convPtr != NULL) {
771 Tcl_DString dsBuf;
772 char *returnString;
773
774 len = DdeQueryStringW(ddeInstance, ddeItem, NULL, 0, CP_WINUNICODE);
775 Tcl_DStringInit(&dString);
776 Tcl_DStringInit(&dsBuf);
777 Tcl_DStringSetLength(&dString, (len + 1) * sizeof(WCHAR) - 1);
778 utilString = (WCHAR *) Tcl_DStringValue(&dString);
779 DdeQueryStringW(ddeInstance, ddeItem, utilString, (DWORD) len + 1,
780 CP_WINUNICODE);
781 if (_wcsicmp(utilString, TCL_DDE_EXECUTE_RESULT) == 0) {
782 returnString =
783 Tcl_GetStringFromObj(convPtr->returnPackagePtr, &len);
784 if (uFmt != CF_TEXT) {
785 Tcl_DStringInit(&dsBuf);
786 Tcl_UtfToWCharDString(returnString, len, &dsBuf);
787 returnString = Tcl_DStringValue(&dsBuf);
788 len = Tcl_DStringLength(&dsBuf) + sizeof(WCHAR) - 1;
789 }
790 ddeReturn = DdeCreateDataHandle(ddeInstance, (BYTE *)returnString,
791 (DWORD) len+1, 0, ddeItem, uFmt, 0);
792 } else {
793 if (Tcl_IsSafe(convPtr->riPtr->interp)) {
794 ddeReturn = NULL;
795 } else {
796 Tcl_DString ds;
797 Tcl_Obj *variableObjPtr;
798
799 Tcl_DStringInit(&ds);
800 Tcl_WCharToUtfDString(utilString, wcslen(utilString), &ds);
801 variableObjPtr = Tcl_GetVar2Ex(
802 convPtr->riPtr->interp, Tcl_DStringValue(&ds), NULL,
803 TCL_GLOBAL_ONLY);
804 if (variableObjPtr != NULL) {
805 returnString = Tcl_GetStringFromObj(variableObjPtr, &len);
806 if (uFmt != CF_TEXT) {
807 Tcl_DStringInit(&dsBuf);
808 Tcl_UtfToWCharDString(returnString, len, &dsBuf);
809 returnString = Tcl_DStringValue(&dsBuf);
810 len = Tcl_DStringLength(&dsBuf) + sizeof(WCHAR) - 1;
811 }
812 ddeReturn = DdeCreateDataHandle(ddeInstance,
813 (BYTE *)returnString, (DWORD) len+1, 0, ddeItem,
814 uFmt, 0);
815 } else {
816 ddeReturn = NULL;
817 }
818 Tcl_DStringFree(&ds);
819 }
820 }
821 Tcl_DStringFree(&dsBuf);
822 Tcl_DStringFree(&dString);
823 }
824 return ddeReturn;
825
826 #if !CBF_FAIL_POKES
827 case XTYP_POKE:
828 /*
829 * This is a poke for a Tcl variable, only implemented in
830 * debug/UNICODE mode.
831 */
832 ddeReturn = DDE_FNOTPROCESSED;
833
834 if ((uFmt != CF_TEXT) && (uFmt != CF_UNICODETEXT)) {
835 return ddeReturn;
836 }
837
838 for (convPtr = tsdPtr->currentConversations; (convPtr != NULL)
839 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
840 /*
841 * Empty loop body.
842 */
843 }
844
845 if (convPtr && !Tcl_IsSafe(convPtr->riPtr->interp)) {
846 Tcl_DString ds, ds2;
847 Tcl_Obj *variableObjPtr;
848 DWORD len2;
849
850 Tcl_DStringInit(&dString);
851 Tcl_DStringInit(&ds2);
852 len = DdeQueryStringW(ddeInstance, ddeItem, NULL, 0, CP_WINUNICODE);
853 Tcl_DStringSetLength(&dString, (len + 1) * sizeof(WCHAR) - 1);
854 utilString = (WCHAR *) Tcl_DStringValue(&dString);
855 DdeQueryStringW(ddeInstance, ddeItem, utilString, (DWORD) len + 1,
856 CP_WINUNICODE);
857 Tcl_DStringInit(&ds);
858 Tcl_WCharToUtfDString(utilString, wcslen(utilString), &ds);
859 utilString = (WCHAR *) DdeAccessData(hData, &len2);
860 len = len2;
861 if (uFmt != CF_TEXT) {
862 Tcl_DStringInit(&ds2);
863 Tcl_WCharToUtfDString(utilString, wcslen(utilString), &ds2);
864 utilString = (WCHAR *) Tcl_DStringValue(&ds2);
865 }
866 variableObjPtr = Tcl_NewStringObj((char *)utilString, -1);
867
868 Tcl_SetVar2Ex(convPtr->riPtr->interp, Tcl_DStringValue(&ds), NULL,
869 variableObjPtr, TCL_GLOBAL_ONLY);
870
871 Tcl_DStringFree(&ds2);
872 Tcl_DStringFree(&ds);
873 Tcl_DStringFree(&dString);
874 ddeReturn = (HDDEDATA) DDE_FACK;
875 }
876 return ddeReturn;
877
878 #endif
879 case XTYP_EXECUTE: {
880 /*
881 * Execute this script. The results will be saved into a list object
882 * which will be retrieved later. See ExecuteRemoteObject.
883 */
884
885 Tcl_Obj *returnPackagePtr;
886 char *string;
887
888 for (convPtr = tsdPtr->currentConversations; (convPtr != NULL)
889 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
890 /*
891 * Empty loop body.
892 */
893 }
894
895 if (convPtr == NULL) {
896 return (HDDEDATA) DDE_FNOTPROCESSED;
897 }
898
899 utilString = (WCHAR *) DdeAccessData(hData, &dlen);
900 string = (char *) utilString;
901 if (!dlen) {
902 /* Empty binary array. */
903 ddeObjectPtr = Tcl_NewObj();
904 } else if ((dlen & 1) || utilString[(dlen>>1)-1]) {
905 /* Cannot be Unicode, so assume utf-8 */
906 if (!string[dlen-1]) {
907 dlen--;
908 }
909 ddeObjectPtr = Tcl_NewStringObj(string, dlen);
910 } else {
911 /* Unicode */
912 Tcl_DString dsBuf;
913
914 Tcl_DStringInit(&dsBuf);
915 Tcl_WCharToUtfDString(utilString, (dlen>>1) - 1, &dsBuf);
916 ddeObjectPtr = Tcl_NewStringObj(Tcl_DStringValue(&dsBuf),
917 Tcl_DStringLength(&dsBuf));
918 Tcl_DStringFree(&dsBuf);
919 }
920 Tcl_IncrRefCount(ddeObjectPtr);
921 DdeUnaccessData(hData);
922 if (convPtr->returnPackagePtr != NULL) {
923 Tcl_DecrRefCount(convPtr->returnPackagePtr);
924 }
925 convPtr->returnPackagePtr = NULL;
926 returnPackagePtr = ExecuteRemoteObject(convPtr->riPtr, ddeObjectPtr);
927 Tcl_IncrRefCount(returnPackagePtr);
928 for (convPtr = tsdPtr->currentConversations; (convPtr != NULL)
929 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
930 /*
931 * Empty loop body.
932 */
933 }
934 if (convPtr != NULL) {
935 convPtr->returnPackagePtr = returnPackagePtr;
936 } else {
937 Tcl_DecrRefCount(returnPackagePtr);
938 }
939 Tcl_DecrRefCount(ddeObjectPtr);
940 if (returnPackagePtr == NULL) {
941 return (HDDEDATA) DDE_FNOTPROCESSED;
942 } else {
943 return (HDDEDATA) DDE_FACK;
944 }
945 }
946
947 case XTYP_WILDCONNECT: {
948 /*
949 * Dde wants a list of services and topics that we support.
950 */
951
952 HSZPAIR *returnPtr;
953 Tcl_Size i;
954 DWORD numItems;
955
956 for (i = 0, riPtr = tsdPtr->interpListPtr; riPtr != NULL;
957 i++, riPtr = riPtr->nextPtr) {
958 /*
959 * Empty loop body.
960 */
961 }
962
963 if ((size_t)i >= UINT_MAX/sizeof(HSZPAIR)) {
964 return NULL;
965 }
966 numItems = (DWORD)i;
967 ddeReturn = DdeCreateDataHandle(ddeInstance, NULL,
968 (numItems + 1) * (DWORD)sizeof(HSZPAIR), 0, 0, 0, 0);
969 returnPtr = (HSZPAIR *) DdeAccessData(ddeReturn, &dlen);
970 len = dlen;
971 for (i = 0, riPtr = tsdPtr->interpListPtr; i < (Tcl_Size)numItems;
972 i++, riPtr = riPtr->nextPtr) {
973 returnPtr[i].hszSvc = DdeCreateStringHandleW(ddeInstance,
974 TCL_DDE_SERVICE_NAME, CP_WINUNICODE);
975 returnPtr[i].hszTopic = DdeCreateStringHandleW(ddeInstance,
976 riPtr->name, CP_WINUNICODE);
977 }
978 returnPtr[i].hszSvc = NULL;
979 returnPtr[i].hszTopic = NULL;
980 DdeUnaccessData(ddeReturn);
981 return ddeReturn;
982 }
983
984 default:
985 return NULL;
986 }
987 }
988
989 /*
990 *----------------------------------------------------------------------
991 *
992 * DdeExitProc --
993 *
994 * Gets rid of our DDE server when we go away.
995 *
996 * Results:
997 * None.
998 *
999 * Side effects:
1000 * The DDE server is deleted.
1001 *
1002 *----------------------------------------------------------------------
1003 */
1004
1005 static void
1006 DdeExitProc(
1007 void *dummy) /* Not used. */
1008 {
1009 (void)dummy;
1010 DdeNameService(ddeInstance, NULL, 0, DNS_UNREGISTER);
1011 DdeUninitialize(ddeInstance);
1012 ddeInstance = 0;
1013 }
1014
1015 /*
1016 *----------------------------------------------------------------------
1017 *
1018 * MakeDdeConnection --
1019 *
1020 * This function is a utility used to connect to a DDE server when given
1021 * a server name and a topic name.
1022 *
1023 * Results:
1024 * A standard Tcl result.
1025 *
1026 * Side effects:
1027 * Passes back a conversation through ddeConvPtr
1028 *
1029 *----------------------------------------------------------------------
1030 */
1031
1032 static int
1033 MakeDdeConnection(
1034 Tcl_Interp *interp, /* Used to report errors. */
1035 const WCHAR *name, /* The connection to use. */
1036 HCONV *ddeConvPtr)
1037 {
1038 HSZ ddeTopic, ddeService;
1039 HCONV ddeConv;
1040
1041 ddeService = DdeCreateStringHandleW(ddeInstance, TCL_DDE_SERVICE_NAME, CP_WINUNICODE);
1042 ddeTopic = DdeCreateStringHandleW(ddeInstance, name, CP_WINUNICODE);
1043
1044 ddeConv = DdeConnect(ddeInstance, ddeService, ddeTopic, NULL);
1045 DdeFreeStringHandle(ddeInstance, ddeService);
1046 DdeFreeStringHandle(ddeInstance, ddeTopic);
1047
1048 if (ddeConv == (HCONV) NULL) {
1049 if (interp != NULL) {
1050 Tcl_DString dString;
1051
1052 Tcl_DStringInit(&dString);
1053 Tcl_WCharToUtfDString(name, wcslen(name), &dString);
1054 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1055 "no registered server named \"%s\"", Tcl_DStringValue(&dString)));
1056 Tcl_DStringFree(&dString);
1057 Tcl_SetErrorCode(interp, "TCL", "DDE", "NO_SERVER", (char *)NULL);
1058 }
1059 return TCL_ERROR;
1060 }
1061
1062 *ddeConvPtr = ddeConv;
1063 return TCL_OK;
1064 }
1065
1066 /*
1067 *----------------------------------------------------------------------
1068 *
1069 * DdeGetServicesList --
1070 *
1071 * This function obtains the list of DDE services.
1072 *
1073 * The functions between here and this function are all involved with
1074 * handling the DDE callbacks for this. They are: DdeCreateClient,
1075 * DdeClientWindowProc, DdeServicesOnAck, and DdeEnumWindowsCallback
1076 *
1077 * Results:
1078 * A standard Tcl result.
1079 *
1080 * Side effects:
1081 * Sets the services list into the interp result.
1082 *
1083 *----------------------------------------------------------------------
1084 */
1085
1086 static int
1087 DdeCreateClient(
1088 DdeEnumServices *es)
1089 {
1090 WNDCLASSEXW wc;
1091 static const WCHAR *szDdeClientClassName = L"TclEval client class";
1092 static const WCHAR *szDdeClientWindowName = L"TclEval client window";
1093
1094 memset(&wc, 0, sizeof(wc));
1095 wc.cbSize = sizeof(wc);
1096 wc.lpfnWndProc = DdeClientWindowProc;
1097 wc.lpszClassName = szDdeClientClassName;
1098 wc.cbWndExtra = sizeof(DdeEnumServices *);
1099
1100 /*
1101 * Register and create the callback window.
1102 */
1103
1104 RegisterClassExW(&wc);
1105 es->hwnd = CreateWindowExW(0, szDdeClientClassName, szDdeClientWindowName,
1106 WS_POPUP, 0, 0, 0, 0, NULL, NULL, NULL, (LPVOID)es);
1107 return TCL_OK;
1108 }
1109
1110 static LRESULT CALLBACK
1111 DdeClientWindowProc(
1112 HWND hwnd, /* What window is the message for */
1113 UINT uMsg, /* The type of message received */
1114 WPARAM wParam,
1115 LPARAM lParam) /* (Potentially) our local handle */
1116 {
1117 switch (uMsg) {
1118 case WM_CREATE: {
1119 LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
1120 DdeEnumServices *es =
1121 (DdeEnumServices *) lpcs->lpCreateParams;
1122
1123 #ifdef _WIN64
1124 SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) es);
1125 #else
1126 SetWindowLongW(hwnd, GWL_USERDATA, (LONG) es);
1127 #endif
1128 return (LRESULT) 0L;
1129 }
1130 case WM_DDE_ACK:
1131 return DdeServicesOnAck(hwnd, wParam, lParam);
1132 default:
1133 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1134 }
1135 }
1136
1137 static LRESULT
1138 DdeServicesOnAck(
1139 HWND hwnd,
1140 WPARAM wParam,
1141 LPARAM lParam)
1142 {
1143 HWND hwndRemote = (HWND)wParam;
1144 ATOM service = (ATOM)LOWORD(lParam);
1145 ATOM topic = (ATOM)HIWORD(lParam);
1146 DdeEnumServices *es;
1147 WCHAR sz[255];
1148 Tcl_DString dString;
1149
1150 #ifdef _WIN64
1151 es = (DdeEnumServices *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
1152 #else
1153 es = (DdeEnumServices *) GetWindowLongW(hwnd, GWL_USERDATA);
1154 #endif
1155
1156 if (((es->service == (ATOM)0) || (es->service == service))
1157 && ((es->topic == (ATOM)0) || (es->topic == topic))) {
1158 Tcl_Obj *matchPtr = Tcl_NewListObj(0, NULL);
1159 Tcl_Obj *resultPtr = Tcl_GetObjResult(es->interp);
1160
1161 GlobalGetAtomNameW(service, sz, 255);
1162 Tcl_DStringInit(&dString);
1163 Tcl_WCharToUtfDString(sz, wcslen(sz), &dString);
1164 Tcl_ListObjAppendElement(NULL, matchPtr, Tcl_NewStringObj(Tcl_DStringValue(&dString), -1));
1165 Tcl_DStringFree(&dString);
1166 GlobalGetAtomNameW(topic, sz, 255);
1167 Tcl_DStringInit(&dString);
1168 Tcl_WCharToUtfDString(sz, wcslen(sz), &dString);
1169 Tcl_ListObjAppendElement(NULL, matchPtr, Tcl_NewStringObj(Tcl_DStringValue(&dString), -1));
1170 Tcl_DStringFree(&dString);
1171
1172 /*
1173 * Adding the hwnd as a third list element provides a unique
1174 * identifier in the case of multiple servers with the name
1175 * application and topic names.
1176 */
1177 /*
1178 * Needs a TIP though:
1179 * Tcl_ListObjAppendElement(NULL, matchPtr,
1180 * Tcl_NewLongObj((long)hwndRemote));
1181 */
1182
1183 if (Tcl_IsShared(resultPtr)) {
1184 resultPtr = Tcl_DuplicateObj(resultPtr);
1185 }
1186 if (Tcl_ListObjAppendElement(es->interp, resultPtr,
1187 matchPtr) == TCL_OK) {
1188 Tcl_SetObjResult(es->interp, resultPtr);
1189 }
1190 }
1191
1192 /*
1193 * Tell the server we are no longer interested.
1194 */
1195
1196 PostMessageW(hwndRemote, WM_DDE_TERMINATE, (WPARAM)hwnd, 0L);
1197 return 0L;
1198 }
1199
1200 static BOOL CALLBACK
1201 DdeEnumWindowsCallback(
1202 HWND hwndTarget,
1203 LPARAM lParam)
1204 {
1205 DWORD_PTR dwResult = 0;
1206 DdeEnumServices *es = (DdeEnumServices *) lParam;
1207
1208 SendMessageTimeoutW(hwndTarget, WM_DDE_INITIATE, (WPARAM)es->hwnd,
1209 MAKELONG(es->service, es->topic), SMTO_ABORTIFHUNG, 1000,
1210 &dwResult);
1211 return TRUE;
1212 }
1213
1214 static int
1215 DdeGetServicesList(
1216 Tcl_Interp *interp,
1217 const WCHAR *serviceName,
1218 const WCHAR *topicName)
1219 {
1220 DdeEnumServices es;
1221
1222 es.interp = interp;
1223 es.result = TCL_OK;
1224 es.service = (serviceName == NULL)
1225 ? (ATOM)0 : GlobalAddAtomW(serviceName);
1226 es.topic = (topicName == NULL) ? (ATOM)0 : GlobalAddAtomW(topicName);
1227
1228 Tcl_ResetResult(interp); /* our list is to be appended to result. */
1229 DdeCreateClient(&es);
1230 EnumWindows(DdeEnumWindowsCallback, (LPARAM)&es);
1231
1232 if (IsWindow(es.hwnd)) {
1233 DestroyWindow(es.hwnd);
1234 }
1235 if (es.service != (ATOM)0) {
1236 GlobalDeleteAtom(es.service);
1237 }
1238 if (es.topic != (ATOM)0) {
1239 GlobalDeleteAtom(es.topic);
1240 }
1241 return es.result;
1242 }
1243
1244 /*
1245 *----------------------------------------------------------------------
1246 *
1247 * SetDdeError --
1248 *
1249 * Sets the interp result to a cogent error message describing the last
1250 * DDE error.
1251 *
1252 * Results:
1253 * None.
1254 *
1255 * Side effects:
1256 * The interp's result object is changed.
1257 *
1258 *----------------------------------------------------------------------
1259 */
1260
1261 static void
1262 SetDdeError(
1263 Tcl_Interp *interp) /* The interp to put the message in. */
1264 {
1265 const char *errorMessage, *errorCode;
1266
1267 switch (DdeGetLastError(ddeInstance)) {
1268 case DMLERR_DATAACKTIMEOUT:
1269 case DMLERR_EXECACKTIMEOUT:
1270 case DMLERR_POKEACKTIMEOUT:
1271 errorMessage = "remote interpreter did not respond";
1272 errorCode = "TIMEOUT";
1273 break;
1274 case DMLERR_BUSY:
1275 errorMessage = "remote server is busy";
1276 errorCode = "BUSY";
1277 break;
1278 case DMLERR_NOTPROCESSED:
1279 errorMessage = "remote server cannot handle this command";
1280 errorCode = "NOCANDO";
1281 break;
1282 default:
1283 errorMessage = "dde command failed";
1284 errorCode = "FAILED";
1285 }
1286
1287 Tcl_SetObjResult(interp, Tcl_NewStringObj(errorMessage, -1));
1288 Tcl_SetErrorCode(interp, "TCL", "DDE", errorCode, (char *)NULL);
1289 }
1290
1291 /*
1292 *----------------------------------------------------------------------
1293 *
1294 * DdeObjCmd --
1295 *
1296 * This function is invoked to process the "dde" Tcl command. See the
1297 * user documentation for details on what it does.
1298 *
1299 * Results:
1300 * A standard Tcl result.
1301 *
1302 * Side effects:
1303 * See the user documentation.
1304 *
1305 *----------------------------------------------------------------------
1306 */
1307
1308 static int
1309 DdeObjCmd(
1310 void *dummy, /* Not used. */
1311 Tcl_Interp *interp, /* The interp we are sending from */
1312 Tcl_Size objc, /* Number of arguments */
1313 Tcl_Obj *const *objv) /* The arguments */
1314 {
1315 static const char *const ddeCommands[] = {
1316 "servername", "execute", "poke", "request", "services", "eval", NULL};
1317 enum DdeSubcommands {
1318 DDE_SERVERNAME, DDE_EXECUTE, DDE_POKE, DDE_REQUEST, DDE_SERVICES,
1319 DDE_EVAL
1320 };
1321 static const char *const ddeSrvOptions[] = {
1322 "-force", "-handler", "--", NULL
1323 };
1324 enum DdeSrvOptions {
1325 DDE_SERVERNAME_EXACT, DDE_SERVERNAME_HANDLER, DDE_SERVERNAME_LAST,
1326 };
1327 static const char *const ddeExecOptions[] = {
1328 "-async", "-binary", NULL
1329 };
1330 enum DdeExecOptions {
1331 DDE_EXEC_ASYNC, DDE_EXEC_BINARY
1332 };
1333 static const char *const ddeEvalOptions[] = {
1334 "-async", NULL
1335 };
1336 static const char *const ddeReqOptions[] = {
1337 "-binary", NULL
1338 };
1339
1340 int index, argIndex;
1341 Tcl_Size length, i, firstArg = 0;
1342 int flags = 0, result = TCL_OK;
1343 HSZ ddeService = NULL, ddeTopic = NULL, ddeItem = NULL, ddeCookie = NULL;
1344 HDDEDATA ddeData = NULL, ddeItemData = NULL, ddeReturn;
1345 HCONV hConv = NULL;
1346 const WCHAR *serviceName = NULL, *topicName = NULL;
1347 const char *string;
1348 DWORD ddeResult;
1349 Tcl_Obj *objPtr, *handlerPtr = NULL;
1350 Tcl_DString serviceBuf, topicBuf, itemBuf;
1351 (void)dummy;
1352
1353 /*
1354 * Initialize DDE server/client
1355 */
1356
1357 if (objc < 2) {
1358 Tcl_WrongNumArgs(interp, 1, objv, "command ?arg ...?");
1359 return TCL_ERROR;
1360 }
1361
1362 if (Tcl_GetIndexFromObj(interp, objv[1], ddeCommands, "command", 0,
1363 &index) != TCL_OK) {
1364 return TCL_ERROR;
1365 }
1366
1367 Tcl_DStringInit(&serviceBuf);
1368 Tcl_DStringInit(&topicBuf);
1369 Tcl_DStringInit(&itemBuf);
1370 switch ((enum DdeSubcommands) index) {
1371 case DDE_SERVERNAME:
1372 for (i = 2; i < objc; i++) {
1373 if (Tcl_GetIndexFromObj(interp, objv[i], ddeSrvOptions,
1374 "option", 0, &argIndex) != TCL_OK) {
1375 /*
1376 * If it is the last argument, it might be a server name
1377 * instead of a bad argument.
1378 */
1379
1380 if (i != objc-1) {
1381 return TCL_ERROR;
1382 }
1383 Tcl_ResetResult(interp);
1384 break;
1385 }
1386 if (argIndex == DDE_SERVERNAME_EXACT) {
1387 flags |= DDE_FLAG_FORCE;
1388 } else if (argIndex == DDE_SERVERNAME_HANDLER) {
1389 if ((objc - i) == 1) { /* return current handler */
1390 RegisteredInterp *riPtr = DdeGetRegistrationPtr(interp);
1391
1392 if (riPtr && riPtr->handlerPtr) {
1393 Tcl_SetObjResult(interp, riPtr->handlerPtr);
1394 } else {
1395 Tcl_ResetResult(interp);
1396 }
1397 return TCL_OK;
1398 }
1399 handlerPtr = objv[++i];
1400 } else if (argIndex == DDE_SERVERNAME_LAST) {
1401 i++;
1402 break;
1403 }
1404 }
1405
1406 if ((objc - i) > 1) {
1407 Tcl_ResetResult(interp);
1408 Tcl_WrongNumArgs(interp, 2, objv,
1409 "?-force? ?-handler proc? ?--? ?serverName?");
1410 return TCL_ERROR;
1411 }
1412
1413 firstArg = (objc == i) ? 1 : i;
1414 break;
1415 case DDE_EXECUTE:
1416 if (objc == 5) {
1417 firstArg = 2;
1418 break;
1419 } else if ((objc >= 6) && (objc <= 7)) {
1420 firstArg = objc - 3;
1421 for (i = 2; i < firstArg; i++) {
1422 if (Tcl_GetIndexFromObj(interp, objv[i], ddeExecOptions,
1423 "option", 0, &argIndex) != TCL_OK) {
1424 goto wrongDdeExecuteArgs;
1425 }
1426 if (argIndex == DDE_EXEC_ASYNC) {
1427 flags |= DDE_FLAG_ASYNC;
1428 } else {
1429 flags |= DDE_FLAG_BINARY;
1430 }
1431 }
1432 break;
1433 }
1434 /* otherwise... */
1435 wrongDdeExecuteArgs:
1436 Tcl_WrongNumArgs(interp, 2, objv,
1437 "?-async? ?-binary? serviceName topicName value");
1438 return TCL_ERROR;
1439 case DDE_POKE:
1440 if (objc == 6) {
1441 firstArg = 2;
1442 break;
1443 } else if ((objc == 7) && (Tcl_GetIndexFromObj(NULL, objv[2],
1444 ddeReqOptions, "option", 0, &argIndex) == TCL_OK)) {
1445 flags |= DDE_FLAG_BINARY;
1446 firstArg = 3;
1447 break;
1448 }
1449
1450 /*
1451 * Otherwise...
1452 */
1453
1454 Tcl_WrongNumArgs(interp, 2, objv,
1455 "?-binary? serviceName topicName item value");
1456 return TCL_ERROR;
1457 case DDE_REQUEST:
1458 if (objc == 5) {
1459 firstArg = 2;
1460 break;
1461 } else if ((objc == 6) && (Tcl_GetIndexFromObj(NULL, objv[2],
1462 ddeReqOptions, "option", 0, &argIndex) == TCL_OK)) {
1463 flags |= DDE_FLAG_BINARY;
1464 firstArg = 3;
1465 break;
1466 }
1467
1468 /*
1469 * Otherwise ...
1470 */
1471
1472 Tcl_WrongNumArgs(interp, 2, objv,
1473 "?-binary? serviceName topicName value");
1474 return TCL_ERROR;
1475 case DDE_SERVICES:
1476 if (objc != 4) {
1477 Tcl_WrongNumArgs(interp, 2, objv, "serviceName topicName");
1478 return TCL_ERROR;
1479 }
1480 firstArg = 2;
1481 break;
1482 case DDE_EVAL:
1483 if (objc < 4) {
1484 wrongDdeEvalArgs:
1485 Tcl_WrongNumArgs(interp, 2, objv, "?-async? serviceName args");
1486 return TCL_ERROR;
1487 } else {
1488 firstArg = 2;
1489 if (Tcl_GetIndexFromObj(NULL, objv[2], ddeEvalOptions, "option",
1490 0, &argIndex) == TCL_OK) {
1491 if (objc < 5) {
1492 goto wrongDdeEvalArgs;
1493 }
1494 flags |= DDE_FLAG_ASYNC;
1495 firstArg++;
1496 }
1497 break;
1498 }
1499 }
1500
1501 Initialize();
1502
1503 if (firstArg != 1) {
1504 const char *src = Tcl_GetStringFromObj(objv[firstArg], &length);
1505
1506 Tcl_DStringInit(&serviceBuf);
1507 Tcl_UtfToWCharDString(src, length, &serviceBuf);
1508 serviceName = (WCHAR *) Tcl_DStringValue(&serviceBuf);
1509 length = Tcl_DStringLength(&serviceBuf) / sizeof(WCHAR);
1510 } else {
1511 length = 0;
1512 }
1513
1514 if (length == 0) {
1515 serviceName = NULL;
1516 } else if ((index != DDE_SERVERNAME) && (index != DDE_EVAL)) {
1517 ddeService = DdeCreateStringHandleW(ddeInstance, serviceName,
1518 CP_WINUNICODE);
1519 }
1520
1521 if ((index != DDE_SERVERNAME) && (index != DDE_EVAL)) {
1522 const char *src = Tcl_GetStringFromObj(objv[firstArg + 1], &length);
1523
1524 Tcl_DStringInit(&topicBuf);
1525 topicName = Tcl_UtfToWCharDString(src, length, &topicBuf);
1526 length = Tcl_DStringLength(&topicBuf) / sizeof(WCHAR);
1527 if (length == 0) {
1528 topicName = NULL;
1529 } else {
1530 ddeTopic = DdeCreateStringHandleW(ddeInstance, topicName,
1531 CP_WINUNICODE);
1532 }
1533 }
1534
1535 switch ((enum DdeSubcommands) index) {
1536 case DDE_SERVERNAME:
1537 serviceName = DdeSetServerName(interp, serviceName, flags,
1538 handlerPtr);
1539 if (serviceName != NULL) {
1540 Tcl_DString dsBuf;
1541
1542 Tcl_DStringInit(&dsBuf);
1543 Tcl_WCharToUtfDString(serviceName, wcslen(serviceName), &dsBuf);
1544 Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_DStringValue(&dsBuf),
1545 Tcl_DStringLength(&dsBuf)));
1546 Tcl_DStringFree(&dsBuf);
1547 } else {
1548 Tcl_ResetResult(interp);
1549 }
1550 break;
1551
1552 case DDE_EXECUTE: {
1553 Tcl_Size dataLength;
1554 const void *dataString;
1555 Tcl_DString dsBuf;
1556
1557 Tcl_DStringInit(&dsBuf);
1558 if (flags & DDE_FLAG_BINARY) {
1559 dataString =
1560 Tcl_GetByteArrayFromObj(objv[firstArg + 2], &dataLength);
1561 } else {
1562 const char *src;
1563
1564 src = Tcl_GetStringFromObj(objv[firstArg + 2], &dataLength);
1565 Tcl_DStringInit(&dsBuf);
1566 dataString =
1567 Tcl_UtfToWCharDString(src, dataLength, &dsBuf);
1568 dataLength = Tcl_DStringLength(&dsBuf) + sizeof(WCHAR);
1569 }
1570
1571 if (dataLength + 1 < 2) {
1572 Tcl_SetObjResult(interp,
1573 Tcl_NewStringObj("cannot execute null data", -1));
1574 Tcl_DStringFree(&dsBuf);
1575 Tcl_SetErrorCode(interp, "TCL", "DDE", "NULL", (char *)NULL);
1576 result = TCL_ERROR;
1577 break;
1578 }
1579 hConv = DdeConnect(ddeInstance, ddeService, ddeTopic, NULL);
1580 DdeFreeStringHandle(ddeInstance, ddeService);
1581 DdeFreeStringHandle(ddeInstance, ddeTopic);
1582
1583 if (hConv == NULL) {
1584 Tcl_DStringFree(&dsBuf);
1585 SetDdeError(interp);
1586 result = TCL_ERROR;
1587 break;
1588 }
1589
1590 ddeData = DdeCreateDataHandle(ddeInstance, (BYTE *) dataString,
1591 (DWORD) dataLength, 0, 0, (flags & DDE_FLAG_BINARY) ? CF_TEXT : CF_UNICODETEXT, 0);
1592 if (ddeData != NULL) {
1593 if (flags & DDE_FLAG_ASYNC) {
1594 DdeClientTransaction((LPBYTE) ddeData, 0xFFFFFFFF, hConv, 0,
1595 (flags & DDE_FLAG_BINARY) ? CF_TEXT : CF_UNICODETEXT, XTYP_EXECUTE, TIMEOUT_ASYNC, &ddeResult);
1596 DdeAbandonTransaction(ddeInstance, hConv, ddeResult);
1597 } else {
1598 ddeReturn = DdeClientTransaction((LPBYTE) ddeData, 0xFFFFFFFF,
1599 hConv, 0, (flags & DDE_FLAG_BINARY) ? CF_TEXT : CF_UNICODETEXT, XTYP_EXECUTE, 30000, NULL);
1600 if (ddeReturn == 0) {
1601 SetDdeError(interp);
1602 result = TCL_ERROR;
1603 }
1604 }
1605 DdeFreeDataHandle(ddeData);
1606 } else {
1607 SetDdeError(interp);
1608 result = TCL_ERROR;
1609 }
1610 Tcl_DStringFree(&dsBuf);
1611 break;
1612 }
1613 case DDE_REQUEST: {
1614 const WCHAR *itemString;
1615 const char *src;
1616
1617 src = Tcl_GetStringFromObj(objv[firstArg + 2], &length);
1618 Tcl_DStringInit(&itemBuf);
1619 itemString = Tcl_UtfToWCharDString(src, length, &itemBuf);
1620 length = Tcl_DStringLength(&itemBuf) / sizeof(WCHAR);
1621
1622 if (length == 0) {
1623 Tcl_SetObjResult(interp,
1624 Tcl_NewStringObj("cannot request value of null data", -1));
1625 Tcl_SetErrorCode(interp, "TCL", "DDE", "NULL", (char *)NULL);
1626 result = TCL_ERROR;
1627 goto cleanup;
1628 }
1629 hConv = DdeConnect(ddeInstance, ddeService, ddeTopic, NULL);
1630 DdeFreeStringHandle(ddeInstance, ddeService);
1631 DdeFreeStringHandle(ddeInstance, ddeTopic);
1632
1633 if (hConv == NULL) {
1634 SetDdeError(interp);
1635 result = TCL_ERROR;
1636 } else {
1637 Tcl_Obj *returnObjPtr;
1638 ddeItem = DdeCreateStringHandleW(ddeInstance, itemString,
1639 CP_WINUNICODE);
1640 if (ddeItem != NULL) {
1641 ddeData = DdeClientTransaction(NULL, 0, hConv, ddeItem,
1642 (flags & DDE_FLAG_BINARY) ? CF_TEXT : CF_UNICODETEXT, XTYP_REQUEST, 5000, NULL);
1643 if (ddeData == NULL) {
1644 SetDdeError(interp);
1645 result = TCL_ERROR;
1646 } else {
1647 DWORD tmp;
1648 WCHAR *dataString = (WCHAR *) DdeAccessData(ddeData, &tmp);
1649
1650 if (flags & DDE_FLAG_BINARY) {
1651 returnObjPtr =
1652 Tcl_NewByteArrayObj((BYTE *) dataString, tmp);
1653 } else {
1654 Tcl_DString dsBuf;
1655
1656 if ((tmp >= sizeof(WCHAR))
1657 && !dataString[tmp / sizeof(WCHAR) - 1]) {
1658 tmp -= (DWORD)sizeof(WCHAR);
1659 }
1660 Tcl_DStringInit(&dsBuf);
1661 Tcl_WCharToUtfDString(dataString, tmp>>1, &dsBuf);
1662 returnObjPtr =
1663 Tcl_NewStringObj(Tcl_DStringValue(&dsBuf),
1664 Tcl_DStringLength(&dsBuf));
1665 Tcl_DStringFree(&dsBuf);
1666 }
1667 DdeUnaccessData(ddeData);
1668 DdeFreeDataHandle(ddeData);
1669 Tcl_SetObjResult(interp, returnObjPtr);
1670 }
1671 } else {
1672 SetDdeError(interp);
1673 result = TCL_ERROR;
1674 }
1675 }
1676 break;
1677 }
1678 case DDE_POKE: {
1679 Tcl_DString dsBuf;
1680 const WCHAR *itemString;
1681 BYTE *dataString;
1682 const char *src;
1683
1684 src = Tcl_GetStringFromObj(objv[firstArg + 2], &length);
1685 Tcl_DStringInit(&itemBuf);
1686 itemString = Tcl_UtfToWCharDString(src, length, &itemBuf);
1687 length = Tcl_DStringLength(&itemBuf) / sizeof(WCHAR);
1688 if (length == 0) {
1689 Tcl_SetObjResult(interp,
1690 Tcl_NewStringObj("cannot have a null item", -1));
1691 Tcl_SetErrorCode(interp, "TCL", "DDE", "NULL", (char *)NULL);
1692 result = TCL_ERROR;
1693 goto cleanup;
1694 }
1695 Tcl_DStringInit(&dsBuf);
1696 if (flags & DDE_FLAG_BINARY) {
1697 dataString = (BYTE *)
1698 Tcl_GetByteArrayFromObj(objv[firstArg + 3], &length);
1699 } else {
1700 const char *data =
1701 Tcl_GetStringFromObj(objv[firstArg + 3], &length);
1702 Tcl_DStringInit(&dsBuf);
1703 dataString = (BYTE *)
1704 Tcl_UtfToWCharDString(data, length, &dsBuf);
1705 length = Tcl_DStringLength(&dsBuf) + sizeof(WCHAR);
1706 }
1707
1708 hConv = DdeConnect(ddeInstance, ddeService, ddeTopic, NULL);
1709 DdeFreeStringHandle(ddeInstance, ddeService);
1710 DdeFreeStringHandle(ddeInstance, ddeTopic);
1711
1712 if (hConv == NULL) {
1713 SetDdeError(interp);
1714 result = TCL_ERROR;
1715 } else {
1716 ddeItem = DdeCreateStringHandleW(ddeInstance, itemString,
1717 CP_WINUNICODE);
1718 if (ddeItem != NULL) {
1719 ddeData = DdeClientTransaction(dataString, (DWORD) length,
1720 hConv, ddeItem, (flags & DDE_FLAG_BINARY) ? CF_TEXT : CF_UNICODETEXT, XTYP_POKE, 5000, NULL);
1721 if (ddeData == NULL) {
1722 SetDdeError(interp);
1723 result = TCL_ERROR;
1724 }
1725 } else {
1726 SetDdeError(interp);
1727 result = TCL_ERROR;
1728 }
1729 }
1730 Tcl_DStringFree(&dsBuf);
1731 break;
1732 }
1733
1734 case DDE_SERVICES:
1735 result = DdeGetServicesList(interp, serviceName, topicName);
1736 break;
1737
1738 case DDE_EVAL: {
1739 RegisteredInterp *riPtr;
1740 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
1741
1742 if (serviceName == NULL) {
1743 Tcl_SetObjResult(interp,
1744 Tcl_NewStringObj("invalid service name \"\"", -1));
1745 Tcl_SetErrorCode(interp, "TCL", "DDE", "NO_SERVER", (char *)NULL);
1746 result = TCL_ERROR;
1747 goto cleanup;
1748 }
1749
1750 objc -= firstArg + 1;
1751 objv += firstArg + 1;
1752
1753 /*
1754 * See if the target interpreter is local. If so, execute the command
1755 * directly without going through the DDE server. Don't exchange
1756 * objects between interps. The target interp could compile an object,
1757 * producing a bytecode structure that refers to other objects owned
1758 * by the target interp. If the target interp is then deleted, the
1759 * bytecode structure would be referring to deallocated objects.
1760 */
1761
1762 for (riPtr = tsdPtr->interpListPtr; riPtr != NULL;
1763 riPtr = riPtr->nextPtr) {
1764 if (_wcsicmp(serviceName, riPtr->name) == 0) {
1765 break;
1766 }
1767 }
1768
1769 if (riPtr != NULL) {
1770 Tcl_Interp *sendInterp;
1771
1772 /*
1773 * This command is to a local interp. No need to go through the
1774 * server.
1775 */
1776
1777 Tcl_Preserve(riPtr);
1778 sendInterp = riPtr->interp;
1779 Tcl_Preserve(sendInterp);
1780
1781 /*
1782 * Don't exchange objects between interps. The target interp would
1783 * compile an object, producing a bytecode structure that refers
1784 * to other objects owned by the target interp. If the target
1785 * interp is then deleted, the bytecode structure would be
1786 * referring to deallocated objects.
1787 */
1788
1789 if (Tcl_IsSafe(riPtr->interp) && (riPtr->handlerPtr == NULL)) {
1790 Tcl_SetObjResult(riPtr->interp, Tcl_NewStringObj(
1791 "permission denied: a handler procedure must be"
1792 " defined for use in a safe interp", -1));
1793 Tcl_SetErrorCode(interp, "TCL", "DDE", "SECURITY_CHECK",
1794 (char *)NULL);
1795 result = TCL_ERROR;
1796 }
1797
1798 if (result == TCL_OK) {
1799 if (objc == 1) {
1800 objPtr = objv[0];
1801 } else {
1802 objPtr = Tcl_ConcatObj(objc, objv);
1803 }
1804 if (riPtr->handlerPtr != NULL) {
1805 /* add the dde request data to the handler proc list */
1806 /*
1807 *result = Tcl_ListObjReplace(sendInterp, objPtr, 0, 0, 1,
1808 * &(riPtr->handlerPtr));
1809 */
1810 Tcl_Obj *cmdPtr = Tcl_DuplicateObj(riPtr->handlerPtr);
1811 result = Tcl_ListObjAppendElement(sendInterp, cmdPtr,
1812 objPtr);
1813 if (result == TCL_OK) {
1814 objPtr = cmdPtr;
1815 }
1816 }
1817 }
1818 if (result == TCL_OK) {
1819 Tcl_IncrRefCount(objPtr);
1820 result = Tcl_EvalObjEx(sendInterp, objPtr, TCL_EVAL_GLOBAL);
1821 Tcl_DecrRefCount(objPtr);
1822 }
1823 if (interp != sendInterp) {
1824 if (result == TCL_ERROR) {
1825 /*
1826 * An error occurred, so transfer error information from
1827 * the destination interpreter back to our interpreter.
1828 */
1829
1830 Tcl_ResetResult(interp);
1831 objPtr = Tcl_GetVar2Ex(sendInterp, "errorInfo", NULL,
1832 TCL_GLOBAL_ONLY);
1833 if (objPtr) {
1834 Tcl_AppendObjToErrorInfo(interp, objPtr);
1835 }
1836
1837 objPtr = Tcl_GetVar2Ex(sendInterp, "errorCode", NULL,
1838 TCL_GLOBAL_ONLY);
1839 if (objPtr) {
1840 Tcl_SetObjErrorCode(interp, objPtr);
1841 }
1842 }
1843 Tcl_SetObjResult(interp, Tcl_GetObjResult(sendInterp));
1844 }
1845 Tcl_Release(riPtr);
1846 Tcl_Release(sendInterp);
1847 } else {
1848 Tcl_DString dsBuf;
1849
1850 /*
1851 * This is a non-local request. Send the script to the server and
1852 * poll it for a result.
1853 */
1854
1855 if (MakeDdeConnection(interp, serviceName, &hConv) != TCL_OK) {
1856 invalidServerResponse:
1857 Tcl_SetObjResult(interp,
1858 Tcl_NewStringObj("invalid data returned from server", -1));
1859 Tcl_SetErrorCode(interp, "TCL", "DDE", "BAD_RESPONSE", (char *)NULL);
1860 result = TCL_ERROR;
1861 goto cleanup;
1862 }
1863
1864 objPtr = Tcl_ConcatObj(objc, objv);
1865 string = Tcl_GetStringFromObj(objPtr, &length);
1866 Tcl_DStringInit(&dsBuf);
1867 Tcl_UtfToWCharDString(string, length, &dsBuf);
1868 string = Tcl_DStringValue(&dsBuf);
1869 length = Tcl_DStringLength(&dsBuf) + sizeof(WCHAR);
1870 ddeItemData = DdeCreateDataHandle(ddeInstance, (BYTE *) string,
1871 (DWORD) length, 0, 0, CF_UNICODETEXT, 0);
1872 Tcl_DStringFree(&dsBuf);
1873
1874 if (flags & DDE_FLAG_ASYNC) {
1875 ddeData = DdeClientTransaction((LPBYTE) ddeItemData,
1876 0xFFFFFFFF, hConv, 0,
1877 CF_UNICODETEXT, XTYP_EXECUTE, TIMEOUT_ASYNC, &ddeResult);
1878 DdeAbandonTransaction(ddeInstance, hConv, ddeResult);
1879 } else {
1880 ddeData = DdeClientTransaction((LPBYTE) ddeItemData,
1881 0xFFFFFFFF, hConv, 0,
1882 CF_UNICODETEXT, XTYP_EXECUTE, 30000, NULL);
1883 if (ddeData != 0) {
1884 ddeCookie = DdeCreateStringHandleW(ddeInstance,
1885 TCL_DDE_EXECUTE_RESULT, CP_WINUNICODE);
1886 ddeData = DdeClientTransaction(NULL, 0, hConv, ddeCookie,
1887 CF_UNICODETEXT, XTYP_REQUEST, 30000, NULL);
1888 }
1889 }
1890
1891 Tcl_DecrRefCount(objPtr);
1892
1893 if (ddeData == 0) {
1894 SetDdeError(interp);
1895 result = TCL_ERROR;
1896 goto cleanup;
1897 }
1898
1899 if (!(flags & DDE_FLAG_ASYNC)) {
1900 Tcl_Obj *resultPtr;
1901 WCHAR *ddeDataString;
1902
1903 /*
1904 * The return handle has a two or four element list in it. The
1905 * first element is the return code (TCL_OK, TCL_ERROR, etc.).
1906 * The second is the result of the script. If the return code
1907 * is TCL_ERROR, then the third element is the value of the
1908 * variable "errorCode", and the fourth is the value of the
1909 * variable "errorInfo".
1910 */
1911
1912 length = DdeGetData(ddeData, NULL, 0, 0);
1913 ddeDataString = (WCHAR *) Tcl_Alloc(length);
1914 DdeGetData(ddeData, (BYTE *) ddeDataString, (DWORD) length, 0);
1915 if (length > (Tcl_Size)sizeof(WCHAR)) {
1916 length -= sizeof(WCHAR);
1917 }
1918 Tcl_DStringInit(&dsBuf);
1919 Tcl_WCharToUtfDString(ddeDataString, length>>1, &dsBuf);
1920 resultPtr = Tcl_NewStringObj(Tcl_DStringValue(&dsBuf),
1921 Tcl_DStringLength(&dsBuf));
1922 Tcl_DStringFree(&dsBuf);
1923 Tcl_Free((char *) ddeDataString);
1924
1925 if (Tcl_ListObjIndex(NULL, resultPtr, 0, &objPtr) != TCL_OK) {
1926 Tcl_DecrRefCount(resultPtr);
1927 goto invalidServerResponse;
1928 }
1929 if (Tcl_GetIntFromObj(NULL, objPtr, &result) != TCL_OK) {
1930 Tcl_DecrRefCount(resultPtr);
1931 goto invalidServerResponse;
1932 }
1933 if (result == TCL_ERROR) {
1934 Tcl_ResetResult(interp);
1935
1936 if (Tcl_ListObjIndex(NULL, resultPtr, 3,
1937 &objPtr) != TCL_OK) {
1938 Tcl_DecrRefCount(resultPtr);
1939 goto invalidServerResponse;
1940 }
1941 Tcl_AppendObjToErrorInfo(interp, objPtr);
1942
1943 Tcl_ListObjIndex(NULL, resultPtr, 2, &objPtr);
1944 Tcl_SetObjErrorCode(interp, objPtr);
1945 }
1946 if (Tcl_ListObjIndex(NULL, resultPtr, 1, &objPtr) != TCL_OK) {
1947 Tcl_DecrRefCount(resultPtr);
1948 goto invalidServerResponse;
1949 }
1950 Tcl_SetObjResult(interp, objPtr);
1951 Tcl_DecrRefCount(resultPtr);
1952 }
1953 }
1954 }
1955 }
1956
1957 cleanup:
1958 if (ddeCookie != NULL) {
1959 DdeFreeStringHandle(ddeInstance, ddeCookie);
1960 }
1961 if (ddeItem != NULL) {
1962 DdeFreeStringHandle(ddeInstance, ddeItem);
1963 }
1964 if (ddeItemData != NULL) {
1965 DdeFreeDataHandle(ddeItemData);
1966 }
1967 if (ddeData != NULL) {
1968 DdeFreeDataHandle(ddeData);
1969 }
1970 if (hConv != NULL) {
1971 DdeDisconnect(hConv);
1972 }
1973 Tcl_DStringFree(&itemBuf);
1974 Tcl_DStringFree(&topicBuf);
1975 Tcl_DStringFree(&serviceBuf);
1976 return result;
1977 }
1978
1979 /*
1980 * Local variables:
1981 * mode: c
1982 * indent-tabs-mode: t
1983 * tab-width: 8
1984 * c-basic-offset: 4
1985 * fill-column: 78
1986 * End:
1987 */