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