Fix a typo in ReuseDDElParam: GlobalLock supposed to be GlobalUnlock.
[wine/multimedia.git] / dlls / user / dde_misc.c
blob9af48e12bed47b78ddd81b7ebc99ba9f839d11f6
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "winerror.h"
37 #include "dde.h"
38 #include "ddeml.h"
39 #include "win.h"
40 #include "dde_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
46 /* convert between ATOM and HSZ avoiding compiler warnings */
47 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
48 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
50 static WDML_INSTANCE* WDML_InstanceList = NULL;
51 static DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
52 const WCHAR WDML_szEventClass[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
54 static CRITICAL_SECTION_DEBUG critsect_debug =
56 0, 0, &WDML_CritSect,
57 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
58 0, 0, { 0, (DWORD)(__FILE__ ": WDML_CritSect") }
60 CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
62 /* ================================================================
64 * Pure DDE (non DDEML) management
66 * ================================================================ */
69 /*****************************************************************
70 * PackDDElParam (USER32.@)
72 * RETURNS
73 * the packed lParam
75 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
77 HGLOBAL hMem;
78 UINT_PTR *params;
80 switch (msg)
82 case WM_DDE_ACK:
83 case WM_DDE_ADVISE:
84 case WM_DDE_DATA:
85 case WM_DDE_POKE:
86 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
88 ERR("GlobalAlloc failed\n");
89 return 0;
91 if (!(params = GlobalLock(hMem)))
93 ERR("GlobalLock failed (%p)\n", hMem);
94 return 0;
96 params[0] = uiLo;
97 params[1] = uiHi;
98 GlobalUnlock(hMem);
99 return (LPARAM)hMem;
101 case WM_DDE_EXECUTE:
102 return uiHi;
104 default:
105 return MAKELPARAM(uiLo, uiHi);
110 /*****************************************************************
111 * UnpackDDElParam (USER32.@)
113 * RETURNS
114 * success: nonzero
115 * failure: zero
117 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
118 PUINT_PTR uiLo, PUINT_PTR uiHi)
120 UINT_PTR *params;
122 switch (msg)
124 case WM_DDE_ACK:
125 case WM_DDE_ADVISE:
126 case WM_DDE_DATA:
127 case WM_DDE_POKE:
128 if (!lParam) return FALSE;
129 if (!(params = GlobalLock( (HGLOBAL)lParam )))
131 ERR("GlobalLock failed (%lx)\n", lParam);
132 return FALSE;
134 TRACE("unpacked: low %08x, high %08x\n", params[0], params[1]);
135 if (uiLo) *uiLo = params[0];
136 if (uiHi) *uiHi = params[1];
137 GlobalUnlock( (HGLOBAL)lParam );
138 return TRUE;
140 case WM_DDE_EXECUTE:
141 if (uiLo) *uiLo = 0;
142 if (uiHi) *uiHi = lParam;
143 return TRUE;
145 default:
146 if (uiLo) *uiLo = LOWORD(lParam);
147 if (uiHi) *uiHi = HIWORD(lParam);
148 return TRUE;
153 /*****************************************************************
154 * FreeDDElParam (USER32.@)
156 * RETURNS
157 * success: nonzero
158 * failure: zero
160 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
162 switch (msg)
164 case WM_DDE_ACK:
165 case WM_DDE_ADVISE:
166 case WM_DDE_DATA:
167 case WM_DDE_POKE:
168 /* first check if it's a global handle */
169 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
170 return !GlobalFree( (HGLOBAL)lParam );
172 default:
173 return TRUE;
178 /*****************************************************************
179 * ReuseDDElParam (USER32.@)
181 * RETURNS
182 * the packed lParam
184 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
185 UINT_PTR uiLo, UINT_PTR uiHi)
187 UINT_PTR *params;
189 switch (msgIn)
191 case WM_DDE_ACK:
192 case WM_DDE_ADVISE:
193 case WM_DDE_DATA:
194 case WM_DDE_POKE:
195 switch(msgOut)
197 case WM_DDE_ACK:
198 case WM_DDE_ADVISE:
199 case WM_DDE_DATA:
200 case WM_DDE_POKE:
201 if (!lParam) return 0;
202 if (!(params = GlobalLock( (HGLOBAL)lParam )))
204 ERR("GlobalLock failed\n");
205 return 0;
207 params[0] = uiLo;
208 params[1] = uiHi;
209 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
210 GlobalUnlock( (HGLOBAL)lParam );
211 return lParam;
213 case WM_DDE_EXECUTE:
214 FreeDDElParam( msgIn, lParam );
215 return uiHi;
217 default:
218 FreeDDElParam( msgIn, lParam );
219 return MAKELPARAM(uiLo, uiHi);
222 default:
223 return PackDDElParam( msgOut, uiLo, uiHi );
227 /*****************************************************************
228 * ImpersonateDdeClientWindow (USER32.@)
230 * PARAMS
231 * hWndClient [I] handle to DDE client window
232 * hWndServer [I] handle to DDE server window
234 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
236 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
237 return FALSE;
240 /*****************************************************************
241 * DdeSetQualityOfService (USER32.@)
244 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
245 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
247 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
248 return TRUE;
251 /* ================================================================
253 * Instance management
255 * ================================================================ */
257 /******************************************************************************
258 * IncrementInstanceId
260 * generic routine to increment the max instance Id and allocate a new application instance
262 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
264 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
266 pInstance->instanceID = id;
267 TRACE("New instance id %ld allocated\n", id);
270 /******************************************************************
271 * WDML_EventProc
275 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
277 WDML_INSTANCE* pInstance;
278 HSZ hsz1, hsz2;
280 switch (uMsg)
282 case WM_WDML_REGISTER:
283 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
284 /* try calling the Callback */
285 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
287 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
288 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
289 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
290 WDML_DecHSZ(pInstance, hsz1);
291 WDML_DecHSZ(pInstance, hsz2);
293 break;
295 case WM_WDML_UNREGISTER:
296 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
297 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
299 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
300 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
301 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
302 WDML_DecHSZ(pInstance, hsz1);
303 WDML_DecHSZ(pInstance, hsz2);
305 break;
307 case WM_WDML_CONNECT_CONFIRM:
308 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
309 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
311 WDML_CONV* pConv;
312 /* confirm connection...
313 * lookup for this conv handle
315 HWND client = WIN_GetFullHandle( (HWND)wParam );
316 HWND server = WIN_GetFullHandle( (HWND)lParam );
317 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
319 if (pConv->hwndClient == client && pConv->hwndServer == server)
320 break;
322 if (pConv)
324 pConv->wStatus |= ST_ISLOCAL;
326 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
327 pConv->hszTopic, pConv->hszService, 0, 0,
328 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
331 break;
332 default:
333 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
335 return 0;
338 /******************************************************************
339 * WDML_Initialize
343 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
344 DWORD afCmd, DWORD ulRes, BOOL b16)
346 WDML_INSTANCE* pInstance;
347 WDML_INSTANCE* reference_inst;
348 UINT ret;
349 WNDCLASSEXW wndclass;
351 TRACE("(%p,%p,0x%lx,%ld)\n",
352 pidInst, pfnCallback, afCmd, ulRes);
354 if (ulRes)
356 ERR("Reserved value not zero? What does this mean?\n");
357 /* trap this and no more until we know more */
358 return DMLERR_NO_ERROR;
361 /* grab enough heap for one control struct - not really necessary for re-initialise
362 * but allows us to use same validation routines */
363 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
364 if (pInstance == NULL)
366 /* catastrophe !! warn user & abort */
367 ERR("Instance create failed - out of memory\n");
368 return DMLERR_SYS_ERROR;
370 pInstance->next = NULL;
371 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
373 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
375 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
376 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
377 pInstance->threadID = GetCurrentThreadId();
378 pInstance->callback = *pfnCallback;
379 pInstance->win16 = b16;
380 pInstance->nodeList = NULL; /* node will be added later */
381 pInstance->monitorFlags = afCmd & MF_MASK;
382 pInstance->servers = NULL;
383 pInstance->convs[0] = NULL;
384 pInstance->convs[1] = NULL;
385 pInstance->links[0] = NULL;
386 pInstance->links[1] = NULL;
388 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
390 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
392 if (!pInstance->clientOnly)
394 /* Check for other way of setting Client-only !! */
395 pInstance->clientOnly =
396 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
399 TRACE("instance created - checking validity \n");
401 if (*pidInst == 0)
403 /* Initialisation of new Instance Identifier */
404 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
406 EnterCriticalSection(&WDML_CritSect);
408 if (WDML_InstanceList == NULL)
410 /* can't be another instance in this case, assign to the base pointer */
411 WDML_InstanceList = pInstance;
413 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
414 * present
415 * ------------------------------- NOTE NOTE NOTE --------------------------
417 * the manual is not clear if this condition
418 * applies to the first call to DdeInitialize from an application, or the
419 * first call for a given callback !!!
422 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
423 TRACE("First application instance detected OK\n");
424 /* allocate new instance ID */
425 WDML_IncrementInstanceId(pInstance);
427 else
429 /* really need to chain the new one in to the latest here, but after checking conditions
430 * such as trying to start a conversation from an application trying to monitor */
431 reference_inst = WDML_InstanceList;
432 TRACE("Subsequent application instance - starting checks\n");
433 while (reference_inst->next != NULL)
436 * This set of tests will work if application uses same instance Id
437 * at application level once allocated - which is what manual implies
438 * should happen. If someone tries to be
439 * clever (lazy ?) it will fail to pick up that later calls are for
440 * the same application - should we trust them ?
442 if (pInstance->instanceID == reference_inst->instanceID)
444 /* Check 1 - must be same Client-only state */
446 if (pInstance->clientOnly != reference_inst->clientOnly)
448 ret = DMLERR_DLL_USAGE;
449 goto theError;
452 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
454 if (pInstance->monitor != reference_inst->monitor)
456 ret = DMLERR_INVALIDPARAMETER;
457 goto theError;
460 /* Check 3 - must supply different callback address */
462 if (pInstance->callback == reference_inst->callback)
464 ret = DMLERR_DLL_USAGE;
465 goto theError;
468 reference_inst = reference_inst->next;
470 /* All cleared, add to chain */
472 TRACE("Application Instance checks finished\n");
473 WDML_IncrementInstanceId(pInstance);
474 reference_inst->next = pInstance;
476 LeaveCriticalSection(&WDML_CritSect);
478 *pidInst = pInstance->instanceID;
480 /* for deadlock issues, windows must always be created when outside the critical section */
481 wndclass.cbSize = sizeof(wndclass);
482 wndclass.style = 0;
483 wndclass.lpfnWndProc = WDML_EventProc;
484 wndclass.cbClsExtra = 0;
485 wndclass.cbWndExtra = sizeof(ULONG_PTR);
486 wndclass.hInstance = 0;
487 wndclass.hIcon = 0;
488 wndclass.hCursor = 0;
489 wndclass.hbrBackground = 0;
490 wndclass.lpszMenuName = NULL;
491 wndclass.lpszClassName = WDML_szEventClass;
492 wndclass.hIconSm = 0;
494 RegisterClassExW(&wndclass);
496 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
497 WS_POPUP, 0, 0, 0, 0,
498 0, 0, 0, 0);
500 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
502 TRACE("New application instance processing finished OK\n");
504 else
506 /* Reinitialisation situation --- FIX */
507 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
509 EnterCriticalSection(&WDML_CritSect);
511 if (WDML_InstanceList == NULL)
513 ret = DMLERR_INVALIDPARAMETER;
514 goto theError;
516 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
517 /* can't reinitialise if we have initialised nothing !! */
518 reference_inst = WDML_InstanceList;
519 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
521 * MS allows initialisation without specifying a callback, should we allow addition of the
522 * callback by a later call to initialise ? - if so this lot will have to change
524 while (reference_inst->next != NULL)
526 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
528 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
530 if (reference_inst->clientOnly)
532 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
534 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
536 if (!(afCmd & APPCMD_CLIENTONLY))
538 ret = DMLERR_INVALIDPARAMETER;
539 goto theError;
543 /* Check 2 - cannot change monitor modes */
545 if (pInstance->monitor != reference_inst->monitor)
547 ret = DMLERR_INVALIDPARAMETER;
548 goto theError;
551 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
553 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
555 ret = DMLERR_INVALIDPARAMETER;
556 goto theError;
558 break;
560 reference_inst = reference_inst->next;
562 if (reference_inst->next == NULL)
564 ret = DMLERR_INVALIDPARAMETER;
565 goto theError;
567 /* All checked - change relevant flags */
569 reference_inst->CBFflags = pInstance->CBFflags;
570 reference_inst->clientOnly = pInstance->clientOnly;
571 reference_inst->monitorFlags = pInstance->monitorFlags;
572 LeaveCriticalSection(&WDML_CritSect);
575 return DMLERR_NO_ERROR;
576 theError:
577 HeapFree(GetProcessHeap(), 0, pInstance);
578 LeaveCriticalSection(&WDML_CritSect);
579 return ret;
582 /******************************************************************************
583 * DdeInitializeA (USER32.@)
585 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
586 DWORD afCmd, DWORD ulRes)
588 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
591 /******************************************************************************
592 * DdeInitializeW [USER32.@]
593 * Registers an application with the DDEML
595 * PARAMS
596 * pidInst [I] Pointer to instance identifier
597 * pfnCallback [I] Pointer to callback function
598 * afCmd [I] Set of command and filter flags
599 * ulRes [I] Reserved
601 * RETURNS
602 * Success: DMLERR_NO_ERROR
603 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
605 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
606 DWORD afCmd, DWORD ulRes)
608 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
611 /*****************************************************************
612 * DdeUninitialize [USER32.@] Frees DDEML resources
614 * PARAMS
615 * idInst [I] Instance identifier
617 * RETURNS
618 * Success: TRUE
619 * Failure: FALSE
622 BOOL WINAPI DdeUninitialize(DWORD idInst)
624 /* Stage one - check if we have a handle for this instance
626 WDML_INSTANCE* pInstance;
627 WDML_CONV* pConv;
628 WDML_CONV* pConvNext;
630 TRACE("(%ld)\n", idInst);
632 EnterCriticalSection(&WDML_CritSect);
634 /* First check instance
636 pInstance = WDML_GetInstance(idInst);
637 if (pInstance == NULL)
639 LeaveCriticalSection(&WDML_CritSect);
641 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
643 return FALSE;
646 /* first terminate all conversations client side
647 * this shall close existing links...
649 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
651 pConvNext = pConv->next;
652 DdeDisconnect((HCONV)pConv);
654 if (pInstance->convs[WDML_CLIENT_SIDE])
655 FIXME("still pending conversations\n");
657 /* then unregister all known service names */
658 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
660 /* Free the nodes that were not freed by this instance
661 * and remove the nodes from the list of HSZ nodes.
663 WDML_FreeAllHSZ(pInstance);
665 DestroyWindow(pInstance->hwndEvent);
667 /* OK now delete the instance handle itself */
669 if (WDML_InstanceList == pInstance)
671 /* special case - the first/only entry */
672 WDML_InstanceList = pInstance->next;
674 else
676 /* general case, remove entry */
677 WDML_INSTANCE* inst;
679 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
680 inst->next = pInstance->next;
682 /* leave crit sect and release the heap entry
684 HeapFree(GetProcessHeap(), 0, pInstance);
685 LeaveCriticalSection(&WDML_CritSect);
686 return TRUE;
689 /******************************************************************
690 * WDML_NotifyThreadExit
694 void WDML_NotifyThreadDetach(void)
696 WDML_INSTANCE* pInstance;
697 WDML_INSTANCE* next;
698 DWORD tid = GetCurrentThreadId();
700 EnterCriticalSection(&WDML_CritSect);
701 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
703 next = pInstance->next;
704 if (pInstance->threadID == tid)
706 DdeUninitialize(pInstance->instanceID);
709 LeaveCriticalSection(&WDML_CritSect);
712 /******************************************************************
713 * WDML_InvokeCallback
717 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
718 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
719 ULONG_PTR dwData1, ULONG_PTR dwData2)
721 HDDEDATA ret;
723 if (pInstance == NULL)
724 return NULL;
725 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
726 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
727 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
728 if (pInstance->win16)
730 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
731 hsz1, hsz2, hdata, dwData1, dwData2);
733 else
735 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
737 TRACE("done => %p\n", ret);
738 return ret;
741 /*****************************************************************************
742 * WDML_GetInstance
744 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
745 * for an instance Id, or NULL if the entry does not exist
748 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
750 WDML_INSTANCE* pInstance;
752 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
754 if (pInstance->instanceID == instId)
756 if (GetCurrentThreadId() != pInstance->threadID)
758 FIXME("Tried to get instance from wrong thread\n");
759 continue;
761 return pInstance;
764 TRACE("Instance entry missing\n");
765 return NULL;
768 /******************************************************************
769 * WDML_GetInstanceFromWnd
773 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
775 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
778 /******************************************************************************
779 * DdeGetLastError [USER32.@] Gets most recent error code
781 * PARAMS
782 * idInst [I] Instance identifier
784 * RETURNS
785 * Last error code
787 UINT WINAPI DdeGetLastError(DWORD idInst)
789 DWORD error_code;
790 WDML_INSTANCE* pInstance;
792 EnterCriticalSection(&WDML_CritSect);
794 /* First check instance
796 pInstance = WDML_GetInstance(idInst);
797 if (pInstance == NULL)
799 error_code = DMLERR_INVALIDPARAMETER;
801 else
803 error_code = pInstance->lastError;
804 pInstance->lastError = 0;
807 LeaveCriticalSection(&WDML_CritSect);
808 return error_code;
811 /* ================================================================
813 * String management
815 * ================================================================ */
818 /******************************************************************
819 * WDML_FindNode
823 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
825 HSZNode* pNode;
827 if (pInstance == NULL) return NULL;
829 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
831 if (pNode->hsz == hsz) break;
833 if (!pNode) WARN("HSZ %p not found\n", hsz);
834 return pNode;
837 /******************************************************************
838 * WDML_MakeAtomFromHsz
840 * Creates a global atom from an existing HSZ
841 * Generally used before sending an HSZ as an atom to a remote app
843 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
845 WCHAR nameBuffer[MAX_BUFFER_LEN];
847 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
848 return GlobalAddAtomW(nameBuffer);
849 WARN("HSZ %p not found\n", hsz);
850 return 0;
853 /******************************************************************
854 * WDML_MakeHszFromAtom
856 * Creates a HSZ from an existing global atom
857 * Generally used while receiving a global atom and transforming it
858 * into an HSZ
860 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
862 WCHAR nameBuffer[MAX_BUFFER_LEN];
864 if (!atom) return NULL;
866 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
868 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
869 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
871 WARN("ATOM 0x%x not found\n", atom);
872 return 0;
875 /******************************************************************
876 * WDML_IncHSZ
880 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
882 HSZNode* pNode;
884 pNode = WDML_FindNode(pInstance, hsz);
885 if (!pNode) return FALSE;
887 pNode->refCount++;
888 return TRUE;
891 /******************************************************************************
892 * WDML_DecHSZ (INTERNAL)
894 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
895 * of HSZ nodes
896 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
898 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
900 HSZNode* pPrev = NULL;
901 HSZNode* pCurrent;
903 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
905 /* If we found the node we were looking for and its ref count is one,
906 * we can remove it
908 if (pCurrent->hsz == hsz)
910 if (--pCurrent->refCount == 0)
912 if (pCurrent == pInstance->nodeList)
914 pInstance->nodeList = pCurrent->next;
916 else
918 pPrev->next = pCurrent->next;
920 HeapFree(GetProcessHeap(), 0, pCurrent);
921 DeleteAtom(HSZ2ATOM(hsz));
923 return TRUE;
926 WARN("HSZ %p not found\n", hsz);
928 return FALSE;
931 /******************************************************************************
932 * WDML_FreeAllHSZ (INTERNAL)
934 * Frees up all the strings still allocated in the list and
935 * remove all the nodes from the list of HSZ nodes.
937 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
939 /* Free any strings created in this instance.
941 while (pInstance->nodeList != NULL)
943 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
947 /******************************************************************************
948 * InsertHSZNode (INTERNAL)
950 * Insert a node to the head of the list.
952 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
954 if (hsz != 0)
956 HSZNode* pNew = NULL;
957 /* Create a new node for this HSZ.
959 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
960 if (pNew != NULL)
962 pNew->hsz = hsz;
963 pNew->next = pInstance->nodeList;
964 pNew->refCount = 1;
965 pInstance->nodeList = pNew;
967 else
969 ERR("Primary HSZ Node allocation failed - out of memory\n");
974 /******************************************************************
975 * WDML_QueryString
979 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
980 int codepage)
982 WCHAR pString[MAX_BUFFER_LEN];
983 int ret;
984 /* If psz is null, we have to return only the length
985 * of the string.
987 if (ptr == NULL)
989 ptr = pString;
990 cchMax = MAX_BUFFER_LEN;
993 switch (codepage)
995 case CP_WINANSI:
996 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
997 break;
998 case CP_WINUNICODE:
999 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1000 break;
1001 default:
1002 ERR("Unknown code page %d\n", codepage);
1003 ret = 0;
1005 return ret;
1008 /*****************************************************************
1009 * DdeQueryStringA [USER32.@]
1011 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1013 DWORD ret = 0;
1014 WDML_INSTANCE* pInstance;
1016 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1018 EnterCriticalSection(&WDML_CritSect);
1020 /* First check instance
1022 pInstance = WDML_GetInstance(idInst);
1023 if (pInstance != NULL)
1025 if (iCodePage == 0) iCodePage = CP_WINANSI;
1026 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1028 LeaveCriticalSection(&WDML_CritSect);
1030 TRACE("returning %ld (%s)\n", ret, debugstr_a(psz));
1031 return ret;
1034 /*****************************************************************
1035 * DdeQueryStringW [USER32.@]
1038 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1040 DWORD ret = 0;
1041 WDML_INSTANCE* pInstance;
1043 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1045 EnterCriticalSection(&WDML_CritSect);
1047 /* First check instance
1049 pInstance = WDML_GetInstance(idInst);
1050 if (pInstance != NULL)
1052 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1053 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1055 LeaveCriticalSection(&WDML_CritSect);
1057 TRACE("returning %ld (%s)\n", ret, debugstr_w(psz));
1058 return ret;
1061 /******************************************************************
1062 * DML_CreateString
1066 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1068 HSZ hsz;
1070 switch (codepage)
1072 case CP_WINANSI:
1073 hsz = ATOM2HSZ(AddAtomA(ptr));
1074 TRACE("added atom %s with HSZ %p, \n", debugstr_a(ptr), hsz);
1075 break;
1076 case CP_WINUNICODE:
1077 hsz = ATOM2HSZ(AddAtomW(ptr));
1078 TRACE("added atom %s with HSZ %p, \n", debugstr_w(ptr), hsz);
1079 break;
1080 default:
1081 ERR("Unknown code page %d\n", codepage);
1082 return 0;
1084 WDML_InsertHSZNode(pInstance, hsz);
1085 return hsz;
1088 /*****************************************************************
1089 * DdeCreateStringHandleA [USER32.@]
1091 * RETURNS
1092 * Success: String handle
1093 * Failure: 0
1095 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1097 HSZ hsz = 0;
1098 WDML_INSTANCE* pInstance;
1100 TRACE("(%ld,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1102 EnterCriticalSection(&WDML_CritSect);
1104 pInstance = WDML_GetInstance(idInst);
1105 if (pInstance)
1107 if (codepage == 0) codepage = CP_WINANSI;
1108 hsz = WDML_CreateString(pInstance, psz, codepage);
1111 LeaveCriticalSection(&WDML_CritSect);
1112 return hsz;
1116 /******************************************************************************
1117 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1119 * PARAMS
1120 * idInst [I] Instance identifier
1121 * psz [I] Pointer to string
1122 * codepage [I] Code page identifier
1123 * RETURNS
1124 * Success: String handle
1125 * Failure: 0
1127 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1129 WDML_INSTANCE* pInstance;
1130 HSZ hsz = 0;
1132 TRACE("(%ld,%s,%d)\n", idInst, debugstr_w(psz), codepage);
1134 EnterCriticalSection(&WDML_CritSect);
1136 pInstance = WDML_GetInstance(idInst);
1137 if (pInstance)
1139 if (codepage == 0) codepage = CP_WINUNICODE;
1140 hsz = WDML_CreateString(pInstance, psz, codepage);
1142 LeaveCriticalSection(&WDML_CritSect);
1144 return hsz;
1147 /*****************************************************************
1148 * DdeFreeStringHandle (USER32.@)
1149 * RETURNS: success: nonzero
1150 * fail: zero
1152 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1154 WDML_INSTANCE* pInstance;
1155 BOOL ret = FALSE;
1157 TRACE("(%ld,%p): \n", idInst, hsz);
1159 EnterCriticalSection(&WDML_CritSect);
1161 /* First check instance
1163 pInstance = WDML_GetInstance(idInst);
1164 if (pInstance)
1165 ret = WDML_DecHSZ(pInstance, hsz);
1167 LeaveCriticalSection(&WDML_CritSect);
1169 return ret;
1172 /*****************************************************************
1173 * DdeKeepStringHandle (USER32.@)
1175 * RETURNS: success: nonzero
1176 * fail: zero
1178 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1180 WDML_INSTANCE* pInstance;
1181 BOOL ret = FALSE;
1183 TRACE("(%ld,%p): \n", idInst, hsz);
1185 EnterCriticalSection(&WDML_CritSect);
1187 /* First check instance
1189 pInstance = WDML_GetInstance(idInst);
1190 if (pInstance)
1191 ret = WDML_IncHSZ(pInstance, hsz);
1193 LeaveCriticalSection(&WDML_CritSect);
1194 return ret;
1197 /*****************************************************************
1198 * DdeCmpStringHandles (USER32.@)
1200 * Compares the value of two string handles. This comparison is
1201 * not case sensitive.
1203 * Returns:
1204 * -1 The value of hsz1 is zero or less than hsz2
1205 * 0 The values of hsz 1 and 2 are the same or both zero.
1206 * 1 The value of hsz2 is zero of less than hsz1
1208 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1210 WCHAR psz1[MAX_BUFFER_LEN];
1211 WCHAR psz2[MAX_BUFFER_LEN];
1212 int ret = 0;
1213 int ret1, ret2;
1215 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1216 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1218 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1220 /* Make sure we found both strings. */
1221 if (ret1 == 0 && ret2 == 0)
1223 /* If both are not found, return both "zero strings". */
1224 ret = 0;
1226 else if (ret1 == 0)
1228 /* If hsz1 is a not found, return hsz1 is "zero string". */
1229 ret = -1;
1231 else if (ret2 == 0)
1233 /* If hsz2 is a not found, return hsz2 is "zero string". */
1234 ret = 1;
1236 else
1238 /* Compare the two strings we got (case insensitive). */
1239 ret = lstrcmpiW(psz1, psz2);
1240 /* Since strcmp returns any number smaller than
1241 * 0 when the first string is found to be less than
1242 * the second one we must make sure we are returning
1243 * the proper values.
1245 if (ret < 0)
1247 ret = -1;
1249 else if (ret > 0)
1251 ret = 1;
1255 return ret;
1258 /* ================================================================
1260 * Data handle management
1262 * ================================================================ */
1264 /*****************************************************************
1265 * DdeCreateDataHandle (USER32.@)
1267 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1268 HSZ hszItem, UINT wFmt, UINT afCmd)
1270 /* For now, we ignore idInst, hszItem.
1271 * The purpose of these arguments still need to be investigated.
1274 HGLOBAL hMem;
1275 LPBYTE pByte;
1276 DDE_DATAHANDLE_HEAD* pDdh;
1277 WCHAR psz[MAX_BUFFER_LEN];
1279 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1281 psz[0] = HSZ2ATOM(hszItem);
1282 psz[1] = 0;
1285 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,fmt %04x,%x)\n",
1286 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1288 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1289 return 0;
1291 /* we use the first 4 bytes to store the size */
1292 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1294 ERR("GlobalAlloc failed\n");
1295 return 0;
1298 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1299 if (!pDdh)
1301 GlobalFree(hMem);
1302 return 0;
1305 pDdh->cfFormat = wFmt;
1306 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1308 pByte = (LPBYTE)(pDdh + 1);
1309 if (pSrc)
1311 memcpy(pByte, pSrc + cbOff, cb);
1313 GlobalUnlock(hMem);
1315 TRACE("=> %p\n", hMem);
1316 return (HDDEDATA)hMem;
1319 /*****************************************************************
1321 * DdeAddData (USER32.@)
1323 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1325 DWORD old_sz, new_sz;
1326 LPBYTE pDst;
1328 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData, pSrc, cb, cbOff);
1330 pDst = DdeAccessData(hData, &old_sz);
1331 if (!pDst) return 0;
1333 new_sz = cb + cbOff;
1334 if (new_sz > old_sz)
1336 DdeUnaccessData(hData);
1337 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1338 GMEM_MOVEABLE | GMEM_DDESHARE);
1339 pDst = DdeAccessData(hData, &old_sz);
1342 if (!pDst) return 0;
1344 memcpy(pDst + cbOff, pSrc, cb);
1345 DdeUnaccessData(hData);
1346 return hData;
1349 /******************************************************************************
1350 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1353 * PARAMS
1354 * hData [I] Handle to DDE object
1355 * pDst [I] Pointer to destination buffer
1356 * cbMax [I] Amount of data to copy
1357 * cbOff [I] Offset to beginning of data
1359 * RETURNS
1360 * Size of memory object associated with handle
1362 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1364 DWORD dwSize, dwRet;
1365 LPBYTE pByte;
1367 TRACE("(%p,%p,%ld,%ld)\n", hData, pDst, cbMax, cbOff);
1369 pByte = DdeAccessData(hData, &dwSize);
1371 if (pByte)
1373 if (!pDst)
1375 dwRet = dwSize;
1377 else if (cbOff + cbMax < dwSize)
1379 dwRet = cbMax;
1381 else if (cbOff < dwSize)
1383 dwRet = dwSize - cbOff;
1385 else
1387 dwRet = 0;
1389 if (pDst && dwRet != 0)
1391 memcpy(pDst, pByte + cbOff, dwRet);
1393 DdeUnaccessData(hData);
1395 else
1397 dwRet = 0;
1399 return dwRet;
1402 /*****************************************************************
1403 * DdeAccessData (USER32.@)
1405 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1407 HGLOBAL hMem = (HGLOBAL)hData;
1408 DDE_DATAHANDLE_HEAD* pDdh;
1410 TRACE("(%p,%p)\n", hData, pcbDataSize);
1412 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1413 if (pDdh == NULL)
1415 ERR("Failed on GlobalLock(%p)\n", hMem);
1416 return 0;
1419 if (pcbDataSize != NULL)
1421 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1423 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1424 return (LPBYTE)(pDdh + 1);
1427 /*****************************************************************
1428 * DdeUnaccessData (USER32.@)
1430 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1432 HGLOBAL hMem = (HGLOBAL)hData;
1434 TRACE("(%p)\n", hData);
1436 GlobalUnlock(hMem);
1438 return TRUE;
1441 /*****************************************************************
1442 * DdeFreeDataHandle (USER32.@)
1444 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1446 TRACE("(%p)\n", hData);
1447 return GlobalFree((HGLOBAL)hData) == 0;
1450 /******************************************************************
1451 * WDML_IsAppOwned
1455 BOOL WDML_IsAppOwned(HDDEDATA hData)
1457 DDE_DATAHANDLE_HEAD* pDdh;
1458 BOOL ret = FALSE;
1460 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1461 if (pDdh != NULL)
1463 ret = pDdh->bAppOwned;
1464 GlobalUnlock((HGLOBAL)hData);
1466 return ret;
1469 /* ================================================================
1471 * Global <=> Data handle management
1473 * ================================================================ */
1475 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1476 * offset size
1477 * (bytes) (bits) comment
1478 * 0 16 bit fields for options (release, ackreq, response...)
1479 * 2 16 clipboard format
1480 * 4 ? data to be used
1482 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1484 DDEDATA* pDd;
1485 HDDEDATA ret = 0;
1486 DWORD size;
1488 if (hMem)
1490 pDd = GlobalLock(hMem);
1491 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1492 if (pDd)
1494 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1495 switch (pDd->cfFormat)
1497 default:
1498 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1499 pDd->cfFormat, hMem);
1500 /* fall thru */
1501 case 0:
1502 case CF_TEXT:
1503 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1504 break;
1505 case CF_BITMAP:
1506 if (size >= sizeof(BITMAP))
1508 BITMAP* bmp = (BITMAP*)pDd->Value;
1509 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1510 if (size >= sizeof(BITMAP) + count)
1512 HBITMAP hbmp;
1514 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1515 bmp->bmPlanes, bmp->bmBitsPixel,
1516 pDd->Value + sizeof(BITMAP))))
1518 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1519 0, 0, CF_BITMAP, 0);
1521 else ERR("Can't create bmp\n");
1523 else
1525 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1527 } else ERR("No bitmap header\n");
1528 break;
1530 GlobalUnlock(hMem);
1533 return ret;
1536 /******************************************************************
1537 * WDML_DataHandle2Global
1541 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1542 BOOL fDeferUpd, BOOL fAckReq)
1544 DDE_DATAHANDLE_HEAD* pDdh;
1545 DWORD dwSize;
1546 HGLOBAL hMem = 0;
1548 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1549 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1550 if (dwSize && pDdh)
1552 WINE_DDEHEAD* wdh = NULL;
1554 switch (pDdh->cfFormat)
1556 default:
1557 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1558 pDdh->cfFormat, hDdeData);
1559 /* fall thru */
1560 case 0:
1561 case CF_TEXT:
1562 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1563 if (hMem && (wdh = GlobalLock(hMem)))
1565 memcpy(wdh + 1, pDdh + 1, dwSize);
1567 break;
1568 case CF_BITMAP:
1569 if (dwSize >= sizeof(HBITMAP))
1571 BITMAP bmp;
1572 DWORD count;
1573 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1575 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1577 count = bmp.bmWidthBytes * bmp.bmHeight;
1578 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1579 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1580 if (hMem && (wdh = GlobalLock(hMem)))
1582 memcpy(wdh + 1, &bmp, sizeof(bmp));
1583 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1587 break;
1589 if (wdh)
1591 wdh->unused = 0;
1592 wdh->fResponse = fResponse;
1593 wdh->fRelease = fRelease;
1594 wdh->fDeferUpd = fDeferUpd;
1595 wdh->fAckReq = fAckReq;
1596 wdh->cfFormat = pDdh->cfFormat;
1597 GlobalUnlock(hMem);
1599 GlobalUnlock((HGLOBAL)hDdeData);
1602 return hMem;
1605 /* ================================================================
1607 * Server management
1609 * ================================================================ */
1611 /******************************************************************
1612 * WDML_AddServer
1616 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1618 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1619 WDML_SERVER* pServer;
1620 WCHAR buf1[256];
1621 WCHAR buf2[256];
1623 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1624 if (pServer == NULL) return NULL;
1626 pServer->hszService = hszService;
1627 WDML_IncHSZ(pInstance, hszService);
1629 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1630 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1631 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1633 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1634 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1636 pServer->filterOn = TRUE;
1638 pServer->next = pInstance->servers;
1639 pInstance->servers = pServer;
1640 return pServer;
1643 /******************************************************************
1644 * WDML_RemoveServer
1648 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1650 WDML_SERVER* pPrev = NULL;
1651 WDML_SERVER* pServer = NULL;
1652 WDML_CONV* pConv;
1653 WDML_CONV* pConvNext;
1655 pServer = pInstance->servers;
1657 while (pServer != NULL)
1659 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1661 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1662 pServer->atomService, pServer->atomServiceSpec);
1663 /* terminate all conversations for given topic */
1664 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1666 pConvNext = pConv->next;
1667 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1669 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1670 /* don't care about return code (whether client window is present or not) */
1671 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
1674 if (pServer == pInstance->servers)
1676 pInstance->servers = pServer->next;
1678 else
1680 pPrev->next = pServer->next;
1683 DestroyWindow(pServer->hwndServer);
1684 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1685 WDML_DecHSZ(pInstance, pServer->hszService);
1687 GlobalDeleteAtom(pServer->atomService);
1688 GlobalDeleteAtom(pServer->atomServiceSpec);
1690 HeapFree(GetProcessHeap(), 0, pServer);
1691 break;
1694 pPrev = pServer;
1695 pServer = pServer->next;
1699 /*****************************************************************************
1700 * WDML_FindServer
1702 * generic routine to return a pointer to the relevant ServiceNode
1703 * for a given service name, or NULL if the entry does not exist
1706 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1708 WDML_SERVER* pServer;
1710 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1712 if (hszService == pServer->hszService)
1714 return pServer;
1717 TRACE("Service name missing\n");
1718 return NULL;
1721 /* ================================================================
1723 * Conversation management
1725 * ================================================================ */
1727 /******************************************************************
1728 * WDML_AddConv
1732 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1733 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1735 WDML_CONV* pConv;
1737 /* no converstation yet, add it */
1738 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1739 if (!pConv) return NULL;
1741 pConv->instance = pInstance;
1742 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1743 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1744 pConv->hwndServer = hwndServer;
1745 pConv->hwndClient = hwndClient;
1746 pConv->transactions = NULL;
1747 pConv->hUser = 0;
1748 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1749 /* check if both side of the conversation are of the same instance */
1750 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1751 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1753 pConv->wStatus |= ST_ISSELF;
1755 pConv->wConvst = XST_NULL;
1757 pConv->next = pInstance->convs[side];
1758 pInstance->convs[side] = pConv;
1760 return pConv;
1763 /******************************************************************
1764 * WDML_FindConv
1768 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1769 HSZ hszService, HSZ hszTopic)
1771 WDML_CONV* pCurrent = NULL;
1773 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1775 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1776 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1778 return pCurrent;
1782 return NULL;
1785 /******************************************************************
1786 * WDML_RemoveConv
1790 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1792 WDML_CONV* pPrev = NULL;
1793 WDML_CONV* pCurrent;
1794 WDML_XACT* pXAct;
1795 WDML_XACT* pXActNext;
1796 HWND hWnd;
1798 if (!pRef)
1799 return;
1801 /* remove any pending transaction */
1802 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1804 pXActNext = pXAct->next;
1805 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1808 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1810 /* FIXME: should we keep the window around ? it seems so (at least on client side
1811 * to let QueryConvInfo work after conv termination, but also to implement
1812 * DdeReconnect...
1814 /* destroy conversation window, but first remove pConv from hWnd.
1815 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1817 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1818 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1820 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1822 WDML_DecHSZ(pRef->instance, pRef->hszService);
1823 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1825 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1827 if (pCurrent == pRef)
1829 if (pCurrent == pRef->instance->convs[side])
1831 pRef->instance->convs[side] = pCurrent->next;
1833 else
1835 pPrev->next = pCurrent->next;
1838 HeapFree(GetProcessHeap(), 0, pCurrent);
1839 break;
1844 /******************************************************************
1845 * WDML_EnableCallback
1847 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1849 if (wCmd == EC_DISABLE)
1851 FIXME("EC_DISABLE is not implemented\n");
1852 return TRUE;
1855 if (wCmd == EC_QUERYWAITING)
1856 return pConv->transactions ? TRUE : FALSE;
1858 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1860 FIXME("Unknown command code %04x\n", wCmd);
1861 return FALSE;
1864 while (pConv->transactions)
1866 WDML_XACT *pXAct = pConv->transactions;
1867 WDML_UnQueueTransaction(pConv, pXAct);
1869 if (pConv->wStatus & ST_CLIENT)
1871 /*WDML_ClientHandle(pConv, pXAct);*/
1872 FIXME("Client delayed transaction queue handling is not supported\n");
1874 else
1875 WDML_ServerHandle(pConv, pXAct);
1877 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1879 if (wCmd == EC_ENABLEONE) break;
1881 return TRUE;
1884 /*****************************************************************
1885 * DdeEnableCallback (USER32.@)
1887 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1889 BOOL ret = FALSE;
1890 WDML_CONV *pConv;
1892 TRACE("(%ld, %p, %04x)\n", idInst, hConv, wCmd);
1894 EnterCriticalSection(&WDML_CritSect);
1896 pConv = WDML_GetConv(hConv, TRUE);
1898 if (pConv && pConv->instance->instanceID == idInst)
1899 ret = WDML_EnableCallback(pConv, wCmd);
1901 LeaveCriticalSection(&WDML_CritSect);
1902 return ret;
1905 /******************************************************************
1906 * WDML_GetConv
1910 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1912 WDML_CONV* pConv = (WDML_CONV*)hConv;
1914 /* FIXME: should do better checking */
1915 if (pConv == NULL) return NULL;
1917 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1919 FIXME("found conv but ain't connected\n");
1920 return NULL;
1922 if (GetCurrentThreadId() != pConv->instance->threadID)
1924 FIXME("wrong thread ID\n");
1925 return NULL;
1928 return pConv;
1931 /******************************************************************
1932 * WDML_GetConvFromWnd
1936 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1938 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
1941 /******************************************************************
1942 * WDML_PostAck
1946 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1947 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1949 DDEACK ddeAck;
1950 HWND from, to;
1952 if (side == WDML_SERVER_SIDE)
1954 from = pConv->hwndServer;
1955 to = pConv->hwndClient;
1957 else
1959 to = pConv->hwndServer;
1960 from = pConv->hwndClient;
1963 ddeAck.bAppReturnCode = appRetCode;
1964 ddeAck.reserved = 0;
1965 ddeAck.fBusy = fBusy;
1966 ddeAck.fAck = fAck;
1968 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1970 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1971 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1972 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
1974 pConv->wStatus &= ~ST_CONNECTED;
1975 FreeDDElParam(WM_DDE_ACK, lParam);
1976 return FALSE;
1978 return TRUE;
1981 /*****************************************************************
1982 * DdeSetUserHandle (USER32.@)
1984 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1986 WDML_CONV* pConv;
1987 BOOL ret = TRUE;
1989 TRACE("(%p,%lx,%lx)\n", hConv, id, hUser);
1991 EnterCriticalSection(&WDML_CritSect);
1993 pConv = WDML_GetConv(hConv, FALSE);
1994 if (pConv == NULL)
1996 ret = FALSE;
1997 goto theError;
1999 if (id == QID_SYNC)
2001 pConv->hUser = hUser;
2003 else
2005 WDML_XACT* pXAct;
2007 pXAct = WDML_FindTransaction(pConv, id);
2008 if (pXAct)
2010 pXAct->hUser = hUser;
2012 else
2014 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2015 ret = FALSE;
2018 theError:
2019 LeaveCriticalSection(&WDML_CritSect);
2020 return ret;
2023 /******************************************************************
2024 * WDML_GetLocalConvInfo
2028 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2030 BOOL ret = TRUE;
2031 WDML_LINK* pLink;
2032 WDML_SIDE side;
2034 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2035 ci->hszSvcPartner = pConv->hszService;
2036 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2037 ci->hszTopic = pConv->hszTopic;
2038 ci->wStatus = pConv->wStatus;
2040 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2042 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2044 if (pLink->hConv == (HCONV)pConv)
2046 ci->wStatus |= ST_ADVISE;
2047 break;
2051 /* FIXME: non handled status flags:
2052 ST_BLOCKED
2053 ST_BLOCKNEXT
2054 ST_INLIST
2057 ci->wConvst = pConv->wConvst; /* FIXME */
2059 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2060 ci->hConvList = 0;
2061 ci->ConvCtxt = pConv->convContext;
2062 if (ci->wStatus & ST_CLIENT)
2064 ci->hwnd = pConv->hwndClient;
2065 ci->hwndPartner = pConv->hwndServer;
2067 else
2069 ci->hwnd = pConv->hwndServer;
2070 ci->hwndPartner = pConv->hwndClient;
2072 if (id == QID_SYNC)
2074 ci->hUser = pConv->hUser;
2075 ci->hszItem = 0;
2076 ci->wFmt = 0;
2077 ci->wType = 0;
2079 else
2081 WDML_XACT* pXAct;
2083 pXAct = WDML_FindTransaction(pConv, id);
2084 if (pXAct)
2086 ci->hUser = pXAct->hUser;
2087 ci->hszItem = pXAct->hszItem;
2088 ci->wFmt = pXAct->wFmt;
2089 ci->wType = pXAct->wType;
2091 else
2093 ret = 0;
2094 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2097 return ret;
2100 /******************************************************************
2101 * DdeQueryConvInfo (USER32.@)
2104 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2106 UINT ret = lpConvInfo->cb;
2107 CONVINFO ci;
2108 WDML_CONV* pConv;
2110 TRACE("(%p,%lx,%p)\n", hConv, id, lpConvInfo);
2112 if (!hConv)
2114 FIXME("hConv is NULL\n");
2115 return 0;
2118 EnterCriticalSection(&WDML_CritSect);
2120 pConv = WDML_GetConv(hConv, FALSE);
2121 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2123 ret = 0;
2125 else if ((ULONG_PTR)hConv & 1)
2127 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2128 if (pConv != NULL)
2130 FIXME("Request on remote conversation information is not implemented yet\n");
2131 ret = 0;
2134 LeaveCriticalSection(&WDML_CritSect);
2135 if (ret != 0)
2136 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2137 return ret;
2140 /* ================================================================
2142 * Link (hot & warm) management
2144 * ================================================================ */
2146 /******************************************************************
2147 * WDML_AddLink
2151 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2152 UINT wType, HSZ hszItem, UINT wFmt)
2154 WDML_LINK* pLink;
2156 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2157 if (pLink == NULL)
2159 ERR("OOM\n");
2160 return;
2163 pLink->hConv = hConv;
2164 pLink->transactionType = wType;
2165 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2166 pLink->uFmt = wFmt;
2167 pLink->next = pInstance->links[side];
2168 pInstance->links[side] = pLink;
2171 /******************************************************************
2172 * WDML_RemoveLink
2176 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2177 HSZ hszItem, UINT uFmt)
2179 WDML_LINK* pPrev = NULL;
2180 WDML_LINK* pCurrent = NULL;
2182 pCurrent = pInstance->links[side];
2184 while (pCurrent != NULL)
2186 if (pCurrent->hConv == hConv &&
2187 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2188 pCurrent->uFmt == uFmt)
2190 if (pCurrent == pInstance->links[side])
2192 pInstance->links[side] = pCurrent->next;
2194 else
2196 pPrev->next = pCurrent->next;
2199 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2200 HeapFree(GetProcessHeap(), 0, pCurrent);
2201 break;
2204 pPrev = pCurrent;
2205 pCurrent = pCurrent->next;
2209 /* this function is called to remove all links related to the conv.
2210 It should be called from both client and server when terminating
2211 the conversation.
2213 /******************************************************************
2214 * WDML_RemoveAllLinks
2218 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2220 WDML_LINK* pPrev = NULL;
2221 WDML_LINK* pCurrent = NULL;
2222 WDML_LINK* pNext = NULL;
2224 pCurrent = pInstance->links[side];
2226 while (pCurrent != NULL)
2228 if (pCurrent->hConv == (HCONV)pConv)
2230 if (pCurrent == pInstance->links[side])
2232 pInstance->links[side] = pCurrent->next;
2233 pNext = pCurrent->next;
2235 else
2237 pPrev->next = pCurrent->next;
2238 pNext = pCurrent->next;
2241 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2243 HeapFree(GetProcessHeap(), 0, pCurrent);
2244 pCurrent = NULL;
2247 if (pCurrent)
2249 pPrev = pCurrent;
2250 pCurrent = pCurrent->next;
2252 else
2254 pCurrent = pNext;
2259 /******************************************************************
2260 * WDML_FindLink
2264 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2265 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2267 WDML_LINK* pCurrent = NULL;
2269 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2271 /* we don't need to check for transaction type as it can be altered */
2273 if (pCurrent->hConv == hConv &&
2274 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2275 (!use_fmt || pCurrent->uFmt == uFmt))
2277 break;
2282 return pCurrent;
2285 /* ================================================================
2287 * Transaction management
2289 * ================================================================ */
2291 /******************************************************************
2292 * WDML_AllocTransaction
2294 * Alloc a transaction structure for handling the message ddeMsg
2296 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2297 UINT wFmt, HSZ hszItem)
2299 WDML_XACT* pXAct;
2300 static WORD tid = 1; /* FIXME: wrap around */
2302 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2303 if (!pXAct)
2305 pInstance->lastError = DMLERR_MEMORY_ERROR;
2306 return NULL;
2309 pXAct->xActID = tid++;
2310 pXAct->ddeMsg = ddeMsg;
2311 pXAct->hDdeData = 0;
2312 pXAct->hUser = 0;
2313 pXAct->next = NULL;
2314 pXAct->wType = 0;
2315 pXAct->wFmt = wFmt;
2316 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2317 pXAct->atom = 0;
2318 pXAct->hMem = 0;
2319 pXAct->lParam = 0;
2321 return pXAct;
2324 /******************************************************************
2325 * WDML_QueueTransaction
2327 * Adds a transaction to the list of transaction
2329 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2331 WDML_XACT** pt;
2333 /* advance to last in queue */
2334 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2335 *pt = pXAct;
2338 /******************************************************************
2339 * WDML_UnQueueTransaction
2343 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2345 WDML_XACT** pt;
2347 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2349 if (*pt == pXAct)
2351 *pt = pXAct->next;
2352 return TRUE;
2355 return FALSE;
2358 /******************************************************************
2359 * WDML_FreeTransaction
2363 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2365 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2366 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2368 GlobalFree(pXAct->hMem);
2370 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2372 HeapFree(GetProcessHeap(), 0, pXAct);
2375 /******************************************************************
2376 * WDML_FindTransaction
2380 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2382 WDML_XACT* pXAct;
2384 tid = HIWORD(tid);
2385 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2387 if (pXAct->xActID == tid)
2388 break;
2390 return pXAct;
2393 /* ================================================================
2395 * Information broadcast across DDEML implementations
2397 * ================================================================ */
2399 struct tagWDML_BroadcastPmt
2401 LPCWSTR clsName;
2402 UINT uMsg;
2403 WPARAM wParam;
2404 LPARAM lParam;
2407 /******************************************************************
2408 * WDML_BroadcastEnumProc
2412 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2414 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2415 WCHAR buffer[128];
2417 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2418 lstrcmpiW(buffer, s->clsName) == 0)
2420 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2422 return TRUE;
2425 /******************************************************************
2426 * WDML_BroadcastDDEWindows
2430 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2432 struct tagWDML_BroadcastPmt s;
2434 s.clsName = clsName;
2435 s.uMsg = uMsg;
2436 s.wParam = wParam;
2437 s.lParam = lParam;
2438 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);