user32: Set uiLo and uiHi to zero on error.
[wine/wine-kai.git] / dlls / user32 / dde_misc.c
blob974fde3f30c6945bed9c73cea633ee576daaf236
1 /*
2 * DDEML library
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
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.
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.
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
26 #include "config.h"
27 #include "wine/port.h"
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "dde.h"
37 #include "ddeml.h"
38 #include "win.h"
39 #include "dde_private.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
45 /* convert between ATOM and HSZ avoiding compiler warnings */
46 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
47 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
49 static WDML_INSTANCE* WDML_InstanceList = NULL;
50 static LONG WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
51 const WCHAR WDML_szEventClass[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
53 /* protection for instance list */
54 static CRITICAL_SECTION WDML_CritSect;
55 static CRITICAL_SECTION_DEBUG critsect_debug =
57 0, 0, &WDML_CritSect,
58 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
59 0, 0, { (DWORD_PTR)(__FILE__ ": WDML_CritSect") }
61 static CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
63 /* ================================================================
65 * Pure DDE (non DDEML) management
67 * ================================================================ */
70 /*****************************************************************
71 * PackDDElParam (USER32.@)
73 * RETURNS
74 * the packed lParam
76 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
78 HGLOBAL hMem;
79 UINT_PTR *params;
81 switch (msg)
83 case WM_DDE_ACK:
84 case WM_DDE_ADVISE:
85 case WM_DDE_DATA:
86 case WM_DDE_POKE:
87 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
89 ERR("GlobalAlloc failed\n");
90 return 0;
92 if (!(params = GlobalLock(hMem)))
94 ERR("GlobalLock failed (%p)\n", hMem);
95 return 0;
97 params[0] = uiLo;
98 params[1] = uiHi;
99 GlobalUnlock(hMem);
100 return (LPARAM)hMem;
102 case WM_DDE_EXECUTE:
103 return uiHi;
105 default:
106 return MAKELPARAM(uiLo, uiHi);
111 /*****************************************************************
112 * UnpackDDElParam (USER32.@)
114 * RETURNS
115 * success: nonzero
116 * failure: zero
118 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
119 PUINT_PTR uiLo, PUINT_PTR uiHi)
121 UINT_PTR *params;
123 switch (msg)
125 case WM_DDE_ACK:
126 case WM_DDE_ADVISE:
127 case WM_DDE_DATA:
128 case WM_DDE_POKE:
129 if (!lParam || !(params = GlobalLock((HGLOBAL)lParam)))
131 if (uiLo) *uiLo = 0;
132 if (uiHi) *uiHi = 0;
133 return FALSE;
135 TRACE("unpacked: low %08lx, high %08lx\n", params[0], params[1]);
136 if (uiLo) *uiLo = params[0];
137 if (uiHi) *uiHi = params[1];
138 GlobalUnlock( (HGLOBAL)lParam );
139 return TRUE;
141 case WM_DDE_EXECUTE:
142 if (uiLo) *uiLo = 0;
143 if (uiHi) *uiHi = lParam;
144 return TRUE;
146 default:
147 if (uiLo) *uiLo = LOWORD(lParam);
148 if (uiHi) *uiHi = HIWORD(lParam);
149 return TRUE;
154 /*****************************************************************
155 * FreeDDElParam (USER32.@)
157 * RETURNS
158 * success: nonzero
159 * failure: zero
161 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
163 switch (msg)
165 case WM_DDE_ACK:
166 case WM_DDE_ADVISE:
167 case WM_DDE_DATA:
168 case WM_DDE_POKE:
169 /* first check if it's a global handle */
170 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
171 return !GlobalFree( (HGLOBAL)lParam );
173 default:
174 return TRUE;
179 /*****************************************************************
180 * ReuseDDElParam (USER32.@)
182 * RETURNS
183 * the packed lParam
185 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
186 UINT_PTR uiLo, UINT_PTR uiHi)
188 UINT_PTR *params;
190 switch (msgIn)
192 case WM_DDE_ACK:
193 case WM_DDE_ADVISE:
194 case WM_DDE_DATA:
195 case WM_DDE_POKE:
196 switch(msgOut)
198 case WM_DDE_ACK:
199 case WM_DDE_ADVISE:
200 case WM_DDE_DATA:
201 case WM_DDE_POKE:
202 if (!lParam) return 0;
203 if (!(params = GlobalLock( (HGLOBAL)lParam )))
205 ERR("GlobalLock failed\n");
206 return 0;
208 params[0] = uiLo;
209 params[1] = uiHi;
210 TRACE("Reusing pack %08lx %08lx\n", uiLo, uiHi);
211 GlobalUnlock( (HGLOBAL)lParam );
212 return lParam;
214 case WM_DDE_EXECUTE:
215 FreeDDElParam( msgIn, lParam );
216 return uiHi;
218 default:
219 FreeDDElParam( msgIn, lParam );
220 return MAKELPARAM(uiLo, uiHi);
223 default:
224 return PackDDElParam( msgOut, uiLo, uiHi );
228 /*****************************************************************
229 * ImpersonateDdeClientWindow (USER32.@)
231 * PARAMS
232 * hWndClient [I] handle to DDE client window
233 * hWndServer [I] handle to DDE server window
235 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
237 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
238 return FALSE;
241 /*****************************************************************
242 * DdeSetQualityOfService (USER32.@)
245 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
246 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
248 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
249 return TRUE;
252 /* ================================================================
254 * Instance management
256 * ================================================================ */
258 /******************************************************************************
259 * IncrementInstanceId
261 * generic routine to increment the max instance Id and allocate a new application instance
263 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
265 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
267 pInstance->instanceID = id;
268 TRACE("New instance id %d allocated\n", id);
271 /******************************************************************
272 * WDML_EventProc
276 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
278 WDML_INSTANCE* pInstance;
279 HSZ hsz1, hsz2;
281 switch (uMsg)
283 case WM_WDML_REGISTER:
284 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
285 /* try calling the Callback */
286 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
288 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
289 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
290 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
291 WDML_DecHSZ(pInstance, hsz1);
292 WDML_DecHSZ(pInstance, hsz2);
294 break;
296 case WM_WDML_UNREGISTER:
297 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
298 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
300 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
301 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
302 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
303 WDML_DecHSZ(pInstance, hsz1);
304 WDML_DecHSZ(pInstance, hsz2);
306 break;
308 case WM_WDML_CONNECT_CONFIRM:
309 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
310 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
312 WDML_CONV* pConv;
313 /* confirm connection...
314 * lookup for this conv handle
316 HWND client = WIN_GetFullHandle( (HWND)wParam );
317 HWND server = WIN_GetFullHandle( (HWND)lParam );
318 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
320 if (pConv->hwndClient == client && pConv->hwndServer == server)
321 break;
323 if (pConv)
325 pConv->wStatus |= ST_ISLOCAL;
327 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
328 pConv->hszTopic, pConv->hszService, 0, 0,
329 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
332 break;
333 default:
334 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
336 return 0;
339 /******************************************************************
340 * WDML_Initialize
344 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
345 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
347 WDML_INSTANCE* pInstance;
348 WDML_INSTANCE* reference_inst;
349 UINT ret;
350 WNDCLASSEXW wndclass;
352 TRACE("(%p,%p,0x%x,%d)\n",
353 pidInst, pfnCallback, afCmd, ulRes);
355 if (ulRes)
357 ERR("Reserved value not zero? What does this mean?\n");
358 /* trap this and no more until we know more */
359 return DMLERR_NO_ERROR;
362 /* grab enough heap for one control struct - not really necessary for re-initialise
363 * but allows us to use same validation routines */
364 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
365 if (pInstance == NULL)
367 /* catastrophe !! warn user & abort */
368 ERR("Instance create failed - out of memory\n");
369 return DMLERR_SYS_ERROR;
371 pInstance->next = NULL;
372 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
374 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
376 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
377 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
378 pInstance->threadID = GetCurrentThreadId();
379 pInstance->callback = *pfnCallback;
380 pInstance->unicode = bUnicode;
381 pInstance->win16 = b16;
382 pInstance->nodeList = NULL; /* node will be added later */
383 pInstance->monitorFlags = afCmd & MF_MASK;
384 pInstance->wStatus = 0;
385 pInstance->servers = NULL;
386 pInstance->convs[0] = NULL;
387 pInstance->convs[1] = NULL;
388 pInstance->links[0] = NULL;
389 pInstance->links[1] = NULL;
391 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
393 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
395 if (!pInstance->clientOnly)
397 /* Check for other way of setting Client-only !! */
398 pInstance->clientOnly =
399 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
402 TRACE("instance created - checking validity\n");
404 if (*pidInst == 0)
406 /* Initialisation of new Instance Identifier */
407 TRACE("new instance, callback %p flags %X\n",pfnCallback,afCmd);
409 EnterCriticalSection(&WDML_CritSect);
411 if (WDML_InstanceList == NULL)
413 /* can't be another instance in this case, assign to the base pointer */
414 WDML_InstanceList = pInstance;
416 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
417 * present
418 * ------------------------------- NOTE NOTE NOTE --------------------------
420 * the manual is not clear if this condition
421 * applies to the first call to DdeInitialize from an application, or the
422 * first call for a given callback !!!
425 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
426 TRACE("First application instance detected OK\n");
427 /* allocate new instance ID */
428 WDML_IncrementInstanceId(pInstance);
430 else
432 /* really need to chain the new one in to the latest here, but after checking conditions
433 * such as trying to start a conversation from an application trying to monitor */
434 reference_inst = WDML_InstanceList;
435 TRACE("Subsequent application instance - starting checks\n");
436 while (reference_inst->next != NULL)
439 * This set of tests will work if application uses same instance Id
440 * at application level once allocated - which is what manual implies
441 * should happen. If someone tries to be
442 * clever (lazy ?) it will fail to pick up that later calls are for
443 * the same application - should we trust them ?
445 if (pInstance->instanceID == reference_inst->instanceID)
447 /* Check 1 - must be same Client-only state */
449 if (pInstance->clientOnly != reference_inst->clientOnly)
451 ret = DMLERR_DLL_USAGE;
452 goto theError;
455 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
457 if (pInstance->monitor != reference_inst->monitor)
459 ret = DMLERR_INVALIDPARAMETER;
460 goto theError;
463 /* Check 3 - must supply different callback address */
465 if (pInstance->callback == reference_inst->callback)
467 ret = DMLERR_DLL_USAGE;
468 goto theError;
471 reference_inst = reference_inst->next;
473 /* All cleared, add to chain */
475 TRACE("Application Instance checks finished\n");
476 WDML_IncrementInstanceId(pInstance);
477 reference_inst->next = pInstance;
479 LeaveCriticalSection(&WDML_CritSect);
481 *pidInst = pInstance->instanceID;
483 /* for deadlock issues, windows must always be created when outside the critical section */
484 wndclass.cbSize = sizeof(wndclass);
485 wndclass.style = 0;
486 wndclass.lpfnWndProc = WDML_EventProc;
487 wndclass.cbClsExtra = 0;
488 wndclass.cbWndExtra = sizeof(ULONG_PTR);
489 wndclass.hInstance = 0;
490 wndclass.hIcon = 0;
491 wndclass.hCursor = 0;
492 wndclass.hbrBackground = 0;
493 wndclass.lpszMenuName = NULL;
494 wndclass.lpszClassName = WDML_szEventClass;
495 wndclass.hIconSm = 0;
497 RegisterClassExW(&wndclass);
499 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
500 WS_POPUP, 0, 0, 0, 0,
501 0, 0, 0, 0);
503 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
505 TRACE("New application instance processing finished OK\n");
507 else
509 /* Reinitialisation situation --- FIX */
510 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst, pfnCallback, afCmd, ulRes);
512 EnterCriticalSection(&WDML_CritSect);
514 if (WDML_InstanceList == NULL)
516 ret = DMLERR_INVALIDPARAMETER;
517 goto theError;
519 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
520 /* can't reinitialise if we have initialised nothing !! */
521 reference_inst = WDML_InstanceList;
522 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
524 * MS allows initialisation without specifying a callback, should we allow addition of the
525 * callback by a later call to initialise ? - if so this lot will have to change
527 while (reference_inst->next != NULL)
529 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
531 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
533 if (reference_inst->clientOnly)
535 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
537 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
539 if (!(afCmd & APPCMD_CLIENTONLY))
541 ret = DMLERR_INVALIDPARAMETER;
542 goto theError;
546 /* Check 2 - cannot change monitor modes */
548 if (pInstance->monitor != reference_inst->monitor)
550 ret = DMLERR_INVALIDPARAMETER;
551 goto theError;
554 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
556 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
558 ret = DMLERR_INVALIDPARAMETER;
559 goto theError;
561 break;
563 reference_inst = reference_inst->next;
565 if (reference_inst->next == NULL)
567 ret = DMLERR_INVALIDPARAMETER;
568 goto theError;
570 /* All checked - change relevant flags */
572 reference_inst->CBFflags = pInstance->CBFflags;
573 reference_inst->clientOnly = pInstance->clientOnly;
574 reference_inst->monitorFlags = pInstance->monitorFlags;
575 LeaveCriticalSection(&WDML_CritSect);
578 return DMLERR_NO_ERROR;
579 theError:
580 HeapFree(GetProcessHeap(), 0, pInstance);
581 LeaveCriticalSection(&WDML_CritSect);
582 return ret;
585 /******************************************************************************
586 * DdeInitializeA (USER32.@)
588 * See DdeInitializeW.
590 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
591 DWORD afCmd, DWORD ulRes)
593 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
596 /******************************************************************************
597 * DdeInitializeW [USER32.@]
598 * Registers an application with the DDEML
600 * PARAMS
601 * pidInst [I] Pointer to instance identifier
602 * pfnCallback [I] Pointer to callback function
603 * afCmd [I] Set of command and filter flags
604 * ulRes [I] Reserved
606 * RETURNS
607 * Success: DMLERR_NO_ERROR
608 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
610 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
611 DWORD afCmd, DWORD ulRes)
613 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
616 /*****************************************************************
617 * DdeUninitialize [USER32.@] Frees DDEML resources
619 * PARAMS
620 * idInst [I] Instance identifier
622 * RETURNS
623 * Success: TRUE
624 * Failure: FALSE
627 BOOL WINAPI DdeUninitialize(DWORD idInst)
629 /* Stage one - check if we have a handle for this instance
631 WDML_INSTANCE* pInstance;
632 WDML_CONV* pConv;
633 WDML_CONV* pConvNext;
635 TRACE("(%d)\n", idInst);
637 /* First check instance
639 pInstance = WDML_GetInstance(idInst);
640 if (pInstance == NULL)
643 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
645 return FALSE;
648 /* first terminate all conversations client side
649 * this shall close existing links...
651 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
653 pConvNext = pConv->next;
654 DdeDisconnect((HCONV)pConv);
656 if (pInstance->convs[WDML_CLIENT_SIDE])
657 FIXME("still pending conversations\n");
659 /* then unregister all known service names */
660 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
662 /* Free the nodes that were not freed by this instance
663 * and remove the nodes from the list of HSZ nodes.
665 WDML_FreeAllHSZ(pInstance);
667 DestroyWindow(pInstance->hwndEvent);
669 /* OK now delete the instance handle itself */
671 if (WDML_InstanceList == pInstance)
673 /* special case - the first/only entry */
674 WDML_InstanceList = pInstance->next;
676 else
678 /* general case, remove entry */
679 WDML_INSTANCE* inst;
681 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
682 inst->next = pInstance->next;
684 /* release the heap entry
686 HeapFree(GetProcessHeap(), 0, pInstance);
688 return TRUE;
691 /******************************************************************
692 * WDML_NotifyThreadExit
696 void WDML_NotifyThreadDetach(void)
698 WDML_INSTANCE* pInstance;
699 WDML_INSTANCE* next;
700 DWORD tid = GetCurrentThreadId();
702 EnterCriticalSection(&WDML_CritSect);
703 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
705 next = pInstance->next;
706 if (pInstance->threadID == tid)
708 LeaveCriticalSection(&WDML_CritSect);
709 DdeUninitialize(pInstance->instanceID);
710 EnterCriticalSection(&WDML_CritSect);
713 LeaveCriticalSection(&WDML_CritSect);
716 /******************************************************************
717 * WDML_InvokeCallback
721 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
722 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
723 ULONG_PTR dwData1, ULONG_PTR dwData2)
725 HDDEDATA ret;
727 if (pInstance == NULL)
728 return NULL;
730 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
731 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
732 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
733 if (pInstance->win16)
735 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
736 hsz1, hsz2, hdata, dwData1, dwData2);
738 else
740 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
742 TRACE("done => %p\n", ret);
743 return ret;
746 /*****************************************************************************
747 * WDML_GetInstance
749 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
750 * for an instance Id, or NULL if the entry does not exist
753 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
755 WDML_INSTANCE* pInstance;
757 EnterCriticalSection(&WDML_CritSect);
759 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
761 if (pInstance->instanceID == instId)
763 if (GetCurrentThreadId() != pInstance->threadID)
765 FIXME("Tried to get instance from wrong thread\n");
766 continue;
768 break;
772 LeaveCriticalSection(&WDML_CritSect);
774 if (!pInstance)
775 WARN("Instance entry missing for id %04x\n", instId);
776 return pInstance;
779 /******************************************************************
780 * WDML_GetInstanceFromWnd
784 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
786 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
789 /******************************************************************************
790 * DdeGetLastError [USER32.@] Gets most recent error code
792 * PARAMS
793 * idInst [I] Instance identifier
795 * RETURNS
796 * Last error code
798 UINT WINAPI DdeGetLastError(DWORD idInst)
800 DWORD error_code;
801 WDML_INSTANCE* pInstance;
803 /* First check instance
805 pInstance = WDML_GetInstance(idInst);
806 if (pInstance == NULL)
808 error_code = DMLERR_INVALIDPARAMETER;
810 else
812 error_code = pInstance->lastError;
813 pInstance->lastError = 0;
816 return error_code;
819 /* ================================================================
821 * String management
823 * ================================================================ */
826 /******************************************************************
827 * WDML_FindNode
831 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
833 HSZNode* pNode;
835 if (pInstance == NULL) return NULL;
837 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
839 if (pNode->hsz == hsz) break;
841 if (!pNode) WARN("HSZ %p not found\n", hsz);
842 return pNode;
845 /******************************************************************
846 * WDML_MakeAtomFromHsz
848 * Creates a global atom from an existing HSZ
849 * Generally used before sending an HSZ as an atom to a remote app
851 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
853 WCHAR nameBuffer[MAX_BUFFER_LEN];
855 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
856 return GlobalAddAtomW(nameBuffer);
857 WARN("HSZ %p not found\n", hsz);
858 return 0;
861 /******************************************************************
862 * WDML_MakeHszFromAtom
864 * Creates a HSZ from an existing global atom
865 * Generally used while receiving a global atom and transforming it
866 * into an HSZ
868 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
870 WCHAR nameBuffer[MAX_BUFFER_LEN];
872 if (!atom) return NULL;
874 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
876 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
877 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
879 WARN("ATOM 0x%x not found\n", atom);
880 return 0;
883 /******************************************************************
884 * WDML_IncHSZ
888 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
890 HSZNode* pNode;
892 pNode = WDML_FindNode(pInstance, hsz);
893 if (!pNode) return FALSE;
895 pNode->refCount++;
896 return TRUE;
899 /******************************************************************************
900 * WDML_DecHSZ (INTERNAL)
902 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
903 * of HSZ nodes
904 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
906 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
908 HSZNode* pPrev = NULL;
909 HSZNode* pCurrent;
911 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
913 /* If we found the node we were looking for and its ref count is one,
914 * we can remove it
916 if (pCurrent->hsz == hsz)
918 if (--pCurrent->refCount == 0)
920 if (pCurrent == pInstance->nodeList)
922 pInstance->nodeList = pCurrent->next;
924 else
926 pPrev->next = pCurrent->next;
928 HeapFree(GetProcessHeap(), 0, pCurrent);
929 DeleteAtom(HSZ2ATOM(hsz));
931 return TRUE;
934 WARN("HSZ %p not found\n", hsz);
936 return FALSE;
939 /******************************************************************************
940 * WDML_FreeAllHSZ (INTERNAL)
942 * Frees up all the strings still allocated in the list and
943 * remove all the nodes from the list of HSZ nodes.
945 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
947 /* Free any strings created in this instance.
949 while (pInstance->nodeList != NULL)
951 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
955 /******************************************************************************
956 * InsertHSZNode (INTERNAL)
958 * Insert a node to the head of the list.
960 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
962 if (hsz != 0)
964 HSZNode* pNew = NULL;
965 /* Create a new node for this HSZ.
967 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
968 if (pNew != NULL)
970 pNew->hsz = hsz;
971 pNew->next = pInstance->nodeList;
972 pNew->refCount = 1;
973 pInstance->nodeList = pNew;
975 else
977 ERR("Primary HSZ Node allocation failed - out of memory\n");
982 /******************************************************************
983 * WDML_QueryString
987 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
988 int codepage)
990 WCHAR pString[MAX_BUFFER_LEN];
991 int ret;
992 /* If psz is null, we have to return only the length
993 * of the string.
995 if (ptr == NULL)
997 ptr = pString;
998 cchMax = MAX_BUFFER_LEN;
1001 switch (codepage)
1003 case CP_WINANSI:
1004 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1005 break;
1006 case CP_WINUNICODE:
1007 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1008 break;
1009 default:
1010 ERR("Unknown code page %d\n", codepage);
1011 ret = 0;
1013 return ret;
1016 /*****************************************************************
1017 * DdeQueryStringA [USER32.@]
1019 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1021 DWORD ret = 0;
1022 WDML_INSTANCE* pInstance;
1024 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1026 /* First check instance
1028 pInstance = WDML_GetInstance(idInst);
1029 if (pInstance != NULL)
1031 if (iCodePage == 0) iCodePage = CP_WINANSI;
1032 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1035 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
1036 return ret;
1039 /*****************************************************************
1040 * DdeQueryStringW [USER32.@]
1043 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1045 DWORD ret = 0;
1046 WDML_INSTANCE* pInstance;
1048 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1050 /* First check instance
1052 pInstance = WDML_GetInstance(idInst);
1053 if (pInstance != NULL)
1055 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1056 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1059 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
1060 return ret;
1063 /******************************************************************
1064 * DML_CreateString
1068 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1070 HSZ hsz;
1072 switch (codepage)
1074 case CP_WINANSI:
1075 hsz = ATOM2HSZ(AddAtomA(ptr));
1076 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1077 break;
1078 case CP_WINUNICODE:
1079 hsz = ATOM2HSZ(AddAtomW(ptr));
1080 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1081 break;
1082 default:
1083 ERR("Unknown code page %d\n", codepage);
1084 return 0;
1086 WDML_InsertHSZNode(pInstance, hsz);
1087 return hsz;
1090 /*****************************************************************
1091 * DdeCreateStringHandleA [USER32.@]
1093 * See DdeCreateStringHandleW.
1095 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1097 HSZ hsz = 0;
1098 WDML_INSTANCE* pInstance;
1100 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1102 pInstance = WDML_GetInstance(idInst);
1103 if (pInstance)
1105 if (codepage == 0) codepage = CP_WINANSI;
1106 hsz = WDML_CreateString(pInstance, psz, codepage);
1109 return hsz;
1113 /******************************************************************************
1114 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1116 * PARAMS
1117 * idInst [I] Instance identifier
1118 * psz [I] Pointer to string
1119 * codepage [I] Code page identifier
1120 * RETURNS
1121 * Success: String handle
1122 * Failure: 0
1124 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1126 WDML_INSTANCE* pInstance;
1127 HSZ hsz = 0;
1129 TRACE("(%d,%s,%d)\n", idInst, debugstr_w(psz), codepage);
1131 pInstance = WDML_GetInstance(idInst);
1132 if (pInstance)
1134 if (codepage == 0) codepage = CP_WINUNICODE;
1135 hsz = WDML_CreateString(pInstance, psz, codepage);
1138 return hsz;
1141 /*****************************************************************
1142 * DdeFreeStringHandle (USER32.@)
1143 * RETURNS
1144 * success: nonzero
1145 * fail: zero
1147 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1149 WDML_INSTANCE* pInstance;
1150 BOOL ret = FALSE;
1152 TRACE("(%d,%p):\n", idInst, hsz);
1154 /* First check instance
1156 pInstance = WDML_GetInstance(idInst);
1157 if (pInstance)
1158 ret = WDML_DecHSZ(pInstance, hsz);
1160 return ret;
1163 /*****************************************************************
1164 * DdeKeepStringHandle (USER32.@)
1166 * RETURNS
1167 * success: nonzero
1168 * fail: zero
1170 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1172 WDML_INSTANCE* pInstance;
1173 BOOL ret = FALSE;
1175 TRACE("(%d,%p):\n", idInst, hsz);
1177 /* First check instance
1179 pInstance = WDML_GetInstance(idInst);
1180 if (pInstance)
1181 ret = WDML_IncHSZ(pInstance, hsz);
1183 return ret;
1186 /*****************************************************************
1187 * DdeCmpStringHandles (USER32.@)
1189 * Compares the value of two string handles. This comparison is
1190 * not case sensitive.
1192 * PARAMS
1193 * hsz1 [I] Handle to the first string
1194 * hsz2 [I] Handle to the second string
1196 * RETURNS
1197 * -1 The value of hsz1 is zero or less than hsz2
1198 * 0 The values of hsz 1 and 2 are the same or both zero.
1199 * 1 The value of hsz2 is zero of less than hsz1
1201 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1203 WCHAR psz1[MAX_BUFFER_LEN];
1204 WCHAR psz2[MAX_BUFFER_LEN];
1205 int ret = 0;
1206 int ret1, ret2;
1208 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1209 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1211 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1213 /* Make sure we found both strings. */
1214 if (ret1 == 0 && ret2 == 0)
1216 /* If both are not found, return both "zero strings". */
1217 ret = 0;
1219 else if (ret1 == 0)
1221 /* If hsz1 is a not found, return hsz1 is "zero string". */
1222 ret = -1;
1224 else if (ret2 == 0)
1226 /* If hsz2 is a not found, return hsz2 is "zero string". */
1227 ret = 1;
1229 else
1231 /* Compare the two strings we got (case insensitive). */
1232 ret = lstrcmpiW(psz1, psz2);
1233 /* Since strcmp returns any number smaller than
1234 * 0 when the first string is found to be less than
1235 * the second one we must make sure we are returning
1236 * the proper values.
1238 if (ret < 0)
1240 ret = -1;
1242 else if (ret > 0)
1244 ret = 1;
1248 return ret;
1251 /* ================================================================
1253 * Data handle management
1255 * ================================================================ */
1257 /*****************************************************************
1258 * DdeCreateDataHandle (USER32.@)
1260 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1261 HSZ hszItem, UINT wFmt, UINT afCmd)
1263 /* For now, we ignore idInst, hszItem.
1264 * The purpose of these arguments still need to be investigated.
1267 HGLOBAL hMem;
1268 LPBYTE pByte;
1269 DDE_DATAHANDLE_HEAD* pDdh;
1270 WCHAR psz[MAX_BUFFER_LEN];
1272 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1274 psz[0] = HSZ2ATOM(hszItem);
1275 psz[1] = 0;
1278 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1279 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1281 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1282 return 0;
1284 /* we use the first 4 bytes to store the size */
1285 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1287 ERR("GlobalAlloc failed\n");
1288 return 0;
1291 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1292 if (!pDdh)
1294 GlobalFree(hMem);
1295 return 0;
1298 pDdh->cfFormat = wFmt;
1299 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1301 pByte = (LPBYTE)(pDdh + 1);
1302 if (pSrc)
1304 memcpy(pByte, pSrc + cbOff, cb);
1306 GlobalUnlock(hMem);
1308 TRACE("=> %p\n", hMem);
1309 return (HDDEDATA)hMem;
1312 /*****************************************************************
1314 * DdeAddData (USER32.@)
1316 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1318 DWORD old_sz, new_sz;
1319 LPBYTE pDst;
1321 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1323 pDst = DdeAccessData(hData, &old_sz);
1324 if (!pDst) return 0;
1326 new_sz = cb + cbOff;
1327 if (new_sz > old_sz)
1329 DdeUnaccessData(hData);
1330 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1331 GMEM_MOVEABLE | GMEM_DDESHARE);
1332 pDst = DdeAccessData(hData, &old_sz);
1335 if (!pDst) return 0;
1337 memcpy(pDst + cbOff, pSrc, cb);
1338 DdeUnaccessData(hData);
1339 return hData;
1342 /******************************************************************************
1343 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1346 * PARAMS
1347 * hData [I] Handle to DDE object
1348 * pDst [I] Pointer to destination buffer
1349 * cbMax [I] Amount of data to copy
1350 * cbOff [I] Offset to beginning of data
1352 * RETURNS
1353 * Size of memory object associated with handle
1355 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1357 DWORD dwSize, dwRet;
1358 LPBYTE pByte;
1360 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1362 pByte = DdeAccessData(hData, &dwSize);
1364 if (pByte)
1366 if (!pDst)
1368 dwRet = dwSize;
1370 else if (cbOff + cbMax < dwSize)
1372 dwRet = cbMax;
1374 else if (cbOff < dwSize)
1376 dwRet = dwSize - cbOff;
1378 else
1380 dwRet = 0;
1382 if (pDst && dwRet != 0)
1384 memcpy(pDst, pByte + cbOff, dwRet);
1386 DdeUnaccessData(hData);
1388 else
1390 dwRet = 0;
1392 return dwRet;
1395 /*****************************************************************
1396 * DdeAccessData (USER32.@)
1398 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1400 HGLOBAL hMem = (HGLOBAL)hData;
1401 DDE_DATAHANDLE_HEAD* pDdh;
1403 TRACE("(%p,%p)\n", hData, pcbDataSize);
1405 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1406 if (pDdh == NULL)
1408 ERR("Failed on GlobalLock(%p)\n", hMem);
1409 return 0;
1412 if (pcbDataSize != NULL)
1414 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1416 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1417 return (LPBYTE)(pDdh + 1);
1420 /*****************************************************************
1421 * DdeUnaccessData (USER32.@)
1423 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1425 HGLOBAL hMem = (HGLOBAL)hData;
1427 TRACE("(%p)\n", hData);
1429 GlobalUnlock(hMem);
1431 return TRUE;
1434 /*****************************************************************
1435 * DdeFreeDataHandle (USER32.@)
1437 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1439 TRACE("(%p)\n", hData);
1440 return GlobalFree((HGLOBAL)hData) == 0;
1443 /******************************************************************
1444 * WDML_IsAppOwned
1448 BOOL WDML_IsAppOwned(HDDEDATA hData)
1450 DDE_DATAHANDLE_HEAD* pDdh;
1451 BOOL ret = FALSE;
1453 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1454 if (pDdh != NULL)
1456 ret = pDdh->bAppOwned;
1457 GlobalUnlock((HGLOBAL)hData);
1459 return ret;
1462 /* ================================================================
1464 * Global <=> Data handle management
1466 * ================================================================ */
1468 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1469 * offset size
1470 * (bytes) (bits) comment
1471 * 0 16 bit fields for options (release, ackreq, response...)
1472 * 2 16 clipboard format
1473 * 4 ? data to be used
1475 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1477 DDEDATA* pDd;
1478 HDDEDATA ret = 0;
1479 DWORD size;
1481 if (hMem)
1483 pDd = GlobalLock(hMem);
1484 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1485 if (pDd)
1487 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1488 switch (pDd->cfFormat)
1490 default:
1491 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1492 pDd->cfFormat, hMem);
1493 /* fall thru */
1494 case 0:
1495 case CF_TEXT:
1496 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1497 break;
1498 case CF_BITMAP:
1499 if (size >= sizeof(BITMAP))
1501 BITMAP* bmp = (BITMAP*)pDd->Value;
1502 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1503 if (size >= sizeof(BITMAP) + count)
1505 HBITMAP hbmp;
1507 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1508 bmp->bmPlanes, bmp->bmBitsPixel,
1509 pDd->Value + sizeof(BITMAP))))
1511 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1512 0, 0, CF_BITMAP, 0);
1514 else ERR("Can't create bmp\n");
1516 else
1518 ERR("Wrong count: %u / %d\n", size, count);
1520 } else ERR("No bitmap header\n");
1521 break;
1523 GlobalUnlock(hMem);
1526 return ret;
1529 /******************************************************************
1530 * WDML_DataHandle2Global
1534 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1535 BOOL fDeferUpd, BOOL fAckReq)
1537 DDE_DATAHANDLE_HEAD* pDdh;
1538 DWORD dwSize;
1539 HGLOBAL hMem = 0;
1541 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1542 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1543 if (dwSize && pDdh)
1545 WINE_DDEHEAD* wdh = NULL;
1547 switch (pDdh->cfFormat)
1549 default:
1550 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1551 pDdh->cfFormat, hDdeData);
1552 /* fall thru */
1553 case 0:
1554 case CF_TEXT:
1555 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1556 if (hMem && (wdh = GlobalLock(hMem)))
1558 memcpy(wdh + 1, pDdh + 1, dwSize);
1560 break;
1561 case CF_BITMAP:
1562 if (dwSize >= sizeof(HBITMAP))
1564 BITMAP bmp;
1565 DWORD count;
1566 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1568 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1570 count = bmp.bmWidthBytes * bmp.bmHeight;
1571 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1572 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1573 if (hMem && (wdh = GlobalLock(hMem)))
1575 memcpy(wdh + 1, &bmp, sizeof(bmp));
1576 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1580 break;
1582 if (wdh)
1584 wdh->unused = 0;
1585 wdh->fResponse = fResponse;
1586 wdh->fRelease = fRelease;
1587 wdh->fDeferUpd = fDeferUpd;
1588 wdh->fAckReq = fAckReq;
1589 wdh->cfFormat = pDdh->cfFormat;
1590 GlobalUnlock(hMem);
1592 GlobalUnlock((HGLOBAL)hDdeData);
1595 return hMem;
1598 /* ================================================================
1600 * Server management
1602 * ================================================================ */
1604 /******************************************************************
1605 * WDML_AddServer
1609 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1611 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1612 WDML_SERVER* pServer;
1613 WCHAR buf1[256];
1614 WCHAR buf2[256];
1616 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1617 if (pServer == NULL) return NULL;
1619 pServer->hszService = hszService;
1620 WDML_IncHSZ(pInstance, hszService);
1622 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1623 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1624 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1626 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1627 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1629 pServer->filterOn = TRUE;
1631 pServer->next = pInstance->servers;
1632 pInstance->servers = pServer;
1633 return pServer;
1636 /******************************************************************
1637 * WDML_RemoveServer
1641 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1643 WDML_SERVER* pPrev = NULL;
1644 WDML_SERVER* pServer = NULL;
1645 WDML_CONV* pConv;
1646 WDML_CONV* pConvNext;
1648 pServer = pInstance->servers;
1650 while (pServer != NULL)
1652 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1654 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1655 pServer->atomService, pServer->atomServiceSpec);
1656 /* terminate all conversations for given topic */
1657 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1659 pConvNext = pConv->next;
1660 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1662 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1663 /* don't care about return code (whether client window is present or not) */
1664 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
1667 if (pServer == pInstance->servers)
1669 pInstance->servers = pServer->next;
1671 else
1673 pPrev->next = pServer->next;
1676 DestroyWindow(pServer->hwndServer);
1677 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1678 WDML_DecHSZ(pInstance, pServer->hszService);
1680 GlobalDeleteAtom(pServer->atomService);
1681 GlobalDeleteAtom(pServer->atomServiceSpec);
1683 HeapFree(GetProcessHeap(), 0, pServer);
1684 break;
1687 pPrev = pServer;
1688 pServer = pServer->next;
1692 /*****************************************************************************
1693 * WDML_FindServer
1695 * generic routine to return a pointer to the relevant ServiceNode
1696 * for a given service name, or NULL if the entry does not exist
1699 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1701 WDML_SERVER* pServer;
1703 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1705 if (hszService == pServer->hszService)
1707 return pServer;
1710 TRACE("Service name missing\n");
1711 return NULL;
1714 /* ================================================================
1716 * Conversation management
1718 * ================================================================ */
1720 /******************************************************************
1721 * WDML_AddConv
1725 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1726 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1728 WDML_CONV* pConv;
1730 /* no converstation yet, add it */
1731 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1732 if (!pConv) return NULL;
1734 pConv->instance = pInstance;
1735 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1736 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1737 pConv->magic = WDML_CONV_MAGIC;
1738 pConv->hwndServer = hwndServer;
1739 pConv->hwndClient = hwndClient;
1740 pConv->transactions = NULL;
1741 pConv->hUser = 0;
1742 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1743 pConv->wStatus |= pInstance->wStatus;
1744 /* check if both side of the conversation are of the same instance */
1745 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1746 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1748 pConv->wStatus |= ST_ISSELF;
1750 pConv->wConvst = XST_NULL;
1752 pConv->next = pInstance->convs[side];
1753 pInstance->convs[side] = pConv;
1755 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1757 return pConv;
1760 /******************************************************************
1761 * WDML_FindConv
1765 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1766 HSZ hszService, HSZ hszTopic)
1768 WDML_CONV* pCurrent = NULL;
1770 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1772 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1773 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1775 return pCurrent;
1779 return NULL;
1782 /******************************************************************
1783 * WDML_RemoveConv
1787 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1789 WDML_CONV* pPrev = NULL;
1790 WDML_CONV* pCurrent;
1791 WDML_XACT* pXAct;
1792 WDML_XACT* pXActNext;
1793 HWND hWnd;
1795 if (!pRef)
1796 return;
1798 /* remove any pending transaction */
1799 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1801 pXActNext = pXAct->next;
1802 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1805 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1807 /* FIXME: should we keep the window around ? it seems so (at least on client side
1808 * to let QueryConvInfo work after conv termination, but also to implement
1809 * DdeReconnect...
1811 /* destroy conversation window, but first remove pConv from hWnd.
1812 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1814 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1815 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1817 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1819 WDML_DecHSZ(pRef->instance, pRef->hszService);
1820 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1822 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1824 if (pCurrent == pRef)
1826 if (pCurrent == pRef->instance->convs[side])
1828 pRef->instance->convs[side] = pCurrent->next;
1830 else
1832 pPrev->next = pCurrent->next;
1834 pCurrent->magic = 0;
1835 HeapFree(GetProcessHeap(), 0, pCurrent);
1836 break;
1841 /******************************************************************
1842 * WDML_EnableCallback
1844 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1846 if (wCmd == EC_DISABLE)
1848 pConv->wStatus |= ST_BLOCKED;
1849 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
1850 return TRUE;
1853 if (wCmd == EC_QUERYWAITING)
1854 return pConv->transactions ? TRUE : FALSE;
1856 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1858 FIXME("Unknown command code %04x\n", wCmd);
1859 return FALSE;
1862 if (wCmd == EC_ENABLEALL)
1864 pConv->wStatus &= ~ST_BLOCKED;
1865 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
1868 while (pConv->transactions)
1870 WDML_XACT *pXAct = pConv->transactions;
1872 if (pConv->wStatus & ST_CLIENT)
1874 /* transaction should be in the queue until handled */
1875 WDML_ClientHandle(pConv, pXAct, 0, NULL);
1876 WDML_UnQueueTransaction(pConv, pXAct);
1878 else
1880 /* transaction should be removed from the queue before handling */
1881 WDML_UnQueueTransaction(pConv, pXAct);
1882 WDML_ServerHandle(pConv, pXAct);
1885 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1887 if (wCmd == EC_ENABLEONE) break;
1889 return TRUE;
1892 /*****************************************************************
1893 * DdeEnableCallback (USER32.@)
1895 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1897 BOOL ret = FALSE;
1898 WDML_CONV *pConv;
1900 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
1902 if (hConv)
1904 pConv = WDML_GetConv(hConv, TRUE);
1906 if (pConv && pConv->instance->instanceID == idInst)
1907 ret = WDML_EnableCallback(pConv, wCmd);
1909 else
1911 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
1913 if (!pInstance)
1914 return FALSE;
1916 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
1917 pInstance->wStatus |= wCmd;
1919 if (wCmd == EC_DISABLE)
1921 pInstance->wStatus |= ST_BLOCKED;
1922 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1924 else if (wCmd == EC_ENABLEALL)
1926 pInstance->wStatus &= ~ST_BLOCKED;
1927 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1930 ret = TRUE;
1932 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
1934 ret = WDML_EnableCallback(pConv, wCmd);
1935 if (ret && wCmd == EC_QUERYWAITING) break;
1939 return ret;
1942 /******************************************************************
1943 * WDML_GetConv
1947 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1949 WDML_CONV* pConv = (WDML_CONV*)hConv;
1951 /* FIXME: should do better checking */
1952 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1954 if (!pConv->instance || pConv->instance->threadID != GetCurrentThreadId())
1956 WARN("wrong thread ID\n");
1957 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
1958 return NULL;
1961 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1963 WARN("found conv but ain't connected\n");
1964 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
1965 return NULL;
1968 return pConv;
1971 /******************************************************************
1972 * WDML_GetConvFromWnd
1976 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1978 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
1981 /******************************************************************
1982 * WDML_PostAck
1986 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1987 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
1989 DDEACK ddeAck;
1990 HWND from, to;
1992 if (side == WDML_SERVER_SIDE)
1994 from = pConv->hwndServer;
1995 to = pConv->hwndClient;
1997 else
1999 to = pConv->hwndServer;
2000 from = pConv->hwndClient;
2003 ddeAck.bAppReturnCode = appRetCode;
2004 ddeAck.reserved = 0;
2005 ddeAck.fBusy = fBusy;
2006 ddeAck.fAck = fAck;
2008 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2010 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2011 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2012 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2014 pConv->wStatus &= ~ST_CONNECTED;
2015 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2016 FreeDDElParam(WM_DDE_ACK, lParam);
2017 return FALSE;
2019 return TRUE;
2022 /*****************************************************************
2023 * DdeSetUserHandle (USER32.@)
2025 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2027 WDML_CONV* pConv;
2029 TRACE("(%p,%x,%x)\n", hConv, id, hUser);
2031 pConv = WDML_GetConv(hConv, FALSE);
2032 if (pConv == NULL)
2033 return FALSE;
2035 if (id == QID_SYNC)
2037 pConv->hUser = hUser;
2039 else
2041 WDML_XACT* pXAct;
2043 pXAct = WDML_FindTransaction(pConv, id);
2044 if (pXAct)
2046 pXAct->hUser = hUser;
2048 else
2050 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2051 return FALSE;
2054 return TRUE;
2057 /******************************************************************
2058 * WDML_GetLocalConvInfo
2062 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2064 BOOL ret = TRUE;
2065 WDML_LINK* pLink;
2066 WDML_SIDE side;
2068 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2069 ci->hszSvcPartner = pConv->hszService;
2070 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2071 ci->hszTopic = pConv->hszTopic;
2072 ci->wStatus = pConv->wStatus;
2074 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2076 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2078 if (pLink->hConv == (HCONV)pConv)
2080 ci->wStatus |= ST_ADVISE;
2081 break;
2085 /* FIXME: non handled status flags:
2086 ST_BLOCKED
2087 ST_BLOCKNEXT
2088 ST_INLIST
2091 ci->wConvst = pConv->wConvst; /* FIXME */
2093 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2094 ci->hConvList = 0;
2095 ci->ConvCtxt = pConv->convContext;
2096 if (ci->wStatus & ST_CLIENT)
2098 ci->hwnd = pConv->hwndClient;
2099 ci->hwndPartner = pConv->hwndServer;
2101 else
2103 ci->hwnd = pConv->hwndServer;
2104 ci->hwndPartner = pConv->hwndClient;
2106 if (id == QID_SYNC)
2108 ci->hUser = pConv->hUser;
2109 ci->hszItem = 0;
2110 ci->wFmt = 0;
2111 ci->wType = 0;
2113 else
2115 WDML_XACT* pXAct;
2117 pXAct = WDML_FindTransaction(pConv, id);
2118 if (pXAct)
2120 ci->hUser = pXAct->hUser;
2121 ci->hszItem = pXAct->hszItem;
2122 ci->wFmt = pXAct->wFmt;
2123 ci->wType = pXAct->wType;
2125 else
2127 ret = 0;
2128 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2131 return ret;
2134 /******************************************************************
2135 * DdeQueryConvInfo (USER32.@)
2137 * FIXME: Set last DDE error on failure.
2139 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2141 UINT ret = lpConvInfo->cb;
2142 CONVINFO ci;
2143 WDML_CONV* pConv;
2145 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2147 if (!hConv)
2149 FIXME("hConv is NULL\n");
2150 return 0;
2153 pConv = WDML_GetConv(hConv, FALSE);
2154 if (pConv != NULL)
2156 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2157 ret = 0;
2159 else
2161 if ((ULONG_PTR)hConv & 1)
2163 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2164 if (pConv != NULL)
2165 FIXME("Request on remote conversation information is not implemented yet\n");
2167 ret = 0;
2170 if (ret != 0)
2171 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2172 return ret;
2175 /* ================================================================
2177 * Link (hot & warm) management
2179 * ================================================================ */
2181 /******************************************************************
2182 * WDML_AddLink
2186 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2187 UINT wType, HSZ hszItem, UINT wFmt)
2189 WDML_LINK* pLink;
2191 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2192 if (pLink == NULL)
2194 ERR("OOM\n");
2195 return;
2198 pLink->hConv = hConv;
2199 pLink->transactionType = wType;
2200 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2201 pLink->uFmt = wFmt;
2202 pLink->next = pInstance->links[side];
2203 pInstance->links[side] = pLink;
2206 /******************************************************************
2207 * WDML_RemoveLink
2211 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2212 HSZ hszItem, UINT uFmt)
2214 WDML_LINK* pPrev = NULL;
2215 WDML_LINK* pCurrent = NULL;
2217 pCurrent = pInstance->links[side];
2219 while (pCurrent != NULL)
2221 if (pCurrent->hConv == hConv &&
2222 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2223 pCurrent->uFmt == uFmt)
2225 if (pCurrent == pInstance->links[side])
2227 pInstance->links[side] = pCurrent->next;
2229 else
2231 pPrev->next = pCurrent->next;
2234 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2235 HeapFree(GetProcessHeap(), 0, pCurrent);
2236 break;
2239 pPrev = pCurrent;
2240 pCurrent = pCurrent->next;
2244 /* this function is called to remove all links related to the conv.
2245 It should be called from both client and server when terminating
2246 the conversation.
2248 /******************************************************************
2249 * WDML_RemoveAllLinks
2253 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2255 WDML_LINK* pPrev = NULL;
2256 WDML_LINK* pCurrent = NULL;
2257 WDML_LINK* pNext = NULL;
2259 pCurrent = pInstance->links[side];
2261 while (pCurrent != NULL)
2263 if (pCurrent->hConv == (HCONV)pConv)
2265 if (pCurrent == pInstance->links[side])
2267 pInstance->links[side] = pCurrent->next;
2268 pNext = pCurrent->next;
2270 else
2272 pPrev->next = pCurrent->next;
2273 pNext = pCurrent->next;
2276 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2278 HeapFree(GetProcessHeap(), 0, pCurrent);
2279 pCurrent = NULL;
2282 if (pCurrent)
2284 pPrev = pCurrent;
2285 pCurrent = pCurrent->next;
2287 else
2289 pCurrent = pNext;
2294 /******************************************************************
2295 * WDML_FindLink
2299 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2300 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2302 WDML_LINK* pCurrent = NULL;
2304 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2306 /* we don't need to check for transaction type as it can be altered */
2308 if (pCurrent->hConv == hConv &&
2309 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2310 (!use_fmt || pCurrent->uFmt == uFmt))
2312 break;
2317 return pCurrent;
2320 /* ================================================================
2322 * Transaction management
2324 * ================================================================ */
2326 /******************************************************************
2327 * WDML_AllocTransaction
2329 * Alloc a transaction structure for handling the message ddeMsg
2331 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2332 UINT wFmt, HSZ hszItem)
2334 WDML_XACT* pXAct;
2335 static WORD tid = 1; /* FIXME: wrap around */
2337 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2338 if (!pXAct)
2340 pInstance->lastError = DMLERR_MEMORY_ERROR;
2341 return NULL;
2344 pXAct->xActID = tid++;
2345 pXAct->ddeMsg = ddeMsg;
2346 pXAct->hDdeData = 0;
2347 pXAct->hUser = 0;
2348 pXAct->next = NULL;
2349 pXAct->wType = 0;
2350 pXAct->wFmt = wFmt;
2351 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2352 pXAct->atom = 0;
2353 pXAct->hMem = 0;
2354 pXAct->lParam = 0;
2356 return pXAct;
2359 /******************************************************************
2360 * WDML_QueueTransaction
2362 * Adds a transaction to the list of transaction
2364 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2366 WDML_XACT** pt;
2368 /* advance to last in queue */
2369 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2370 *pt = pXAct;
2373 /******************************************************************
2374 * WDML_UnQueueTransaction
2378 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2380 WDML_XACT** pt;
2382 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2384 if (*pt == pXAct)
2386 *pt = pXAct->next;
2387 return TRUE;
2390 return FALSE;
2393 /******************************************************************
2394 * WDML_FreeTransaction
2398 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2400 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2401 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2403 GlobalFree(pXAct->hMem);
2405 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2407 HeapFree(GetProcessHeap(), 0, pXAct);
2410 /******************************************************************
2411 * WDML_FindTransaction
2415 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2417 WDML_XACT* pXAct;
2419 tid = HIWORD(tid);
2420 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2422 if (pXAct->xActID == tid)
2423 break;
2425 return pXAct;
2428 /* ================================================================
2430 * Information broadcast across DDEML implementations
2432 * ================================================================ */
2434 struct tagWDML_BroadcastPmt
2436 LPCWSTR clsName;
2437 UINT uMsg;
2438 WPARAM wParam;
2439 LPARAM lParam;
2442 /******************************************************************
2443 * WDML_BroadcastEnumProc
2447 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2449 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2450 WCHAR buffer[128];
2452 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2453 lstrcmpiW(buffer, s->clsName) == 0)
2455 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2457 return TRUE;
2460 /******************************************************************
2461 * WDML_BroadcastDDEWindows
2465 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2467 struct tagWDML_BroadcastPmt s;
2469 s.clsName = clsName;
2470 s.uMsg = uMsg;
2471 s.wParam = wParam;
2472 s.lParam = lParam;
2473 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);