1 /*
2 * DDEML library
3 *
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
7 * Copyright 2000 Corel
8 * Copyright 2001 Eric Pouech
9 * Copyright 2003, 2004, 2005 Dmitry Timoshkov
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "dde.h"
37 #include "ddeml.h"
38 #include "win.h"
39 #include "dde_private.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
44
45 /* convert between ATOM and HSZ avoiding compiler warnings */
46 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
47 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
48
49 static WDML_INSTANCE* WDML_InstanceList = NULL;
50 static LONG WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
51 const WCHAR WDML_szEventClass[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
52
53 /* protection for instance list */
54 static CRITICAL_SECTION WDML_CritSect;
55 static CRITICAL_SECTION_DEBUG critsect_debug =
56 {
57 0, 0, &WDML_CritSect,
58 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
59 0, 0, { (DWORD_PTR)(__FILE__ ": WDML_CritSect") }
60 };
61 static CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
62
63 /* ================================================================
64 *
65 * Pure DDE (non DDEML) management
66 *
67 * ================================================================ */
68
69
70 /*****************************************************************
71 * PackDDElParam (USER32.@)
72 *
73 * RETURNS
74 * the packed lParam
75 */
76 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
77 {
78 HGLOBAL hMem;
79 UINT_PTR *params;
80
81 switch (msg)
82 {
83 case WM_DDE_ACK:
84 case WM_DDE_ADVISE:
85 case WM_DDE_DATA:
86 case WM_DDE_POKE:
87 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
88 {
89 ERR("GlobalAlloc failed\n");
90 return 0;
91 }
92 if (!(params = GlobalLock(hMem)))
93 {
94 ERR("GlobalLock failed (%p)\n", hMem);
95 return 0;
96 }
97 params[0] = uiLo;
98 params[1] = uiHi;
99 GlobalUnlock(hMem);
100 return (LPARAM)hMem;
101
102 case WM_DDE_EXECUTE:
103 return uiHi;
104
105 default:
106 return MAKELONG(uiLo, uiHi);
107 }
108 }
109
110
111 /*****************************************************************
112 * UnpackDDElParam (USER32.@)
113 *
114 * RETURNS
115 * success: nonzero
116 * failure: zero
117 */
118 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
119 PUINT_PTR uiLo, PUINT_PTR uiHi)
120 {
121 UINT_PTR *params;
122
123 switch (msg)
124 {
125 case WM_DDE_ACK:
126 case WM_DDE_ADVISE:
127 case WM_DDE_DATA:
128 case WM_DDE_POKE:
129 if (!lParam || !(params = GlobalLock((HGLOBAL)lParam)))
130 {
131 if (uiLo) *uiLo = 0;
132 if (uiHi) *uiHi = 0;
133 return FALSE;
134 }
135 if (uiLo) *uiLo = params[0];
136 if (uiHi) *uiHi = params[1];
137 GlobalUnlock( (HGLOBAL)lParam );
138 return TRUE;
139
140 case WM_DDE_EXECUTE:
141 if (uiLo) *uiLo = 0;
142 if (uiHi) *uiHi = lParam;
143 return TRUE;
144
145 default:
146 if (uiLo) *uiLo = LOWORD(lParam);
147 if (uiHi) *uiHi = HIWORD(lParam);
148 return TRUE;
149 }
150 }
151
152
153 /*****************************************************************
154 * FreeDDElParam (USER32.@)
155 *
156 * RETURNS
157 * success: nonzero
158 * failure: zero
159 */
160 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
161 {
162 switch (msg)
163 {
164 case WM_DDE_ACK:
165 case WM_DDE_ADVISE:
166 case WM_DDE_DATA:
167 case WM_DDE_POKE:
168 /* first check if it's a global handle */
169 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
170 return !GlobalFree( (HGLOBAL)lParam );
171
172 default:
173 return TRUE;
174 }
175 }
176
177
178 /*****************************************************************
179 * ReuseDDElParam (USER32.@)
180 *
181 * RETURNS
182 * the packed lParam
183 */
184 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
185 UINT_PTR uiLo, UINT_PTR uiHi)
186 {
187 UINT_PTR *params;
188
189 switch (msgIn)
190 {
191 case WM_DDE_ACK:
192 case WM_DDE_ADVISE:
193 case WM_DDE_DATA:
194 case WM_DDE_POKE:
195 switch(msgOut)
196 {
197 case WM_DDE_ACK:
198 case WM_DDE_ADVISE:
199 case WM_DDE_DATA:
200 case WM_DDE_POKE:
201 if (!lParam) return 0;
202 if (!(params = GlobalLock( (HGLOBAL)lParam )))
203 {
204 ERR("GlobalLock failed\n");
205 return 0;
206 }
207 params[0] = uiLo;
208 params[1] = uiHi;
209 TRACE("Reusing pack %08lx %08lx\n", uiLo, uiHi);
210 GlobalUnlock( (HGLOBAL)lParam );
211 return lParam;
212
213 case WM_DDE_EXECUTE:
214 FreeDDElParam( msgIn, lParam );
215 return uiHi;
216
217 default:
218 FreeDDElParam( msgIn, lParam );
219 return MAKELPARAM(uiLo, uiHi);
220 }
221
222 default:
223 return PackDDElParam( msgOut, uiLo, uiHi );
224 }
225 }
226
227 /*****************************************************************
228 * ImpersonateDdeClientWindow (USER32.@)
229 *
230 * PARAMS
231 * hWndClient [I] handle to DDE client window
232 * hWndServer [I] handle to DDE server window
233 */
234 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
235 {
236 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
237 return FALSE;
238 }
239
240 /*****************************************************************
241 * DdeSetQualityOfService (USER32.@)
242 */
243
244 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
245 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
246 {
247 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
248 return TRUE;
249 }
250
251 /* ================================================================
252 *
253 * Instance management
254 *
255 * ================================================================ */
256
257 /******************************************************************************
258 * IncrementInstanceId
259 *
260 * generic routine to increment the max instance Id and allocate a new application instance
261 */
262 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
263 {
264 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
265
266 pInstance->instanceID = id;
267 TRACE("New instance id %d allocated\n", id);
268 }
269
270 /******************************************************************
271 * WDML_EventProc
272 *
273 *
274 */
275 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
276 {
277 WDML_INSTANCE* pInstance;
278 HSZ hsz1, hsz2;
279
280 switch (uMsg)
281 {
282 case WM_WDML_REGISTER:
283 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
284 /* try calling the Callback */
285 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
286 {
287 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
288 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
289 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
290 WDML_DecHSZ(pInstance, hsz1);
291 WDML_DecHSZ(pInstance, hsz2);
292 }
293 break;
294
295 case WM_WDML_UNREGISTER:
296 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
297 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
298 {
299 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
300 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
301 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
302 WDML_DecHSZ(pInstance, hsz1);
303 WDML_DecHSZ(pInstance, hsz2);
304 }
305 break;
306
307 case WM_WDML_CONNECT_CONFIRM:
308 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
309 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
310 {
311 WDML_CONV* pConv;
312 /* confirm connection...
313 * lookup for this conv handle
314 */
315 HWND client = WIN_GetFullHandle( (HWND)wParam );
316 HWND server = WIN_GetFullHandle( (HWND)lParam );
317 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
318 {
319 if (pConv->hwndClient == client && pConv->hwndServer == server)
320 break;
321 }
322 if (pConv)
323 {
324 pConv->wStatus |= ST_ISLOCAL;
325
326 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
327 pConv->hszTopic, pConv->hszService, 0, 0,
328 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
329 }
330 }
331 break;
332 default:
333 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
334 }
335 return 0;
336 }
337
338 /******************************************************************
339 * WDML_Initialize
340 *
341 *
342 */
343 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
344 DWORD afCmd, DWORD ulRes, BOOL bUnicode)
345 {
346 WDML_INSTANCE* pInstance;
347 WDML_INSTANCE* reference_inst;
348 UINT ret;
349 WNDCLASSEXW wndclass;
350
351 TRACE("(%p,%p,0x%x,%d,0x%x)\n",
352 pidInst, pfnCallback, afCmd, ulRes, bUnicode);
353
354 if (ulRes)
355 {
356 ERR("Reserved value not zero? What does this mean?\n");
357 /* trap this and no more until we know more */
358 return DMLERR_NO_ERROR;
359 }
360
361 /* grab enough heap for one control struct - not really necessary for re-initialise
362 * but allows us to use same validation routines */
363 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
364 if (pInstance == NULL)
365 {
366 /* catastrophe !! warn user & abort */
367 ERR("Instance create failed - out of memory\n");
368 return DMLERR_SYS_ERROR;
369 }
370 pInstance->next = NULL;
371 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
372
373 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
374
375 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
376 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
377 pInstance->threadID = GetCurrentThreadId();
378 pInstance->callback = *pfnCallback;
379 pInstance->unicode = bUnicode;
380 pInstance->nodeList = NULL; /* node will be added later */
381 pInstance->monitorFlags = afCmd & MF_MASK;
382 pInstance->wStatus = 0;
383 pInstance->lastError = DMLERR_NO_ERROR;
384 pInstance->servers = NULL;
385 pInstance->convs[0] = NULL;
386 pInstance->convs[1] = NULL;
387 pInstance->links[0] = NULL;
388 pInstance->links[1] = NULL;
389
390 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
391
392 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
393
394 if (!pInstance->clientOnly)
395 {
396 /* Check for other way of setting Client-only !! */
397 pInstance->clientOnly =
398 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
399 }
400
401 TRACE("instance created - checking validity\n");
402
403 if (*pidInst == 0)
404 {
405 /* Initialisation of new Instance Identifier */
406 TRACE("new instance, callback %p flags %X\n",pfnCallback,afCmd);
407
408 EnterCriticalSection(&WDML_CritSect);
409
410 if (WDML_InstanceList == NULL)
411 {
412 /* can't be another instance in this case, assign to the base pointer */
413 WDML_InstanceList = pInstance;
414
415 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
416 * present
417 * ------------------------------- NOTE NOTE NOTE --------------------------
418 *
419 * the manual is not clear if this condition
420 * applies to the first call to DdeInitialize from an application, or the
421 * first call for a given callback !!!
422 */
423
424 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
425 TRACE("First application instance detected OK\n");
426 /* allocate new instance ID */
427 WDML_IncrementInstanceId(pInstance);
428 }
429 else
430 {
431 /* really need to chain the new one in to the latest here, but after checking conditions
432 * such as trying to start a conversation from an application trying to monitor */
433 reference_inst = WDML_InstanceList;
434 TRACE("Subsequent application instance - starting checks\n");
435 while (reference_inst->next != NULL)
436 {
437 /*
438 * This set of tests will work if application uses same instance Id
439 * at application level once allocated - which is what manual implies
440 * should happen. If someone tries to be
441 * clever (lazy ?) it will fail to pick up that later calls are for
442 * the same application - should we trust them ?
443 */
444 if (pInstance->instanceID == reference_inst->instanceID)
445 {
446 /* Check 1 - must be same Client-only state */
447
448 if (pInstance->clientOnly != reference_inst->clientOnly)
449 {
450 ret = DMLERR_DLL_USAGE;
451 goto theError;
452 }
453
454 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
455
456 if (pInstance->monitor != reference_inst->monitor)
457 {
458 ret = DMLERR_INVALIDPARAMETER;
459 goto theError;
460 }
461
462 /* Check 3 - must supply different callback address */
463
464 if (pInstance->callback == reference_inst->callback)
465 {
466 ret = DMLERR_DLL_USAGE;
467 goto theError;
468 }
469 }
470 reference_inst = reference_inst->next;
471 }
472 /* All cleared, add to chain */
473
474 TRACE("Application Instance checks finished\n");
475 WDML_IncrementInstanceId(pInstance);
476 reference_inst->next = pInstance;
477 }
478 LeaveCriticalSection(&WDML_CritSect);
479
480 *pidInst = pInstance->instanceID;
481
482 /* for deadlock issues, windows must always be created when outside the critical section */
483 wndclass.cbSize = sizeof(wndclass);
484 wndclass.style = 0;
485 wndclass.lpfnWndProc = WDML_EventProc;
486 wndclass.cbClsExtra = 0;
487 wndclass.cbWndExtra = sizeof(ULONG_PTR);
488 wndclass.hInstance = 0;
489 wndclass.hIcon = 0;
490 wndclass.hCursor = 0;
491 wndclass.hbrBackground = 0;
492 wndclass.lpszMenuName = NULL;
493 wndclass.lpszClassName = WDML_szEventClass;
494 wndclass.hIconSm = 0;
495
496 RegisterClassExW(&wndclass);
497
498 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
499 WS_POPUP, 0, 0, 0, 0,
500 0, 0, 0, 0);
501
502 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
503
504 TRACE("New application instance processing finished OK\n");
505 }
506 else
507 {
508 /* Reinitialisation situation --- FIX */
509 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst, pfnCallback, afCmd, ulRes);
510
511 EnterCriticalSection(&WDML_CritSect);
512
513 if (WDML_InstanceList == NULL)
514 {
515 ret = DMLERR_INVALIDPARAMETER;
516 goto theError;
517 }
518 /* can't reinitialise if we have initialised nothing !! */
519 reference_inst = WDML_InstanceList;
520 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
521 /*
522 * MS allows initialisation without specifying a callback, should we allow addition of the
523 * callback by a later call to initialise ? - if so this lot will have to change
524 */
525 while (reference_inst->next != NULL)
526 {
527 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
528 {
529 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
530
531 if (reference_inst->clientOnly)
532 {
533 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
534 {
535 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
536
537 if (!(afCmd & APPCMD_CLIENTONLY))
538 {
539 ret = DMLERR_INVALIDPARAMETER;
540 goto theError;
541 }
542 }
543 }
544 /* Check 2 - cannot change monitor modes */
545
546 if (pInstance->monitor != reference_inst->monitor)
547 {
548 ret = DMLERR_INVALIDPARAMETER;
549 goto theError;
550 }
551
552 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
553
554 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
555 {
556 ret = DMLERR_INVALIDPARAMETER;
557 goto theError;
558 }
559 break;
560 }
561 reference_inst = reference_inst->next;
562 }
563 if (reference_inst->next == NULL)
564 {
565 ret = DMLERR_INVALIDPARAMETER;
566 goto theError;
567 }
568 /* All checked - change relevant flags */
569
570 reference_inst->CBFflags = pInstance->CBFflags;
571 reference_inst->clientOnly = pInstance->clientOnly;
572 reference_inst->monitorFlags = pInstance->monitorFlags;
573
574 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
575
576 LeaveCriticalSection(&WDML_CritSect);
577 }
578
579 return DMLERR_NO_ERROR;
580 theError:
581 HeapFree(GetProcessHeap(), 0, pInstance);
582 LeaveCriticalSection(&WDML_CritSect);
583 return ret;
584 }
585
586 /******************************************************************************
587 * DdeInitializeA (USER32.@)
588 *
589 * See DdeInitializeW.
590 */
591 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
592 DWORD afCmd, DWORD ulRes)
593 {
594 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
595 }
596
597 /******************************************************************************
598 * DdeInitializeW [USER32.@]
599 * Registers an application with the DDEML
600 *
601 * PARAMS
602 * pidInst [I] Pointer to instance identifier
603 * pfnCallback [I] Pointer to callback function
604 * afCmd [I] Set of command and filter flags
605 * ulRes [I] Reserved
606 *
607 * RETURNS
608 * Success: DMLERR_NO_ERROR
609 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
610 */
611 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
612 DWORD afCmd, DWORD ulRes)
613 {
614 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE);
615 }
616
617 /*****************************************************************
618 * DdeUninitialize [USER32.@] Frees DDEML resources
619 *
620 * PARAMS
621 * idInst [I] Instance identifier
622 *
623 * RETURNS
624 * Success: TRUE
625 * Failure: FALSE
626 */
627
628 BOOL WINAPI DdeUninitialize(DWORD idInst)
629 {
630 /* Stage one - check if we have a handle for this instance
631 */
632 WDML_INSTANCE* pInstance;
633 WDML_CONV* pConv;
634 WDML_CONV* pConvNext;
635
636 TRACE("(%d)\n", idInst);
637
638 /* First check instance
639 */
640 pInstance = WDML_GetInstance(idInst);
641 if (pInstance == NULL)
642 {
643 /*
644 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
645 */
646 return FALSE;
647 }
648
649 /* first terminate all conversations client side
650 * this shall close existing links...
651 */
652 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
653 {
654 pConvNext = pConv->next;
655 DdeDisconnect((HCONV)pConv);
656 }
657 if (pInstance->convs[WDML_CLIENT_SIDE])
658 FIXME("still pending conversations\n");
659
660 /* then unregister all known service names */
661 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
662
663 /* Free the nodes that were not freed by this instance
664 * and remove the nodes from the list of HSZ nodes.
665 */
666 WDML_FreeAllHSZ(pInstance);
667
668 DestroyWindow(pInstance->hwndEvent);
669
670 /* OK now delete the instance handle itself */
671
672 if (WDML_InstanceList == pInstance)
673 {
674 /* special case - the first/only entry */
675 WDML_InstanceList = pInstance->next;
676 }
677 else
678 {
679 /* general case, remove entry */
680 WDML_INSTANCE* inst;
681
682 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
683 inst->next = pInstance->next;
684 }
685 /* release the heap entry
686 */
687 HeapFree(GetProcessHeap(), 0, pInstance);
688
689 return TRUE;
690 }
691
692 /******************************************************************
693 * WDML_NotifyThreadExit
694 *
695 *
696 */
697 void WDML_NotifyThreadDetach(void)
698 {
699 WDML_INSTANCE* pInstance;
700 WDML_INSTANCE* next;
701 DWORD tid = GetCurrentThreadId();
702
703 EnterCriticalSection(&WDML_CritSect);
704 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
705 {
706 next = pInstance->next;
707 if (pInstance->threadID == tid)
708 {
709 LeaveCriticalSection(&WDML_CritSect);
710 DdeUninitialize(pInstance->instanceID);
711 EnterCriticalSection(&WDML_CritSect);
712 }
713 }
714 LeaveCriticalSection(&WDML_CritSect);
715 }
716
717 /******************************************************************
718 * WDML_InvokeCallback
719 *
720 *
721 */
722 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
723 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
724 ULONG_PTR dwData1, ULONG_PTR dwData2)
725 {
726 HDDEDATA ret;
727
728 if (pInstance == NULL)
729 return NULL;
730
731 TRACE("invoking CB[%p] (%x %x %p %p %p %p %lx %lx)\n",
732 pInstance->callback, uType, uFmt,
733 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
734 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
735 TRACE("done => %p\n", ret);
736 return ret;
737 }
738
739 /*****************************************************************************
740 * WDML_GetInstance
741 *
742 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
743 * for an instance Id, or NULL if the entry does not exist
744 *
745 */
746 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
747 {
748 WDML_INSTANCE* pInstance;
749
750 EnterCriticalSection(&WDML_CritSect);
751
752 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
753 {
754 if (pInstance->instanceID == instId)
755 {
756 if (GetCurrentThreadId() != pInstance->threadID)
757 {
758 FIXME("Tried to get instance from wrong thread\n");
759 continue;
760 }
761 break;
762 }
763 }
764
765 LeaveCriticalSection(&WDML_CritSect);
766
767 if (!pInstance)
768 WARN("Instance entry missing for id %04x\n", instId);
769 return pInstance;
770 }
771
772 /******************************************************************
773 * WDML_GetInstanceFromWnd
774 *
775 *
776 */
777 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
778 {
779 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
780 }
781
782 /******************************************************************************
783 * DdeGetLastError [USER32.@] Gets most recent error code
784 *
785 * PARAMS
786 * idInst [I] Instance identifier
787 *
788 * RETURNS
789 * Last error code
790 */
791 UINT WINAPI DdeGetLastError(DWORD idInst)
792 {
793 DWORD error_code;
794 WDML_INSTANCE* pInstance;
795
796 /* First check instance
797 */
798 pInstance = WDML_GetInstance(idInst);
799 if (pInstance == NULL)
800 {
801 error_code = DMLERR_INVALIDPARAMETER;
802 }
803 else
804 {
805 error_code = pInstance->lastError;
806 pInstance->lastError = 0;
807 }
808
809 return error_code;
810 }
811
812 /******************************************************************
813 * WDML_SetAllLastError
814 *
815 *
816 */
817 static void WDML_SetAllLastError(DWORD lastError)
818 {
819 DWORD threadID;
820 WDML_INSTANCE* pInstance;
821 threadID = GetCurrentThreadId();
822 pInstance = WDML_InstanceList;
823 while (pInstance)
824 {
825 if (pInstance->threadID == threadID)
826 pInstance->lastError = lastError;
827 pInstance = pInstance->next;
828 }
829 }
830
831 /* ================================================================
832 *
833 * String management
834 *
835 * ================================================================ */
836
837
838 /******************************************************************
839 * WDML_FindNode
840 *
841 *
842 */
843 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
844 {
845 HSZNode* pNode;
846
847 if (pInstance == NULL) return NULL;
848
849 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
850 {
851 if (pNode->hsz == hsz) break;
852 }
853 if (!pNode) WARN("HSZ %p not found\n", hsz);
854 return pNode;
855 }
856
857 /******************************************************************
858 * WDML_MakeAtomFromHsz
859 *
860 * Creates a global atom from an existing HSZ
861 * Generally used before sending an HSZ as an atom to a remote app
862 */
863 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
864 {
865 WCHAR nameBuffer[MAX_BUFFER_LEN];
866
867 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
868 return GlobalAddAtomW(nameBuffer);
869 WARN("HSZ %p not found\n", hsz);
870 return 0;
871 }
872
873 /******************************************************************
874 * WDML_MakeHszFromAtom
875 *
876 * Creates a HSZ from an existing global atom
877 * Generally used while receiving a global atom and transforming it
878 * into an HSZ
879 */
880 HSZ WDML_MakeHszFromAtom(const WDML_INSTANCE* pInstance, ATOM atom)
881 {
882 WCHAR nameBuffer[MAX_BUFFER_LEN];
883
884 if (!atom) return NULL;
885
886 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
887 {
888 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
889 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
890 }
891 WARN("ATOM 0x%x not found\n", atom);
892 return 0;
893 }
894
895 /******************************************************************
896 * WDML_IncHSZ
897 *
898 *
899 */
900 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
901 {
902 HSZNode* pNode;
903
904 pNode = WDML_FindNode(pInstance, hsz);
905 if (!pNode) return FALSE;
906
907 pNode->refCount++;
908 return TRUE;
909 }
910
911 /******************************************************************************
912 * WDML_DecHSZ (INTERNAL)
913 *
914 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
915 * of HSZ nodes
916 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
917 */
918 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
919 {
920 HSZNode* pPrev = NULL;
921 HSZNode* pCurrent;
922
923 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
924 {
925 /* If we found the node we were looking for and its ref count is one,
926 * we can remove it
927 */
928 if (pCurrent->hsz == hsz)
929 {
930 if (--pCurrent->refCount == 0)
931 {
932 if (pCurrent == pInstance->nodeList)
933 {
934 pInstance->nodeList = pCurrent->next;
935 }
936 else
937 {
938 pPrev->next = pCurrent->next;
939 }
940 HeapFree(GetProcessHeap(), 0, pCurrent);
941 DeleteAtom(HSZ2ATOM(hsz));
942 }
943 return TRUE;
944 }
945 }
946 WARN("HSZ %p not found\n", hsz);
947
948 return FALSE;
949 }
950
951 /******************************************************************************
952 * WDML_FreeAllHSZ (INTERNAL)
953 *
954 * Frees up all the strings still allocated in the list and
955 * remove all the nodes from the list of HSZ nodes.
956 */
957 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
958 {
959 /* Free any strings created in this instance.
960 */
961 while (pInstance->nodeList != NULL)
962 {
963 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
964 }
965 }
966
967 /******************************************************************************
968 * InsertHSZNode (INTERNAL)
969 *
970 * Insert a node to the head of the list.
971 */
972 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
973 {
974 if (hsz != 0)
975 {
976 HSZNode* pNew = NULL;
977 /* Create a new node for this HSZ.
978 */
979 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
980 if (pNew != NULL)
981 {
982 pNew->hsz = hsz;
983 pNew->next = pInstance->nodeList;
984 pNew->refCount = 1;
985 pInstance->nodeList = pNew;
986 }
987 else
988 {
989 ERR("Primary HSZ Node allocation failed - out of memory\n");
990 }
991 }
992 }
993
994 /******************************************************************
995 * WDML_QueryString
996 *
997 *
998 */
999 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
1000 int codepage)
1001 {
1002 WCHAR pString[MAX_BUFFER_LEN];
1003 int ret;
1004 /* If psz is null, we have to return only the length
1005 * of the string.
1006 */
1007 if (ptr == NULL)
1008 {
1009 ptr = pString;
1010 cchMax = MAX_BUFFER_LEN;
1011 }
1012
1013 /* if there is no input windows returns a NULL string */
1014 if (hsz == NULL)
1015 {
1016 CHAR *t_ptr = ptr;
1017 *t_ptr = '\0';
1018 return 1;
1019 }
1020
1021 switch (codepage)
1022 {
1023 case CP_WINANSI:
1024 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1025 break;
1026 case CP_WINUNICODE:
1027 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1028 break;
1029 default:
1030 ERR("Unknown code page %d\n", codepage);
1031 ret = 0;
1032 }
1033 return ret;
1034 }
1035
1036 /*****************************************************************
1037 * DdeQueryStringA [USER32.@]
1038 */
1039 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1040 {
1041 DWORD ret = 0;
1042 WDML_INSTANCE* pInstance;
1043
1044 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1045
1046 /* First check instance
1047 */
1048 pInstance = WDML_GetInstance(idInst);
1049 if (pInstance != NULL)
1050 {
1051 if (iCodePage == 0) iCodePage = CP_WINANSI;
1052 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1053 }
1054
1055 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
1056 return ret;
1057 }
1058
1059 /*****************************************************************
1060 * DdeQueryStringW [USER32.@]
1061 */
1062
1063 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1064 {
1065 DWORD ret = 0;
1066 WDML_INSTANCE* pInstance;
1067
1068 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1069
1070 /* First check instance
1071 */
1072 pInstance = WDML_GetInstance(idInst);
1073 if (pInstance != NULL)
1074 {
1075 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1076 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1077 }
1078
1079 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
1080 return ret;
1081 }
1082
1083 /******************************************************************
1084 * DML_CreateString
1085 *
1086 *
1087 */
1088 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1089 {
1090 HSZ hsz;
1091
1092 switch (codepage)
1093 {
1094 case CP_WINANSI:
1095 hsz = ATOM2HSZ(AddAtomA(ptr));
1096 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1097 break;
1098 case CP_WINUNICODE:
1099 hsz = ATOM2HSZ(AddAtomW(ptr));
1100 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1101 break;
1102 default:
1103 ERR("Unknown code page %d\n", codepage);
1104 return 0;
1105 }
1106 WDML_InsertHSZNode(pInstance, hsz);
1107 return hsz;
1108 }
1109
1110 /*****************************************************************
1111 * DdeCreateStringHandleA [USER32.@]
1112 *
1113 * See DdeCreateStringHandleW.
1114 */
1115 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1116 {
1117 HSZ hsz = 0;
1118 WDML_INSTANCE* pInstance;
1119
1120 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1121
1122 pInstance = WDML_GetInstance(idInst);
1123 if (pInstance == NULL)
1124 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1125 else
1126 {
1127 if (codepage == 0) codepage = CP_WINANSI;
1128 hsz = WDML_CreateString(pInstance, psz, codepage);
1129 }
1130
1131 return hsz;
1132 }
1133
1134
1135 /******************************************************************************
1136 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1137 *
1138 * PARAMS
1139 * idInst [I] Instance identifier
1140 * psz [I] Pointer to string
1141 * codepage [I] Code page identifier
1142 * RETURNS
1143 * Success: String handle
1144 * Failure: 0
1145 */
1146 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1147 {
1148 WDML_INSTANCE* pInstance;
1149 HSZ hsz = 0;
1150
1151 pInstance = WDML_GetInstance(idInst);
1152 if (pInstance == NULL)
1153 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1154 else
1155 {
1156 if (codepage == 0) codepage = CP_WINUNICODE;
1157 hsz = WDML_CreateString(pInstance, psz, codepage);
1158 }
1159
1160 return hsz;
1161 }
1162
1163 /*****************************************************************
1164 * DdeFreeStringHandle (USER32.@)
1165 * RETURNS
1166 * success: nonzero
1167 * fail: zero
1168 */
1169 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1170 {
1171 WDML_INSTANCE* pInstance;
1172 BOOL ret = FALSE;
1173
1174 TRACE("(%d,%p):\n", idInst, hsz);
1175
1176 /* First check instance
1177 */
1178 pInstance = WDML_GetInstance(idInst);
1179 if (pInstance)
1180 ret = WDML_DecHSZ(pInstance, hsz);
1181
1182 return ret;
1183 }
1184
1185 /*****************************************************************
1186 * DdeKeepStringHandle (USER32.@)
1187 *
1188 * RETURNS
1189 * success: nonzero
1190 * fail: zero
1191 */
1192 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1193 {
1194 WDML_INSTANCE* pInstance;
1195 BOOL ret = FALSE;
1196
1197 TRACE("(%d,%p):\n", idInst, hsz);
1198
1199 /* First check instance
1200 */
1201 pInstance = WDML_GetInstance(idInst);
1202 if (pInstance)
1203 ret = WDML_IncHSZ(pInstance, hsz);
1204
1205 return ret;
1206 }
1207
1208 /*****************************************************************
1209 * DdeCmpStringHandles (USER32.@)
1210 *
1211 * Compares the value of two string handles. This comparison is
1212 * not case sensitive.
1213 *
1214 * PARAMS
1215 * hsz1 [I] Handle to the first string
1216 * hsz2 [I] Handle to the second string
1217 *
1218 * RETURNS
1219 * -1 The value of hsz1 is zero or less than hsz2
1220 * 0 The values of hsz 1 and 2 are the same or both zero.
1221 * 1 The value of hsz2 is zero of less than hsz1
1222 */
1223 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1224 {
1225 WCHAR psz1[MAX_BUFFER_LEN];
1226 WCHAR psz2[MAX_BUFFER_LEN];
1227 int ret = 0;
1228 int ret1, ret2;
1229
1230 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1231 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1232
1233 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1234
1235 /* Make sure we found both strings. */
1236 if (ret1 == 0 && ret2 == 0)
1237 {
1238 /* If both are not found, return both "zero strings". */
1239 ret = 0;
1240 }
1241 else if (ret1 == 0)
1242 {
1243 /* If hsz1 is a not found, return hsz1 is "zero string". */
1244 ret = -1;
1245 }
1246 else if (ret2 == 0)
1247 {
1248 /* If hsz2 is a not found, return hsz2 is "zero string". */
1249 ret = 1;
1250 }
1251 else
1252 {
1253 /* Compare the two strings we got (case insensitive). */
1254 ret = lstrcmpiW(psz1, psz2);
1255 /* Since strcmp returns any number smaller than
1256 * 0 when the first string is found to be less than
1257 * the second one we must make sure we are returning
1258 * the proper values.
1259 */
1260 if (ret < 0)
1261 {
1262 ret = -1;
1263 }
1264 else if (ret > 0)
1265 {
1266 ret = 1;
1267 }
1268 }
1269
1270 return ret;
1271 }
1272
1273 /* ================================================================
1274 *
1275 * Data handle management
1276 *
1277 * ================================================================ */
1278
1279 /*****************************************************************
1280 * DdeCreateDataHandle (USER32.@)
1281 */
1282 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1283 HSZ hszItem, UINT wFmt, UINT afCmd)
1284 {
1285
1286 /* Other than check for validity we will ignore for now idInst, hszItem.
1287 * The purpose of these arguments still need to be investigated.
1288 */
1289
1290 WDML_INSTANCE* pInstance;
1291 HGLOBAL hMem;
1292 LPBYTE pByte;
1293 DDE_DATAHANDLE_HEAD* pDdh;
1294 WCHAR psz[MAX_BUFFER_LEN];
1295
1296 pInstance = WDML_GetInstance(idInst);
1297 if (pInstance == NULL)
1298 {
1299 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1300 return NULL;
1301 }
1302
1303 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1304 {
1305 psz[0] = HSZ2ATOM(hszItem);
1306 psz[1] = 0;
1307 }
1308
1309 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1310 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1311
1312 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1313 return 0;
1314
1315 /* we use the first 4 bytes to store the size */
1316 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1317 {
1318 ERR("GlobalAlloc failed\n");
1319 return 0;
1320 }
1321
1322 pDdh = GlobalLock(hMem);
1323 if (!pDdh)
1324 {
1325 GlobalFree(hMem);
1326 return 0;
1327 }
1328
1329 pDdh->cfFormat = wFmt;
1330 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1331
1332 pByte = (LPBYTE)(pDdh + 1);
1333 if (pSrc)
1334 {
1335 memcpy(pByte, pSrc + cbOff, cb);
1336 }
1337 GlobalUnlock(hMem);
1338
1339 TRACE("=> %p\n", hMem);
1340 return hMem;
1341 }
1342
1343 /*****************************************************************
1344 *
1345 * DdeAddData (USER32.@)
1346 */
1347 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1348 {
1349 DWORD old_sz, new_sz;
1350 LPBYTE pDst;
1351
1352 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1353
1354 pDst = DdeAccessData(hData, &old_sz);
1355 if (!pDst) return 0;
1356
1357 new_sz = cb + cbOff;
1358 if (new_sz > old_sz)
1359 {
1360 DdeUnaccessData(hData);
1361 hData = GlobalReAlloc(hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1362 GMEM_MOVEABLE | GMEM_DDESHARE);
1363 pDst = DdeAccessData(hData, &old_sz);
1364 }
1365
1366 if (!pDst) return 0;
1367
1368 memcpy(pDst + cbOff, pSrc, cb);
1369 DdeUnaccessData(hData);
1370 return hData;
1371 }
1372
1373 /******************************************************************************
1374 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1375 *
1376 *
1377 * PARAMS
1378 * hData [I] Handle to DDE object
1379 * pDst [I] Pointer to destination buffer
1380 * cbMax [I] Amount of data to copy
1381 * cbOff [I] Offset to beginning of data
1382 *
1383 * RETURNS
1384 * Size of memory object associated with handle
1385 */
1386 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1387 {
1388 DWORD dwSize, dwRet;
1389 LPBYTE pByte;
1390
1391 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1392
1393 pByte = DdeAccessData(hData, &dwSize);
1394
1395 if (pByte)
1396 {
1397 if (!pDst)
1398 {
1399 dwRet = dwSize;
1400 }
1401 else if (cbOff + cbMax < dwSize)
1402 {
1403 dwRet = cbMax;
1404 }
1405 else if (cbOff < dwSize)
1406 {
1407 dwRet = dwSize - cbOff;
1408 }
1409 else
1410 {
1411 dwRet = 0;
1412 }
1413 if (pDst && dwRet != 0)
1414 {
1415 memcpy(pDst, pByte + cbOff, dwRet);
1416 }
1417 DdeUnaccessData(hData);
1418 }
1419 else
1420 {
1421 dwRet = 0;
1422 }
1423 return dwRet;
1424 }
1425
1426 /*****************************************************************
1427 * DdeAccessData (USER32.@)
1428 */
1429 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1430 {
1431 HGLOBAL hMem = hData;
1432 DDE_DATAHANDLE_HEAD* pDdh;
1433
1434 TRACE("(%p,%p)\n", hData, pcbDataSize);
1435
1436 pDdh = GlobalLock(hMem);
1437 if (pDdh == NULL)
1438 {
1439 ERR("Failed on GlobalLock(%p)\n", hMem);
1440 return 0;
1441 }
1442
1443 if (pcbDataSize != NULL)
1444 {
1445 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1446 }
1447 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1448 return (LPBYTE)(pDdh + 1);
1449 }
1450
1451 /*****************************************************************
1452 * DdeUnaccessData (USER32.@)
1453 */
1454 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1455 {
1456 HGLOBAL hMem = hData;
1457
1458 TRACE("(%p)\n", hData);
1459
1460 GlobalUnlock(hMem);
1461
1462 return TRUE;
1463 }
1464
1465 /*****************************************************************
1466 * DdeFreeDataHandle (USER32.@)
1467 */
1468 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1469 {
1470 TRACE("(%p)\n", hData);
1471 return GlobalFree(hData) == 0;
1472 }
1473
1474 /******************************************************************
1475 * WDML_IsAppOwned
1476 *
1477 *
1478 */
1479 BOOL WDML_IsAppOwned(HDDEDATA hData)
1480 {
1481 DDE_DATAHANDLE_HEAD* pDdh;
1482 BOOL ret = FALSE;
1483
1484 pDdh = GlobalLock(hData);
1485 if (pDdh != NULL)
1486 {
1487 ret = pDdh->bAppOwned;
1488 GlobalUnlock(hData);
1489 }
1490 return ret;
1491 }
1492
1493 /* ================================================================
1494 *
1495 * Global <=> Data handle management
1496 *
1497 * ================================================================ */
1498
1499 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1500 * offset size
1501 * (bytes) (bits) comment
1502 * 0 16 bit fields for options (release, ackreq, response...)
1503 * 2 16 clipboard format
1504 * 4 ? data to be used
1505 */
1506 HDDEDATA WDML_Global2DataHandle(WDML_CONV* pConv, HGLOBAL hMem, WINE_DDEHEAD* p)
1507 {
1508 DDEDATA* pDd;
1509 HDDEDATA ret = 0;
1510 DWORD size;
1511
1512 if (hMem)
1513 {
1514 pDd = GlobalLock(hMem);
1515 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1516 if (pDd)
1517 {
1518 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1519 switch (pDd->cfFormat)
1520 {
1521 default:
1522 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1523 pDd->cfFormat, hMem);
1524 /* fall thru */
1525 case 0:
1526 case CF_TEXT:
1527 ret = DdeCreateDataHandle(pConv->instance->instanceID, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1528 break;
1529 case CF_BITMAP:
1530 if (size >= sizeof(BITMAP))
1531 {
1532 BITMAP* bmp = (BITMAP*)pDd->Value;
1533 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1534 if (size >= sizeof(BITMAP) + count)
1535 {
1536 HBITMAP hbmp;
1537
1538 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1539 bmp->bmPlanes, bmp->bmBitsPixel,
1540 pDd->Value + sizeof(BITMAP))))
1541 {
1542 ret = DdeCreateDataHandle(pConv->instance->instanceID, (LPBYTE)&hbmp, sizeof(hbmp),
1543 0, 0, CF_BITMAP, 0);
1544 }
1545 else ERR("Can't create bmp\n");
1546 }
1547 else
1548 {
1549 ERR("Wrong count: %u / %d\n", size, count);
1550 }
1551 } else ERR("No bitmap header\n");
1552 break;
1553 }
1554 GlobalUnlock(hMem);
1555 }
1556 }
1557 return ret;
1558 }
1559
1560 /******************************************************************
1561 * WDML_DataHandle2Global
1562 *
1563 *
1564 */
1565 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1566 BOOL fDeferUpd, BOOL fAckReq)
1567 {
1568 DDE_DATAHANDLE_HEAD* pDdh;
1569 DWORD dwSize;
1570 HGLOBAL hMem = 0;
1571
1572 dwSize = GlobalSize(hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1573 pDdh = GlobalLock(hDdeData);
1574 if (dwSize && pDdh)
1575 {
1576 WINE_DDEHEAD* wdh = NULL;
1577
1578 switch (pDdh->cfFormat)
1579 {
1580 default:
1581 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1582 pDdh->cfFormat, hDdeData);
1583 /* fall thru */
1584 case 0:
1585 case CF_TEXT:
1586 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1587 if (hMem && (wdh = GlobalLock(hMem)))
1588 {
1589 memcpy(wdh + 1, pDdh + 1, dwSize);
1590 }
1591 break;
1592 case CF_BITMAP:
1593 if (dwSize >= sizeof(HBITMAP))
1594 {
1595 BITMAP bmp;
1596 DWORD count;
1597 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1598
1599 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1600 {
1601 count = bmp.bmWidthBytes * bmp.bmHeight;
1602 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1603 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1604 if (hMem && (wdh = GlobalLock(hMem)))
1605 {
1606 memcpy(wdh + 1, &bmp, sizeof(bmp));
1607 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1608 }
1609 }
1610 }
1611 break;
1612 }
1613 if (wdh)
1614 {
1615 wdh->unused = 0;
1616 wdh->fResponse = fResponse;
1617 wdh->fRelease = fRelease;
1618 wdh->fDeferUpd = fDeferUpd;
1619 wdh->fAckReq = fAckReq;
1620 wdh->cfFormat = pDdh->cfFormat;
1621 GlobalUnlock(hMem);
1622 }
1623 GlobalUnlock(hDdeData);
1624 }
1625
1626 return hMem;
1627 }
1628
1629 /* ================================================================
1630 *
1631 * Server management
1632 *
1633 * ================================================================ */
1634
1635 /******************************************************************
1636 * WDML_AddServer
1637 *
1638 *
1639 */
1640 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1641 {
1642 static const WCHAR fmtW[] = {'%','s','(','','x','%','*','x',')',0};
1643 WDML_SERVER* pServer;
1644 WCHAR buf1[256];
1645 WCHAR buf2[256];
1646
1647 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1648 if (pServer == NULL) return NULL;
1649
1650 pServer->hszService = hszService;
1651 WDML_IncHSZ(pInstance, hszService);
1652
1653 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1654 snprintfW(buf2, 256, fmtW, buf1, 2*sizeof(ULONG_PTR), GetCurrentProcessId());
1655 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1656
1657 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1658 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1659
1660 pServer->filterOn = TRUE;
1661
1662 pServer->next = pInstance->servers;
1663 pInstance->servers = pServer;
1664 return pServer;
1665 }
1666
1667 /******************************************************************
1668 * WDML_RemoveServer
1669 *
1670 *
1671 */
1672 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1673 {
1674 WDML_SERVER* pPrev = NULL;
1675 WDML_SERVER* pServer = NULL;
1676 WDML_CONV* pConv;
1677 WDML_CONV* pConvNext;
1678
1679 pServer = pInstance->servers;
1680
1681 while (pServer != NULL)
1682 {
1683 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1684 {
1685 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1686 pServer->atomService, pServer->atomServiceSpec);
1687 /* terminate all conversations for given topic */
1688 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1689 {
1690 pConvNext = pConv->next;
1691 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1692 {
1693 HWND client = pConv->hwndClient, server = pConv->hwndServer;
1694 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1695 /* don't care about return code (whether client window is present or not) */
1696 PostMessageW(client, WM_DDE_TERMINATE, (WPARAM)server, 0);
1697 }
1698 }
1699 if (pServer == pInstance->servers)
1700 {
1701 pInstance->servers = pServer->next;
1702 }
1703 else
1704 {
1705 pPrev->next = pServer->next;
1706 }
1707
1708 DestroyWindow(pServer->hwndServer);
1709 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1710 WDML_DecHSZ(pInstance, pServer->hszService);
1711
1712 GlobalDeleteAtom(pServer->atomService);
1713 GlobalDeleteAtom(pServer->atomServiceSpec);
1714
1715 HeapFree(GetProcessHeap(), 0, pServer);
1716 break;
1717 }
1718
1719 pPrev = pServer;
1720 pServer = pServer->next;
1721 }
1722 }
1723
1724 /*****************************************************************************
1725 * WDML_FindServer
1726 *
1727 * generic routine to return a pointer to the relevant ServiceNode
1728 * for a given service name, or NULL if the entry does not exist
1729 *
1730 */
1731 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1732 {
1733 WDML_SERVER* pServer;
1734
1735 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1736 {
1737 if (hszService == pServer->hszService)
1738 {
1739 return pServer;
1740 }
1741 }
1742 TRACE("Service name missing\n");
1743 return NULL;
1744 }
1745
1746 /* ================================================================
1747 *
1748 * Conversation management
1749 *
1750 * ================================================================ */
1751
1752 /******************************************************************
1753 * WDML_AddConv
1754 *
1755 *
1756 */
1757 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1758 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1759 {
1760 WDML_CONV* pConv;
1761
1762 /* no conversation yet, add it */
1763 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1764 if (!pConv) return NULL;
1765
1766 pConv->instance = pInstance;
1767 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1768 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1769 pConv->magic = WDML_CONV_MAGIC;
1770 pConv->hwndServer = hwndServer;
1771 pConv->hwndClient = hwndClient;
1772 pConv->transactions = NULL;
1773 pConv->hUser = 0;
1774 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1775 pConv->wStatus |= pInstance->wStatus;
1776 /* check if both side of the conversation are of the same instance */
1777 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1778 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1779 {
1780 pConv->wStatus |= ST_ISSELF;
1781 }
1782 pConv->wConvst = XST_NULL;
1783
1784 pConv->next = pInstance->convs[side];
1785 pInstance->convs[side] = pConv;
1786
1787 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv->wStatus, pInstance);
1788
1789 return pConv;
1790 }
1791
1792 /******************************************************************
1793 * WDML_FindConv
1794 *
1795 *
1796 */
1797 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1798 HSZ hszService, HSZ hszTopic)
1799 {
1800 WDML_CONV* pCurrent = NULL;
1801
1802 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1803 {
1804 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1805 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1806 {
1807 return pCurrent;
1808 }
1809
1810 }
1811 return NULL;
1812 }
1813
1814 /******************************************************************
1815 * WDML_RemoveConv
1816 *
1817 *
1818 */
1819 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1820 {
1821 WDML_CONV* pPrev = NULL;
1822 WDML_CONV* pCurrent;
1823 WDML_XACT* pXAct;
1824 WDML_XACT* pXActNext;
1825 HWND hWnd;
1826
1827 if (!pRef)
1828 return;
1829
1830 /* remove any pending transaction */
1831 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1832 {
1833 pXActNext = pXAct->next;
1834 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1835 }
1836
1837 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1838
1839 /* FIXME: should we keep the window around ? it seems so (at least on client side
1840 * to let QueryConvInfo work after conv termination, but also to implement
1841 * DdeReconnect...
1842 */
1843 /* destroy conversation window, but first remove pConv from hWnd.
1844 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1845 */
1846 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1847 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1848
1849 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1850
1851 WDML_DecHSZ(pRef->instance, pRef->hszService);
1852 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1853
1854 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1855 {
1856 if (pCurrent == pRef)
1857 {
1858 if (pCurrent == pRef->instance->convs[side])
1859 {
1860 pRef->instance->convs[side] = pCurrent->next;
1861 }
1862 else
1863 {
1864 pPrev->next = pCurrent->next;
1865 }
1866 pCurrent->magic = 0;
1867 HeapFree(GetProcessHeap(), 0, pCurrent);
1868 break;
1869 }
1870 }
1871 }
1872
1873 /******************************************************************
1874 * WDML_EnableCallback
1875 */
1876 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1877 {
1878 if (wCmd == EC_DISABLE)
1879 {
1880 pConv->wStatus |= ST_BLOCKED;
1881 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
1882 return TRUE;
1883 }
1884
1885 if (wCmd == EC_QUERYWAITING)
1886 return pConv->transactions ? TRUE : FALSE;
1887
1888 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1889 {
1890 FIXME("Unknown command code %04x\n", wCmd);
1891 return FALSE;
1892 }
1893
1894 if (wCmd == EC_ENABLEALL)
1895 {
1896 pConv->wStatus &= ~ST_BLOCKED;
1897 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
1898 }
1899
1900 while (pConv->transactions)
1901 {
1902 WDML_XACT *pXAct = pConv->transactions;
1903
1904 if (pConv->wStatus & ST_CLIENT)
1905 {
1906 /* transaction should be in the queue until handled */
1907 WDML_ClientHandle(pConv, pXAct, 0, NULL);
1908 WDML_UnQueueTransaction(pConv, pXAct);
1909 }
1910 else
1911 {
1912 /* transaction should be removed from the queue before handling */
1913 WDML_UnQueueTransaction(pConv, pXAct);
1914 WDML_ServerHandle(pConv, pXAct);
1915 }
1916
1917 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1918
1919 if (wCmd == EC_ENABLEONE) break;
1920 }
1921 return TRUE;
1922 }
1923
1924 /*****************************************************************
1925 * DdeEnableCallback (USER32.@)
1926 */
1927 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1928 {
1929 BOOL ret = FALSE;
1930 WDML_CONV *pConv;
1931
1932 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
1933
1934 if (hConv)
1935 {
1936 pConv = WDML_GetConv(hConv, TRUE);
1937
1938 if (pConv && pConv->instance->instanceID == idInst)
1939 ret = WDML_EnableCallback(pConv, wCmd);
1940 }
1941 else
1942 {
1943 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
1944
1945 if (!pInstance)
1946 return FALSE;
1947
1948 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
1949 pInstance->wStatus |= wCmd;
1950
1951 if (wCmd == EC_DISABLE)
1952 {
1953 pInstance->wStatus |= ST_BLOCKED;
1954 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1955 }
1956 else if (wCmd == EC_ENABLEALL)
1957 {
1958 pInstance->wStatus &= ~ST_BLOCKED;
1959 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1960 }
1961
1962 ret = TRUE;
1963
1964 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
1965 {
1966 ret = WDML_EnableCallback(pConv, wCmd);
1967 if (ret && wCmd == EC_QUERYWAITING) break;
1968 }
1969 }
1970
1971 return ret;
1972 }
1973
1974 /******************************************************************
1975 * WDML_GetConv
1976 *
1977 *
1978 */
1979 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1980 {
1981 WDML_CONV* pConv = (WDML_CONV*)hConv;
1982
1983 /* FIXME: should do better checking */
1984 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1985
1986 if (!pConv->instance || pConv->instance->threadID != GetCurrentThreadId())
1987 {
1988 WARN("wrong thread ID\n");
1989 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
1990 return NULL;
1991 }
1992
1993 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1994 {
1995 WARN("found conv but ain't connected\n");
1996 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
1997 return NULL;
1998 }
1999
2000 return pConv;
2001 }
2002
2003 /******************************************************************
2004 * WDML_GetConvFromWnd
2005 *
2006 *
2007 */
2008 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
2009 {
2010 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
2011 }
2012
2013 /******************************************************************
2014 * WDML_PostAck
2015 *
2016 *
2017 */
2018 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
2019 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
2020 {
2021 DDEACK ddeAck;
2022 HWND from, to;
2023
2024 if (side == WDML_SERVER_SIDE)
2025 {
2026 from = pConv->hwndServer;
2027 to = pConv->hwndClient;
2028 }
2029 else
2030 {
2031 to = pConv->hwndServer;
2032 from = pConv->hwndClient;
2033 }
2034
2035 ddeAck.bAppReturnCode = appRetCode;
2036 ddeAck.reserved = 0;
2037 ddeAck.fBusy = fBusy;
2038 ddeAck.fAck = fAck;
2039
2040 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2041
2042 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2043 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2044 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2045 {
2046 pConv->wStatus &= ~ST_CONNECTED;
2047 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2048 FreeDDElParam(WM_DDE_ACK, lParam);
2049 return FALSE;
2050 }
2051 return TRUE;
2052 }
2053
2054 /*****************************************************************
2055 * DdeSetUserHandle (USER32.@)
2056 */
2057 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2058 {
2059 WDML_CONV* pConv;
2060
2061 pConv = WDML_GetConv(hConv, FALSE);
2062 if (pConv == NULL)
2063 return FALSE;
2064
2065 if (id == QID_SYNC)
2066 {
2067 pConv->hUser = hUser;
2068 }
2069 else
2070 {
2071 WDML_XACT* pXAct;
2072
2073 pXAct = WDML_FindTransaction(pConv, id);
2074 if (pXAct)
2075 {
2076 pXAct->hUser = hUser;
2077 }
2078 else
2079 {
2080 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2081 return FALSE;
2082 }
2083 }
2084 return TRUE;
2085 }
2086
2087 /******************************************************************
2088 * WDML_GetLocalConvInfo
2089 *
2090 *
2091 */
2092 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2093 {
2094 BOOL ret = TRUE;
2095 WDML_LINK* pLink;
2096 WDML_SIDE side;
2097
2098 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2099 ci->hszSvcPartner = pConv->hszService;
2100 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2101 ci->hszTopic = pConv->hszTopic;
2102 ci->wStatus = pConv->wStatus;
2103
2104 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2105
2106 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2107 {
2108 if (pLink->hConv == (HCONV)pConv)
2109 {
2110 ci->wStatus |= ST_ADVISE;
2111 break;
2112 }
2113 }
2114
2115 /* FIXME: non handled status flags:
2116 ST_BLOCKED
2117 ST_BLOCKNEXT
2118 ST_INLIST
2119 */
2120
2121 ci->wConvst = pConv->wConvst; /* FIXME */
2122
2123 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2124 ci->hConvList = 0;
2125 ci->ConvCtxt = pConv->convContext;
2126 if (ci->wStatus & ST_CLIENT)
2127 {
2128 ci->hwnd = pConv->hwndClient;
2129 ci->hwndPartner = pConv->hwndServer;
2130 }
2131 else
2132 {
2133 ci->hwnd = pConv->hwndServer;
2134 ci->hwndPartner = pConv->hwndClient;
2135 }
2136 if (id == QID_SYNC)
2137 {
2138 ci->hUser = pConv->hUser;
2139 ci->hszItem = 0;
2140 ci->wFmt = 0;
2141 ci->wType = 0;
2142 }
2143 else
2144 {
2145 WDML_XACT* pXAct;
2146
2147 pXAct = WDML_FindTransaction(pConv, id);
2148 if (pXAct)
2149 {
2150 ci->hUser = pXAct->hUser;
2151 ci->hszItem = pXAct->hszItem;
2152 ci->wFmt = pXAct->wFmt;
2153 ci->wType = pXAct->wType;
2154 }
2155 else
2156 {
2157 ret = 0;
2158 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2159 }
2160 }
2161 return ret;
2162 }
2163
2164 /******************************************************************
2165 * DdeQueryConvInfo (USER32.@)
2166 *
2167 * FIXME: Set last DDE error on failure.
2168 */
2169 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2170 {
2171 UINT ret = lpConvInfo->cb;
2172 CONVINFO ci;
2173 WDML_CONV* pConv;
2174
2175 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2176
2177 if (!hConv)
2178 {
2179 FIXME("hConv is NULL\n");
2180 return 0;
2181 }
2182
2183 pConv = WDML_GetConv(hConv, FALSE);
2184 if (pConv != NULL)
2185 {
2186 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2187 ret = 0;
2188 }
2189 else
2190 {
2191 if ((ULONG_PTR)hConv & 1)
2192 {
2193 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2194 if (pConv != NULL)
2195 FIXME("Request on remote conversation information is not implemented yet\n");
2196 }
2197 ret = 0;
2198 }
2199
2200 if (ret != 0)
2201 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2202 return ret;
2203 }
2204
2205 /* ================================================================
2206 *
2207 * Link (hot & warm) management
2208 *
2209 * ================================================================ */
2210
2211 /******************************************************************
2212 * WDML_AddLink
2213 *
2214 *
2215 */
2216 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2217 UINT wType, HSZ hszItem, UINT wFmt)
2218 {
2219 WDML_LINK* pLink;
2220
2221 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2222 if (pLink == NULL)
2223 {
2224 ERR("OOM\n");
2225 return;
2226 }
2227
2228 pLink->hConv = hConv;
2229 pLink->transactionType = wType;
2230 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2231 pLink->uFmt = wFmt;
2232 pLink->next = pInstance->links[side];
2233 pInstance->links[side] = pLink;
2234 }
2235
2236 /******************************************************************
2237 * WDML_RemoveLink
2238 *
2239 *
2240 */
2241 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2242 HSZ hszItem, UINT uFmt)
2243 {
2244 WDML_LINK* pPrev = NULL;
2245 WDML_LINK* pCurrent = NULL;
2246
2247 pCurrent = pInstance->links[side];
2248
2249 while (pCurrent != NULL)
2250 {
2251 if (pCurrent->hConv == hConv &&
2252 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2253 pCurrent->uFmt == uFmt)
2254 {
2255 if (pCurrent == pInstance->links[side])
2256 {
2257 pInstance->links[side] = pCurrent->next;
2258 }
2259 else
2260 {
2261 pPrev->next = pCurrent->next;
2262 }
2263
2264 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2265 HeapFree(GetProcessHeap(), 0, pCurrent);
2266 break;
2267 }
2268
2269 pPrev = pCurrent;
2270 pCurrent = pCurrent->next;
2271 }
2272 }
2273
2274 /* this function is called to remove all links related to the conv.
2275 It should be called from both client and server when terminating
2276 the conversation.
2277 */
2278 /******************************************************************
2279 * WDML_RemoveAllLinks
2280 *
2281 *
2282 */
2283 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2284 {
2285 WDML_LINK* pPrev = NULL;
2286 WDML_LINK* pCurrent = NULL;
2287 WDML_LINK* pNext = NULL;
2288
2289 pCurrent = pInstance->links[side];
2290
2291 while (pCurrent != NULL)
2292 {
2293 if (pCurrent->hConv == (HCONV)pConv)
2294 {
2295 if (pCurrent == pInstance->links[side])
2296 {
2297 pInstance->links[side] = pCurrent->next;
2298 pNext = pCurrent->next;
2299 }
2300 else
2301 {
2302 pPrev->next = pCurrent->next;
2303 pNext = pCurrent->next;
2304 }
2305
2306 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2307
2308 HeapFree(GetProcessHeap(), 0, pCurrent);
2309 pCurrent = NULL;
2310 }
2311
2312 if (pCurrent)
2313 {
2314 pPrev = pCurrent;
2315 pCurrent = pCurrent->next;
2316 }
2317 else
2318 {
2319 pCurrent = pNext;
2320 }
2321 }
2322 }
2323
2324 /******************************************************************
2325 * WDML_FindLink
2326 *
2327 *
2328 */
2329 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2330 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2331 {
2332 WDML_LINK* pCurrent = NULL;
2333
2334 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2335 {
2336 /* we don't need to check for transaction type as it can be altered */
2337
2338 if (pCurrent->hConv == hConv &&
2339 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2340 (!use_fmt || pCurrent->uFmt == uFmt))
2341 {
2342 break;
2343 }
2344
2345 }
2346
2347 return pCurrent;
2348 }
2349
2350 /* ================================================================
2351 *
2352 * Transaction management
2353 *
2354 * ================================================================ */
2355
2356 /******************************************************************
2357 * WDML_AllocTransaction
2358 *
2359 * Alloc a transaction structure for handling the message ddeMsg
2360 */
2361 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2362 UINT wFmt, HSZ hszItem)
2363 {
2364 WDML_XACT* pXAct;
2365 static WORD tid = 1; /* FIXME: wrap around */
2366
2367 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2368 if (!pXAct)
2369 {
2370 pInstance->lastError = DMLERR_MEMORY_ERROR;
2371 return NULL;
2372 }
2373
2374 pXAct->xActID = tid++;
2375 pXAct->ddeMsg = ddeMsg;
2376 pXAct->hDdeData = 0;
2377 pXAct->hUser = 0;
2378 pXAct->next = NULL;
2379 pXAct->wType = 0;
2380 pXAct->wFmt = wFmt;
2381 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2382 pXAct->atom = 0;
2383 pXAct->hMem = 0;
2384 pXAct->lParam = 0;
2385
2386 return pXAct;
2387 }
2388
2389 /******************************************************************
2390 * WDML_QueueTransaction
2391 *
2392 * Adds a transaction to the list of transaction
2393 */
2394 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2395 {
2396 WDML_XACT** pt;
2397
2398 /* advance to last in queue */
2399 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2400 *pt = pXAct;
2401 }
2402
2403 /******************************************************************
2404 * WDML_UnQueueTransaction
2405 *
2406 *
2407 */
2408 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2409 {
2410 WDML_XACT** pt;
2411
2412 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2413 {
2414 if (*pt == pXAct)
2415 {
2416 *pt = pXAct->next;
2417 return TRUE;
2418 }
2419 }
2420 return FALSE;
2421 }
2422
2423 /******************************************************************
2424 * WDML_FreeTransaction
2425 *
2426 *
2427 */
2428 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2429 {
2430 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2431 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2432 {
2433 GlobalFree(pXAct->hMem);
2434 }
2435 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2436
2437 HeapFree(GetProcessHeap(), 0, pXAct);
2438 }
2439
2440 /******************************************************************
2441 * WDML_FindTransaction
2442 *
2443 *
2444 */
2445 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2446 {
2447 WDML_XACT* pXAct;
2448
2449 tid = HIWORD(tid);
2450 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2451 {
2452 if (pXAct->xActID == tid)
2453 break;
2454 }
2455 return pXAct;
2456 }
2457
2458 /* ================================================================
2459 *
2460 * Information broadcast across DDEML implementations
2461 *
2462 * ================================================================ */
2463
2464 struct tagWDML_BroadcastPmt
2465 {
2466 LPCWSTR clsName;
2467 UINT uMsg;
2468 WPARAM wParam;
2469 LPARAM lParam;
2470 };
2471
2472 /******************************************************************
2473 * WDML_BroadcastEnumProc
2474 *
2475 *
2476 */
2477 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2478 {
2479 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2480 WCHAR buffer[128];
2481
2482 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2483 lstrcmpiW(buffer, s->clsName) == 0)
2484 {
2485 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2486 }
2487 return TRUE;
2488 }
2489
2490 /******************************************************************
2491 * WDML_BroadcastDDEWindows
2492 *
2493 *
2494 */
2495 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2496 {
2497 struct tagWDML_BroadcastPmt s;
2498
2499 s.clsName = clsName;
2500 s.uMsg = uMsg;
2501 s.wParam = wParam;
2502 s.lParam = lParam;
2503 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);
2504 }
2505
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.