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