Implement NtAccessCheck.
[wine/gsoc-2012-control.git] / dlls / user / dde / misc.c
blob5799b4575f09795cdbbec321aa52cbb06162982c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * DDEML library
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
9 * Copyright 2000 Corel
10 * Copyright 2001 Eric Pouech
11 * Copyright 2003, 2004, 2005 Dmitry Timoshkov
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "config.h"
29 #include "wine/port.h"
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winerror.h"
39 #include "dde.h"
40 #include "ddeml.h"
41 #include "win.h"
42 #include "dde/dde_private.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
48 /* convert between ATOM and HSZ avoiding compiler warnings */
49 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
50 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
52 static WDML_INSTANCE* WDML_InstanceList = NULL;
53 static DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
54 const WCHAR WDML_szEventClass[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
56 static CRITICAL_SECTION_DEBUG critsect_debug =
58 0, 0, &WDML_CritSect,
59 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60 0, 0, { 0, (DWORD)(__FILE__ ": WDML_CritSect") }
62 CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
64 /* ================================================================
66 * Pure DDE (non DDEML) management
68 * ================================================================ */
71 /*****************************************************************
72 * PackDDElParam (USER32.@)
74 * RETURNS
75 * the packed lParam
77 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
79 HGLOBAL hMem;
80 UINT_PTR *params;
82 switch (msg)
84 case WM_DDE_ACK:
85 case WM_DDE_ADVISE:
86 case WM_DDE_DATA:
87 case WM_DDE_POKE:
88 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
90 ERR("GlobalAlloc failed\n");
91 return 0;
93 if (!(params = GlobalLock(hMem)))
95 ERR("GlobalLock failed (%p)\n", hMem);
96 return 0;
98 params[0] = uiLo;
99 params[1] = uiHi;
100 GlobalUnlock(hMem);
101 return (LPARAM)hMem;
103 case WM_DDE_EXECUTE:
104 return uiHi;
106 default:
107 return MAKELPARAM(uiLo, uiHi);
112 /*****************************************************************
113 * UnpackDDElParam (USER32.@)
115 * RETURNS
116 * success: nonzero
117 * failure: zero
119 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
120 PUINT_PTR uiLo, PUINT_PTR uiHi)
122 UINT_PTR *params;
124 switch (msg)
126 case WM_DDE_ACK:
127 case WM_DDE_ADVISE:
128 case WM_DDE_DATA:
129 case WM_DDE_POKE:
130 if (!lParam) return FALSE;
131 if (!(params = GlobalLock( (HGLOBAL)lParam )))
133 ERR("GlobalLock failed (%lx)\n", lParam);
134 return FALSE;
136 TRACE("unpacked: low %08x, high %08x\n", params[0], params[1]);
137 if (uiLo) *uiLo = params[0];
138 if (uiHi) *uiHi = params[1];
139 GlobalUnlock( (HGLOBAL)lParam );
140 return TRUE;
142 case WM_DDE_EXECUTE:
143 if (uiLo) *uiLo = 0;
144 if (uiHi) *uiHi = lParam;
145 return TRUE;
147 default:
148 if (uiLo) *uiLo = LOWORD(lParam);
149 if (uiHi) *uiHi = HIWORD(lParam);
150 return TRUE;
155 /*****************************************************************
156 * FreeDDElParam (USER32.@)
158 * RETURNS
159 * success: nonzero
160 * failure: zero
162 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
164 switch (msg)
166 case WM_DDE_ACK:
167 case WM_DDE_ADVISE:
168 case WM_DDE_DATA:
169 case WM_DDE_POKE:
170 /* first check if it's a global handle */
171 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
172 return !GlobalFree( (HGLOBAL)lParam );
174 default:
175 return TRUE;
180 /*****************************************************************
181 * ReuseDDElParam (USER32.@)
183 * RETURNS
184 * the packed lParam
186 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
187 UINT_PTR uiLo, UINT_PTR uiHi)
189 UINT_PTR *params;
191 switch (msgIn)
193 case WM_DDE_ACK:
194 case WM_DDE_ADVISE:
195 case WM_DDE_DATA:
196 case WM_DDE_POKE:
197 switch(msgOut)
199 case WM_DDE_ACK:
200 case WM_DDE_ADVISE:
201 case WM_DDE_DATA:
202 case WM_DDE_POKE:
203 if (!lParam) return 0;
204 if (!(params = GlobalLock( (HGLOBAL)lParam )))
206 ERR("GlobalLock failed\n");
207 return 0;
209 params[0] = uiLo;
210 params[1] = uiHi;
211 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
212 GlobalLock( (HGLOBAL)lParam );
213 return lParam;
215 case WM_DDE_EXECUTE:
216 FreeDDElParam( msgIn, lParam );
217 return uiHi;
219 default:
220 FreeDDElParam( msgIn, lParam );
221 return MAKELPARAM(uiLo, uiHi);
224 default:
225 return PackDDElParam( msgOut, uiLo, uiHi );
229 /*****************************************************************
230 * ImpersonateDdeClientWindow (USER32.@)
232 * PARAMS
233 * hWndClient [I] handle to DDE client window
234 * hWndServer [I] handle to DDE server window
236 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
238 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
239 return FALSE;
242 /*****************************************************************
243 * DdeSetQualityOfService (USER32.@)
246 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
247 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
249 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
250 return TRUE;
253 /* ================================================================
255 * Instance management
257 * ================================================================ */
259 /******************************************************************************
260 * IncrementInstanceId
262 * generic routine to increment the max instance Id and allocate a new application instance
264 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
266 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
268 pInstance->instanceID = id;
269 TRACE("New instance id %ld allocated\n", id);
272 /******************************************************************
273 * WDML_EventProc
277 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
279 WDML_INSTANCE* pInstance;
280 HSZ hsz1, hsz2;
282 switch (uMsg)
284 case WM_WDML_REGISTER:
285 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
286 /* try calling the Callback */
287 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
289 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
290 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
291 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
292 WDML_DecHSZ(pInstance, hsz1);
293 WDML_DecHSZ(pInstance, hsz2);
295 break;
297 case WM_WDML_UNREGISTER:
298 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
299 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
301 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
302 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
303 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
304 WDML_DecHSZ(pInstance, hsz1);
305 WDML_DecHSZ(pInstance, hsz2);
307 break;
309 case WM_WDML_CONNECT_CONFIRM:
310 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
311 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
313 WDML_CONV* pConv;
314 /* confirm connection...
315 * lookup for this conv handle
317 HWND client = WIN_GetFullHandle( (HWND)wParam );
318 HWND server = WIN_GetFullHandle( (HWND)lParam );
319 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
321 if (pConv->hwndClient == client && pConv->hwndServer == server)
322 break;
324 if (pConv)
326 pConv->wStatus |= ST_ISLOCAL;
328 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
329 pConv->hszTopic, pConv->hszService, 0, 0,
330 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
333 break;
334 default:
335 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
337 return 0;
340 /******************************************************************
341 * WDML_Initialize
345 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
346 DWORD afCmd, DWORD ulRes, BOOL b16)
348 WDML_INSTANCE* pInstance;
349 WDML_INSTANCE* reference_inst;
350 UINT ret;
351 WNDCLASSEXW wndclass;
353 TRACE("(%p,%p,0x%lx,%ld)\n",
354 pidInst, pfnCallback, afCmd, ulRes);
356 if (ulRes)
358 ERR("Reserved value not zero? What does this mean?\n");
359 /* trap this and no more until we know more */
360 return DMLERR_NO_ERROR;
363 /* grab enough heap for one control struct - not really necessary for re-initialise
364 * but allows us to use same validation routines */
365 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
366 if (pInstance == NULL)
368 /* catastrophe !! warn user & abort */
369 ERR("Instance create failed - out of memory\n");
370 return DMLERR_SYS_ERROR;
372 pInstance->next = NULL;
373 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
375 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
377 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
378 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
379 pInstance->threadID = GetCurrentThreadId();
380 pInstance->callback = *pfnCallback;
381 pInstance->win16 = b16;
382 pInstance->nodeList = NULL; /* node will be added later */
383 pInstance->monitorFlags = afCmd & MF_MASK;
384 pInstance->servers = NULL;
385 pInstance->convs[0] = NULL;
386 pInstance->convs[1] = NULL;
387 pInstance->links[0] = NULL;
388 pInstance->links[1] = NULL;
390 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
392 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
394 if (!pInstance->clientOnly)
396 /* Check for other way of setting Client-only !! */
397 pInstance->clientOnly =
398 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
401 TRACE("instance created - checking validity \n");
403 if (*pidInst == 0)
405 /* Initialisation of new Instance Identifier */
406 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
408 EnterCriticalSection(&WDML_CritSect);
410 if (WDML_InstanceList == NULL)
412 /* can't be another instance in this case, assign to the base pointer */
413 WDML_InstanceList = pInstance;
415 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
416 * present
417 * ------------------------------- NOTE NOTE NOTE --------------------------
419 * the manual is not clear if this condition
420 * applies to the first call to DdeInitialize from an application, or the
421 * first call for a given callback !!!
424 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
425 TRACE("First application instance detected OK\n");
426 /* allocate new instance ID */
427 WDML_IncrementInstanceId(pInstance);
429 else
431 /* really need to chain the new one in to the latest here, but after checking conditions
432 * such as trying to start a conversation from an application trying to monitor */
433 reference_inst = WDML_InstanceList;
434 TRACE("Subsequent application instance - starting checks\n");
435 while (reference_inst->next != NULL)
438 * This set of tests will work if application uses same instance Id
439 * at application level once allocated - which is what manual implies
440 * should happen. If someone tries to be
441 * clever (lazy ?) it will fail to pick up that later calls are for
442 * the same application - should we trust them ?
444 if (pInstance->instanceID == reference_inst->instanceID)
446 /* Check 1 - must be same Client-only state */
448 if (pInstance->clientOnly != reference_inst->clientOnly)
450 ret = DMLERR_DLL_USAGE;
451 goto theError;
454 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
456 if (pInstance->monitor != reference_inst->monitor)
458 ret = DMLERR_INVALIDPARAMETER;
459 goto theError;
462 /* Check 3 - must supply different callback address */
464 if (pInstance->callback == reference_inst->callback)
466 ret = DMLERR_DLL_USAGE;
467 goto theError;
470 reference_inst = reference_inst->next;
472 /* All cleared, add to chain */
474 TRACE("Application Instance checks finished\n");
475 WDML_IncrementInstanceId(pInstance);
476 reference_inst->next = pInstance;
478 LeaveCriticalSection(&WDML_CritSect);
480 *pidInst = pInstance->instanceID;
482 /* for deadlock issues, windows must always be created when outside the critical section */
483 wndclass.cbSize = sizeof(wndclass);
484 wndclass.style = 0;
485 wndclass.lpfnWndProc = WDML_EventProc;
486 wndclass.cbClsExtra = 0;
487 wndclass.cbWndExtra = sizeof(ULONG_PTR);
488 wndclass.hInstance = 0;
489 wndclass.hIcon = 0;
490 wndclass.hCursor = 0;
491 wndclass.hbrBackground = 0;
492 wndclass.lpszMenuName = NULL;
493 wndclass.lpszClassName = WDML_szEventClass;
494 wndclass.hIconSm = 0;
496 RegisterClassExW(&wndclass);
498 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
499 WS_POPUP, 0, 0, 0, 0,
500 0, 0, 0, 0);
502 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
504 TRACE("New application instance processing finished OK\n");
506 else
508 /* Reinitialisation situation --- FIX */
509 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
511 EnterCriticalSection(&WDML_CritSect);
513 if (WDML_InstanceList == NULL)
515 ret = DMLERR_INVALIDPARAMETER;
516 goto theError;
518 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
519 /* can't reinitialise if we have initialised nothing !! */
520 reference_inst = WDML_InstanceList;
521 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
523 * MS allows initialisation without specifying a callback, should we allow addition of the
524 * callback by a later call to initialise ? - if so this lot will have to change
526 while (reference_inst->next != NULL)
528 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
530 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
532 if (reference_inst->clientOnly)
534 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
536 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
538 if (!(afCmd & APPCMD_CLIENTONLY))
540 ret = DMLERR_INVALIDPARAMETER;
541 goto theError;
545 /* Check 2 - cannot change monitor modes */
547 if (pInstance->monitor != reference_inst->monitor)
549 ret = DMLERR_INVALIDPARAMETER;
550 goto theError;
553 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
555 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
557 ret = DMLERR_INVALIDPARAMETER;
558 goto theError;
560 break;
562 reference_inst = reference_inst->next;
564 if (reference_inst->next == NULL)
566 ret = DMLERR_INVALIDPARAMETER;
567 goto theError;
569 /* All checked - change relevant flags */
571 reference_inst->CBFflags = pInstance->CBFflags;
572 reference_inst->clientOnly = pInstance->clientOnly;
573 reference_inst->monitorFlags = pInstance->monitorFlags;
574 LeaveCriticalSection(&WDML_CritSect);
577 return DMLERR_NO_ERROR;
578 theError:
579 HeapFree(GetProcessHeap(), 0, pInstance);
580 LeaveCriticalSection(&WDML_CritSect);
581 return ret;
584 /******************************************************************************
585 * DdeInitializeA (USER32.@)
587 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
588 DWORD afCmd, DWORD ulRes)
590 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
593 /******************************************************************************
594 * DdeInitializeW [USER32.@]
595 * Registers an application with the DDEML
597 * PARAMS
598 * pidInst [I] Pointer to instance identifier
599 * pfnCallback [I] Pointer to callback function
600 * afCmd [I] Set of command and filter flags
601 * ulRes [I] Reserved
603 * RETURNS
604 * Success: DMLERR_NO_ERROR
605 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
607 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
608 DWORD afCmd, DWORD ulRes)
610 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
613 /*****************************************************************
614 * DdeUninitialize [USER32.@] Frees DDEML resources
616 * PARAMS
617 * idInst [I] Instance identifier
619 * RETURNS
620 * Success: TRUE
621 * Failure: FALSE
624 BOOL WINAPI DdeUninitialize(DWORD idInst)
626 /* Stage one - check if we have a handle for this instance
628 WDML_INSTANCE* pInstance;
629 WDML_CONV* pConv;
630 WDML_CONV* pConvNext;
632 TRACE("(%ld)\n", idInst);
634 EnterCriticalSection(&WDML_CritSect);
636 /* First check instance
638 pInstance = WDML_GetInstance(idInst);
639 if (pInstance == NULL)
641 LeaveCriticalSection(&WDML_CritSect);
643 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
645 return FALSE;
648 /* first terminate all conversations client side
649 * this shall close existing links...
651 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
653 pConvNext = pConv->next;
654 DdeDisconnect((HCONV)pConv);
656 if (pInstance->convs[WDML_CLIENT_SIDE])
657 FIXME("still pending conversations\n");
659 /* then unregister all known service names */
660 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
662 /* Free the nodes that were not freed by this instance
663 * and remove the nodes from the list of HSZ nodes.
665 WDML_FreeAllHSZ(pInstance);
667 DestroyWindow(pInstance->hwndEvent);
669 /* OK now delete the instance handle itself */
671 if (WDML_InstanceList == pInstance)
673 /* special case - the first/only entry */
674 WDML_InstanceList = pInstance->next;
676 else
678 /* general case, remove entry */
679 WDML_INSTANCE* inst;
681 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
682 inst->next = pInstance->next;
684 /* leave crit sect and release the heap entry
686 HeapFree(GetProcessHeap(), 0, pInstance);
687 LeaveCriticalSection(&WDML_CritSect);
688 return TRUE;
691 /******************************************************************
692 * WDML_NotifyThreadExit
696 void WDML_NotifyThreadDetach(void)
698 WDML_INSTANCE* pInstance;
699 WDML_INSTANCE* next;
700 DWORD tid = GetCurrentThreadId();
702 EnterCriticalSection(&WDML_CritSect);
703 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
705 next = pInstance->next;
706 if (pInstance->threadID == tid)
708 DdeUninitialize(pInstance->instanceID);
711 LeaveCriticalSection(&WDML_CritSect);
714 /******************************************************************
715 * WDML_InvokeCallback
719 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
720 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
721 ULONG_PTR dwData1, ULONG_PTR dwData2)
723 HDDEDATA ret;
725 if (pInstance == NULL)
726 return NULL;
727 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
728 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
729 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
730 if (pInstance->win16)
732 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
733 hsz1, hsz2, hdata, dwData1, dwData2);
735 else
737 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
739 TRACE("done => %p\n", ret);
740 return ret;
743 /*****************************************************************************
744 * WDML_GetInstance
746 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
747 * for an instance Id, or NULL if the entry does not exist
750 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
752 WDML_INSTANCE* pInstance;
754 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
756 if (pInstance->instanceID == instId)
758 if (GetCurrentThreadId() != pInstance->threadID)
760 FIXME("Tried to get instance from wrong thread\n");
761 continue;
763 return pInstance;
766 TRACE("Instance entry missing\n");
767 return NULL;
770 /******************************************************************
771 * WDML_GetInstanceFromWnd
775 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
777 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
780 /******************************************************************************
781 * DdeGetLastError [USER32.@] Gets most recent error code
783 * PARAMS
784 * idInst [I] Instance identifier
786 * RETURNS
787 * Last error code
789 UINT WINAPI DdeGetLastError(DWORD idInst)
791 DWORD error_code;
792 WDML_INSTANCE* pInstance;
794 EnterCriticalSection(&WDML_CritSect);
796 /* First check instance
798 pInstance = WDML_GetInstance(idInst);
799 if (pInstance == NULL)
801 error_code = DMLERR_INVALIDPARAMETER;
803 else
805 error_code = pInstance->lastError;
806 pInstance->lastError = 0;
809 LeaveCriticalSection(&WDML_CritSect);
810 return error_code;
813 /* ================================================================
815 * String management
817 * ================================================================ */
820 /******************************************************************
821 * WDML_FindNode
825 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
827 HSZNode* pNode;
829 if (pInstance == NULL) return NULL;
831 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
833 if (pNode->hsz == hsz) break;
835 if (!pNode) WARN("HSZ %p not found\n", hsz);
836 return pNode;
839 /******************************************************************
840 * WDML_MakeAtomFromHsz
842 * Creates a global atom from an existing HSZ
843 * Generally used before sending an HSZ as an atom to a remote app
845 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
847 WCHAR nameBuffer[MAX_BUFFER_LEN];
849 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
850 return GlobalAddAtomW(nameBuffer);
851 WARN("HSZ %p not found\n", hsz);
852 return 0;
855 /******************************************************************
856 * WDML_MakeHszFromAtom
858 * Creates a HSZ from an existing global atom
859 * Generally used while receiving a global atom and transforming it
860 * into an HSZ
862 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
864 WCHAR nameBuffer[MAX_BUFFER_LEN];
866 if (!atom) return NULL;
868 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
870 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
871 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
873 WARN("ATOM 0x%x not found\n", atom);
874 return 0;
877 /******************************************************************
878 * WDML_IncHSZ
882 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
884 HSZNode* pNode;
886 pNode = WDML_FindNode(pInstance, hsz);
887 if (!pNode) return FALSE;
889 pNode->refCount++;
890 return TRUE;
893 /******************************************************************************
894 * WDML_DecHSZ (INTERNAL)
896 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
897 * of HSZ nodes
898 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
900 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
902 HSZNode* pPrev = NULL;
903 HSZNode* pCurrent;
905 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
907 /* If we found the node we were looking for and its ref count is one,
908 * we can remove it
910 if (pCurrent->hsz == hsz)
912 if (--pCurrent->refCount == 0)
914 if (pCurrent == pInstance->nodeList)
916 pInstance->nodeList = pCurrent->next;
918 else
920 pPrev->next = pCurrent->next;
922 HeapFree(GetProcessHeap(), 0, pCurrent);
923 DeleteAtom(HSZ2ATOM(hsz));
925 return TRUE;
928 WARN("HSZ %p not found\n", hsz);
930 return FALSE;
933 /******************************************************************************
934 * WDML_FreeAllHSZ (INTERNAL)
936 * Frees up all the strings still allocated in the list and
937 * remove all the nodes from the list of HSZ nodes.
939 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
941 /* Free any strings created in this instance.
943 while (pInstance->nodeList != NULL)
945 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
949 /******************************************************************************
950 * InsertHSZNode (INTERNAL)
952 * Insert a node to the head of the list.
954 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
956 if (hsz != 0)
958 HSZNode* pNew = NULL;
959 /* Create a new node for this HSZ.
961 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
962 if (pNew != NULL)
964 pNew->hsz = hsz;
965 pNew->next = pInstance->nodeList;
966 pNew->refCount = 1;
967 pInstance->nodeList = pNew;
969 else
971 ERR("Primary HSZ Node allocation failed - out of memory\n");
976 /******************************************************************
977 * WDML_QueryString
981 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
982 int codepage)
984 WCHAR pString[MAX_BUFFER_LEN];
985 int ret;
986 /* If psz is null, we have to return only the length
987 * of the string.
989 if (ptr == NULL)
991 ptr = pString;
992 cchMax = MAX_BUFFER_LEN;
995 switch (codepage)
997 case CP_WINANSI:
998 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
999 break;
1000 case CP_WINUNICODE:
1001 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1002 break;
1003 default:
1004 ERR("Unknown code page %d\n", codepage);
1005 ret = 0;
1007 return ret;
1010 /*****************************************************************
1011 * DdeQueryStringA [USER32.@]
1013 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1015 DWORD ret = 0;
1016 WDML_INSTANCE* pInstance;
1018 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1020 EnterCriticalSection(&WDML_CritSect);
1022 /* First check instance
1024 pInstance = WDML_GetInstance(idInst);
1025 if (pInstance != NULL)
1027 if (iCodePage == 0) iCodePage = CP_WINANSI;
1028 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1030 LeaveCriticalSection(&WDML_CritSect);
1032 TRACE("returning %ld (%s)\n", ret, debugstr_a(psz));
1033 return ret;
1036 /*****************************************************************
1037 * DdeQueryStringW [USER32.@]
1040 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1042 DWORD ret = 0;
1043 WDML_INSTANCE* pInstance;
1045 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1047 EnterCriticalSection(&WDML_CritSect);
1049 /* First check instance
1051 pInstance = WDML_GetInstance(idInst);
1052 if (pInstance != NULL)
1054 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1055 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1057 LeaveCriticalSection(&WDML_CritSect);
1059 TRACE("returning %ld (%s)\n", ret, debugstr_w(psz));
1060 return ret;
1063 /******************************************************************
1064 * DML_CreateString
1068 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1070 HSZ hsz;
1072 switch (codepage)
1074 case CP_WINANSI:
1075 hsz = ATOM2HSZ(AddAtomA(ptr));
1076 TRACE("added atom %s with HSZ %p, \n", debugstr_a(ptr), hsz);
1077 break;
1078 case CP_WINUNICODE:
1079 hsz = ATOM2HSZ(AddAtomW(ptr));
1080 TRACE("added atom %s with HSZ %p, \n", debugstr_w(ptr), hsz);
1081 break;
1082 default:
1083 ERR("Unknown code page %d\n", codepage);
1084 return 0;
1086 WDML_InsertHSZNode(pInstance, hsz);
1087 return hsz;
1090 /*****************************************************************
1091 * DdeCreateStringHandleA [USER32.@]
1093 * RETURNS
1094 * Success: String handle
1095 * Failure: 0
1097 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1099 HSZ hsz = 0;
1100 WDML_INSTANCE* pInstance;
1102 TRACE("(%ld,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1104 EnterCriticalSection(&WDML_CritSect);
1106 pInstance = WDML_GetInstance(idInst);
1107 if (pInstance)
1109 if (codepage == 0) codepage = CP_WINANSI;
1110 hsz = WDML_CreateString(pInstance, psz, codepage);
1113 LeaveCriticalSection(&WDML_CritSect);
1114 return hsz;
1118 /******************************************************************************
1119 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1121 * PARAMS
1122 * idInst [I] Instance identifier
1123 * psz [I] Pointer to string
1124 * codepage [I] Code page identifier
1125 * RETURNS
1126 * Success: String handle
1127 * Failure: 0
1129 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1131 WDML_INSTANCE* pInstance;
1132 HSZ hsz = 0;
1134 TRACE("(%ld,%s,%d)\n", idInst, debugstr_w(psz), codepage);
1136 EnterCriticalSection(&WDML_CritSect);
1138 pInstance = WDML_GetInstance(idInst);
1139 if (pInstance)
1141 if (codepage == 0) codepage = CP_WINUNICODE;
1142 hsz = WDML_CreateString(pInstance, psz, codepage);
1144 LeaveCriticalSection(&WDML_CritSect);
1146 return hsz;
1149 /*****************************************************************
1150 * DdeFreeStringHandle (USER32.@)
1151 * RETURNS: 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: success: nonzero
1178 * fail: zero
1180 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1182 WDML_INSTANCE* pInstance;
1183 BOOL ret = FALSE;
1185 TRACE("(%ld,%p): \n", idInst, hsz);
1187 EnterCriticalSection(&WDML_CritSect);
1189 /* First check instance
1191 pInstance = WDML_GetInstance(idInst);
1192 if (pInstance)
1193 ret = WDML_IncHSZ(pInstance, hsz);
1195 LeaveCriticalSection(&WDML_CritSect);
1196 return ret;
1199 /*****************************************************************
1200 * DdeCmpStringHandles (USER32.@)
1202 * Compares the value of two string handles. This comparison is
1203 * not case sensitive.
1205 * Returns:
1206 * -1 The value of hsz1 is zero or less than hsz2
1207 * 0 The values of hsz 1 and 2 are the same or both zero.
1208 * 1 The value of hsz2 is zero of less than hsz1
1210 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1212 WCHAR psz1[MAX_BUFFER_LEN];
1213 WCHAR psz2[MAX_BUFFER_LEN];
1214 int ret = 0;
1215 int ret1, ret2;
1217 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1218 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1220 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1222 /* Make sure we found both strings. */
1223 if (ret1 == 0 && ret2 == 0)
1225 /* If both are not found, return both "zero strings". */
1226 ret = 0;
1228 else if (ret1 == 0)
1230 /* If hsz1 is a not found, return hsz1 is "zero string". */
1231 ret = -1;
1233 else if (ret2 == 0)
1235 /* If hsz2 is a not found, return hsz2 is "zero string". */
1236 ret = 1;
1238 else
1240 /* Compare the two strings we got (case insensitive). */
1241 ret = lstrcmpiW(psz1, psz2);
1242 /* Since strcmp returns any number smaller than
1243 * 0 when the first string is found to be less than
1244 * the second one we must make sure we are returning
1245 * the proper values.
1247 if (ret < 0)
1249 ret = -1;
1251 else if (ret > 0)
1253 ret = 1;
1257 return ret;
1260 /* ================================================================
1262 * Data handle management
1264 * ================================================================ */
1266 /*****************************************************************
1267 * DdeCreateDataHandle (USER32.@)
1269 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1270 HSZ hszItem, UINT wFmt, UINT afCmd)
1272 /* For now, we ignore idInst, hszItem.
1273 * The purpose of these arguments still need to be investigated.
1276 HGLOBAL hMem;
1277 LPBYTE pByte;
1278 DDE_DATAHANDLE_HEAD* pDdh;
1279 WCHAR psz[MAX_BUFFER_LEN];
1281 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1283 psz[0] = HSZ2ATOM(hszItem);
1284 psz[1] = 0;
1287 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,fmt %04x,%x)\n",
1288 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1290 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1291 return 0;
1293 /* we use the first 4 bytes to store the size */
1294 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1296 ERR("GlobalAlloc failed\n");
1297 return 0;
1300 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1301 if (!pDdh)
1303 GlobalFree(hMem);
1304 return 0;
1307 pDdh->cfFormat = wFmt;
1308 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1310 pByte = (LPBYTE)(pDdh + 1);
1311 if (pSrc)
1313 memcpy(pByte, pSrc + cbOff, cb);
1315 GlobalUnlock(hMem);
1317 TRACE("=> %p\n", hMem);
1318 return (HDDEDATA)hMem;
1321 /*****************************************************************
1323 * DdeAddData (USER32.@)
1325 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1327 DWORD old_sz, new_sz;
1328 LPBYTE pDst;
1330 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData, pSrc, cb, cbOff);
1332 pDst = DdeAccessData(hData, &old_sz);
1333 if (!pDst) return 0;
1335 new_sz = cb + cbOff;
1336 if (new_sz > old_sz)
1338 DdeUnaccessData(hData);
1339 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1340 GMEM_MOVEABLE | GMEM_DDESHARE);
1341 pDst = DdeAccessData(hData, &old_sz);
1344 if (!pDst) return 0;
1346 memcpy(pDst + cbOff, pSrc, cb);
1347 DdeUnaccessData(hData);
1348 return hData;
1351 /******************************************************************************
1352 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1355 * PARAMS
1356 * hData [I] Handle to DDE object
1357 * pDst [I] Pointer to destination buffer
1358 * cbMax [I] Amount of data to copy
1359 * cbOff [I] Offset to beginning of data
1361 * RETURNS
1362 * Size of memory object associated with handle
1364 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1366 DWORD dwSize, dwRet;
1367 LPBYTE pByte;
1369 TRACE("(%p,%p,%ld,%ld)\n", hData, pDst, cbMax, cbOff);
1371 pByte = DdeAccessData(hData, &dwSize);
1373 if (pByte)
1375 if (!pDst)
1377 dwRet = dwSize;
1379 else if (cbOff + cbMax < dwSize)
1381 dwRet = cbMax;
1383 else if (cbOff < dwSize)
1385 dwRet = dwSize - cbOff;
1387 else
1389 dwRet = 0;
1391 if (pDst && dwRet != 0)
1393 memcpy(pDst, pByte + cbOff, dwRet);
1395 DdeUnaccessData(hData);
1397 else
1399 dwRet = 0;
1401 return dwRet;
1404 /*****************************************************************
1405 * DdeAccessData (USER32.@)
1407 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1409 HGLOBAL hMem = (HGLOBAL)hData;
1410 DDE_DATAHANDLE_HEAD* pDdh;
1412 TRACE("(%p,%p)\n", hData, pcbDataSize);
1414 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1415 if (pDdh == NULL)
1417 ERR("Failed on GlobalLock(%p)\n", hMem);
1418 return 0;
1421 if (pcbDataSize != NULL)
1423 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1425 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1426 return (LPBYTE)(pDdh + 1);
1429 /*****************************************************************
1430 * DdeUnaccessData (USER32.@)
1432 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1434 HGLOBAL hMem = (HGLOBAL)hData;
1436 TRACE("(%p)\n", hData);
1438 GlobalUnlock(hMem);
1440 return TRUE;
1443 /*****************************************************************
1444 * DdeFreeDataHandle (USER32.@)
1446 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1448 TRACE("(%p)\n", hData);
1449 return GlobalFree((HGLOBAL)hData) == 0;
1452 /******************************************************************
1453 * WDML_IsAppOwned
1457 BOOL WDML_IsAppOwned(HDDEDATA hData)
1459 DDE_DATAHANDLE_HEAD* pDdh;
1460 BOOL ret = FALSE;
1462 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1463 if (pDdh != NULL)
1465 ret = pDdh->bAppOwned;
1466 GlobalUnlock((HGLOBAL)hData);
1468 return ret;
1471 /* ================================================================
1473 * Global <=> Data handle management
1475 * ================================================================ */
1477 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1478 * offset size
1479 * (bytes) (bits) comment
1480 * 0 16 bit fields for options (release, ackreq, response...)
1481 * 2 16 clipboard format
1482 * 4 ? data to be used
1484 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1486 DDEDATA* pDd;
1487 HDDEDATA ret = 0;
1488 DWORD size;
1490 if (hMem)
1492 pDd = GlobalLock(hMem);
1493 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1494 if (pDd)
1496 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1497 switch (pDd->cfFormat)
1499 default:
1500 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1501 pDd->cfFormat, hMem);
1502 /* fall thru */
1503 case 0:
1504 case CF_TEXT:
1505 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1506 break;
1507 case CF_BITMAP:
1508 if (size >= sizeof(BITMAP))
1510 BITMAP* bmp = (BITMAP*)pDd->Value;
1511 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1512 if (size >= sizeof(BITMAP) + count)
1514 HBITMAP hbmp;
1516 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1517 bmp->bmPlanes, bmp->bmBitsPixel,
1518 pDd->Value + sizeof(BITMAP))))
1520 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1521 0, 0, CF_BITMAP, 0);
1523 else ERR("Can't create bmp\n");
1525 else
1527 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1529 } else ERR("No bitmap header\n");
1530 break;
1532 GlobalUnlock(hMem);
1535 return ret;
1538 /******************************************************************
1539 * WDML_DataHandle2Global
1543 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1544 BOOL fDeferUpd, BOOL fAckReq)
1546 DDE_DATAHANDLE_HEAD* pDdh;
1547 DWORD dwSize;
1548 HGLOBAL hMem = 0;
1550 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1551 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1552 if (dwSize && pDdh)
1554 WINE_DDEHEAD* wdh = NULL;
1556 switch (pDdh->cfFormat)
1558 default:
1559 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1560 pDdh->cfFormat, hDdeData);
1561 /* fall thru */
1562 case 0:
1563 case CF_TEXT:
1564 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1565 if (hMem && (wdh = GlobalLock(hMem)))
1567 memcpy(wdh + 1, pDdh + 1, dwSize);
1569 break;
1570 case CF_BITMAP:
1571 if (dwSize >= sizeof(HBITMAP))
1573 BITMAP bmp;
1574 DWORD count;
1575 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1577 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1579 count = bmp.bmWidthBytes * bmp.bmHeight;
1580 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1581 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1582 if (hMem && (wdh = GlobalLock(hMem)))
1584 memcpy(wdh + 1, &bmp, sizeof(bmp));
1585 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1589 break;
1591 if (wdh)
1593 wdh->unused = 0;
1594 wdh->fResponse = fResponse;
1595 wdh->fRelease = fRelease;
1596 wdh->fDeferUpd = fDeferUpd;
1597 wdh->fAckReq = fAckReq;
1598 wdh->cfFormat = pDdh->cfFormat;
1599 GlobalUnlock(hMem);
1601 GlobalUnlock((HGLOBAL)hDdeData);
1604 return hMem;
1607 /* ================================================================
1609 * Server management
1611 * ================================================================ */
1613 /******************************************************************
1614 * WDML_AddServer
1618 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1620 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1621 WDML_SERVER* pServer;
1622 WCHAR buf1[256];
1623 WCHAR buf2[256];
1625 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1626 if (pServer == NULL) return NULL;
1628 pServer->hszService = hszService;
1629 WDML_IncHSZ(pInstance, hszService);
1631 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1632 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1633 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1635 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1636 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1638 pServer->filterOn = TRUE;
1640 pServer->next = pInstance->servers;
1641 pInstance->servers = pServer;
1642 return pServer;
1645 /******************************************************************
1646 * WDML_RemoveServer
1650 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1652 WDML_SERVER* pPrev = NULL;
1653 WDML_SERVER* pServer = NULL;
1654 WDML_CONV* pConv;
1655 WDML_CONV* pConvNext;
1657 pServer = pInstance->servers;
1659 while (pServer != NULL)
1661 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1663 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1664 pServer->atomService, pServer->atomServiceSpec);
1665 /* terminate all conversations for given topic */
1666 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1668 pConvNext = pConv->next;
1669 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1671 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1672 /* don't care about return code (whether client window is present or not) */
1673 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
1676 if (pServer == pInstance->servers)
1678 pInstance->servers = pServer->next;
1680 else
1682 pPrev->next = pServer->next;
1685 DestroyWindow(pServer->hwndServer);
1686 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1687 WDML_DecHSZ(pInstance, pServer->hszService);
1689 GlobalDeleteAtom(pServer->atomService);
1690 GlobalDeleteAtom(pServer->atomServiceSpec);
1692 HeapFree(GetProcessHeap(), 0, pServer);
1693 break;
1696 pPrev = pServer;
1697 pServer = pServer->next;
1701 /*****************************************************************************
1702 * WDML_FindServer
1704 * generic routine to return a pointer to the relevant ServiceNode
1705 * for a given service name, or NULL if the entry does not exist
1708 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1710 WDML_SERVER* pServer;
1712 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1714 if (hszService == pServer->hszService)
1716 return pServer;
1719 TRACE("Service name missing\n");
1720 return NULL;
1723 /* ================================================================
1725 * Conversation management
1727 * ================================================================ */
1729 /******************************************************************
1730 * WDML_AddConv
1734 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1735 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1737 WDML_CONV* pConv;
1739 /* no converstation yet, add it */
1740 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1741 if (!pConv) return NULL;
1743 pConv->instance = pInstance;
1744 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1745 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1746 pConv->hwndServer = hwndServer;
1747 pConv->hwndClient = hwndClient;
1748 pConv->transactions = NULL;
1749 pConv->hUser = 0;
1750 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1751 /* check if both side of the conversation are of the same instance */
1752 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1753 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1755 pConv->wStatus |= ST_ISSELF;
1757 pConv->wConvst = XST_NULL;
1759 pConv->next = pInstance->convs[side];
1760 pInstance->convs[side] = pConv;
1762 return pConv;
1765 /******************************************************************
1766 * WDML_FindConv
1770 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1771 HSZ hszService, HSZ hszTopic)
1773 WDML_CONV* pCurrent = NULL;
1775 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1777 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1778 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1780 return pCurrent;
1784 return NULL;
1787 /******************************************************************
1788 * WDML_RemoveConv
1792 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1794 WDML_CONV* pPrev = NULL;
1795 WDML_CONV* pCurrent;
1796 WDML_XACT* pXAct;
1797 WDML_XACT* pXActNext;
1798 HWND hWnd;
1800 if (!pRef)
1801 return;
1803 /* remove any pending transaction */
1804 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1806 pXActNext = pXAct->next;
1807 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1810 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1812 /* FIXME: should we keep the window around ? it seems so (at least on client side
1813 * to let QueryConvInfo work after conv termination, but also to implement
1814 * DdeReconnect...
1816 /* destroy conversation window, but first remove pConv from hWnd.
1817 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1819 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1820 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1822 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1824 WDML_DecHSZ(pRef->instance, pRef->hszService);
1825 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1827 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1829 if (pCurrent == pRef)
1831 if (pCurrent == pRef->instance->convs[side])
1833 pRef->instance->convs[side] = pCurrent->next;
1835 else
1837 pPrev->next = pCurrent->next;
1840 HeapFree(GetProcessHeap(), 0, pCurrent);
1841 break;
1846 /******************************************************************
1847 * WDML_EnableCallback
1849 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1851 if (wCmd == EC_DISABLE)
1853 FIXME("EC_DISABLE is not implemented\n");
1854 return TRUE;
1857 if (wCmd == EC_QUERYWAITING)
1858 return pConv->transactions ? TRUE : FALSE;
1860 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1862 FIXME("Unknown command code %04x\n", wCmd);
1863 return FALSE;
1866 while (pConv->transactions)
1868 WDML_XACT *pXAct = pConv->transactions;
1869 WDML_UnQueueTransaction(pConv, pXAct);
1871 if (pConv->wStatus & ST_CLIENT)
1873 /*WDML_ClientHandle(pConv, pXAct);*/
1874 FIXME("Client delayed transaction queue handling is not supported\n");
1876 else
1877 WDML_ServerHandle(pConv, pXAct);
1879 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1881 if (wCmd == EC_ENABLEONE) break;
1883 return TRUE;
1886 /*****************************************************************
1887 * DdeEnableCallback (USER32.@)
1889 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1891 BOOL ret = FALSE;
1892 WDML_CONV *pConv;
1894 TRACE("(%ld, %p, %04x)\n", idInst, hConv, wCmd);
1896 EnterCriticalSection(&WDML_CritSect);
1898 pConv = WDML_GetConv(hConv, TRUE);
1900 if (pConv && pConv->instance->instanceID == idInst)
1901 ret = WDML_EnableCallback(pConv, wCmd);
1903 LeaveCriticalSection(&WDML_CritSect);
1904 return ret;
1907 /******************************************************************
1908 * WDML_GetConv
1912 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1914 WDML_CONV* pConv = (WDML_CONV*)hConv;
1916 /* FIXME: should do better checking */
1917 if (pConv == NULL) return NULL;
1919 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1921 FIXME("found conv but ain't connected\n");
1922 return NULL;
1924 if (GetCurrentThreadId() != pConv->instance->threadID)
1926 FIXME("wrong thread ID\n");
1927 return NULL;
1930 return pConv;
1933 /******************************************************************
1934 * WDML_GetConvFromWnd
1938 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1940 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
1943 /******************************************************************
1944 * WDML_PostAck
1948 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1949 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1951 DDEACK ddeAck;
1952 HWND from, to;
1954 if (side == WDML_SERVER_SIDE)
1956 from = pConv->hwndServer;
1957 to = pConv->hwndClient;
1959 else
1961 to = pConv->hwndServer;
1962 from = pConv->hwndClient;
1965 ddeAck.bAppReturnCode = appRetCode;
1966 ddeAck.reserved = 0;
1967 ddeAck.fBusy = fBusy;
1968 ddeAck.fAck = fAck;
1970 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1972 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1973 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1974 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
1976 pConv->wStatus &= ~ST_CONNECTED;
1977 FreeDDElParam(WM_DDE_ACK, lParam);
1978 return FALSE;
1980 return TRUE;
1983 /*****************************************************************
1984 * DdeSetUserHandle (USER32.@)
1986 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1988 WDML_CONV* pConv;
1989 BOOL ret = TRUE;
1991 TRACE("(%p,%lx,%lx)\n", hConv, id, hUser);
1993 EnterCriticalSection(&WDML_CritSect);
1995 pConv = WDML_GetConv(hConv, FALSE);
1996 if (pConv == NULL)
1998 ret = FALSE;
1999 goto theError;
2001 if (id == QID_SYNC)
2003 pConv->hUser = hUser;
2005 else
2007 WDML_XACT* pXAct;
2009 pXAct = WDML_FindTransaction(pConv, id);
2010 if (pXAct)
2012 pXAct->hUser = hUser;
2014 else
2016 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2017 ret = FALSE;
2020 theError:
2021 LeaveCriticalSection(&WDML_CritSect);
2022 return ret;
2025 /******************************************************************
2026 * WDML_GetLocalConvInfo
2030 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2032 BOOL ret = TRUE;
2033 WDML_LINK* pLink;
2034 WDML_SIDE side;
2036 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2037 ci->hszSvcPartner = pConv->hszService;
2038 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2039 ci->hszTopic = pConv->hszTopic;
2040 ci->wStatus = pConv->wStatus;
2042 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2044 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2046 if (pLink->hConv == (HCONV)pConv)
2048 ci->wStatus |= ST_ADVISE;
2049 break;
2053 /* FIXME: non handled status flags:
2054 ST_BLOCKED
2055 ST_BLOCKNEXT
2056 ST_INLIST
2059 ci->wConvst = pConv->wConvst; /* FIXME */
2061 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2062 ci->hConvList = 0;
2063 ci->ConvCtxt = pConv->convContext;
2064 if (ci->wStatus & ST_CLIENT)
2066 ci->hwnd = pConv->hwndClient;
2067 ci->hwndPartner = pConv->hwndServer;
2069 else
2071 ci->hwnd = pConv->hwndServer;
2072 ci->hwndPartner = pConv->hwndClient;
2074 if (id == QID_SYNC)
2076 ci->hUser = pConv->hUser;
2077 ci->hszItem = 0;
2078 ci->wFmt = 0;
2079 ci->wType = 0;
2081 else
2083 WDML_XACT* pXAct;
2085 pXAct = WDML_FindTransaction(pConv, id);
2086 if (pXAct)
2088 ci->hUser = pXAct->hUser;
2089 ci->hszItem = pXAct->hszItem;
2090 ci->wFmt = pXAct->wFmt;
2091 ci->wType = pXAct->wType;
2093 else
2095 ret = 0;
2096 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2099 return ret;
2102 /******************************************************************
2103 * DdeQueryConvInfo (USER32.@)
2106 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2108 UINT ret = lpConvInfo->cb;
2109 CONVINFO ci;
2110 WDML_CONV* pConv;
2112 TRACE("(%p,%lx,%p)\n", hConv, id, lpConvInfo);
2114 if (!hConv)
2116 FIXME("hConv is NULL\n");
2117 return 0;
2120 EnterCriticalSection(&WDML_CritSect);
2122 pConv = WDML_GetConv(hConv, FALSE);
2123 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2125 ret = 0;
2127 else if ((ULONG_PTR)hConv & 1)
2129 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2130 if (pConv != NULL)
2132 FIXME("Request on remote conversation information is not implemented yet\n");
2133 ret = 0;
2136 LeaveCriticalSection(&WDML_CritSect);
2137 if (ret != 0)
2138 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2139 return ret;
2142 /* ================================================================
2144 * Link (hot & warm) management
2146 * ================================================================ */
2148 /******************************************************************
2149 * WDML_AddLink
2153 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2154 UINT wType, HSZ hszItem, UINT wFmt)
2156 WDML_LINK* pLink;
2158 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2159 if (pLink == NULL)
2161 ERR("OOM\n");
2162 return;
2165 pLink->hConv = hConv;
2166 pLink->transactionType = wType;
2167 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2168 pLink->uFmt = wFmt;
2169 pLink->next = pInstance->links[side];
2170 pInstance->links[side] = pLink;
2173 /******************************************************************
2174 * WDML_RemoveLink
2178 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2179 HSZ hszItem, UINT uFmt)
2181 WDML_LINK* pPrev = NULL;
2182 WDML_LINK* pCurrent = NULL;
2184 pCurrent = pInstance->links[side];
2186 while (pCurrent != NULL)
2188 if (pCurrent->hConv == hConv &&
2189 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2190 pCurrent->uFmt == uFmt)
2192 if (pCurrent == pInstance->links[side])
2194 pInstance->links[side] = pCurrent->next;
2196 else
2198 pPrev->next = pCurrent->next;
2201 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2202 HeapFree(GetProcessHeap(), 0, pCurrent);
2203 break;
2206 pPrev = pCurrent;
2207 pCurrent = pCurrent->next;
2211 /* this function is called to remove all links related to the conv.
2212 It should be called from both client and server when terminating
2213 the conversation.
2215 /******************************************************************
2216 * WDML_RemoveAllLinks
2220 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2222 WDML_LINK* pPrev = NULL;
2223 WDML_LINK* pCurrent = NULL;
2224 WDML_LINK* pNext = NULL;
2226 pCurrent = pInstance->links[side];
2228 while (pCurrent != NULL)
2230 if (pCurrent->hConv == (HCONV)pConv)
2232 if (pCurrent == pInstance->links[side])
2234 pInstance->links[side] = pCurrent->next;
2235 pNext = pCurrent->next;
2237 else
2239 pPrev->next = pCurrent->next;
2240 pNext = pCurrent->next;
2243 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2245 HeapFree(GetProcessHeap(), 0, pCurrent);
2246 pCurrent = NULL;
2249 if (pCurrent)
2251 pPrev = pCurrent;
2252 pCurrent = pCurrent->next;
2254 else
2256 pCurrent = pNext;
2261 /******************************************************************
2262 * WDML_FindLink
2266 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2267 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2269 WDML_LINK* pCurrent = NULL;
2271 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2273 /* we don't need to check for transaction type as it can be altered */
2275 if (pCurrent->hConv == hConv &&
2276 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2277 (!use_fmt || pCurrent->uFmt == uFmt))
2279 break;
2284 return pCurrent;
2287 /* ================================================================
2289 * Transaction management
2291 * ================================================================ */
2293 /******************************************************************
2294 * WDML_AllocTransaction
2296 * Alloc a transaction structure for handling the message ddeMsg
2298 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2299 UINT wFmt, HSZ hszItem)
2301 WDML_XACT* pXAct;
2302 static WORD tid = 1; /* FIXME: wrap around */
2304 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2305 if (!pXAct)
2307 pInstance->lastError = DMLERR_MEMORY_ERROR;
2308 return NULL;
2311 pXAct->xActID = tid++;
2312 pXAct->ddeMsg = ddeMsg;
2313 pXAct->hDdeData = 0;
2314 pXAct->hUser = 0;
2315 pXAct->next = NULL;
2316 pXAct->wType = 0;
2317 pXAct->wFmt = wFmt;
2318 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2319 pXAct->atom = 0;
2320 pXAct->hMem = 0;
2321 pXAct->lParam = 0;
2323 return pXAct;
2326 /******************************************************************
2327 * WDML_QueueTransaction
2329 * Adds a transaction to the list of transaction
2331 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2333 WDML_XACT** pt;
2335 /* advance to last in queue */
2336 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2337 *pt = pXAct;
2340 /******************************************************************
2341 * WDML_UnQueueTransaction
2345 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2347 WDML_XACT** pt;
2349 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2351 if (*pt == pXAct)
2353 *pt = pXAct->next;
2354 return TRUE;
2357 return FALSE;
2360 /******************************************************************
2361 * WDML_FreeTransaction
2365 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2367 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2368 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2370 GlobalFree(pXAct->hMem);
2372 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2374 HeapFree(GetProcessHeap(), 0, pXAct);
2377 /******************************************************************
2378 * WDML_FindTransaction
2382 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2384 WDML_XACT* pXAct;
2386 tid = HIWORD(tid);
2387 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2389 if (pXAct->xActID == tid)
2390 break;
2392 return pXAct;
2395 /* ================================================================
2397 * Information broadcast across DDEML implementations
2399 * ================================================================ */
2401 struct tagWDML_BroadcastPmt
2403 LPCWSTR clsName;
2404 UINT uMsg;
2405 WPARAM wParam;
2406 LPARAM lParam;
2409 /******************************************************************
2410 * WDML_BroadcastEnumProc
2414 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2416 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2417 WCHAR buffer[128];
2419 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2420 lstrcmpiW(buffer, s->clsName) == 0)
2422 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2424 return TRUE;
2427 /******************************************************************
2428 * WDML_BroadcastDDEWindows
2432 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2434 struct tagWDML_BroadcastPmt s;
2436 s.clsName = clsName;
2437 s.uMsg = uMsg;
2438 s.wParam = wParam;
2439 s.lParam = lParam;
2440 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);