shdocvw: Return IHTMLDocument2's IDispatch in get_Document.
[wine/hacks.git] / dlls / user32 / dde_misc.c
blobac4657e4118514e27965d3ee4a364510b3874a92
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)
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->nodeList = NULL; /* node will be added later */
381 pInstance->monitorFlags = afCmd & MF_MASK;
382 pInstance->wStatus = 0;
383 pInstance->lastError = DMLERR_NO_ERROR;
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);
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);
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[%p] (%x %x %p %p %p %p %lx %lx)\n",
732 pInstance->callback, uType, uFmt,
733 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
734 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
735 TRACE("done => %p\n", ret);
736 return ret;
739 /*****************************************************************************
740 * WDML_GetInstance
742 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
743 * for an instance Id, or NULL if the entry does not exist
746 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
748 WDML_INSTANCE* pInstance;
750 EnterCriticalSection(&WDML_CritSect);
752 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
754 if (pInstance->instanceID == instId)
756 if (GetCurrentThreadId() != pInstance->threadID)
758 FIXME("Tried to get instance from wrong thread\n");
759 continue;
761 break;
765 LeaveCriticalSection(&WDML_CritSect);
767 if (!pInstance)
768 WARN("Instance entry missing for id %04x\n", instId);
769 return pInstance;
772 /******************************************************************
773 * WDML_GetInstanceFromWnd
777 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
779 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
782 /******************************************************************************
783 * DdeGetLastError [USER32.@] Gets most recent error code
785 * PARAMS
786 * idInst [I] Instance identifier
788 * RETURNS
789 * Last error code
791 UINT WINAPI DdeGetLastError(DWORD idInst)
793 DWORD error_code;
794 WDML_INSTANCE* pInstance;
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 return error_code;
812 /******************************************************************
813 * WDML_SetAllLastError
817 static void WDML_SetAllLastError(DWORD lastError)
819 DWORD threadID;
820 WDML_INSTANCE* pInstance;
821 threadID = GetCurrentThreadId();
822 pInstance = WDML_InstanceList;
823 while (pInstance)
825 if (pInstance->threadID == threadID)
826 pInstance->lastError = lastError;
827 pInstance = pInstance->next;
831 /* ================================================================
833 * String management
835 * ================================================================ */
838 /******************************************************************
839 * WDML_FindNode
843 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
845 HSZNode* pNode;
847 if (pInstance == NULL) return NULL;
849 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
851 if (pNode->hsz == hsz) break;
853 if (!pNode) WARN("HSZ %p not found\n", hsz);
854 return pNode;
857 /******************************************************************
858 * WDML_MakeAtomFromHsz
860 * Creates a global atom from an existing HSZ
861 * Generally used before sending an HSZ as an atom to a remote app
863 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
865 WCHAR nameBuffer[MAX_BUFFER_LEN];
867 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
868 return GlobalAddAtomW(nameBuffer);
869 WARN("HSZ %p not found\n", hsz);
870 return 0;
873 /******************************************************************
874 * WDML_MakeHszFromAtom
876 * Creates a HSZ from an existing global atom
877 * Generally used while receiving a global atom and transforming it
878 * into an HSZ
880 HSZ WDML_MakeHszFromAtom(const WDML_INSTANCE* pInstance, ATOM atom)
882 WCHAR nameBuffer[MAX_BUFFER_LEN];
884 if (!atom) return NULL;
886 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
888 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
889 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
891 WARN("ATOM 0x%x not found\n", atom);
892 return 0;
895 /******************************************************************
896 * WDML_IncHSZ
900 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
902 HSZNode* pNode;
904 pNode = WDML_FindNode(pInstance, hsz);
905 if (!pNode) return FALSE;
907 pNode->refCount++;
908 return TRUE;
911 /******************************************************************************
912 * WDML_DecHSZ (INTERNAL)
914 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
915 * of HSZ nodes
916 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
918 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
920 HSZNode* pPrev = NULL;
921 HSZNode* pCurrent;
923 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
925 /* If we found the node we were looking for and its ref count is one,
926 * we can remove it
928 if (pCurrent->hsz == hsz)
930 if (--pCurrent->refCount == 0)
932 if (pCurrent == pInstance->nodeList)
934 pInstance->nodeList = pCurrent->next;
936 else
938 pPrev->next = pCurrent->next;
940 HeapFree(GetProcessHeap(), 0, pCurrent);
941 DeleteAtom(HSZ2ATOM(hsz));
943 return TRUE;
946 WARN("HSZ %p not found\n", hsz);
948 return FALSE;
951 /******************************************************************************
952 * WDML_FreeAllHSZ (INTERNAL)
954 * Frees up all the strings still allocated in the list and
955 * remove all the nodes from the list of HSZ nodes.
957 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
959 /* Free any strings created in this instance.
961 while (pInstance->nodeList != NULL)
963 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
967 /******************************************************************************
968 * InsertHSZNode (INTERNAL)
970 * Insert a node to the head of the list.
972 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
974 if (hsz != 0)
976 HSZNode* pNew = NULL;
977 /* Create a new node for this HSZ.
979 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
980 if (pNew != NULL)
982 pNew->hsz = hsz;
983 pNew->next = pInstance->nodeList;
984 pNew->refCount = 1;
985 pInstance->nodeList = pNew;
987 else
989 ERR("Primary HSZ Node allocation failed - out of memory\n");
994 /******************************************************************
995 * WDML_QueryString
999 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
1000 int codepage)
1002 WCHAR pString[MAX_BUFFER_LEN];
1003 int ret;
1004 /* If psz is null, we have to return only the length
1005 * of the string.
1007 if (ptr == NULL)
1009 ptr = pString;
1010 cchMax = MAX_BUFFER_LEN;
1013 /* if there is no input windows returns a NULL string */
1014 if (hsz == NULL)
1016 CHAR *t_ptr = ptr;
1017 *t_ptr = '\0';
1018 return 1;
1021 switch (codepage)
1023 case CP_WINANSI:
1024 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1025 break;
1026 case CP_WINUNICODE:
1027 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1028 break;
1029 default:
1030 ERR("Unknown code page %d\n", codepage);
1031 ret = 0;
1033 return ret;
1036 /*****************************************************************
1037 * DdeQueryStringA [USER32.@]
1039 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1041 DWORD ret = 0;
1042 WDML_INSTANCE* pInstance;
1044 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1046 /* First check instance
1048 pInstance = WDML_GetInstance(idInst);
1049 if (pInstance != NULL)
1051 if (iCodePage == 0) iCodePage = CP_WINANSI;
1052 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1055 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
1056 return ret;
1059 /*****************************************************************
1060 * DdeQueryStringW [USER32.@]
1063 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1065 DWORD ret = 0;
1066 WDML_INSTANCE* pInstance;
1068 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1070 /* First check instance
1072 pInstance = WDML_GetInstance(idInst);
1073 if (pInstance != NULL)
1075 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1076 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1079 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
1080 return ret;
1083 /******************************************************************
1084 * DML_CreateString
1088 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1090 HSZ hsz;
1092 switch (codepage)
1094 case CP_WINANSI:
1095 hsz = ATOM2HSZ(AddAtomA(ptr));
1096 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1097 break;
1098 case CP_WINUNICODE:
1099 hsz = ATOM2HSZ(AddAtomW(ptr));
1100 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1101 break;
1102 default:
1103 ERR("Unknown code page %d\n", codepage);
1104 return 0;
1106 WDML_InsertHSZNode(pInstance, hsz);
1107 return hsz;
1110 /*****************************************************************
1111 * DdeCreateStringHandleA [USER32.@]
1113 * See DdeCreateStringHandleW.
1115 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1117 HSZ hsz = 0;
1118 WDML_INSTANCE* pInstance;
1120 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1122 pInstance = WDML_GetInstance(idInst);
1123 if (pInstance == NULL)
1124 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1125 else
1127 if (codepage == 0) codepage = CP_WINANSI;
1128 hsz = WDML_CreateString(pInstance, psz, codepage);
1131 return hsz;
1135 /******************************************************************************
1136 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1138 * PARAMS
1139 * idInst [I] Instance identifier
1140 * psz [I] Pointer to string
1141 * codepage [I] Code page identifier
1142 * RETURNS
1143 * Success: String handle
1144 * Failure: 0
1146 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1148 WDML_INSTANCE* pInstance;
1149 HSZ hsz = 0;
1151 pInstance = WDML_GetInstance(idInst);
1152 if (pInstance == NULL)
1153 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1154 else
1156 if (codepage == 0) codepage = CP_WINUNICODE;
1157 hsz = WDML_CreateString(pInstance, psz, codepage);
1160 return hsz;
1163 /*****************************************************************
1164 * DdeFreeStringHandle (USER32.@)
1165 * RETURNS
1166 * success: nonzero
1167 * fail: zero
1169 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1171 WDML_INSTANCE* pInstance;
1172 BOOL ret = FALSE;
1174 TRACE("(%d,%p):\n", idInst, hsz);
1176 /* First check instance
1178 pInstance = WDML_GetInstance(idInst);
1179 if (pInstance)
1180 ret = WDML_DecHSZ(pInstance, hsz);
1182 return ret;
1185 /*****************************************************************
1186 * DdeKeepStringHandle (USER32.@)
1188 * RETURNS
1189 * success: nonzero
1190 * fail: zero
1192 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1194 WDML_INSTANCE* pInstance;
1195 BOOL ret = FALSE;
1197 TRACE("(%d,%p):\n", idInst, hsz);
1199 /* First check instance
1201 pInstance = WDML_GetInstance(idInst);
1202 if (pInstance)
1203 ret = WDML_IncHSZ(pInstance, hsz);
1205 return ret;
1208 /*****************************************************************
1209 * DdeCmpStringHandles (USER32.@)
1211 * Compares the value of two string handles. This comparison is
1212 * not case sensitive.
1214 * PARAMS
1215 * hsz1 [I] Handle to the first string
1216 * hsz2 [I] Handle to the second string
1218 * RETURNS
1219 * -1 The value of hsz1 is zero or less than hsz2
1220 * 0 The values of hsz 1 and 2 are the same or both zero.
1221 * 1 The value of hsz2 is zero of less than hsz1
1223 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1225 WCHAR psz1[MAX_BUFFER_LEN];
1226 WCHAR psz2[MAX_BUFFER_LEN];
1227 int ret = 0;
1228 int ret1, ret2;
1230 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1231 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1233 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1235 /* Make sure we found both strings. */
1236 if (ret1 == 0 && ret2 == 0)
1238 /* If both are not found, return both "zero strings". */
1239 ret = 0;
1241 else if (ret1 == 0)
1243 /* If hsz1 is a not found, return hsz1 is "zero string". */
1244 ret = -1;
1246 else if (ret2 == 0)
1248 /* If hsz2 is a not found, return hsz2 is "zero string". */
1249 ret = 1;
1251 else
1253 /* Compare the two strings we got (case insensitive). */
1254 ret = lstrcmpiW(psz1, psz2);
1255 /* Since strcmp returns any number smaller than
1256 * 0 when the first string is found to be less than
1257 * the second one we must make sure we are returning
1258 * the proper values.
1260 if (ret < 0)
1262 ret = -1;
1264 else if (ret > 0)
1266 ret = 1;
1270 return ret;
1273 /* ================================================================
1275 * Data handle management
1277 * ================================================================ */
1279 /*****************************************************************
1280 * DdeCreateDataHandle (USER32.@)
1282 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1283 HSZ hszItem, UINT wFmt, UINT afCmd)
1286 /* Other than check for validity we will ignore for now idInst, hszItem.
1287 * The purpose of these arguments still need to be investigated.
1290 WDML_INSTANCE* pInstance;
1291 HGLOBAL hMem;
1292 LPBYTE pByte;
1293 DDE_DATAHANDLE_HEAD* pDdh;
1294 WCHAR psz[MAX_BUFFER_LEN];
1296 pInstance = WDML_GetInstance(idInst);
1297 if (pInstance == NULL)
1299 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1300 return NULL;
1303 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1305 psz[0] = HSZ2ATOM(hszItem);
1306 psz[1] = 0;
1309 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1310 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1312 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1313 return 0;
1315 /* we use the first 4 bytes to store the size */
1316 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1318 ERR("GlobalAlloc failed\n");
1319 return 0;
1322 pDdh = GlobalLock(hMem);
1323 if (!pDdh)
1325 GlobalFree(hMem);
1326 return 0;
1329 pDdh->cfFormat = wFmt;
1330 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1332 pByte = (LPBYTE)(pDdh + 1);
1333 if (pSrc)
1335 memcpy(pByte, pSrc + cbOff, cb);
1337 GlobalUnlock(hMem);
1339 TRACE("=> %p\n", hMem);
1340 return hMem;
1343 /*****************************************************************
1345 * DdeAddData (USER32.@)
1347 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1349 DWORD old_sz, new_sz;
1350 LPBYTE pDst;
1352 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1354 pDst = DdeAccessData(hData, &old_sz);
1355 if (!pDst) return 0;
1357 new_sz = cb + cbOff;
1358 if (new_sz > old_sz)
1360 DdeUnaccessData(hData);
1361 hData = GlobalReAlloc(hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1362 GMEM_MOVEABLE | GMEM_DDESHARE);
1363 pDst = DdeAccessData(hData, &old_sz);
1366 if (!pDst) return 0;
1368 memcpy(pDst + cbOff, pSrc, cb);
1369 DdeUnaccessData(hData);
1370 return hData;
1373 /******************************************************************************
1374 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1377 * PARAMS
1378 * hData [I] Handle to DDE object
1379 * pDst [I] Pointer to destination buffer
1380 * cbMax [I] Amount of data to copy
1381 * cbOff [I] Offset to beginning of data
1383 * RETURNS
1384 * Size of memory object associated with handle
1386 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1388 DWORD dwSize, dwRet;
1389 LPBYTE pByte;
1391 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1393 pByte = DdeAccessData(hData, &dwSize);
1395 if (pByte)
1397 if (!pDst)
1399 dwRet = dwSize;
1401 else if (cbOff + cbMax < dwSize)
1403 dwRet = cbMax;
1405 else if (cbOff < dwSize)
1407 dwRet = dwSize - cbOff;
1409 else
1411 dwRet = 0;
1413 if (pDst && dwRet != 0)
1415 memcpy(pDst, pByte + cbOff, dwRet);
1417 DdeUnaccessData(hData);
1419 else
1421 dwRet = 0;
1423 return dwRet;
1426 /*****************************************************************
1427 * DdeAccessData (USER32.@)
1429 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1431 HGLOBAL hMem = hData;
1432 DDE_DATAHANDLE_HEAD* pDdh;
1434 TRACE("(%p,%p)\n", hData, pcbDataSize);
1436 pDdh = GlobalLock(hMem);
1437 if (pDdh == NULL)
1439 ERR("Failed on GlobalLock(%p)\n", hMem);
1440 return 0;
1443 if (pcbDataSize != NULL)
1445 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1447 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1448 return (LPBYTE)(pDdh + 1);
1451 /*****************************************************************
1452 * DdeUnaccessData (USER32.@)
1454 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1456 HGLOBAL hMem = hData;
1458 TRACE("(%p)\n", hData);
1460 GlobalUnlock(hMem);
1462 return TRUE;
1465 /*****************************************************************
1466 * DdeFreeDataHandle (USER32.@)
1468 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1470 TRACE("(%p)\n", hData);
1472 /* 1 is the handle value returned by an asynchronous operation. */
1473 if (hData == (HDDEDATA)1)
1474 return TRUE;
1476 return GlobalFree(hData) == 0;
1479 /******************************************************************
1480 * WDML_IsAppOwned
1484 BOOL WDML_IsAppOwned(HDDEDATA hData)
1486 DDE_DATAHANDLE_HEAD* pDdh;
1487 BOOL ret = FALSE;
1489 pDdh = GlobalLock(hData);
1490 if (pDdh != NULL)
1492 ret = pDdh->bAppOwned;
1493 GlobalUnlock(hData);
1495 return ret;
1498 /* ================================================================
1500 * Global <=> Data handle management
1502 * ================================================================ */
1504 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1505 * offset size
1506 * (bytes) (bits) comment
1507 * 0 16 bit fields for options (release, ackreq, response...)
1508 * 2 16 clipboard format
1509 * 4 ? data to be used
1511 HDDEDATA WDML_Global2DataHandle(WDML_CONV* pConv, HGLOBAL hMem, WINE_DDEHEAD* p)
1513 DDEDATA* pDd;
1514 HDDEDATA ret = 0;
1515 DWORD size;
1517 if (hMem)
1519 pDd = GlobalLock(hMem);
1520 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1521 if (pDd)
1523 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1524 switch (pDd->cfFormat)
1526 default:
1527 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1528 pDd->cfFormat, hMem);
1529 /* fall thru */
1530 case 0:
1531 case CF_TEXT:
1532 ret = DdeCreateDataHandle(pConv->instance->instanceID, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1533 break;
1534 case CF_BITMAP:
1535 if (size >= sizeof(BITMAP))
1537 BITMAP* bmp = (BITMAP*)pDd->Value;
1538 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1539 if (size >= sizeof(BITMAP) + count)
1541 HBITMAP hbmp;
1543 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1544 bmp->bmPlanes, bmp->bmBitsPixel,
1545 pDd->Value + sizeof(BITMAP))))
1547 ret = DdeCreateDataHandle(pConv->instance->instanceID, (LPBYTE)&hbmp, sizeof(hbmp),
1548 0, 0, CF_BITMAP, 0);
1550 else ERR("Can't create bmp\n");
1552 else
1554 ERR("Wrong count: %u / %d\n", size, count);
1556 } else ERR("No bitmap header\n");
1557 break;
1559 GlobalUnlock(hMem);
1562 return ret;
1565 /******************************************************************
1566 * WDML_DataHandle2Global
1570 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1571 BOOL fDeferUpd, BOOL fAckReq)
1573 DDE_DATAHANDLE_HEAD* pDdh;
1574 DWORD dwSize;
1575 HGLOBAL hMem = 0;
1577 dwSize = GlobalSize(hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1578 pDdh = GlobalLock(hDdeData);
1579 if (dwSize && pDdh)
1581 WINE_DDEHEAD* wdh = NULL;
1583 switch (pDdh->cfFormat)
1585 default:
1586 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1587 pDdh->cfFormat, hDdeData);
1588 /* fall thru */
1589 case 0:
1590 case CF_TEXT:
1591 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1592 if (hMem && (wdh = GlobalLock(hMem)))
1594 memcpy(wdh + 1, pDdh + 1, dwSize);
1596 break;
1597 case CF_BITMAP:
1598 if (dwSize >= sizeof(HBITMAP))
1600 BITMAP bmp;
1601 DWORD count;
1602 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1604 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1606 count = bmp.bmWidthBytes * bmp.bmHeight;
1607 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1608 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1609 if (hMem && (wdh = GlobalLock(hMem)))
1611 memcpy(wdh + 1, &bmp, sizeof(bmp));
1612 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1616 break;
1618 if (wdh)
1620 wdh->unused = 0;
1621 wdh->fResponse = fResponse;
1622 wdh->fRelease = fRelease;
1623 wdh->fDeferUpd = fDeferUpd;
1624 wdh->fAckReq = fAckReq;
1625 wdh->cfFormat = pDdh->cfFormat;
1626 GlobalUnlock(hMem);
1628 GlobalUnlock(hDdeData);
1631 return hMem;
1634 /* ================================================================
1636 * Server management
1638 * ================================================================ */
1640 /******************************************************************
1641 * WDML_AddServer
1645 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1647 static const WCHAR fmtW[] = {'%','s','(','0','x','%','*','x',')',0};
1648 WDML_SERVER* pServer;
1649 WCHAR buf1[256];
1650 WCHAR buf2[256];
1652 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1653 if (pServer == NULL) return NULL;
1655 pServer->hszService = hszService;
1656 WDML_IncHSZ(pInstance, hszService);
1658 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1659 snprintfW(buf2, 256, fmtW, buf1, 2*sizeof(ULONG_PTR), GetCurrentProcessId());
1660 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1662 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1663 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1665 pServer->filterOn = TRUE;
1667 pServer->next = pInstance->servers;
1668 pInstance->servers = pServer;
1669 return pServer;
1672 /******************************************************************
1673 * WDML_RemoveServer
1677 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1679 WDML_SERVER* pPrev = NULL;
1680 WDML_SERVER* pServer = NULL;
1681 WDML_CONV* pConv;
1682 WDML_CONV* pConvNext;
1684 pServer = pInstance->servers;
1686 while (pServer != NULL)
1688 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1690 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1691 pServer->atomService, pServer->atomServiceSpec);
1692 /* terminate all conversations for given topic */
1693 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1695 pConvNext = pConv->next;
1696 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1698 HWND client = pConv->hwndClient, server = pConv->hwndServer;
1699 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1700 /* don't care about return code (whether client window is present or not) */
1701 PostMessageW(client, WM_DDE_TERMINATE, (WPARAM)server, 0);
1704 if (pServer == pInstance->servers)
1706 pInstance->servers = pServer->next;
1708 else
1710 pPrev->next = pServer->next;
1713 DestroyWindow(pServer->hwndServer);
1714 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1715 WDML_DecHSZ(pInstance, pServer->hszService);
1717 GlobalDeleteAtom(pServer->atomService);
1718 GlobalDeleteAtom(pServer->atomServiceSpec);
1720 HeapFree(GetProcessHeap(), 0, pServer);
1721 break;
1724 pPrev = pServer;
1725 pServer = pServer->next;
1729 /*****************************************************************************
1730 * WDML_FindServer
1732 * generic routine to return a pointer to the relevant ServiceNode
1733 * for a given service name, or NULL if the entry does not exist
1736 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1738 WDML_SERVER* pServer;
1740 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1742 if (hszService == pServer->hszService)
1744 return pServer;
1747 TRACE("Service name missing\n");
1748 return NULL;
1751 /* ================================================================
1753 * Conversation management
1755 * ================================================================ */
1757 /******************************************************************
1758 * WDML_AddConv
1762 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1763 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1765 WDML_CONV* pConv;
1767 /* no conversation yet, add it */
1768 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1769 if (!pConv) return NULL;
1771 pConv->instance = pInstance;
1772 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1773 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1774 pConv->magic = WDML_CONV_MAGIC;
1775 pConv->hwndServer = hwndServer;
1776 pConv->hwndClient = hwndClient;
1777 pConv->transactions = NULL;
1778 pConv->hUser = 0;
1779 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1780 pConv->wStatus |= pInstance->wStatus;
1781 /* check if both side of the conversation are of the same instance */
1782 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1783 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1785 pConv->wStatus |= ST_ISSELF;
1787 pConv->wConvst = XST_NULL;
1789 pConv->next = pInstance->convs[side];
1790 pInstance->convs[side] = pConv;
1792 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv->wStatus, pInstance);
1794 return pConv;
1797 /******************************************************************
1798 * WDML_FindConv
1802 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1803 HSZ hszService, HSZ hszTopic)
1805 WDML_CONV* pCurrent = NULL;
1807 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1809 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1810 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1812 return pCurrent;
1816 return NULL;
1819 /******************************************************************
1820 * WDML_RemoveConv
1824 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1826 WDML_CONV* pPrev = NULL;
1827 WDML_CONV* pCurrent;
1828 WDML_XACT* pXAct;
1829 WDML_XACT* pXActNext;
1830 HWND hWnd;
1832 if (!pRef)
1833 return;
1835 /* remove any pending transaction */
1836 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1838 pXActNext = pXAct->next;
1839 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1842 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1844 /* FIXME: should we keep the window around ? it seems so (at least on client side
1845 * to let QueryConvInfo work after conv termination, but also to implement
1846 * DdeReconnect...
1848 /* destroy conversation window, but first remove pConv from hWnd.
1849 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1851 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1852 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1854 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1856 WDML_DecHSZ(pRef->instance, pRef->hszService);
1857 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1859 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1861 if (pCurrent == pRef)
1863 if (pCurrent == pRef->instance->convs[side])
1865 pRef->instance->convs[side] = pCurrent->next;
1867 else
1869 pPrev->next = pCurrent->next;
1871 pCurrent->magic = 0;
1872 HeapFree(GetProcessHeap(), 0, pCurrent);
1873 break;
1878 /******************************************************************
1879 * WDML_EnableCallback
1881 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1883 if (wCmd == EC_DISABLE)
1885 pConv->wStatus |= ST_BLOCKED;
1886 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
1887 return TRUE;
1890 if (wCmd == EC_QUERYWAITING)
1891 return pConv->transactions ? TRUE : FALSE;
1893 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1895 FIXME("Unknown command code %04x\n", wCmd);
1896 return FALSE;
1899 if (wCmd == EC_ENABLEALL)
1901 pConv->wStatus &= ~ST_BLOCKED;
1902 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
1905 while (pConv->transactions)
1907 WDML_XACT *pXAct = pConv->transactions;
1909 if (pConv->wStatus & ST_CLIENT)
1911 /* transaction should be in the queue until handled */
1912 WDML_ClientHandle(pConv, pXAct, 0, NULL);
1913 WDML_UnQueueTransaction(pConv, pXAct);
1915 else
1917 /* transaction should be removed from the queue before handling */
1918 WDML_UnQueueTransaction(pConv, pXAct);
1919 WDML_ServerHandle(pConv, pXAct);
1922 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1924 if (wCmd == EC_ENABLEONE) break;
1926 return TRUE;
1929 /*****************************************************************
1930 * DdeEnableCallback (USER32.@)
1932 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1934 BOOL ret = FALSE;
1935 WDML_CONV *pConv;
1937 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
1939 if (hConv)
1941 pConv = WDML_GetConv(hConv, TRUE);
1943 if (pConv && pConv->instance->instanceID == idInst)
1944 ret = WDML_EnableCallback(pConv, wCmd);
1946 else
1948 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
1950 if (!pInstance)
1951 return FALSE;
1953 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
1954 pInstance->wStatus |= wCmd;
1956 if (wCmd == EC_DISABLE)
1958 pInstance->wStatus |= ST_BLOCKED;
1959 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1961 else if (wCmd == EC_ENABLEALL)
1963 pInstance->wStatus &= ~ST_BLOCKED;
1964 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1967 ret = TRUE;
1969 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
1971 ret = WDML_EnableCallback(pConv, wCmd);
1972 if (ret && wCmd == EC_QUERYWAITING) break;
1976 return ret;
1979 /******************************************************************
1980 * WDML_GetConv
1984 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1986 WDML_CONV* pConv = (WDML_CONV*)hConv;
1988 /* FIXME: should do better checking */
1989 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1991 if (!pConv->instance)
1993 WARN("wrong thread ID, no instance\n");
1994 return NULL;
1997 if (pConv->instance->threadID != GetCurrentThreadId())
1999 WARN("wrong thread ID\n");
2000 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
2001 return NULL;
2004 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
2006 WARN("found conv but ain't connected\n");
2007 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
2008 return NULL;
2011 return pConv;
2014 /******************************************************************
2015 * WDML_GetConvFromWnd
2019 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
2021 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
2024 /******************************************************************
2025 * WDML_PostAck
2029 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
2030 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
2032 DDEACK ddeAck;
2033 HWND from, to;
2035 if (side == WDML_SERVER_SIDE)
2037 from = pConv->hwndServer;
2038 to = pConv->hwndClient;
2040 else
2042 to = pConv->hwndServer;
2043 from = pConv->hwndClient;
2046 ddeAck.bAppReturnCode = appRetCode;
2047 ddeAck.reserved = 0;
2048 ddeAck.fBusy = fBusy;
2049 ddeAck.fAck = fAck;
2051 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2053 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2054 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2055 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2057 pConv->wStatus &= ~ST_CONNECTED;
2058 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2059 FreeDDElParam(WM_DDE_ACK, lParam);
2060 return FALSE;
2062 return TRUE;
2065 /*****************************************************************
2066 * DdeSetUserHandle (USER32.@)
2068 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2070 WDML_CONV* pConv;
2072 pConv = WDML_GetConv(hConv, FALSE);
2073 if (pConv == NULL)
2074 return FALSE;
2076 if (id == QID_SYNC)
2078 pConv->hUser = hUser;
2080 else
2082 WDML_XACT* pXAct;
2084 pXAct = WDML_FindTransaction(pConv, id);
2085 if (pXAct)
2087 pXAct->hUser = hUser;
2089 else
2091 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2092 return FALSE;
2095 return TRUE;
2098 /******************************************************************
2099 * WDML_GetLocalConvInfo
2103 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2105 BOOL ret = TRUE;
2106 WDML_LINK* pLink;
2107 WDML_SIDE side;
2109 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2110 ci->hszSvcPartner = pConv->hszService;
2111 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2112 ci->hszTopic = pConv->hszTopic;
2113 ci->wStatus = pConv->wStatus;
2115 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2117 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2119 if (pLink->hConv == (HCONV)pConv)
2121 ci->wStatus |= ST_ADVISE;
2122 break;
2126 /* FIXME: non handled status flags:
2127 ST_BLOCKED
2128 ST_BLOCKNEXT
2129 ST_INLIST
2132 ci->wConvst = pConv->wConvst; /* FIXME */
2134 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2135 ci->hConvList = 0;
2136 ci->ConvCtxt = pConv->convContext;
2137 if (ci->wStatus & ST_CLIENT)
2139 ci->hwnd = pConv->hwndClient;
2140 ci->hwndPartner = pConv->hwndServer;
2142 else
2144 ci->hwnd = pConv->hwndServer;
2145 ci->hwndPartner = pConv->hwndClient;
2147 if (id == QID_SYNC)
2149 ci->hUser = pConv->hUser;
2150 ci->hszItem = 0;
2151 ci->wFmt = 0;
2152 ci->wType = 0;
2154 else
2156 WDML_XACT* pXAct;
2158 pXAct = WDML_FindTransaction(pConv, id);
2159 if (pXAct)
2161 ci->hUser = pXAct->hUser;
2162 ci->hszItem = pXAct->hszItem;
2163 ci->wFmt = pXAct->wFmt;
2164 ci->wType = pXAct->wType;
2166 else
2168 ret = 0;
2169 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2172 return ret;
2175 /******************************************************************
2176 * DdeQueryConvInfo (USER32.@)
2178 * FIXME: Set last DDE error on failure.
2180 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2182 UINT ret = lpConvInfo->cb;
2183 CONVINFO ci;
2184 WDML_CONV* pConv;
2186 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2188 if (!hConv)
2190 FIXME("hConv is NULL\n");
2191 return 0;
2194 pConv = WDML_GetConv(hConv, FALSE);
2195 if (pConv != NULL)
2197 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2198 ret = 0;
2200 else
2202 if ((ULONG_PTR)hConv & 1)
2204 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2205 if (pConv != NULL)
2206 FIXME("Request on remote conversation information is not implemented yet\n");
2208 ret = 0;
2211 if (ret != 0)
2212 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2213 return ret;
2216 /* ================================================================
2218 * Link (hot & warm) management
2220 * ================================================================ */
2222 /******************************************************************
2223 * WDML_AddLink
2227 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2228 UINT wType, HSZ hszItem, UINT wFmt)
2230 WDML_LINK* pLink;
2232 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2233 if (pLink == NULL)
2235 ERR("OOM\n");
2236 return;
2239 pLink->hConv = hConv;
2240 pLink->transactionType = wType;
2241 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2242 pLink->uFmt = wFmt;
2243 pLink->next = pInstance->links[side];
2244 pInstance->links[side] = pLink;
2247 /******************************************************************
2248 * WDML_RemoveLink
2252 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2253 HSZ hszItem, UINT uFmt)
2255 WDML_LINK* pPrev = NULL;
2256 WDML_LINK* pCurrent = NULL;
2258 pCurrent = pInstance->links[side];
2260 while (pCurrent != NULL)
2262 if (pCurrent->hConv == hConv &&
2263 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2264 pCurrent->uFmt == uFmt)
2266 if (pCurrent == pInstance->links[side])
2268 pInstance->links[side] = pCurrent->next;
2270 else
2272 pPrev->next = pCurrent->next;
2275 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2276 HeapFree(GetProcessHeap(), 0, pCurrent);
2277 break;
2280 pPrev = pCurrent;
2281 pCurrent = pCurrent->next;
2285 /* this function is called to remove all links related to the conv.
2286 It should be called from both client and server when terminating
2287 the conversation.
2289 /******************************************************************
2290 * WDML_RemoveAllLinks
2294 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2296 WDML_LINK* pPrev = NULL;
2297 WDML_LINK* pCurrent = NULL;
2298 WDML_LINK* pNext = NULL;
2300 pCurrent = pInstance->links[side];
2302 while (pCurrent != NULL)
2304 if (pCurrent->hConv == (HCONV)pConv)
2306 if (pCurrent == pInstance->links[side])
2308 pInstance->links[side] = pCurrent->next;
2309 pNext = pCurrent->next;
2311 else
2313 pPrev->next = pCurrent->next;
2314 pNext = pCurrent->next;
2317 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2319 HeapFree(GetProcessHeap(), 0, pCurrent);
2320 pCurrent = NULL;
2323 if (pCurrent)
2325 pPrev = pCurrent;
2326 pCurrent = pCurrent->next;
2328 else
2330 pCurrent = pNext;
2335 /******************************************************************
2336 * WDML_FindLink
2340 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2341 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2343 WDML_LINK* pCurrent = NULL;
2345 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2347 /* we don't need to check for transaction type as it can be altered */
2349 if (pCurrent->hConv == hConv &&
2350 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2351 (!use_fmt || pCurrent->uFmt == uFmt))
2353 break;
2358 return pCurrent;
2361 /* ================================================================
2363 * Transaction management
2365 * ================================================================ */
2367 /******************************************************************
2368 * WDML_AllocTransaction
2370 * Alloc a transaction structure for handling the message ddeMsg
2372 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2373 UINT wFmt, HSZ hszItem)
2375 WDML_XACT* pXAct;
2376 static WORD tid = 1; /* FIXME: wrap around */
2378 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2379 if (!pXAct)
2381 pInstance->lastError = DMLERR_MEMORY_ERROR;
2382 return NULL;
2385 pXAct->xActID = tid++;
2386 pXAct->ddeMsg = ddeMsg;
2387 pXAct->hDdeData = 0;
2388 pXAct->hUser = 0;
2389 pXAct->next = NULL;
2390 pXAct->wType = 0;
2391 pXAct->wFmt = wFmt;
2392 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2393 pXAct->atom = 0;
2394 pXAct->hMem = 0;
2395 pXAct->lParam = 0;
2397 return pXAct;
2400 /******************************************************************
2401 * WDML_QueueTransaction
2403 * Adds a transaction to the list of transaction
2405 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2407 WDML_XACT** pt;
2409 /* advance to last in queue */
2410 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2411 *pt = pXAct;
2414 /******************************************************************
2415 * WDML_UnQueueTransaction
2419 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2421 WDML_XACT** pt;
2423 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2425 if (*pt == pXAct)
2427 *pt = pXAct->next;
2428 return TRUE;
2431 return FALSE;
2434 /******************************************************************
2435 * WDML_FreeTransaction
2439 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2441 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2442 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2444 GlobalFree(pXAct->hMem);
2446 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2448 HeapFree(GetProcessHeap(), 0, pXAct);
2451 /******************************************************************
2452 * WDML_FindTransaction
2456 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2458 WDML_XACT* pXAct;
2460 tid = HIWORD(tid);
2461 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2463 if (pXAct->xActID == tid)
2464 break;
2466 return pXAct;
2469 /* ================================================================
2471 * Information broadcast across DDEML implementations
2473 * ================================================================ */
2475 struct tagWDML_BroadcastPmt
2477 LPCWSTR clsName;
2478 UINT uMsg;
2479 WPARAM wParam;
2480 LPARAM lParam;
2483 /******************************************************************
2484 * WDML_BroadcastEnumProc
2488 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2490 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2491 WCHAR buffer[128];
2493 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2494 lstrcmpiW(buffer, s->clsName) == 0)
2496 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2498 return TRUE;
2501 /******************************************************************
2502 * WDML_BroadcastDDEWindows
2506 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2508 struct tagWDML_BroadcastPmt s;
2510 s.clsName = clsName;
2511 s.uMsg = uMsg;
2512 s.wParam = wParam;
2513 s.lParam = lParam;
2514 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);