push 2f3ca95c4974ba229fa47d638b3044f50788f3bd
[wine/hacks.git] / dlls / user32 / dde_misc.c
blob2b7fb81b892904c45a055c72a5970f9debd14915
1 /*
2 * DDEML library
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
7 * Copyright 2000 Corel
8 * Copyright 2001 Eric Pouech
9 * Copyright 2003, 2004, 2005 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wine/port.h"
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "dde.h"
37 #include "ddeml.h"
38 #include "win.h"
39 #include "dde_private.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
45 /* convert between ATOM and HSZ avoiding compiler warnings */
46 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
47 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
49 static WDML_INSTANCE* WDML_InstanceList = NULL;
50 static LONG WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
51 const WCHAR WDML_szEventClass[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
53 /* protection for instance list */
54 static CRITICAL_SECTION WDML_CritSect;
55 static CRITICAL_SECTION_DEBUG critsect_debug =
57 0, 0, &WDML_CritSect,
58 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
59 0, 0, { (DWORD_PTR)(__FILE__ ": WDML_CritSect") }
61 static CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
63 /* ================================================================
65 * Pure DDE (non DDEML) management
67 * ================================================================ */
70 /*****************************************************************
71 * PackDDElParam (USER32.@)
73 * RETURNS
74 * the packed lParam
76 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
78 HGLOBAL hMem;
79 UINT_PTR *params;
81 switch (msg)
83 case WM_DDE_ACK:
84 case WM_DDE_ADVISE:
85 case WM_DDE_DATA:
86 case WM_DDE_POKE:
87 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
89 ERR("GlobalAlloc failed\n");
90 return 0;
92 if (!(params = GlobalLock(hMem)))
94 ERR("GlobalLock failed (%p)\n", hMem);
95 return 0;
97 params[0] = uiLo;
98 params[1] = uiHi;
99 GlobalUnlock(hMem);
100 return (LPARAM)hMem;
102 case WM_DDE_EXECUTE:
103 return uiHi;
105 default:
106 return MAKELONG(uiLo, uiHi);
111 /*****************************************************************
112 * UnpackDDElParam (USER32.@)
114 * RETURNS
115 * success: nonzero
116 * failure: zero
118 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
119 PUINT_PTR uiLo, PUINT_PTR uiHi)
121 UINT_PTR *params;
123 switch (msg)
125 case WM_DDE_ACK:
126 case WM_DDE_ADVISE:
127 case WM_DDE_DATA:
128 case WM_DDE_POKE:
129 if (!lParam || !(params = GlobalLock((HGLOBAL)lParam)))
131 if (uiLo) *uiLo = 0;
132 if (uiHi) *uiHi = 0;
133 return FALSE;
135 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 %08lx %08lx\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 %d 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%x,%d,0x%x)\n",
352 pidInst, pfnCallback, afCmd, ulRes, bUnicode);
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->wStatus = 0;
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 %X\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%x,%d): stub\n", pidInst, pfnCallback, afCmd, ulRes);
511 EnterCriticalSection(&WDML_CritSect);
513 if (WDML_InstanceList == NULL)
515 ret = DMLERR_INVALIDPARAMETER;
516 goto theError;
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;
574 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
576 LeaveCriticalSection(&WDML_CritSect);
579 return DMLERR_NO_ERROR;
580 theError:
581 HeapFree(GetProcessHeap(), 0, pInstance);
582 LeaveCriticalSection(&WDML_CritSect);
583 return ret;
586 /******************************************************************************
587 * DdeInitializeA (USER32.@)
589 * See DdeInitializeW.
591 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
592 DWORD afCmd, DWORD ulRes)
594 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
597 /******************************************************************************
598 * DdeInitializeW [USER32.@]
599 * Registers an application with the DDEML
601 * PARAMS
602 * pidInst [I] Pointer to instance identifier
603 * pfnCallback [I] Pointer to callback function
604 * afCmd [I] Set of command and filter flags
605 * ulRes [I] Reserved
607 * RETURNS
608 * Success: DMLERR_NO_ERROR
609 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
611 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
612 DWORD afCmd, DWORD ulRes)
614 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
617 /*****************************************************************
618 * DdeUninitialize [USER32.@] Frees DDEML resources
620 * PARAMS
621 * idInst [I] Instance identifier
623 * RETURNS
624 * Success: TRUE
625 * Failure: FALSE
628 BOOL WINAPI DdeUninitialize(DWORD idInst)
630 /* Stage one - check if we have a handle for this instance
632 WDML_INSTANCE* pInstance;
633 WDML_CONV* pConv;
634 WDML_CONV* pConvNext;
636 TRACE("(%d)\n", idInst);
638 /* First check instance
640 pInstance = WDML_GetInstance(idInst);
641 if (pInstance == NULL)
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 /* release the heap entry
687 HeapFree(GetProcessHeap(), 0, pInstance);
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 LeaveCriticalSection(&WDML_CritSect);
710 DdeUninitialize(pInstance->instanceID);
711 EnterCriticalSection(&WDML_CritSect);
714 LeaveCriticalSection(&WDML_CritSect);
717 /******************************************************************
718 * WDML_InvokeCallback
722 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
723 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
724 ULONG_PTR dwData1, ULONG_PTR dwData2)
726 HDDEDATA ret;
728 if (pInstance == NULL)
729 return NULL;
731 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
732 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
733 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
734 if (pInstance->win16)
736 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
737 hsz1, hsz2, hdata, dwData1, dwData2);
739 else
741 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
743 TRACE("done => %p\n", ret);
744 return ret;
747 /*****************************************************************************
748 * WDML_GetInstance
750 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
751 * for an instance Id, or NULL if the entry does not exist
754 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
756 WDML_INSTANCE* pInstance;
758 EnterCriticalSection(&WDML_CritSect);
760 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
762 if (pInstance->instanceID == instId)
764 if (GetCurrentThreadId() != pInstance->threadID)
766 FIXME("Tried to get instance from wrong thread\n");
767 continue;
769 break;
773 LeaveCriticalSection(&WDML_CritSect);
775 if (!pInstance)
776 WARN("Instance entry missing for id %04x\n", instId);
777 return pInstance;
780 /******************************************************************
781 * WDML_GetInstanceFromWnd
785 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
787 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
790 /******************************************************************************
791 * DdeGetLastError [USER32.@] Gets most recent error code
793 * PARAMS
794 * idInst [I] Instance identifier
796 * RETURNS
797 * Last error code
799 UINT WINAPI DdeGetLastError(DWORD idInst)
801 DWORD error_code;
802 WDML_INSTANCE* pInstance;
804 /* First check instance
806 pInstance = WDML_GetInstance(idInst);
807 if (pInstance == NULL)
809 error_code = DMLERR_INVALIDPARAMETER;
811 else
813 error_code = pInstance->lastError;
814 pInstance->lastError = 0;
817 return error_code;
820 /******************************************************************
821 * WDML_SetAllLastError
825 static void WDML_SetAllLastError(DWORD lastError)
827 DWORD threadID;
828 WDML_INSTANCE* pInstance;
829 threadID = GetCurrentThreadId();
830 pInstance = WDML_InstanceList;
831 while (pInstance)
833 if (pInstance->threadID == threadID)
834 pInstance->lastError = lastError;
835 pInstance = pInstance->next;
839 /* ================================================================
841 * String management
843 * ================================================================ */
846 /******************************************************************
847 * WDML_FindNode
851 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
853 HSZNode* pNode;
855 if (pInstance == NULL) return NULL;
857 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
859 if (pNode->hsz == hsz) break;
861 if (!pNode) WARN("HSZ %p not found\n", hsz);
862 return pNode;
865 /******************************************************************
866 * WDML_MakeAtomFromHsz
868 * Creates a global atom from an existing HSZ
869 * Generally used before sending an HSZ as an atom to a remote app
871 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
873 WCHAR nameBuffer[MAX_BUFFER_LEN];
875 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
876 return GlobalAddAtomW(nameBuffer);
877 WARN("HSZ %p not found\n", hsz);
878 return 0;
881 /******************************************************************
882 * WDML_MakeHszFromAtom
884 * Creates a HSZ from an existing global atom
885 * Generally used while receiving a global atom and transforming it
886 * into an HSZ
888 HSZ WDML_MakeHszFromAtom(const WDML_INSTANCE* pInstance, ATOM atom)
890 WCHAR nameBuffer[MAX_BUFFER_LEN];
892 if (!atom) return NULL;
894 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
896 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
897 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
899 WARN("ATOM 0x%x not found\n", atom);
900 return 0;
903 /******************************************************************
904 * WDML_IncHSZ
908 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
910 HSZNode* pNode;
912 pNode = WDML_FindNode(pInstance, hsz);
913 if (!pNode) return FALSE;
915 pNode->refCount++;
916 return TRUE;
919 /******************************************************************************
920 * WDML_DecHSZ (INTERNAL)
922 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
923 * of HSZ nodes
924 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
926 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
928 HSZNode* pPrev = NULL;
929 HSZNode* pCurrent;
931 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
933 /* If we found the node we were looking for and its ref count is one,
934 * we can remove it
936 if (pCurrent->hsz == hsz)
938 if (--pCurrent->refCount == 0)
940 if (pCurrent == pInstance->nodeList)
942 pInstance->nodeList = pCurrent->next;
944 else
946 pPrev->next = pCurrent->next;
948 HeapFree(GetProcessHeap(), 0, pCurrent);
949 DeleteAtom(HSZ2ATOM(hsz));
951 return TRUE;
954 WARN("HSZ %p not found\n", hsz);
956 return FALSE;
959 /******************************************************************************
960 * WDML_FreeAllHSZ (INTERNAL)
962 * Frees up all the strings still allocated in the list and
963 * remove all the nodes from the list of HSZ nodes.
965 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
967 /* Free any strings created in this instance.
969 while (pInstance->nodeList != NULL)
971 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
975 /******************************************************************************
976 * InsertHSZNode (INTERNAL)
978 * Insert a node to the head of the list.
980 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
982 if (hsz != 0)
984 HSZNode* pNew = NULL;
985 /* Create a new node for this HSZ.
987 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
988 if (pNew != NULL)
990 pNew->hsz = hsz;
991 pNew->next = pInstance->nodeList;
992 pNew->refCount = 1;
993 pInstance->nodeList = pNew;
995 else
997 ERR("Primary HSZ Node allocation failed - out of memory\n");
1002 /******************************************************************
1003 * WDML_QueryString
1007 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
1008 int codepage)
1010 WCHAR pString[MAX_BUFFER_LEN];
1011 int ret;
1012 /* If psz is null, we have to return only the length
1013 * of the string.
1015 if (ptr == NULL)
1017 ptr = pString;
1018 cchMax = MAX_BUFFER_LEN;
1021 /* if there is no input windows returns a NULL string */
1022 if (hsz == NULL)
1024 CHAR *t_ptr = ptr;
1025 *t_ptr = '\0';
1026 return 1;
1029 switch (codepage)
1031 case CP_WINANSI:
1032 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1033 break;
1034 case CP_WINUNICODE:
1035 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1036 break;
1037 default:
1038 ERR("Unknown code page %d\n", codepage);
1039 ret = 0;
1041 return ret;
1044 /*****************************************************************
1045 * DdeQueryStringA [USER32.@]
1047 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1049 DWORD ret = 0;
1050 WDML_INSTANCE* pInstance;
1052 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1054 /* First check instance
1056 pInstance = WDML_GetInstance(idInst);
1057 if (pInstance != NULL)
1059 if (iCodePage == 0) iCodePage = CP_WINANSI;
1060 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1063 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
1064 return ret;
1067 /*****************************************************************
1068 * DdeQueryStringW [USER32.@]
1071 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1073 DWORD ret = 0;
1074 WDML_INSTANCE* pInstance;
1076 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1078 /* First check instance
1080 pInstance = WDML_GetInstance(idInst);
1081 if (pInstance != NULL)
1083 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1084 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1087 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
1088 return ret;
1091 /******************************************************************
1092 * DML_CreateString
1096 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1098 HSZ hsz;
1100 switch (codepage)
1102 case CP_WINANSI:
1103 hsz = ATOM2HSZ(AddAtomA(ptr));
1104 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1105 break;
1106 case CP_WINUNICODE:
1107 hsz = ATOM2HSZ(AddAtomW(ptr));
1108 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1109 break;
1110 default:
1111 ERR("Unknown code page %d\n", codepage);
1112 return 0;
1114 WDML_InsertHSZNode(pInstance, hsz);
1115 return hsz;
1118 /*****************************************************************
1119 * DdeCreateStringHandleA [USER32.@]
1121 * See DdeCreateStringHandleW.
1123 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1125 HSZ hsz = 0;
1126 WDML_INSTANCE* pInstance;
1128 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1130 pInstance = WDML_GetInstance(idInst);
1131 if (pInstance)
1133 if (codepage == 0) codepage = CP_WINANSI;
1134 hsz = WDML_CreateString(pInstance, psz, codepage);
1137 return hsz;
1141 /******************************************************************************
1142 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1144 * PARAMS
1145 * idInst [I] Instance identifier
1146 * psz [I] Pointer to string
1147 * codepage [I] Code page identifier
1148 * RETURNS
1149 * Success: String handle
1150 * Failure: 0
1152 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1154 WDML_INSTANCE* pInstance;
1155 HSZ hsz = 0;
1157 pInstance = WDML_GetInstance(idInst);
1158 if (pInstance)
1160 if (codepage == 0) codepage = CP_WINUNICODE;
1161 hsz = WDML_CreateString(pInstance, psz, codepage);
1164 return hsz;
1167 /*****************************************************************
1168 * DdeFreeStringHandle (USER32.@)
1169 * RETURNS
1170 * success: nonzero
1171 * fail: zero
1173 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1175 WDML_INSTANCE* pInstance;
1176 BOOL ret = FALSE;
1178 TRACE("(%d,%p):\n", idInst, hsz);
1180 /* First check instance
1182 pInstance = WDML_GetInstance(idInst);
1183 if (pInstance)
1184 ret = WDML_DecHSZ(pInstance, hsz);
1186 return ret;
1189 /*****************************************************************
1190 * DdeKeepStringHandle (USER32.@)
1192 * RETURNS
1193 * success: nonzero
1194 * fail: zero
1196 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1198 WDML_INSTANCE* pInstance;
1199 BOOL ret = FALSE;
1201 TRACE("(%d,%p):\n", idInst, hsz);
1203 /* First check instance
1205 pInstance = WDML_GetInstance(idInst);
1206 if (pInstance)
1207 ret = WDML_IncHSZ(pInstance, hsz);
1209 return ret;
1212 /*****************************************************************
1213 * DdeCmpStringHandles (USER32.@)
1215 * Compares the value of two string handles. This comparison is
1216 * not case sensitive.
1218 * PARAMS
1219 * hsz1 [I] Handle to the first string
1220 * hsz2 [I] Handle to the second string
1222 * RETURNS
1223 * -1 The value of hsz1 is zero or less than hsz2
1224 * 0 The values of hsz 1 and 2 are the same or both zero.
1225 * 1 The value of hsz2 is zero of less than hsz1
1227 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1229 WCHAR psz1[MAX_BUFFER_LEN];
1230 WCHAR psz2[MAX_BUFFER_LEN];
1231 int ret = 0;
1232 int ret1, ret2;
1234 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1235 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1237 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1239 /* Make sure we found both strings. */
1240 if (ret1 == 0 && ret2 == 0)
1242 /* If both are not found, return both "zero strings". */
1243 ret = 0;
1245 else if (ret1 == 0)
1247 /* If hsz1 is a not found, return hsz1 is "zero string". */
1248 ret = -1;
1250 else if (ret2 == 0)
1252 /* If hsz2 is a not found, return hsz2 is "zero string". */
1253 ret = 1;
1255 else
1257 /* Compare the two strings we got (case insensitive). */
1258 ret = lstrcmpiW(psz1, psz2);
1259 /* Since strcmp returns any number smaller than
1260 * 0 when the first string is found to be less than
1261 * the second one we must make sure we are returning
1262 * the proper values.
1264 if (ret < 0)
1266 ret = -1;
1268 else if (ret > 0)
1270 ret = 1;
1274 return ret;
1277 /* ================================================================
1279 * Data handle management
1281 * ================================================================ */
1283 /*****************************************************************
1284 * DdeCreateDataHandle (USER32.@)
1286 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1287 HSZ hszItem, UINT wFmt, UINT afCmd)
1290 /* Other than check for validity we will ignore for now idInst, hszItem.
1291 * The purpose of these arguments still need to be investigated.
1294 WDML_INSTANCE* pInstance;
1295 HGLOBAL hMem;
1296 LPBYTE pByte;
1297 DDE_DATAHANDLE_HEAD* pDdh;
1298 WCHAR psz[MAX_BUFFER_LEN];
1300 pInstance = WDML_GetInstance(idInst);
1301 if (pInstance == NULL)
1303 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1304 return NULL;
1307 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1309 psz[0] = HSZ2ATOM(hszItem);
1310 psz[1] = 0;
1313 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1314 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1316 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1317 return 0;
1319 /* we use the first 4 bytes to store the size */
1320 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1322 ERR("GlobalAlloc failed\n");
1323 return 0;
1326 pDdh = GlobalLock(hMem);
1327 if (!pDdh)
1329 GlobalFree(hMem);
1330 return 0;
1333 pDdh->cfFormat = wFmt;
1334 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1336 pByte = (LPBYTE)(pDdh + 1);
1337 if (pSrc)
1339 memcpy(pByte, pSrc + cbOff, cb);
1341 GlobalUnlock(hMem);
1343 TRACE("=> %p\n", hMem);
1344 return (HDDEDATA)hMem;
1347 /*****************************************************************
1349 * DdeAddData (USER32.@)
1351 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1353 DWORD old_sz, new_sz;
1354 LPBYTE pDst;
1356 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1358 pDst = DdeAccessData(hData, &old_sz);
1359 if (!pDst) return 0;
1361 new_sz = cb + cbOff;
1362 if (new_sz > old_sz)
1364 DdeUnaccessData(hData);
1365 hData = GlobalReAlloc(hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1366 GMEM_MOVEABLE | GMEM_DDESHARE);
1367 pDst = DdeAccessData(hData, &old_sz);
1370 if (!pDst) return 0;
1372 memcpy(pDst + cbOff, pSrc, cb);
1373 DdeUnaccessData(hData);
1374 return hData;
1377 /******************************************************************************
1378 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1381 * PARAMS
1382 * hData [I] Handle to DDE object
1383 * pDst [I] Pointer to destination buffer
1384 * cbMax [I] Amount of data to copy
1385 * cbOff [I] Offset to beginning of data
1387 * RETURNS
1388 * Size of memory object associated with handle
1390 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1392 DWORD dwSize, dwRet;
1393 LPBYTE pByte;
1395 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1397 pByte = DdeAccessData(hData, &dwSize);
1399 if (pByte)
1401 if (!pDst)
1403 dwRet = dwSize;
1405 else if (cbOff + cbMax < dwSize)
1407 dwRet = cbMax;
1409 else if (cbOff < dwSize)
1411 dwRet = dwSize - cbOff;
1413 else
1415 dwRet = 0;
1417 if (pDst && dwRet != 0)
1419 memcpy(pDst, pByte + cbOff, dwRet);
1421 DdeUnaccessData(hData);
1423 else
1425 dwRet = 0;
1427 return dwRet;
1430 /*****************************************************************
1431 * DdeAccessData (USER32.@)
1433 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1435 HGLOBAL hMem = hData;
1436 DDE_DATAHANDLE_HEAD* pDdh;
1438 TRACE("(%p,%p)\n", hData, pcbDataSize);
1440 pDdh = GlobalLock(hMem);
1441 if (pDdh == NULL)
1443 ERR("Failed on GlobalLock(%p)\n", hMem);
1444 return 0;
1447 if (pcbDataSize != NULL)
1449 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1451 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1452 return (LPBYTE)(pDdh + 1);
1455 /*****************************************************************
1456 * DdeUnaccessData (USER32.@)
1458 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1460 HGLOBAL hMem = hData;
1462 TRACE("(%p)\n", hData);
1464 GlobalUnlock(hMem);
1466 return TRUE;
1469 /*****************************************************************
1470 * DdeFreeDataHandle (USER32.@)
1472 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1474 TRACE("(%p)\n", hData);
1475 return GlobalFree(hData) == 0;
1478 /******************************************************************
1479 * WDML_IsAppOwned
1483 BOOL WDML_IsAppOwned(HDDEDATA hData)
1485 DDE_DATAHANDLE_HEAD* pDdh;
1486 BOOL ret = FALSE;
1488 pDdh = GlobalLock(hData);
1489 if (pDdh != NULL)
1491 ret = pDdh->bAppOwned;
1492 GlobalUnlock(hData);
1494 return ret;
1497 /* ================================================================
1499 * Global <=> Data handle management
1501 * ================================================================ */
1503 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1504 * offset size
1505 * (bytes) (bits) comment
1506 * 0 16 bit fields for options (release, ackreq, response...)
1507 * 2 16 clipboard format
1508 * 4 ? data to be used
1510 HDDEDATA WDML_Global2DataHandle(WDML_CONV* pConv, HGLOBAL hMem, WINE_DDEHEAD* p)
1512 DDEDATA* pDd;
1513 HDDEDATA ret = 0;
1514 DWORD size;
1516 if (hMem)
1518 pDd = GlobalLock(hMem);
1519 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1520 if (pDd)
1522 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1523 switch (pDd->cfFormat)
1525 default:
1526 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1527 pDd->cfFormat, hMem);
1528 /* fall thru */
1529 case 0:
1530 case CF_TEXT:
1531 ret = DdeCreateDataHandle(pConv->instance->instanceID, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1532 break;
1533 case CF_BITMAP:
1534 if (size >= sizeof(BITMAP))
1536 BITMAP* bmp = (BITMAP*)pDd->Value;
1537 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1538 if (size >= sizeof(BITMAP) + count)
1540 HBITMAP hbmp;
1542 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1543 bmp->bmPlanes, bmp->bmBitsPixel,
1544 pDd->Value + sizeof(BITMAP))))
1546 ret = DdeCreateDataHandle(pConv->instance->instanceID, (LPBYTE)&hbmp, sizeof(hbmp),
1547 0, 0, CF_BITMAP, 0);
1549 else ERR("Can't create bmp\n");
1551 else
1553 ERR("Wrong count: %u / %d\n", size, count);
1555 } else ERR("No bitmap header\n");
1556 break;
1558 GlobalUnlock(hMem);
1561 return ret;
1564 /******************************************************************
1565 * WDML_DataHandle2Global
1569 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1570 BOOL fDeferUpd, BOOL fAckReq)
1572 DDE_DATAHANDLE_HEAD* pDdh;
1573 DWORD dwSize;
1574 HGLOBAL hMem = 0;
1576 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1577 pDdh = GlobalLock((HGLOBAL)hDdeData);
1578 if (dwSize && pDdh)
1580 WINE_DDEHEAD* wdh = NULL;
1582 switch (pDdh->cfFormat)
1584 default:
1585 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1586 pDdh->cfFormat, hDdeData);
1587 /* fall thru */
1588 case 0:
1589 case CF_TEXT:
1590 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1591 if (hMem && (wdh = GlobalLock(hMem)))
1593 memcpy(wdh + 1, pDdh + 1, dwSize);
1595 break;
1596 case CF_BITMAP:
1597 if (dwSize >= sizeof(HBITMAP))
1599 BITMAP bmp;
1600 DWORD count;
1601 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1603 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1605 count = bmp.bmWidthBytes * bmp.bmHeight;
1606 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1607 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1608 if (hMem && (wdh = GlobalLock(hMem)))
1610 memcpy(wdh + 1, &bmp, sizeof(bmp));
1611 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1615 break;
1617 if (wdh)
1619 wdh->unused = 0;
1620 wdh->fResponse = fResponse;
1621 wdh->fRelease = fRelease;
1622 wdh->fDeferUpd = fDeferUpd;
1623 wdh->fAckReq = fAckReq;
1624 wdh->cfFormat = pDdh->cfFormat;
1625 GlobalUnlock(hMem);
1627 GlobalUnlock((HGLOBAL)hDdeData);
1630 return hMem;
1633 /* ================================================================
1635 * Server management
1637 * ================================================================ */
1639 /******************************************************************
1640 * WDML_AddServer
1644 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1646 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1647 WDML_SERVER* pServer;
1648 WCHAR buf1[256];
1649 WCHAR buf2[256];
1651 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1652 if (pServer == NULL) return NULL;
1654 pServer->hszService = hszService;
1655 WDML_IncHSZ(pInstance, hszService);
1657 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1658 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1659 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1661 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1662 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1664 pServer->filterOn = TRUE;
1666 pServer->next = pInstance->servers;
1667 pInstance->servers = pServer;
1668 return pServer;
1671 /******************************************************************
1672 * WDML_RemoveServer
1676 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1678 WDML_SERVER* pPrev = NULL;
1679 WDML_SERVER* pServer = NULL;
1680 WDML_CONV* pConv;
1681 WDML_CONV* pConvNext;
1683 pServer = pInstance->servers;
1685 while (pServer != NULL)
1687 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1689 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1690 pServer->atomService, pServer->atomServiceSpec);
1691 /* terminate all conversations for given topic */
1692 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1694 pConvNext = pConv->next;
1695 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1697 HWND client = pConv->hwndClient, server = pConv->hwndServer;
1698 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1699 /* don't care about return code (whether client window is present or not) */
1700 PostMessageW(client, WM_DDE_TERMINATE, (WPARAM)server, 0);
1703 if (pServer == pInstance->servers)
1705 pInstance->servers = pServer->next;
1707 else
1709 pPrev->next = pServer->next;
1712 DestroyWindow(pServer->hwndServer);
1713 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1714 WDML_DecHSZ(pInstance, pServer->hszService);
1716 GlobalDeleteAtom(pServer->atomService);
1717 GlobalDeleteAtom(pServer->atomServiceSpec);
1719 HeapFree(GetProcessHeap(), 0, pServer);
1720 break;
1723 pPrev = pServer;
1724 pServer = pServer->next;
1728 /*****************************************************************************
1729 * WDML_FindServer
1731 * generic routine to return a pointer to the relevant ServiceNode
1732 * for a given service name, or NULL if the entry does not exist
1735 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1737 WDML_SERVER* pServer;
1739 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1741 if (hszService == pServer->hszService)
1743 return pServer;
1746 TRACE("Service name missing\n");
1747 return NULL;
1750 /* ================================================================
1752 * Conversation management
1754 * ================================================================ */
1756 /******************************************************************
1757 * WDML_AddConv
1761 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1762 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1764 WDML_CONV* pConv;
1766 /* no conversation yet, add it */
1767 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1768 if (!pConv) return NULL;
1770 pConv->instance = pInstance;
1771 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1772 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1773 pConv->magic = WDML_CONV_MAGIC;
1774 pConv->hwndServer = hwndServer;
1775 pConv->hwndClient = hwndClient;
1776 pConv->transactions = NULL;
1777 pConv->hUser = 0;
1778 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1779 pConv->wStatus |= pInstance->wStatus;
1780 /* check if both side of the conversation are of the same instance */
1781 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1782 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1784 pConv->wStatus |= ST_ISSELF;
1786 pConv->wConvst = XST_NULL;
1788 pConv->next = pInstance->convs[side];
1789 pInstance->convs[side] = pConv;
1791 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv->wStatus, pInstance);
1793 return pConv;
1796 /******************************************************************
1797 * WDML_FindConv
1801 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1802 HSZ hszService, HSZ hszTopic)
1804 WDML_CONV* pCurrent = NULL;
1806 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1808 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1809 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1811 return pCurrent;
1815 return NULL;
1818 /******************************************************************
1819 * WDML_RemoveConv
1823 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1825 WDML_CONV* pPrev = NULL;
1826 WDML_CONV* pCurrent;
1827 WDML_XACT* pXAct;
1828 WDML_XACT* pXActNext;
1829 HWND hWnd;
1831 if (!pRef)
1832 return;
1834 /* remove any pending transaction */
1835 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1837 pXActNext = pXAct->next;
1838 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1841 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1843 /* FIXME: should we keep the window around ? it seems so (at least on client side
1844 * to let QueryConvInfo work after conv termination, but also to implement
1845 * DdeReconnect...
1847 /* destroy conversation window, but first remove pConv from hWnd.
1848 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1850 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1851 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1853 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1855 WDML_DecHSZ(pRef->instance, pRef->hszService);
1856 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1858 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1860 if (pCurrent == pRef)
1862 if (pCurrent == pRef->instance->convs[side])
1864 pRef->instance->convs[side] = pCurrent->next;
1866 else
1868 pPrev->next = pCurrent->next;
1870 pCurrent->magic = 0;
1871 HeapFree(GetProcessHeap(), 0, pCurrent);
1872 break;
1877 /******************************************************************
1878 * WDML_EnableCallback
1880 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1882 if (wCmd == EC_DISABLE)
1884 pConv->wStatus |= ST_BLOCKED;
1885 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
1886 return TRUE;
1889 if (wCmd == EC_QUERYWAITING)
1890 return pConv->transactions ? TRUE : FALSE;
1892 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1894 FIXME("Unknown command code %04x\n", wCmd);
1895 return FALSE;
1898 if (wCmd == EC_ENABLEALL)
1900 pConv->wStatus &= ~ST_BLOCKED;
1901 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
1904 while (pConv->transactions)
1906 WDML_XACT *pXAct = pConv->transactions;
1908 if (pConv->wStatus & ST_CLIENT)
1910 /* transaction should be in the queue until handled */
1911 WDML_ClientHandle(pConv, pXAct, 0, NULL);
1912 WDML_UnQueueTransaction(pConv, pXAct);
1914 else
1916 /* transaction should be removed from the queue before handling */
1917 WDML_UnQueueTransaction(pConv, pXAct);
1918 WDML_ServerHandle(pConv, pXAct);
1921 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1923 if (wCmd == EC_ENABLEONE) break;
1925 return TRUE;
1928 /*****************************************************************
1929 * DdeEnableCallback (USER32.@)
1931 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1933 BOOL ret = FALSE;
1934 WDML_CONV *pConv;
1936 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
1938 if (hConv)
1940 pConv = WDML_GetConv(hConv, TRUE);
1942 if (pConv && pConv->instance->instanceID == idInst)
1943 ret = WDML_EnableCallback(pConv, wCmd);
1945 else
1947 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
1949 if (!pInstance)
1950 return FALSE;
1952 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
1953 pInstance->wStatus |= wCmd;
1955 if (wCmd == EC_DISABLE)
1957 pInstance->wStatus |= ST_BLOCKED;
1958 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1960 else if (wCmd == EC_ENABLEALL)
1962 pInstance->wStatus &= ~ST_BLOCKED;
1963 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1966 ret = TRUE;
1968 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
1970 ret = WDML_EnableCallback(pConv, wCmd);
1971 if (ret && wCmd == EC_QUERYWAITING) break;
1975 return ret;
1978 /******************************************************************
1979 * WDML_GetConv
1983 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1985 WDML_CONV* pConv = (WDML_CONV*)hConv;
1987 /* FIXME: should do better checking */
1988 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1990 if (!pConv->instance || pConv->instance->threadID != GetCurrentThreadId())
1992 WARN("wrong thread ID\n");
1993 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
1994 return NULL;
1997 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1999 WARN("found conv but ain't connected\n");
2000 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
2001 return NULL;
2004 return pConv;
2007 /******************************************************************
2008 * WDML_GetConvFromWnd
2012 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
2014 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
2017 /******************************************************************
2018 * WDML_PostAck
2022 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
2023 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
2025 DDEACK ddeAck;
2026 HWND from, to;
2028 if (side == WDML_SERVER_SIDE)
2030 from = pConv->hwndServer;
2031 to = pConv->hwndClient;
2033 else
2035 to = pConv->hwndServer;
2036 from = pConv->hwndClient;
2039 ddeAck.bAppReturnCode = appRetCode;
2040 ddeAck.reserved = 0;
2041 ddeAck.fBusy = fBusy;
2042 ddeAck.fAck = fAck;
2044 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2046 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2047 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2048 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2050 pConv->wStatus &= ~ST_CONNECTED;
2051 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2052 FreeDDElParam(WM_DDE_ACK, lParam);
2053 return FALSE;
2055 return TRUE;
2058 /*****************************************************************
2059 * DdeSetUserHandle (USER32.@)
2061 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2063 WDML_CONV* pConv;
2065 pConv = WDML_GetConv(hConv, FALSE);
2066 if (pConv == NULL)
2067 return FALSE;
2069 if (id == QID_SYNC)
2071 pConv->hUser = hUser;
2073 else
2075 WDML_XACT* pXAct;
2077 pXAct = WDML_FindTransaction(pConv, id);
2078 if (pXAct)
2080 pXAct->hUser = hUser;
2082 else
2084 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2085 return FALSE;
2088 return TRUE;
2091 /******************************************************************
2092 * WDML_GetLocalConvInfo
2096 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2098 BOOL ret = TRUE;
2099 WDML_LINK* pLink;
2100 WDML_SIDE side;
2102 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2103 ci->hszSvcPartner = pConv->hszService;
2104 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2105 ci->hszTopic = pConv->hszTopic;
2106 ci->wStatus = pConv->wStatus;
2108 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2110 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2112 if (pLink->hConv == (HCONV)pConv)
2114 ci->wStatus |= ST_ADVISE;
2115 break;
2119 /* FIXME: non handled status flags:
2120 ST_BLOCKED
2121 ST_BLOCKNEXT
2122 ST_INLIST
2125 ci->wConvst = pConv->wConvst; /* FIXME */
2127 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2128 ci->hConvList = 0;
2129 ci->ConvCtxt = pConv->convContext;
2130 if (ci->wStatus & ST_CLIENT)
2132 ci->hwnd = pConv->hwndClient;
2133 ci->hwndPartner = pConv->hwndServer;
2135 else
2137 ci->hwnd = pConv->hwndServer;
2138 ci->hwndPartner = pConv->hwndClient;
2140 if (id == QID_SYNC)
2142 ci->hUser = pConv->hUser;
2143 ci->hszItem = 0;
2144 ci->wFmt = 0;
2145 ci->wType = 0;
2147 else
2149 WDML_XACT* pXAct;
2151 pXAct = WDML_FindTransaction(pConv, id);
2152 if (pXAct)
2154 ci->hUser = pXAct->hUser;
2155 ci->hszItem = pXAct->hszItem;
2156 ci->wFmt = pXAct->wFmt;
2157 ci->wType = pXAct->wType;
2159 else
2161 ret = 0;
2162 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2165 return ret;
2168 /******************************************************************
2169 * DdeQueryConvInfo (USER32.@)
2171 * FIXME: Set last DDE error on failure.
2173 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2175 UINT ret = lpConvInfo->cb;
2176 CONVINFO ci;
2177 WDML_CONV* pConv;
2179 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2181 if (!hConv)
2183 FIXME("hConv is NULL\n");
2184 return 0;
2187 pConv = WDML_GetConv(hConv, FALSE);
2188 if (pConv != NULL)
2190 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2191 ret = 0;
2193 else
2195 if ((ULONG_PTR)hConv & 1)
2197 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2198 if (pConv != NULL)
2199 FIXME("Request on remote conversation information is not implemented yet\n");
2201 ret = 0;
2204 if (ret != 0)
2205 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2206 return ret;
2209 /* ================================================================
2211 * Link (hot & warm) management
2213 * ================================================================ */
2215 /******************************************************************
2216 * WDML_AddLink
2220 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2221 UINT wType, HSZ hszItem, UINT wFmt)
2223 WDML_LINK* pLink;
2225 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2226 if (pLink == NULL)
2228 ERR("OOM\n");
2229 return;
2232 pLink->hConv = hConv;
2233 pLink->transactionType = wType;
2234 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2235 pLink->uFmt = wFmt;
2236 pLink->next = pInstance->links[side];
2237 pInstance->links[side] = pLink;
2240 /******************************************************************
2241 * WDML_RemoveLink
2245 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2246 HSZ hszItem, UINT uFmt)
2248 WDML_LINK* pPrev = NULL;
2249 WDML_LINK* pCurrent = NULL;
2251 pCurrent = pInstance->links[side];
2253 while (pCurrent != NULL)
2255 if (pCurrent->hConv == hConv &&
2256 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2257 pCurrent->uFmt == uFmt)
2259 if (pCurrent == pInstance->links[side])
2261 pInstance->links[side] = pCurrent->next;
2263 else
2265 pPrev->next = pCurrent->next;
2268 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2269 HeapFree(GetProcessHeap(), 0, pCurrent);
2270 break;
2273 pPrev = pCurrent;
2274 pCurrent = pCurrent->next;
2278 /* this function is called to remove all links related to the conv.
2279 It should be called from both client and server when terminating
2280 the conversation.
2282 /******************************************************************
2283 * WDML_RemoveAllLinks
2287 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2289 WDML_LINK* pPrev = NULL;
2290 WDML_LINK* pCurrent = NULL;
2291 WDML_LINK* pNext = NULL;
2293 pCurrent = pInstance->links[side];
2295 while (pCurrent != NULL)
2297 if (pCurrent->hConv == (HCONV)pConv)
2299 if (pCurrent == pInstance->links[side])
2301 pInstance->links[side] = pCurrent->next;
2302 pNext = pCurrent->next;
2304 else
2306 pPrev->next = pCurrent->next;
2307 pNext = pCurrent->next;
2310 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2312 HeapFree(GetProcessHeap(), 0, pCurrent);
2313 pCurrent = NULL;
2316 if (pCurrent)
2318 pPrev = pCurrent;
2319 pCurrent = pCurrent->next;
2321 else
2323 pCurrent = pNext;
2328 /******************************************************************
2329 * WDML_FindLink
2333 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2334 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2336 WDML_LINK* pCurrent = NULL;
2338 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2340 /* we don't need to check for transaction type as it can be altered */
2342 if (pCurrent->hConv == hConv &&
2343 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2344 (!use_fmt || pCurrent->uFmt == uFmt))
2346 break;
2351 return pCurrent;
2354 /* ================================================================
2356 * Transaction management
2358 * ================================================================ */
2360 /******************************************************************
2361 * WDML_AllocTransaction
2363 * Alloc a transaction structure for handling the message ddeMsg
2365 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2366 UINT wFmt, HSZ hszItem)
2368 WDML_XACT* pXAct;
2369 static WORD tid = 1; /* FIXME: wrap around */
2371 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2372 if (!pXAct)
2374 pInstance->lastError = DMLERR_MEMORY_ERROR;
2375 return NULL;
2378 pXAct->xActID = tid++;
2379 pXAct->ddeMsg = ddeMsg;
2380 pXAct->hDdeData = 0;
2381 pXAct->hUser = 0;
2382 pXAct->next = NULL;
2383 pXAct->wType = 0;
2384 pXAct->wFmt = wFmt;
2385 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2386 pXAct->atom = 0;
2387 pXAct->hMem = 0;
2388 pXAct->lParam = 0;
2390 return pXAct;
2393 /******************************************************************
2394 * WDML_QueueTransaction
2396 * Adds a transaction to the list of transaction
2398 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2400 WDML_XACT** pt;
2402 /* advance to last in queue */
2403 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2404 *pt = pXAct;
2407 /******************************************************************
2408 * WDML_UnQueueTransaction
2412 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2414 WDML_XACT** pt;
2416 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2418 if (*pt == pXAct)
2420 *pt = pXAct->next;
2421 return TRUE;
2424 return FALSE;
2427 /******************************************************************
2428 * WDML_FreeTransaction
2432 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2434 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2435 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2437 GlobalFree(pXAct->hMem);
2439 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2441 HeapFree(GetProcessHeap(), 0, pXAct);
2444 /******************************************************************
2445 * WDML_FindTransaction
2449 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2451 WDML_XACT* pXAct;
2453 tid = HIWORD(tid);
2454 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2456 if (pXAct->xActID == tid)
2457 break;
2459 return pXAct;
2462 /* ================================================================
2464 * Information broadcast across DDEML implementations
2466 * ================================================================ */
2468 struct tagWDML_BroadcastPmt
2470 LPCWSTR clsName;
2471 UINT uMsg;
2472 WPARAM wParam;
2473 LPARAM lParam;
2476 /******************************************************************
2477 * WDML_BroadcastEnumProc
2481 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2483 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2484 WCHAR buffer[128];
2486 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2487 lstrcmpiW(buffer, s->clsName) == 0)
2489 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2491 return TRUE;
2494 /******************************************************************
2495 * WDML_BroadcastDDEWindows
2499 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2501 struct tagWDML_BroadcastPmt s;
2503 s.clsName = clsName;
2504 s.uMsg = uMsg;
2505 s.wParam = wParam;
2506 s.lParam = lParam;
2507 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);