server: Added access rights mapping to token objects.
[wine/multimedia.git] / dlls / user / dde_misc.c
blobe25319d3e46b1028639ea9d29bdc261e4178dd1b
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 LONG 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, { (DWORD_PTR)(__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 bUnicode, 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->unicode = bUnicode;
380 pInstance->win16 = b16;
381 pInstance->nodeList = NULL; /* node will be added later */
382 pInstance->monitorFlags = afCmd & MF_MASK;
383 pInstance->servers = NULL;
384 pInstance->convs[0] = NULL;
385 pInstance->convs[1] = NULL;
386 pInstance->links[0] = NULL;
387 pInstance->links[1] = NULL;
389 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
391 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
393 if (!pInstance->clientOnly)
395 /* Check for other way of setting Client-only !! */
396 pInstance->clientOnly =
397 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
400 TRACE("instance created - checking validity\n");
402 if (*pidInst == 0)
404 /* Initialisation of new Instance Identifier */
405 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
407 EnterCriticalSection(&WDML_CritSect);
409 if (WDML_InstanceList == NULL)
411 /* can't be another instance in this case, assign to the base pointer */
412 WDML_InstanceList = pInstance;
414 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
415 * present
416 * ------------------------------- NOTE NOTE NOTE --------------------------
418 * the manual is not clear if this condition
419 * applies to the first call to DdeInitialize from an application, or the
420 * first call for a given callback !!!
423 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
424 TRACE("First application instance detected OK\n");
425 /* allocate new instance ID */
426 WDML_IncrementInstanceId(pInstance);
428 else
430 /* really need to chain the new one in to the latest here, but after checking conditions
431 * such as trying to start a conversation from an application trying to monitor */
432 reference_inst = WDML_InstanceList;
433 TRACE("Subsequent application instance - starting checks\n");
434 while (reference_inst->next != NULL)
437 * This set of tests will work if application uses same instance Id
438 * at application level once allocated - which is what manual implies
439 * should happen. If someone tries to be
440 * clever (lazy ?) it will fail to pick up that later calls are for
441 * the same application - should we trust them ?
443 if (pInstance->instanceID == reference_inst->instanceID)
445 /* Check 1 - must be same Client-only state */
447 if (pInstance->clientOnly != reference_inst->clientOnly)
449 ret = DMLERR_DLL_USAGE;
450 goto theError;
453 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
455 if (pInstance->monitor != reference_inst->monitor)
457 ret = DMLERR_INVALIDPARAMETER;
458 goto theError;
461 /* Check 3 - must supply different callback address */
463 if (pInstance->callback == reference_inst->callback)
465 ret = DMLERR_DLL_USAGE;
466 goto theError;
469 reference_inst = reference_inst->next;
471 /* All cleared, add to chain */
473 TRACE("Application Instance checks finished\n");
474 WDML_IncrementInstanceId(pInstance);
475 reference_inst->next = pInstance;
477 LeaveCriticalSection(&WDML_CritSect);
479 *pidInst = pInstance->instanceID;
481 /* for deadlock issues, windows must always be created when outside the critical section */
482 wndclass.cbSize = sizeof(wndclass);
483 wndclass.style = 0;
484 wndclass.lpfnWndProc = WDML_EventProc;
485 wndclass.cbClsExtra = 0;
486 wndclass.cbWndExtra = sizeof(ULONG_PTR);
487 wndclass.hInstance = 0;
488 wndclass.hIcon = 0;
489 wndclass.hCursor = 0;
490 wndclass.hbrBackground = 0;
491 wndclass.lpszMenuName = NULL;
492 wndclass.lpszClassName = WDML_szEventClass;
493 wndclass.hIconSm = 0;
495 RegisterClassExW(&wndclass);
497 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
498 WS_POPUP, 0, 0, 0, 0,
499 0, 0, 0, 0);
501 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
503 TRACE("New application instance processing finished OK\n");
505 else
507 /* Reinitialisation situation --- FIX */
508 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
510 EnterCriticalSection(&WDML_CritSect);
512 if (WDML_InstanceList == NULL)
514 ret = DMLERR_INVALIDPARAMETER;
515 goto theError;
517 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
518 /* can't reinitialise if we have initialised nothing !! */
519 reference_inst = WDML_InstanceList;
520 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
522 * MS allows initialisation without specifying a callback, should we allow addition of the
523 * callback by a later call to initialise ? - if so this lot will have to change
525 while (reference_inst->next != NULL)
527 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
529 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
531 if (reference_inst->clientOnly)
533 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
535 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
537 if (!(afCmd & APPCMD_CLIENTONLY))
539 ret = DMLERR_INVALIDPARAMETER;
540 goto theError;
544 /* Check 2 - cannot change monitor modes */
546 if (pInstance->monitor != reference_inst->monitor)
548 ret = DMLERR_INVALIDPARAMETER;
549 goto theError;
552 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
554 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
556 ret = DMLERR_INVALIDPARAMETER;
557 goto theError;
559 break;
561 reference_inst = reference_inst->next;
563 if (reference_inst->next == NULL)
565 ret = DMLERR_INVALIDPARAMETER;
566 goto theError;
568 /* All checked - change relevant flags */
570 reference_inst->CBFflags = pInstance->CBFflags;
571 reference_inst->clientOnly = pInstance->clientOnly;
572 reference_inst->monitorFlags = pInstance->monitorFlags;
573 LeaveCriticalSection(&WDML_CritSect);
576 return DMLERR_NO_ERROR;
577 theError:
578 HeapFree(GetProcessHeap(), 0, pInstance);
579 LeaveCriticalSection(&WDML_CritSect);
580 return ret;
583 /******************************************************************************
584 * DdeInitializeA (USER32.@)
586 * See DdeInitializeW.
588 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
589 DWORD afCmd, DWORD ulRes)
591 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
594 /******************************************************************************
595 * DdeInitializeW [USER32.@]
596 * Registers an application with the DDEML
598 * PARAMS
599 * pidInst [I] Pointer to instance identifier
600 * pfnCallback [I] Pointer to callback function
601 * afCmd [I] Set of command and filter flags
602 * ulRes [I] Reserved
604 * RETURNS
605 * Success: DMLERR_NO_ERROR
606 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
608 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
609 DWORD afCmd, DWORD ulRes)
611 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
614 /*****************************************************************
615 * DdeUninitialize [USER32.@] Frees DDEML resources
617 * PARAMS
618 * idInst [I] Instance identifier
620 * RETURNS
621 * Success: TRUE
622 * Failure: FALSE
625 BOOL WINAPI DdeUninitialize(DWORD idInst)
627 /* Stage one - check if we have a handle for this instance
629 WDML_INSTANCE* pInstance;
630 WDML_CONV* pConv;
631 WDML_CONV* pConvNext;
633 TRACE("(%ld)\n", idInst);
635 EnterCriticalSection(&WDML_CritSect);
637 /* First check instance
639 pInstance = WDML_GetInstance(idInst);
640 if (pInstance == NULL)
642 LeaveCriticalSection(&WDML_CritSect);
644 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
646 return FALSE;
649 /* first terminate all conversations client side
650 * this shall close existing links...
652 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
654 pConvNext = pConv->next;
655 DdeDisconnect((HCONV)pConv);
657 if (pInstance->convs[WDML_CLIENT_SIDE])
658 FIXME("still pending conversations\n");
660 /* then unregister all known service names */
661 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
663 /* Free the nodes that were not freed by this instance
664 * and remove the nodes from the list of HSZ nodes.
666 WDML_FreeAllHSZ(pInstance);
668 DestroyWindow(pInstance->hwndEvent);
670 /* OK now delete the instance handle itself */
672 if (WDML_InstanceList == pInstance)
674 /* special case - the first/only entry */
675 WDML_InstanceList = pInstance->next;
677 else
679 /* general case, remove entry */
680 WDML_INSTANCE* inst;
682 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
683 inst->next = pInstance->next;
685 /* leave crit sect and release the heap entry
687 HeapFree(GetProcessHeap(), 0, pInstance);
688 LeaveCriticalSection(&WDML_CritSect);
689 return TRUE;
692 /******************************************************************
693 * WDML_NotifyThreadExit
697 void WDML_NotifyThreadDetach(void)
699 WDML_INSTANCE* pInstance;
700 WDML_INSTANCE* next;
701 DWORD tid = GetCurrentThreadId();
703 EnterCriticalSection(&WDML_CritSect);
704 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
706 next = pInstance->next;
707 if (pInstance->threadID == tid)
709 DdeUninitialize(pInstance->instanceID);
712 LeaveCriticalSection(&WDML_CritSect);
715 /******************************************************************
716 * WDML_InvokeCallback
720 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
721 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
722 ULONG_PTR dwData1, ULONG_PTR dwData2)
724 HDDEDATA ret;
726 if (pInstance == NULL)
727 return NULL;
728 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
729 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
730 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
731 if (pInstance->win16)
733 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
734 hsz1, hsz2, hdata, dwData1, dwData2);
736 else
738 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
740 TRACE("done => %p\n", ret);
741 return ret;
744 /*****************************************************************************
745 * WDML_GetInstance
747 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
748 * for an instance Id, or NULL if the entry does not exist
751 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
753 WDML_INSTANCE* pInstance;
755 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
757 if (pInstance->instanceID == instId)
759 if (GetCurrentThreadId() != pInstance->threadID)
761 FIXME("Tried to get instance from wrong thread\n");
762 continue;
764 return pInstance;
767 TRACE("Instance entry missing\n");
768 return NULL;
771 /******************************************************************
772 * WDML_GetInstanceFromWnd
776 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
778 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
781 /******************************************************************************
782 * DdeGetLastError [USER32.@] Gets most recent error code
784 * PARAMS
785 * idInst [I] Instance identifier
787 * RETURNS
788 * Last error code
790 UINT WINAPI DdeGetLastError(DWORD idInst)
792 DWORD error_code;
793 WDML_INSTANCE* pInstance;
795 EnterCriticalSection(&WDML_CritSect);
797 /* First check instance
799 pInstance = WDML_GetInstance(idInst);
800 if (pInstance == NULL)
802 error_code = DMLERR_INVALIDPARAMETER;
804 else
806 error_code = pInstance->lastError;
807 pInstance->lastError = 0;
810 LeaveCriticalSection(&WDML_CritSect);
811 return error_code;
814 /* ================================================================
816 * String management
818 * ================================================================ */
821 /******************************************************************
822 * WDML_FindNode
826 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
828 HSZNode* pNode;
830 if (pInstance == NULL) return NULL;
832 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
834 if (pNode->hsz == hsz) break;
836 if (!pNode) WARN("HSZ %p not found\n", hsz);
837 return pNode;
840 /******************************************************************
841 * WDML_MakeAtomFromHsz
843 * Creates a global atom from an existing HSZ
844 * Generally used before sending an HSZ as an atom to a remote app
846 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
848 WCHAR nameBuffer[MAX_BUFFER_LEN];
850 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
851 return GlobalAddAtomW(nameBuffer);
852 WARN("HSZ %p not found\n", hsz);
853 return 0;
856 /******************************************************************
857 * WDML_MakeHszFromAtom
859 * Creates a HSZ from an existing global atom
860 * Generally used while receiving a global atom and transforming it
861 * into an HSZ
863 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
865 WCHAR nameBuffer[MAX_BUFFER_LEN];
867 if (!atom) return NULL;
869 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
871 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
872 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
874 WARN("ATOM 0x%x not found\n", atom);
875 return 0;
878 /******************************************************************
879 * WDML_IncHSZ
883 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
885 HSZNode* pNode;
887 pNode = WDML_FindNode(pInstance, hsz);
888 if (!pNode) return FALSE;
890 pNode->refCount++;
891 return TRUE;
894 /******************************************************************************
895 * WDML_DecHSZ (INTERNAL)
897 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
898 * of HSZ nodes
899 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
901 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
903 HSZNode* pPrev = NULL;
904 HSZNode* pCurrent;
906 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
908 /* If we found the node we were looking for and its ref count is one,
909 * we can remove it
911 if (pCurrent->hsz == hsz)
913 if (--pCurrent->refCount == 0)
915 if (pCurrent == pInstance->nodeList)
917 pInstance->nodeList = pCurrent->next;
919 else
921 pPrev->next = pCurrent->next;
923 HeapFree(GetProcessHeap(), 0, pCurrent);
924 DeleteAtom(HSZ2ATOM(hsz));
926 return TRUE;
929 WARN("HSZ %p not found\n", hsz);
931 return FALSE;
934 /******************************************************************************
935 * WDML_FreeAllHSZ (INTERNAL)
937 * Frees up all the strings still allocated in the list and
938 * remove all the nodes from the list of HSZ nodes.
940 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
942 /* Free any strings created in this instance.
944 while (pInstance->nodeList != NULL)
946 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
950 /******************************************************************************
951 * InsertHSZNode (INTERNAL)
953 * Insert a node to the head of the list.
955 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
957 if (hsz != 0)
959 HSZNode* pNew = NULL;
960 /* Create a new node for this HSZ.
962 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
963 if (pNew != NULL)
965 pNew->hsz = hsz;
966 pNew->next = pInstance->nodeList;
967 pNew->refCount = 1;
968 pInstance->nodeList = pNew;
970 else
972 ERR("Primary HSZ Node allocation failed - out of memory\n");
977 /******************************************************************
978 * WDML_QueryString
982 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
983 int codepage)
985 WCHAR pString[MAX_BUFFER_LEN];
986 int ret;
987 /* If psz is null, we have to return only the length
988 * of the string.
990 if (ptr == NULL)
992 ptr = pString;
993 cchMax = MAX_BUFFER_LEN;
996 switch (codepage)
998 case CP_WINANSI:
999 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1000 break;
1001 case CP_WINUNICODE:
1002 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1003 break;
1004 default:
1005 ERR("Unknown code page %d\n", codepage);
1006 ret = 0;
1008 return ret;
1011 /*****************************************************************
1012 * DdeQueryStringA [USER32.@]
1014 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1016 DWORD ret = 0;
1017 WDML_INSTANCE* pInstance;
1019 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1021 EnterCriticalSection(&WDML_CritSect);
1023 /* First check instance
1025 pInstance = WDML_GetInstance(idInst);
1026 if (pInstance != NULL)
1028 if (iCodePage == 0) iCodePage = CP_WINANSI;
1029 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1031 LeaveCriticalSection(&WDML_CritSect);
1033 TRACE("returning %ld (%s)\n", ret, debugstr_a(psz));
1034 return ret;
1037 /*****************************************************************
1038 * DdeQueryStringW [USER32.@]
1041 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1043 DWORD ret = 0;
1044 WDML_INSTANCE* pInstance;
1046 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1048 EnterCriticalSection(&WDML_CritSect);
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);
1058 LeaveCriticalSection(&WDML_CritSect);
1060 TRACE("returning %ld (%s)\n", ret, debugstr_w(psz));
1061 return ret;
1064 /******************************************************************
1065 * DML_CreateString
1069 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1071 HSZ hsz;
1073 switch (codepage)
1075 case CP_WINANSI:
1076 hsz = ATOM2HSZ(AddAtomA(ptr));
1077 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1078 break;
1079 case CP_WINUNICODE:
1080 hsz = ATOM2HSZ(AddAtomW(ptr));
1081 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1082 break;
1083 default:
1084 ERR("Unknown code page %d\n", codepage);
1085 return 0;
1087 WDML_InsertHSZNode(pInstance, hsz);
1088 return hsz;
1091 /*****************************************************************
1092 * DdeCreateStringHandleA [USER32.@]
1094 * See DdeCreateStringHandleW.
1096 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1098 HSZ hsz = 0;
1099 WDML_INSTANCE* pInstance;
1101 TRACE("(%ld,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1103 EnterCriticalSection(&WDML_CritSect);
1105 pInstance = WDML_GetInstance(idInst);
1106 if (pInstance)
1108 if (codepage == 0) codepage = CP_WINANSI;
1109 hsz = WDML_CreateString(pInstance, psz, codepage);
1112 LeaveCriticalSection(&WDML_CritSect);
1113 return hsz;
1117 /******************************************************************************
1118 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1120 * PARAMS
1121 * idInst [I] Instance identifier
1122 * psz [I] Pointer to string
1123 * codepage [I] Code page identifier
1124 * RETURNS
1125 * Success: String handle
1126 * Failure: 0
1128 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1130 WDML_INSTANCE* pInstance;
1131 HSZ hsz = 0;
1133 TRACE("(%ld,%s,%d)\n", idInst, debugstr_w(psz), codepage);
1135 EnterCriticalSection(&WDML_CritSect);
1137 pInstance = WDML_GetInstance(idInst);
1138 if (pInstance)
1140 if (codepage == 0) codepage = CP_WINUNICODE;
1141 hsz = WDML_CreateString(pInstance, psz, codepage);
1143 LeaveCriticalSection(&WDML_CritSect);
1145 return hsz;
1148 /*****************************************************************
1149 * DdeFreeStringHandle (USER32.@)
1150 * RETURNS
1151 * success: nonzero
1152 * fail: zero
1154 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1156 WDML_INSTANCE* pInstance;
1157 BOOL ret = FALSE;
1159 TRACE("(%ld,%p):\n", idInst, hsz);
1161 EnterCriticalSection(&WDML_CritSect);
1163 /* First check instance
1165 pInstance = WDML_GetInstance(idInst);
1166 if (pInstance)
1167 ret = WDML_DecHSZ(pInstance, hsz);
1169 LeaveCriticalSection(&WDML_CritSect);
1171 return ret;
1174 /*****************************************************************
1175 * DdeKeepStringHandle (USER32.@)
1177 * RETURNS
1178 * success: nonzero
1179 * fail: zero
1181 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1183 WDML_INSTANCE* pInstance;
1184 BOOL ret = FALSE;
1186 TRACE("(%ld,%p):\n", idInst, hsz);
1188 EnterCriticalSection(&WDML_CritSect);
1190 /* First check instance
1192 pInstance = WDML_GetInstance(idInst);
1193 if (pInstance)
1194 ret = WDML_IncHSZ(pInstance, hsz);
1196 LeaveCriticalSection(&WDML_CritSect);
1197 return ret;
1200 /*****************************************************************
1201 * DdeCmpStringHandles (USER32.@)
1203 * Compares the value of two string handles. This comparison is
1204 * not case sensitive.
1206 * PARAMS
1207 * hsz1 [I] Handle to the first string
1208 * hsz2 [I] Handle to the second string
1210 * RETURNS
1211 * -1 The value of hsz1 is zero or less than hsz2
1212 * 0 The values of hsz 1 and 2 are the same or both zero.
1213 * 1 The value of hsz2 is zero of less than hsz1
1215 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1217 WCHAR psz1[MAX_BUFFER_LEN];
1218 WCHAR psz2[MAX_BUFFER_LEN];
1219 int ret = 0;
1220 int ret1, ret2;
1222 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1223 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1225 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1227 /* Make sure we found both strings. */
1228 if (ret1 == 0 && ret2 == 0)
1230 /* If both are not found, return both "zero strings". */
1231 ret = 0;
1233 else if (ret1 == 0)
1235 /* If hsz1 is a not found, return hsz1 is "zero string". */
1236 ret = -1;
1238 else if (ret2 == 0)
1240 /* If hsz2 is a not found, return hsz2 is "zero string". */
1241 ret = 1;
1243 else
1245 /* Compare the two strings we got (case insensitive). */
1246 ret = lstrcmpiW(psz1, psz2);
1247 /* Since strcmp returns any number smaller than
1248 * 0 when the first string is found to be less than
1249 * the second one we must make sure we are returning
1250 * the proper values.
1252 if (ret < 0)
1254 ret = -1;
1256 else if (ret > 0)
1258 ret = 1;
1262 return ret;
1265 /* ================================================================
1267 * Data handle management
1269 * ================================================================ */
1271 /*****************************************************************
1272 * DdeCreateDataHandle (USER32.@)
1274 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1275 HSZ hszItem, UINT wFmt, UINT afCmd)
1277 /* For now, we ignore idInst, hszItem.
1278 * The purpose of these arguments still need to be investigated.
1281 HGLOBAL hMem;
1282 LPBYTE pByte;
1283 DDE_DATAHANDLE_HEAD* pDdh;
1284 WCHAR psz[MAX_BUFFER_LEN];
1286 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1288 psz[0] = HSZ2ATOM(hszItem);
1289 psz[1] = 0;
1292 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,fmt %04x,%x)\n",
1293 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1295 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1296 return 0;
1298 /* we use the first 4 bytes to store the size */
1299 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1301 ERR("GlobalAlloc failed\n");
1302 return 0;
1305 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1306 if (!pDdh)
1308 GlobalFree(hMem);
1309 return 0;
1312 pDdh->cfFormat = wFmt;
1313 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1315 pByte = (LPBYTE)(pDdh + 1);
1316 if (pSrc)
1318 memcpy(pByte, pSrc + cbOff, cb);
1320 GlobalUnlock(hMem);
1322 TRACE("=> %p\n", hMem);
1323 return (HDDEDATA)hMem;
1326 /*****************************************************************
1328 * DdeAddData (USER32.@)
1330 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1332 DWORD old_sz, new_sz;
1333 LPBYTE pDst;
1335 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData, pSrc, cb, cbOff);
1337 pDst = DdeAccessData(hData, &old_sz);
1338 if (!pDst) return 0;
1340 new_sz = cb + cbOff;
1341 if (new_sz > old_sz)
1343 DdeUnaccessData(hData);
1344 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1345 GMEM_MOVEABLE | GMEM_DDESHARE);
1346 pDst = DdeAccessData(hData, &old_sz);
1349 if (!pDst) return 0;
1351 memcpy(pDst + cbOff, pSrc, cb);
1352 DdeUnaccessData(hData);
1353 return hData;
1356 /******************************************************************************
1357 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1360 * PARAMS
1361 * hData [I] Handle to DDE object
1362 * pDst [I] Pointer to destination buffer
1363 * cbMax [I] Amount of data to copy
1364 * cbOff [I] Offset to beginning of data
1366 * RETURNS
1367 * Size of memory object associated with handle
1369 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1371 DWORD dwSize, dwRet;
1372 LPBYTE pByte;
1374 TRACE("(%p,%p,%ld,%ld)\n", hData, pDst, cbMax, cbOff);
1376 pByte = DdeAccessData(hData, &dwSize);
1378 if (pByte)
1380 if (!pDst)
1382 dwRet = dwSize;
1384 else if (cbOff + cbMax < dwSize)
1386 dwRet = cbMax;
1388 else if (cbOff < dwSize)
1390 dwRet = dwSize - cbOff;
1392 else
1394 dwRet = 0;
1396 if (pDst && dwRet != 0)
1398 memcpy(pDst, pByte + cbOff, dwRet);
1400 DdeUnaccessData(hData);
1402 else
1404 dwRet = 0;
1406 return dwRet;
1409 /*****************************************************************
1410 * DdeAccessData (USER32.@)
1412 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1414 HGLOBAL hMem = (HGLOBAL)hData;
1415 DDE_DATAHANDLE_HEAD* pDdh;
1417 TRACE("(%p,%p)\n", hData, pcbDataSize);
1419 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1420 if (pDdh == NULL)
1422 ERR("Failed on GlobalLock(%p)\n", hMem);
1423 return 0;
1426 if (pcbDataSize != NULL)
1428 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1430 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1431 return (LPBYTE)(pDdh + 1);
1434 /*****************************************************************
1435 * DdeUnaccessData (USER32.@)
1437 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1439 HGLOBAL hMem = (HGLOBAL)hData;
1441 TRACE("(%p)\n", hData);
1443 GlobalUnlock(hMem);
1445 return TRUE;
1448 /*****************************************************************
1449 * DdeFreeDataHandle (USER32.@)
1451 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1453 TRACE("(%p)\n", hData);
1454 return GlobalFree((HGLOBAL)hData) == 0;
1457 /******************************************************************
1458 * WDML_IsAppOwned
1462 BOOL WDML_IsAppOwned(HDDEDATA hData)
1464 DDE_DATAHANDLE_HEAD* pDdh;
1465 BOOL ret = FALSE;
1467 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1468 if (pDdh != NULL)
1470 ret = pDdh->bAppOwned;
1471 GlobalUnlock((HGLOBAL)hData);
1473 return ret;
1476 /* ================================================================
1478 * Global <=> Data handle management
1480 * ================================================================ */
1482 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1483 * offset size
1484 * (bytes) (bits) comment
1485 * 0 16 bit fields for options (release, ackreq, response...)
1486 * 2 16 clipboard format
1487 * 4 ? data to be used
1489 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1491 DDEDATA* pDd;
1492 HDDEDATA ret = 0;
1493 DWORD size;
1495 if (hMem)
1497 pDd = GlobalLock(hMem);
1498 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1499 if (pDd)
1501 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1502 switch (pDd->cfFormat)
1504 default:
1505 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1506 pDd->cfFormat, hMem);
1507 /* fall thru */
1508 case 0:
1509 case CF_TEXT:
1510 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1511 break;
1512 case CF_BITMAP:
1513 if (size >= sizeof(BITMAP))
1515 BITMAP* bmp = (BITMAP*)pDd->Value;
1516 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1517 if (size >= sizeof(BITMAP) + count)
1519 HBITMAP hbmp;
1521 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1522 bmp->bmPlanes, bmp->bmBitsPixel,
1523 pDd->Value + sizeof(BITMAP))))
1525 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1526 0, 0, CF_BITMAP, 0);
1528 else ERR("Can't create bmp\n");
1530 else
1532 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1534 } else ERR("No bitmap header\n");
1535 break;
1537 GlobalUnlock(hMem);
1540 return ret;
1543 /******************************************************************
1544 * WDML_DataHandle2Global
1548 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1549 BOOL fDeferUpd, BOOL fAckReq)
1551 DDE_DATAHANDLE_HEAD* pDdh;
1552 DWORD dwSize;
1553 HGLOBAL hMem = 0;
1555 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1556 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1557 if (dwSize && pDdh)
1559 WINE_DDEHEAD* wdh = NULL;
1561 switch (pDdh->cfFormat)
1563 default:
1564 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1565 pDdh->cfFormat, hDdeData);
1566 /* fall thru */
1567 case 0:
1568 case CF_TEXT:
1569 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1570 if (hMem && (wdh = GlobalLock(hMem)))
1572 memcpy(wdh + 1, pDdh + 1, dwSize);
1574 break;
1575 case CF_BITMAP:
1576 if (dwSize >= sizeof(HBITMAP))
1578 BITMAP bmp;
1579 DWORD count;
1580 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1582 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1584 count = bmp.bmWidthBytes * bmp.bmHeight;
1585 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1586 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1587 if (hMem && (wdh = GlobalLock(hMem)))
1589 memcpy(wdh + 1, &bmp, sizeof(bmp));
1590 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1594 break;
1596 if (wdh)
1598 wdh->unused = 0;
1599 wdh->fResponse = fResponse;
1600 wdh->fRelease = fRelease;
1601 wdh->fDeferUpd = fDeferUpd;
1602 wdh->fAckReq = fAckReq;
1603 wdh->cfFormat = pDdh->cfFormat;
1604 GlobalUnlock(hMem);
1606 GlobalUnlock((HGLOBAL)hDdeData);
1609 return hMem;
1612 /* ================================================================
1614 * Server management
1616 * ================================================================ */
1618 /******************************************************************
1619 * WDML_AddServer
1623 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1625 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1626 WDML_SERVER* pServer;
1627 WCHAR buf1[256];
1628 WCHAR buf2[256];
1630 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1631 if (pServer == NULL) return NULL;
1633 pServer->hszService = hszService;
1634 WDML_IncHSZ(pInstance, hszService);
1636 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1637 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1638 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1640 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1641 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1643 pServer->filterOn = TRUE;
1645 pServer->next = pInstance->servers;
1646 pInstance->servers = pServer;
1647 return pServer;
1650 /******************************************************************
1651 * WDML_RemoveServer
1655 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1657 WDML_SERVER* pPrev = NULL;
1658 WDML_SERVER* pServer = NULL;
1659 WDML_CONV* pConv;
1660 WDML_CONV* pConvNext;
1662 pServer = pInstance->servers;
1664 while (pServer != NULL)
1666 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1668 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1669 pServer->atomService, pServer->atomServiceSpec);
1670 /* terminate all conversations for given topic */
1671 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1673 pConvNext = pConv->next;
1674 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1676 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1677 /* don't care about return code (whether client window is present or not) */
1678 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
1681 if (pServer == pInstance->servers)
1683 pInstance->servers = pServer->next;
1685 else
1687 pPrev->next = pServer->next;
1690 DestroyWindow(pServer->hwndServer);
1691 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1692 WDML_DecHSZ(pInstance, pServer->hszService);
1694 GlobalDeleteAtom(pServer->atomService);
1695 GlobalDeleteAtom(pServer->atomServiceSpec);
1697 HeapFree(GetProcessHeap(), 0, pServer);
1698 break;
1701 pPrev = pServer;
1702 pServer = pServer->next;
1706 /*****************************************************************************
1707 * WDML_FindServer
1709 * generic routine to return a pointer to the relevant ServiceNode
1710 * for a given service name, or NULL if the entry does not exist
1713 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1715 WDML_SERVER* pServer;
1717 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1719 if (hszService == pServer->hszService)
1721 return pServer;
1724 TRACE("Service name missing\n");
1725 return NULL;
1728 /* ================================================================
1730 * Conversation management
1732 * ================================================================ */
1734 /******************************************************************
1735 * WDML_AddConv
1739 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1740 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1742 WDML_CONV* pConv;
1744 /* no converstation yet, add it */
1745 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1746 if (!pConv) return NULL;
1748 pConv->instance = pInstance;
1749 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1750 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1751 pConv->magic = WDML_CONV_MAGIC;
1752 pConv->hwndServer = hwndServer;
1753 pConv->hwndClient = hwndClient;
1754 pConv->transactions = NULL;
1755 pConv->hUser = 0;
1756 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1757 /* check if both side of the conversation are of the same instance */
1758 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1759 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1761 pConv->wStatus |= ST_ISSELF;
1763 pConv->wConvst = XST_NULL;
1765 pConv->next = pInstance->convs[side];
1766 pInstance->convs[side] = pConv;
1768 return pConv;
1771 /******************************************************************
1772 * WDML_FindConv
1776 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1777 HSZ hszService, HSZ hszTopic)
1779 WDML_CONV* pCurrent = NULL;
1781 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1783 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1784 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1786 return pCurrent;
1790 return NULL;
1793 /******************************************************************
1794 * WDML_RemoveConv
1798 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1800 WDML_CONV* pPrev = NULL;
1801 WDML_CONV* pCurrent;
1802 WDML_XACT* pXAct;
1803 WDML_XACT* pXActNext;
1804 HWND hWnd;
1806 if (!pRef)
1807 return;
1809 /* remove any pending transaction */
1810 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1812 pXActNext = pXAct->next;
1813 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1816 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1818 /* FIXME: should we keep the window around ? it seems so (at least on client side
1819 * to let QueryConvInfo work after conv termination, but also to implement
1820 * DdeReconnect...
1822 /* destroy conversation window, but first remove pConv from hWnd.
1823 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1825 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1826 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1828 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1830 WDML_DecHSZ(pRef->instance, pRef->hszService);
1831 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1833 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1835 if (pCurrent == pRef)
1837 if (pCurrent == pRef->instance->convs[side])
1839 pRef->instance->convs[side] = pCurrent->next;
1841 else
1843 pPrev->next = pCurrent->next;
1845 pCurrent->magic = 0;
1846 HeapFree(GetProcessHeap(), 0, pCurrent);
1847 break;
1852 /******************************************************************
1853 * WDML_EnableCallback
1855 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1857 if (wCmd == EC_DISABLE)
1859 FIXME("EC_DISABLE is not implemented\n");
1860 return TRUE;
1863 if (wCmd == EC_QUERYWAITING)
1864 return pConv->transactions ? TRUE : FALSE;
1866 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1868 FIXME("Unknown command code %04x\n", wCmd);
1869 return FALSE;
1872 while (pConv->transactions)
1874 WDML_XACT *pXAct = pConv->transactions;
1875 WDML_UnQueueTransaction(pConv, pXAct);
1877 if (pConv->wStatus & ST_CLIENT)
1879 /*WDML_ClientHandle(pConv, pXAct);*/
1880 FIXME("Client delayed transaction queue handling is not supported\n");
1882 else
1883 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("(%ld, %p, %04x)\n", idInst, hConv, wCmd);
1902 EnterCriticalSection(&WDML_CritSect);
1904 pConv = WDML_GetConv(hConv, TRUE);
1906 if (pConv && pConv->instance->instanceID == idInst)
1907 ret = WDML_EnableCallback(pConv, wCmd);
1909 LeaveCriticalSection(&WDML_CritSect);
1910 return ret;
1913 /******************************************************************
1914 * WDML_GetConv
1918 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1920 WDML_CONV* pConv = (WDML_CONV*)hConv;
1922 /* FIXME: should do better checking */
1923 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1925 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1927 FIXME("found conv but ain't connected\n");
1928 return NULL;
1930 if (!pConv->instance || GetCurrentThreadId() != pConv->instance->threadID)
1932 FIXME("wrong thread ID\n");
1933 return NULL;
1936 return pConv;
1939 /******************************************************************
1940 * WDML_GetConvFromWnd
1944 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1946 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
1949 /******************************************************************
1950 * WDML_PostAck
1954 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1955 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1957 DDEACK ddeAck;
1958 HWND from, to;
1960 if (side == WDML_SERVER_SIDE)
1962 from = pConv->hwndServer;
1963 to = pConv->hwndClient;
1965 else
1967 to = pConv->hwndServer;
1968 from = pConv->hwndClient;
1971 ddeAck.bAppReturnCode = appRetCode;
1972 ddeAck.reserved = 0;
1973 ddeAck.fBusy = fBusy;
1974 ddeAck.fAck = fAck;
1976 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1978 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1979 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1980 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
1982 pConv->wStatus &= ~ST_CONNECTED;
1983 FreeDDElParam(WM_DDE_ACK, lParam);
1984 return FALSE;
1986 return TRUE;
1989 /*****************************************************************
1990 * DdeSetUserHandle (USER32.@)
1992 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1994 WDML_CONV* pConv;
1995 BOOL ret = TRUE;
1997 TRACE("(%p,%lx,%lx)\n", hConv, id, hUser);
1999 EnterCriticalSection(&WDML_CritSect);
2001 pConv = WDML_GetConv(hConv, FALSE);
2002 if (pConv == NULL)
2004 ret = FALSE;
2005 goto theError;
2007 if (id == QID_SYNC)
2009 pConv->hUser = hUser;
2011 else
2013 WDML_XACT* pXAct;
2015 pXAct = WDML_FindTransaction(pConv, id);
2016 if (pXAct)
2018 pXAct->hUser = hUser;
2020 else
2022 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2023 ret = FALSE;
2026 theError:
2027 LeaveCriticalSection(&WDML_CritSect);
2028 return ret;
2031 /******************************************************************
2032 * WDML_GetLocalConvInfo
2036 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2038 BOOL ret = TRUE;
2039 WDML_LINK* pLink;
2040 WDML_SIDE side;
2042 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2043 ci->hszSvcPartner = pConv->hszService;
2044 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2045 ci->hszTopic = pConv->hszTopic;
2046 ci->wStatus = pConv->wStatus;
2048 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2050 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2052 if (pLink->hConv == (HCONV)pConv)
2054 ci->wStatus |= ST_ADVISE;
2055 break;
2059 /* FIXME: non handled status flags:
2060 ST_BLOCKED
2061 ST_BLOCKNEXT
2062 ST_INLIST
2065 ci->wConvst = pConv->wConvst; /* FIXME */
2067 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2068 ci->hConvList = 0;
2069 ci->ConvCtxt = pConv->convContext;
2070 if (ci->wStatus & ST_CLIENT)
2072 ci->hwnd = pConv->hwndClient;
2073 ci->hwndPartner = pConv->hwndServer;
2075 else
2077 ci->hwnd = pConv->hwndServer;
2078 ci->hwndPartner = pConv->hwndClient;
2080 if (id == QID_SYNC)
2082 ci->hUser = pConv->hUser;
2083 ci->hszItem = 0;
2084 ci->wFmt = 0;
2085 ci->wType = 0;
2087 else
2089 WDML_XACT* pXAct;
2091 pXAct = WDML_FindTransaction(pConv, id);
2092 if (pXAct)
2094 ci->hUser = pXAct->hUser;
2095 ci->hszItem = pXAct->hszItem;
2096 ci->wFmt = pXAct->wFmt;
2097 ci->wType = pXAct->wType;
2099 else
2101 ret = 0;
2102 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2105 return ret;
2108 /******************************************************************
2109 * DdeQueryConvInfo (USER32.@)
2111 * FIXME: Set last DDE error on failure.
2113 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2115 UINT ret = lpConvInfo->cb;
2116 CONVINFO ci;
2117 WDML_CONV* pConv;
2119 TRACE("(%p,%lx,%p)\n", hConv, id, lpConvInfo);
2121 if (!hConv)
2123 FIXME("hConv is NULL\n");
2124 return 0;
2127 EnterCriticalSection(&WDML_CritSect);
2129 pConv = WDML_GetConv(hConv, FALSE);
2130 if (pConv != NULL)
2132 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2133 ret = 0;
2135 else
2137 if ((ULONG_PTR)hConv & 1)
2139 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2140 if (pConv != NULL)
2141 FIXME("Request on remote conversation information is not implemented yet\n");
2143 ret = 0;
2145 LeaveCriticalSection(&WDML_CritSect);
2146 if (ret != 0)
2147 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2148 return ret;
2151 /* ================================================================
2153 * Link (hot & warm) management
2155 * ================================================================ */
2157 /******************************************************************
2158 * WDML_AddLink
2162 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2163 UINT wType, HSZ hszItem, UINT wFmt)
2165 WDML_LINK* pLink;
2167 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2168 if (pLink == NULL)
2170 ERR("OOM\n");
2171 return;
2174 pLink->hConv = hConv;
2175 pLink->transactionType = wType;
2176 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2177 pLink->uFmt = wFmt;
2178 pLink->next = pInstance->links[side];
2179 pInstance->links[side] = pLink;
2182 /******************************************************************
2183 * WDML_RemoveLink
2187 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2188 HSZ hszItem, UINT uFmt)
2190 WDML_LINK* pPrev = NULL;
2191 WDML_LINK* pCurrent = NULL;
2193 pCurrent = pInstance->links[side];
2195 while (pCurrent != NULL)
2197 if (pCurrent->hConv == hConv &&
2198 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2199 pCurrent->uFmt == uFmt)
2201 if (pCurrent == pInstance->links[side])
2203 pInstance->links[side] = pCurrent->next;
2205 else
2207 pPrev->next = pCurrent->next;
2210 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2211 HeapFree(GetProcessHeap(), 0, pCurrent);
2212 break;
2215 pPrev = pCurrent;
2216 pCurrent = pCurrent->next;
2220 /* this function is called to remove all links related to the conv.
2221 It should be called from both client and server when terminating
2222 the conversation.
2224 /******************************************************************
2225 * WDML_RemoveAllLinks
2229 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2231 WDML_LINK* pPrev = NULL;
2232 WDML_LINK* pCurrent = NULL;
2233 WDML_LINK* pNext = NULL;
2235 pCurrent = pInstance->links[side];
2237 while (pCurrent != NULL)
2239 if (pCurrent->hConv == (HCONV)pConv)
2241 if (pCurrent == pInstance->links[side])
2243 pInstance->links[side] = pCurrent->next;
2244 pNext = pCurrent->next;
2246 else
2248 pPrev->next = pCurrent->next;
2249 pNext = pCurrent->next;
2252 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2254 HeapFree(GetProcessHeap(), 0, pCurrent);
2255 pCurrent = NULL;
2258 if (pCurrent)
2260 pPrev = pCurrent;
2261 pCurrent = pCurrent->next;
2263 else
2265 pCurrent = pNext;
2270 /******************************************************************
2271 * WDML_FindLink
2275 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2276 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2278 WDML_LINK* pCurrent = NULL;
2280 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2282 /* we don't need to check for transaction type as it can be altered */
2284 if (pCurrent->hConv == hConv &&
2285 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2286 (!use_fmt || pCurrent->uFmt == uFmt))
2288 break;
2293 return pCurrent;
2296 /* ================================================================
2298 * Transaction management
2300 * ================================================================ */
2302 /******************************************************************
2303 * WDML_AllocTransaction
2305 * Alloc a transaction structure for handling the message ddeMsg
2307 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2308 UINT wFmt, HSZ hszItem)
2310 WDML_XACT* pXAct;
2311 static WORD tid = 1; /* FIXME: wrap around */
2313 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2314 if (!pXAct)
2316 pInstance->lastError = DMLERR_MEMORY_ERROR;
2317 return NULL;
2320 pXAct->xActID = tid++;
2321 pXAct->ddeMsg = ddeMsg;
2322 pXAct->hDdeData = 0;
2323 pXAct->hUser = 0;
2324 pXAct->next = NULL;
2325 pXAct->wType = 0;
2326 pXAct->wFmt = wFmt;
2327 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2328 pXAct->atom = 0;
2329 pXAct->hMem = 0;
2330 pXAct->lParam = 0;
2332 return pXAct;
2335 /******************************************************************
2336 * WDML_QueueTransaction
2338 * Adds a transaction to the list of transaction
2340 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2342 WDML_XACT** pt;
2344 /* advance to last in queue */
2345 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2346 *pt = pXAct;
2349 /******************************************************************
2350 * WDML_UnQueueTransaction
2354 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2356 WDML_XACT** pt;
2358 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2360 if (*pt == pXAct)
2362 *pt = pXAct->next;
2363 return TRUE;
2366 return FALSE;
2369 /******************************************************************
2370 * WDML_FreeTransaction
2374 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2376 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2377 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2379 GlobalFree(pXAct->hMem);
2381 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2383 HeapFree(GetProcessHeap(), 0, pXAct);
2386 /******************************************************************
2387 * WDML_FindTransaction
2391 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2393 WDML_XACT* pXAct;
2395 tid = HIWORD(tid);
2396 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2398 if (pXAct->xActID == tid)
2399 break;
2401 return pXAct;
2404 /* ================================================================
2406 * Information broadcast across DDEML implementations
2408 * ================================================================ */
2410 struct tagWDML_BroadcastPmt
2412 LPCWSTR clsName;
2413 UINT uMsg;
2414 WPARAM wParam;
2415 LPARAM lParam;
2418 /******************************************************************
2419 * WDML_BroadcastEnumProc
2423 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2425 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2426 WCHAR buffer[128];
2428 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2429 lstrcmpiW(buffer, s->clsName) == 0)
2431 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2433 return TRUE;
2436 /******************************************************************
2437 * WDML_BroadcastDDEWindows
2441 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2443 struct tagWDML_BroadcastPmt s;
2445 s.clsName = clsName;
2446 s.uMsg = uMsg;
2447 s.wParam = wParam;
2448 s.lParam = lParam;
2449 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);