4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
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
27 #include "wine/port.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 =
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.@)
76 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
87 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
89 ERR("GlobalAlloc failed\n");
92 if (!(params = GlobalLock(hMem)))
94 ERR("GlobalLock failed (%p)\n", hMem);
106 return MAKELPARAM(uiLo, uiHi);
111 /*****************************************************************
112 * UnpackDDElParam (USER32.@)
118 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
119 PUINT_PTR uiLo, PUINT_PTR uiHi)
129 if (!lParam || !(params = GlobalLock((HGLOBAL)lParam)))
135 TRACE("unpacked: low %08lx, high %08lx\n", params[0], params[1]);
136 if (uiLo) *uiLo = params[0];
137 if (uiHi) *uiHi = params[1];
138 GlobalUnlock( (HGLOBAL)lParam );
143 if (uiHi) *uiHi = lParam;
147 if (uiLo) *uiLo = LOWORD(lParam);
148 if (uiHi) *uiHi = HIWORD(lParam);
154 /*****************************************************************
155 * FreeDDElParam (USER32.@)
161 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
169 /* first check if it's a global handle */
170 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
171 return !GlobalFree( (HGLOBAL)lParam );
179 /*****************************************************************
180 * ReuseDDElParam (USER32.@)
185 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
186 UINT_PTR uiLo, UINT_PTR uiHi)
202 if (!lParam) return 0;
203 if (!(params = GlobalLock( (HGLOBAL)lParam )))
205 ERR("GlobalLock failed\n");
210 TRACE("Reusing pack %08lx %08lx\n", uiLo, uiHi);
211 GlobalUnlock( (HGLOBAL)lParam );
215 FreeDDElParam( msgIn, lParam );
219 FreeDDElParam( msgIn, lParam );
220 return MAKELPARAM(uiLo, uiHi);
224 return PackDDElParam( msgOut, uiLo, uiHi );
228 /*****************************************************************
229 * ImpersonateDdeClientWindow (USER32.@)
232 * hWndClient [I] handle to DDE client window
233 * hWndServer [I] handle to DDE server window
235 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
237 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
241 /*****************************************************************
242 * DdeSetQualityOfService (USER32.@)
245 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
246 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
248 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
252 /* ================================================================
254 * Instance management
256 * ================================================================ */
258 /******************************************************************************
259 * IncrementInstanceId
261 * generic routine to increment the max instance Id and allocate a new application instance
263 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
265 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
267 pInstance->instanceID = id;
268 TRACE("New instance id %d allocated\n", id);
271 /******************************************************************
276 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
278 WDML_INSTANCE* pInstance;
283 case WM_WDML_REGISTER:
284 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
285 /* try calling the Callback */
286 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
288 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
289 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
290 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
291 WDML_DecHSZ(pInstance, hsz1);
292 WDML_DecHSZ(pInstance, hsz2);
296 case WM_WDML_UNREGISTER:
297 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
298 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
300 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
301 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
302 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
303 WDML_DecHSZ(pInstance, hsz1);
304 WDML_DecHSZ(pInstance, hsz2);
308 case WM_WDML_CONNECT_CONFIRM:
309 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
310 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
313 /* confirm connection...
314 * lookup for this conv handle
316 HWND client = WIN_GetFullHandle( (HWND)wParam );
317 HWND server = WIN_GetFullHandle( (HWND)lParam );
318 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
320 if (pConv->hwndClient == client && pConv->hwndServer == server)
325 pConv->wStatus |= ST_ISLOCAL;
327 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
328 pConv->hszTopic, pConv->hszService, 0, 0,
329 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
334 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
339 /******************************************************************
344 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
345 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
347 WDML_INSTANCE* pInstance;
348 WDML_INSTANCE* reference_inst;
350 WNDCLASSEXW wndclass;
352 TRACE("(%p,%p,0x%x,%d)\n",
353 pidInst, pfnCallback, afCmd, ulRes);
357 ERR("Reserved value not zero? What does this mean?\n");
358 /* trap this and no more until we know more */
359 return DMLERR_NO_ERROR;
362 /* grab enough heap for one control struct - not really necessary for re-initialise
363 * but allows us to use same validation routines */
364 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
365 if (pInstance == NULL)
367 /* catastrophe !! warn user & abort */
368 ERR("Instance create failed - out of memory\n");
369 return DMLERR_SYS_ERROR;
371 pInstance->next = NULL;
372 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
374 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
376 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
377 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
378 pInstance->threadID = GetCurrentThreadId();
379 pInstance->callback = *pfnCallback;
380 pInstance->unicode = bUnicode;
381 pInstance->win16 = b16;
382 pInstance->nodeList = NULL; /* node will be added later */
383 pInstance->monitorFlags = afCmd & MF_MASK;
384 pInstance->wStatus = 0;
385 pInstance->servers = NULL;
386 pInstance->convs[0] = NULL;
387 pInstance->convs[1] = NULL;
388 pInstance->links[0] = NULL;
389 pInstance->links[1] = NULL;
391 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
393 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
395 if (!pInstance->clientOnly)
397 /* Check for other way of setting Client-only !! */
398 pInstance->clientOnly =
399 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
402 TRACE("instance created - checking validity\n");
406 /* Initialisation of new Instance Identifier */
407 TRACE("new instance, callback %p flags %X\n",pfnCallback,afCmd);
409 EnterCriticalSection(&WDML_CritSect);
411 if (WDML_InstanceList == NULL)
413 /* can't be another instance in this case, assign to the base pointer */
414 WDML_InstanceList = pInstance;
416 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
418 * ------------------------------- NOTE NOTE NOTE --------------------------
420 * the manual is not clear if this condition
421 * applies to the first call to DdeInitialize from an application, or the
422 * first call for a given callback !!!
425 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
426 TRACE("First application instance detected OK\n");
427 /* allocate new instance ID */
428 WDML_IncrementInstanceId(pInstance);
432 /* really need to chain the new one in to the latest here, but after checking conditions
433 * such as trying to start a conversation from an application trying to monitor */
434 reference_inst = WDML_InstanceList;
435 TRACE("Subsequent application instance - starting checks\n");
436 while (reference_inst->next != NULL)
439 * This set of tests will work if application uses same instance Id
440 * at application level once allocated - which is what manual implies
441 * should happen. If someone tries to be
442 * clever (lazy ?) it will fail to pick up that later calls are for
443 * the same application - should we trust them ?
445 if (pInstance->instanceID == reference_inst->instanceID)
447 /* Check 1 - must be same Client-only state */
449 if (pInstance->clientOnly != reference_inst->clientOnly)
451 ret = DMLERR_DLL_USAGE;
455 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
457 if (pInstance->monitor != reference_inst->monitor)
459 ret = DMLERR_INVALIDPARAMETER;
463 /* Check 3 - must supply different callback address */
465 if (pInstance->callback == reference_inst->callback)
467 ret = DMLERR_DLL_USAGE;
471 reference_inst = reference_inst->next;
473 /* All cleared, add to chain */
475 TRACE("Application Instance checks finished\n");
476 WDML_IncrementInstanceId(pInstance);
477 reference_inst->next = pInstance;
479 LeaveCriticalSection(&WDML_CritSect);
481 *pidInst = pInstance->instanceID;
483 /* for deadlock issues, windows must always be created when outside the critical section */
484 wndclass.cbSize = sizeof(wndclass);
486 wndclass.lpfnWndProc = WDML_EventProc;
487 wndclass.cbClsExtra = 0;
488 wndclass.cbWndExtra = sizeof(ULONG_PTR);
489 wndclass.hInstance = 0;
491 wndclass.hCursor = 0;
492 wndclass.hbrBackground = 0;
493 wndclass.lpszMenuName = NULL;
494 wndclass.lpszClassName = WDML_szEventClass;
495 wndclass.hIconSm = 0;
497 RegisterClassExW(&wndclass);
499 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
500 WS_POPUP, 0, 0, 0, 0,
503 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
505 TRACE("New application instance processing finished OK\n");
509 /* Reinitialisation situation --- FIX */
510 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst, pfnCallback, afCmd, ulRes);
512 EnterCriticalSection(&WDML_CritSect);
514 if (WDML_InstanceList == NULL)
516 ret = DMLERR_INVALIDPARAMETER;
519 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
520 /* can't reinitialise if we have initialised nothing !! */
521 reference_inst = WDML_InstanceList;
522 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
524 * MS allows initialisation without specifying a callback, should we allow addition of the
525 * callback by a later call to initialise ? - if so this lot will have to change
527 while (reference_inst->next != NULL)
529 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
531 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
533 if (reference_inst->clientOnly)
535 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
537 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
539 if (!(afCmd & APPCMD_CLIENTONLY))
541 ret = DMLERR_INVALIDPARAMETER;
546 /* Check 2 - cannot change monitor modes */
548 if (pInstance->monitor != reference_inst->monitor)
550 ret = DMLERR_INVALIDPARAMETER;
554 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
556 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
558 ret = DMLERR_INVALIDPARAMETER;
563 reference_inst = reference_inst->next;
565 if (reference_inst->next == NULL)
567 ret = DMLERR_INVALIDPARAMETER;
570 /* All checked - change relevant flags */
572 reference_inst->CBFflags = pInstance->CBFflags;
573 reference_inst->clientOnly = pInstance->clientOnly;
574 reference_inst->monitorFlags = pInstance->monitorFlags;
575 LeaveCriticalSection(&WDML_CritSect);
578 return DMLERR_NO_ERROR;
580 HeapFree(GetProcessHeap(), 0, pInstance);
581 LeaveCriticalSection(&WDML_CritSect);
585 /******************************************************************************
586 * DdeInitializeA (USER32.@)
588 * See DdeInitializeW.
590 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
591 DWORD afCmd, DWORD ulRes)
593 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
596 /******************************************************************************
597 * DdeInitializeW [USER32.@]
598 * Registers an application with the DDEML
601 * pidInst [I] Pointer to instance identifier
602 * pfnCallback [I] Pointer to callback function
603 * afCmd [I] Set of command and filter flags
607 * Success: DMLERR_NO_ERROR
608 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
610 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
611 DWORD afCmd, DWORD ulRes)
613 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
616 /*****************************************************************
617 * DdeUninitialize [USER32.@] Frees DDEML resources
620 * idInst [I] Instance identifier
627 BOOL WINAPI DdeUninitialize(DWORD idInst)
629 /* Stage one - check if we have a handle for this instance
631 WDML_INSTANCE* pInstance;
633 WDML_CONV* pConvNext;
635 TRACE("(%d)\n", idInst);
637 /* First check instance
639 pInstance = WDML_GetInstance(idInst);
640 if (pInstance == NULL)
643 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
648 /* first terminate all conversations client side
649 * this shall close existing links...
651 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
653 pConvNext = pConv->next;
654 DdeDisconnect((HCONV)pConv);
656 if (pInstance->convs[WDML_CLIENT_SIDE])
657 FIXME("still pending conversations\n");
659 /* then unregister all known service names */
660 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
662 /* Free the nodes that were not freed by this instance
663 * and remove the nodes from the list of HSZ nodes.
665 WDML_FreeAllHSZ(pInstance);
667 DestroyWindow(pInstance->hwndEvent);
669 /* OK now delete the instance handle itself */
671 if (WDML_InstanceList == pInstance)
673 /* special case - the first/only entry */
674 WDML_InstanceList = pInstance->next;
678 /* general case, remove entry */
681 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
682 inst->next = pInstance->next;
684 /* release the heap entry
686 HeapFree(GetProcessHeap(), 0, pInstance);
691 /******************************************************************
692 * WDML_NotifyThreadExit
696 void WDML_NotifyThreadDetach(void)
698 WDML_INSTANCE* pInstance;
700 DWORD tid = GetCurrentThreadId();
702 EnterCriticalSection(&WDML_CritSect);
703 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
705 next = pInstance->next;
706 if (pInstance->threadID == tid)
708 LeaveCriticalSection(&WDML_CritSect);
709 DdeUninitialize(pInstance->instanceID);
710 EnterCriticalSection(&WDML_CritSect);
713 LeaveCriticalSection(&WDML_CritSect);
716 /******************************************************************
717 * WDML_InvokeCallback
721 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
722 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
723 ULONG_PTR dwData1, ULONG_PTR dwData2)
727 if (pInstance == NULL)
730 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
731 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
732 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
733 if (pInstance->win16)
735 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
736 hsz1, hsz2, hdata, dwData1, dwData2);
740 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
742 TRACE("done => %p\n", ret);
746 /*****************************************************************************
749 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
750 * for an instance Id, or NULL if the entry does not exist
753 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
755 WDML_INSTANCE* pInstance;
757 EnterCriticalSection(&WDML_CritSect);
759 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
761 if (pInstance->instanceID == instId)
763 if (GetCurrentThreadId() != pInstance->threadID)
765 FIXME("Tried to get instance from wrong thread\n");
772 LeaveCriticalSection(&WDML_CritSect);
775 WARN("Instance entry missing for id %04x\n", instId);
779 /******************************************************************
780 * WDML_GetInstanceFromWnd
784 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
786 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
789 /******************************************************************************
790 * DdeGetLastError [USER32.@] Gets most recent error code
793 * idInst [I] Instance identifier
798 UINT WINAPI DdeGetLastError(DWORD idInst)
801 WDML_INSTANCE* pInstance;
803 /* First check instance
805 pInstance = WDML_GetInstance(idInst);
806 if (pInstance == NULL)
808 error_code = DMLERR_INVALIDPARAMETER;
812 error_code = pInstance->lastError;
813 pInstance->lastError = 0;
819 /* ================================================================
823 * ================================================================ */
826 /******************************************************************
831 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
835 if (pInstance == NULL) return NULL;
837 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
839 if (pNode->hsz == hsz) break;
841 if (!pNode) WARN("HSZ %p not found\n", hsz);
845 /******************************************************************
846 * WDML_MakeAtomFromHsz
848 * Creates a global atom from an existing HSZ
849 * Generally used before sending an HSZ as an atom to a remote app
851 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
853 WCHAR nameBuffer[MAX_BUFFER_LEN];
855 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
856 return GlobalAddAtomW(nameBuffer);
857 WARN("HSZ %p not found\n", hsz);
861 /******************************************************************
862 * WDML_MakeHszFromAtom
864 * Creates a HSZ from an existing global atom
865 * Generally used while receiving a global atom and transforming it
868 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
870 WCHAR nameBuffer[MAX_BUFFER_LEN];
872 if (!atom) return NULL;
874 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
876 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
877 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
879 WARN("ATOM 0x%x not found\n", atom);
883 /******************************************************************
888 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
892 pNode = WDML_FindNode(pInstance, hsz);
893 if (!pNode) return FALSE;
899 /******************************************************************************
900 * WDML_DecHSZ (INTERNAL)
902 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
904 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
906 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
908 HSZNode* pPrev = NULL;
911 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
913 /* If we found the node we were looking for and its ref count is one,
916 if (pCurrent->hsz == hsz)
918 if (--pCurrent->refCount == 0)
920 if (pCurrent == pInstance->nodeList)
922 pInstance->nodeList = pCurrent->next;
926 pPrev->next = pCurrent->next;
928 HeapFree(GetProcessHeap(), 0, pCurrent);
929 DeleteAtom(HSZ2ATOM(hsz));
934 WARN("HSZ %p not found\n", hsz);
939 /******************************************************************************
940 * WDML_FreeAllHSZ (INTERNAL)
942 * Frees up all the strings still allocated in the list and
943 * remove all the nodes from the list of HSZ nodes.
945 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
947 /* Free any strings created in this instance.
949 while (pInstance->nodeList != NULL)
951 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
955 /******************************************************************************
956 * InsertHSZNode (INTERNAL)
958 * Insert a node to the head of the list.
960 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
964 HSZNode* pNew = NULL;
965 /* Create a new node for this HSZ.
967 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
971 pNew->next = pInstance->nodeList;
973 pInstance->nodeList = pNew;
977 ERR("Primary HSZ Node allocation failed - out of memory\n");
982 /******************************************************************
987 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
990 WCHAR pString[MAX_BUFFER_LEN];
992 /* If psz is null, we have to return only the length
998 cchMax = MAX_BUFFER_LEN;
1004 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
1007 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
1010 ERR("Unknown code page %d\n", codepage);
1016 /*****************************************************************
1017 * DdeQueryStringA [USER32.@]
1019 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1022 WDML_INSTANCE* pInstance;
1024 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1026 /* First check instance
1028 pInstance = WDML_GetInstance(idInst);
1029 if (pInstance != NULL)
1031 if (iCodePage == 0) iCodePage = CP_WINANSI;
1032 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1035 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
1039 /*****************************************************************
1040 * DdeQueryStringW [USER32.@]
1043 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1046 WDML_INSTANCE* pInstance;
1048 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1050 /* First check instance
1052 pInstance = WDML_GetInstance(idInst);
1053 if (pInstance != NULL)
1055 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1056 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1059 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
1063 /******************************************************************
1068 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1075 hsz = ATOM2HSZ(AddAtomA(ptr));
1076 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
1079 hsz = ATOM2HSZ(AddAtomW(ptr));
1080 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
1083 ERR("Unknown code page %d\n", codepage);
1086 WDML_InsertHSZNode(pInstance, hsz);
1090 /*****************************************************************
1091 * DdeCreateStringHandleA [USER32.@]
1093 * See DdeCreateStringHandleW.
1095 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1098 WDML_INSTANCE* pInstance;
1100 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
1102 pInstance = WDML_GetInstance(idInst);
1105 if (codepage == 0) codepage = CP_WINANSI;
1106 hsz = WDML_CreateString(pInstance, psz, codepage);
1113 /******************************************************************************
1114 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1117 * idInst [I] Instance identifier
1118 * psz [I] Pointer to string
1119 * codepage [I] Code page identifier
1121 * Success: String handle
1124 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1126 WDML_INSTANCE* pInstance;
1129 TRACE("(%d,%s,%d)\n", idInst, debugstr_w(psz), codepage);
1131 pInstance = WDML_GetInstance(idInst);
1134 if (codepage == 0) codepage = CP_WINUNICODE;
1135 hsz = WDML_CreateString(pInstance, psz, codepage);
1141 /*****************************************************************
1142 * DdeFreeStringHandle (USER32.@)
1147 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1149 WDML_INSTANCE* pInstance;
1152 TRACE("(%d,%p):\n", idInst, hsz);
1154 /* First check instance
1156 pInstance = WDML_GetInstance(idInst);
1158 ret = WDML_DecHSZ(pInstance, hsz);
1163 /*****************************************************************
1164 * DdeKeepStringHandle (USER32.@)
1170 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1172 WDML_INSTANCE* pInstance;
1175 TRACE("(%d,%p):\n", idInst, hsz);
1177 /* First check instance
1179 pInstance = WDML_GetInstance(idInst);
1181 ret = WDML_IncHSZ(pInstance, hsz);
1186 /*****************************************************************
1187 * DdeCmpStringHandles (USER32.@)
1189 * Compares the value of two string handles. This comparison is
1190 * not case sensitive.
1193 * hsz1 [I] Handle to the first string
1194 * hsz2 [I] Handle to the second string
1197 * -1 The value of hsz1 is zero or less than hsz2
1198 * 0 The values of hsz 1 and 2 are the same or both zero.
1199 * 1 The value of hsz2 is zero of less than hsz1
1201 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1203 WCHAR psz1[MAX_BUFFER_LEN];
1204 WCHAR psz2[MAX_BUFFER_LEN];
1208 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1209 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1211 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1213 /* Make sure we found both strings. */
1214 if (ret1 == 0 && ret2 == 0)
1216 /* If both are not found, return both "zero strings". */
1221 /* If hsz1 is a not found, return hsz1 is "zero string". */
1226 /* If hsz2 is a not found, return hsz2 is "zero string". */
1231 /* Compare the two strings we got (case insensitive). */
1232 ret = lstrcmpiW(psz1, psz2);
1233 /* Since strcmp returns any number smaller than
1234 * 0 when the first string is found to be less than
1235 * the second one we must make sure we are returning
1236 * the proper values.
1251 /* ================================================================
1253 * Data handle management
1255 * ================================================================ */
1257 /*****************************************************************
1258 * DdeCreateDataHandle (USER32.@)
1260 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1261 HSZ hszItem, UINT wFmt, UINT afCmd)
1263 /* For now, we ignore idInst, hszItem.
1264 * The purpose of these arguments still need to be investigated.
1269 DDE_DATAHANDLE_HEAD* pDdh;
1270 WCHAR psz[MAX_BUFFER_LEN];
1272 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1274 psz[0] = HSZ2ATOM(hszItem);
1278 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1279 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1281 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1284 /* we use the first 4 bytes to store the size */
1285 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1287 ERR("GlobalAlloc failed\n");
1291 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1298 pDdh->cfFormat = wFmt;
1299 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1301 pByte = (LPBYTE)(pDdh + 1);
1304 memcpy(pByte, pSrc + cbOff, cb);
1308 TRACE("=> %p\n", hMem);
1309 return (HDDEDATA)hMem;
1312 /*****************************************************************
1314 * DdeAddData (USER32.@)
1316 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1318 DWORD old_sz, new_sz;
1321 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1323 pDst = DdeAccessData(hData, &old_sz);
1324 if (!pDst) return 0;
1326 new_sz = cb + cbOff;
1327 if (new_sz > old_sz)
1329 DdeUnaccessData(hData);
1330 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1331 GMEM_MOVEABLE | GMEM_DDESHARE);
1332 pDst = DdeAccessData(hData, &old_sz);
1335 if (!pDst) return 0;
1337 memcpy(pDst + cbOff, pSrc, cb);
1338 DdeUnaccessData(hData);
1342 /******************************************************************************
1343 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1347 * hData [I] Handle to DDE object
1348 * pDst [I] Pointer to destination buffer
1349 * cbMax [I] Amount of data to copy
1350 * cbOff [I] Offset to beginning of data
1353 * Size of memory object associated with handle
1355 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1357 DWORD dwSize, dwRet;
1360 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1362 pByte = DdeAccessData(hData, &dwSize);
1370 else if (cbOff + cbMax < dwSize)
1374 else if (cbOff < dwSize)
1376 dwRet = dwSize - cbOff;
1382 if (pDst && dwRet != 0)
1384 memcpy(pDst, pByte + cbOff, dwRet);
1386 DdeUnaccessData(hData);
1395 /*****************************************************************
1396 * DdeAccessData (USER32.@)
1398 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1400 HGLOBAL hMem = (HGLOBAL)hData;
1401 DDE_DATAHANDLE_HEAD* pDdh;
1403 TRACE("(%p,%p)\n", hData, pcbDataSize);
1405 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1408 ERR("Failed on GlobalLock(%p)\n", hMem);
1412 if (pcbDataSize != NULL)
1414 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1416 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1417 return (LPBYTE)(pDdh + 1);
1420 /*****************************************************************
1421 * DdeUnaccessData (USER32.@)
1423 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1425 HGLOBAL hMem = (HGLOBAL)hData;
1427 TRACE("(%p)\n", hData);
1434 /*****************************************************************
1435 * DdeFreeDataHandle (USER32.@)
1437 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1439 TRACE("(%p)\n", hData);
1440 return GlobalFree((HGLOBAL)hData) == 0;
1443 /******************************************************************
1448 BOOL WDML_IsAppOwned(HDDEDATA hData)
1450 DDE_DATAHANDLE_HEAD* pDdh;
1453 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1456 ret = pDdh->bAppOwned;
1457 GlobalUnlock((HGLOBAL)hData);
1462 /* ================================================================
1464 * Global <=> Data handle management
1466 * ================================================================ */
1468 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1470 * (bytes) (bits) comment
1471 * 0 16 bit fields for options (release, ackreq, response...)
1472 * 2 16 clipboard format
1473 * 4 ? data to be used
1475 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1483 pDd = GlobalLock(hMem);
1484 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1487 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1488 switch (pDd->cfFormat)
1491 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1492 pDd->cfFormat, hMem);
1496 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1499 if (size >= sizeof(BITMAP))
1501 BITMAP* bmp = (BITMAP*)pDd->Value;
1502 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1503 if (size >= sizeof(BITMAP) + count)
1507 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1508 bmp->bmPlanes, bmp->bmBitsPixel,
1509 pDd->Value + sizeof(BITMAP))))
1511 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1512 0, 0, CF_BITMAP, 0);
1514 else ERR("Can't create bmp\n");
1518 ERR("Wrong count: %u / %d\n", size, count);
1520 } else ERR("No bitmap header\n");
1529 /******************************************************************
1530 * WDML_DataHandle2Global
1534 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1535 BOOL fDeferUpd, BOOL fAckReq)
1537 DDE_DATAHANDLE_HEAD* pDdh;
1541 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1542 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1545 WINE_DDEHEAD* wdh = NULL;
1547 switch (pDdh->cfFormat)
1550 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1551 pDdh->cfFormat, hDdeData);
1555 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1556 if (hMem && (wdh = GlobalLock(hMem)))
1558 memcpy(wdh + 1, pDdh + 1, dwSize);
1562 if (dwSize >= sizeof(HBITMAP))
1566 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1568 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1570 count = bmp.bmWidthBytes * bmp.bmHeight;
1571 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1572 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1573 if (hMem && (wdh = GlobalLock(hMem)))
1575 memcpy(wdh + 1, &bmp, sizeof(bmp));
1576 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1585 wdh->fResponse = fResponse;
1586 wdh->fRelease = fRelease;
1587 wdh->fDeferUpd = fDeferUpd;
1588 wdh->fAckReq = fAckReq;
1589 wdh->cfFormat = pDdh->cfFormat;
1592 GlobalUnlock((HGLOBAL)hDdeData);
1598 /* ================================================================
1602 * ================================================================ */
1604 /******************************************************************
1609 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1611 static const WCHAR fmtW[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1612 WDML_SERVER* pServer;
1616 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1617 if (pServer == NULL) return NULL;
1619 pServer->hszService = hszService;
1620 WDML_IncHSZ(pInstance, hszService);
1622 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1623 snprintfW(buf2, 256, fmtW, buf1, GetCurrentProcessId());
1624 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1626 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1627 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1629 pServer->filterOn = TRUE;
1631 pServer->next = pInstance->servers;
1632 pInstance->servers = pServer;
1636 /******************************************************************
1641 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1643 WDML_SERVER* pPrev = NULL;
1644 WDML_SERVER* pServer = NULL;
1646 WDML_CONV* pConvNext;
1648 pServer = pInstance->servers;
1650 while (pServer != NULL)
1652 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1654 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1655 pServer->atomService, pServer->atomServiceSpec);
1656 /* terminate all conversations for given topic */
1657 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1659 pConvNext = pConv->next;
1660 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1662 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1663 /* don't care about return code (whether client window is present or not) */
1664 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
1667 if (pServer == pInstance->servers)
1669 pInstance->servers = pServer->next;
1673 pPrev->next = pServer->next;
1676 DestroyWindow(pServer->hwndServer);
1677 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1678 WDML_DecHSZ(pInstance, pServer->hszService);
1680 GlobalDeleteAtom(pServer->atomService);
1681 GlobalDeleteAtom(pServer->atomServiceSpec);
1683 HeapFree(GetProcessHeap(), 0, pServer);
1688 pServer = pServer->next;
1692 /*****************************************************************************
1695 * generic routine to return a pointer to the relevant ServiceNode
1696 * for a given service name, or NULL if the entry does not exist
1699 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1701 WDML_SERVER* pServer;
1703 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1705 if (hszService == pServer->hszService)
1710 TRACE("Service name missing\n");
1714 /* ================================================================
1716 * Conversation management
1718 * ================================================================ */
1720 /******************************************************************
1725 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1726 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1730 /* no converstation yet, add it */
1731 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1732 if (!pConv) return NULL;
1734 pConv->instance = pInstance;
1735 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1736 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1737 pConv->magic = WDML_CONV_MAGIC;
1738 pConv->hwndServer = hwndServer;
1739 pConv->hwndClient = hwndClient;
1740 pConv->transactions = NULL;
1742 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1743 pConv->wStatus |= pInstance->wStatus;
1744 /* check if both side of the conversation are of the same instance */
1745 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1746 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1748 pConv->wStatus |= ST_ISSELF;
1750 pConv->wConvst = XST_NULL;
1752 pConv->next = pInstance->convs[side];
1753 pInstance->convs[side] = pConv;
1755 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1760 /******************************************************************
1765 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1766 HSZ hszService, HSZ hszTopic)
1768 WDML_CONV* pCurrent = NULL;
1770 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1772 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1773 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1782 /******************************************************************
1787 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1789 WDML_CONV* pPrev = NULL;
1790 WDML_CONV* pCurrent;
1792 WDML_XACT* pXActNext;
1798 /* remove any pending transaction */
1799 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1801 pXActNext = pXAct->next;
1802 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1805 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1807 /* FIXME: should we keep the window around ? it seems so (at least on client side
1808 * to let QueryConvInfo work after conv termination, but also to implement
1811 /* destroy conversation window, but first remove pConv from hWnd.
1812 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1814 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1815 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
1817 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1819 WDML_DecHSZ(pRef->instance, pRef->hszService);
1820 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1822 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1824 if (pCurrent == pRef)
1826 if (pCurrent == pRef->instance->convs[side])
1828 pRef->instance->convs[side] = pCurrent->next;
1832 pPrev->next = pCurrent->next;
1834 pCurrent->magic = 0;
1835 HeapFree(GetProcessHeap(), 0, pCurrent);
1841 /******************************************************************
1842 * WDML_EnableCallback
1844 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
1846 if (wCmd == EC_DISABLE)
1848 pConv->wStatus |= ST_BLOCKED;
1849 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
1853 if (wCmd == EC_QUERYWAITING)
1854 return pConv->transactions ? TRUE : FALSE;
1856 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
1858 FIXME("Unknown command code %04x\n", wCmd);
1862 if (wCmd == EC_ENABLEALL)
1864 pConv->wStatus &= ~ST_BLOCKED;
1865 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
1868 while (pConv->transactions)
1870 WDML_XACT *pXAct = pConv->transactions;
1872 if (pConv->wStatus & ST_CLIENT)
1874 /* transaction should be in the queue until handled */
1875 WDML_ClientHandle(pConv, pXAct, 0, NULL);
1876 WDML_UnQueueTransaction(pConv, pXAct);
1880 /* transaction should be removed from the queue before handling */
1881 WDML_UnQueueTransaction(pConv, pXAct);
1882 WDML_ServerHandle(pConv, pXAct);
1885 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1887 if (wCmd == EC_ENABLEONE) break;
1892 /*****************************************************************
1893 * DdeEnableCallback (USER32.@)
1895 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1900 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
1904 pConv = WDML_GetConv(hConv, TRUE);
1906 if (pConv && pConv->instance->instanceID == idInst)
1907 ret = WDML_EnableCallback(pConv, wCmd);
1911 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
1916 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
1917 pInstance->wStatus |= wCmd;
1919 if (wCmd == EC_DISABLE)
1921 pInstance->wStatus |= ST_BLOCKED;
1922 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1924 else if (wCmd == EC_ENABLEALL)
1926 pInstance->wStatus &= ~ST_BLOCKED;
1927 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
1932 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
1934 ret = WDML_EnableCallback(pConv, wCmd);
1935 if (ret && wCmd == EC_QUERYWAITING) break;
1942 /******************************************************************
1947 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1949 WDML_CONV* pConv = (WDML_CONV*)hConv;
1951 /* FIXME: should do better checking */
1952 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
1954 if (!pConv->instance || pConv->instance->threadID != GetCurrentThreadId())
1956 WARN("wrong thread ID\n");
1957 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
1961 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1963 WARN("found conv but ain't connected\n");
1964 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
1971 /******************************************************************
1972 * WDML_GetConvFromWnd
1976 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1978 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
1981 /******************************************************************
1986 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1987 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
1992 if (side == WDML_SERVER_SIDE)
1994 from = pConv->hwndServer;
1995 to = pConv->hwndClient;
1999 to = pConv->hwndServer;
2000 from = pConv->hwndClient;
2003 ddeAck.bAppReturnCode = appRetCode;
2004 ddeAck.reserved = 0;
2005 ddeAck.fBusy = fBusy;
2008 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2010 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2011 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2012 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2014 pConv->wStatus &= ~ST_CONNECTED;
2015 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2016 FreeDDElParam(WM_DDE_ACK, lParam);
2022 /*****************************************************************
2023 * DdeSetUserHandle (USER32.@)
2025 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2029 TRACE("(%p,%x,%x)\n", hConv, id, hUser);
2031 pConv = WDML_GetConv(hConv, FALSE);
2037 pConv->hUser = hUser;
2043 pXAct = WDML_FindTransaction(pConv, id);
2046 pXAct->hUser = hUser;
2050 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2057 /******************************************************************
2058 * WDML_GetLocalConvInfo
2062 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2068 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2069 ci->hszSvcPartner = pConv->hszService;
2070 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2071 ci->hszTopic = pConv->hszTopic;
2072 ci->wStatus = pConv->wStatus;
2074 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2076 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2078 if (pLink->hConv == (HCONV)pConv)
2080 ci->wStatus |= ST_ADVISE;
2085 /* FIXME: non handled status flags:
2091 ci->wConvst = pConv->wConvst; /* FIXME */
2093 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2095 ci->ConvCtxt = pConv->convContext;
2096 if (ci->wStatus & ST_CLIENT)
2098 ci->hwnd = pConv->hwndClient;
2099 ci->hwndPartner = pConv->hwndServer;
2103 ci->hwnd = pConv->hwndServer;
2104 ci->hwndPartner = pConv->hwndClient;
2108 ci->hUser = pConv->hUser;
2117 pXAct = WDML_FindTransaction(pConv, id);
2120 ci->hUser = pXAct->hUser;
2121 ci->hszItem = pXAct->hszItem;
2122 ci->wFmt = pXAct->wFmt;
2123 ci->wType = pXAct->wType;
2128 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2134 /******************************************************************
2135 * DdeQueryConvInfo (USER32.@)
2137 * FIXME: Set last DDE error on failure.
2139 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2141 UINT ret = lpConvInfo->cb;
2145 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2149 FIXME("hConv is NULL\n");
2153 pConv = WDML_GetConv(hConv, FALSE);
2156 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2161 if ((ULONG_PTR)hConv & 1)
2163 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2165 FIXME("Request on remote conversation information is not implemented yet\n");
2171 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2175 /* ================================================================
2177 * Link (hot & warm) management
2179 * ================================================================ */
2181 /******************************************************************
2186 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2187 UINT wType, HSZ hszItem, UINT wFmt)
2191 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2198 pLink->hConv = hConv;
2199 pLink->transactionType = wType;
2200 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2202 pLink->next = pInstance->links[side];
2203 pInstance->links[side] = pLink;
2206 /******************************************************************
2211 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2212 HSZ hszItem, UINT uFmt)
2214 WDML_LINK* pPrev = NULL;
2215 WDML_LINK* pCurrent = NULL;
2217 pCurrent = pInstance->links[side];
2219 while (pCurrent != NULL)
2221 if (pCurrent->hConv == hConv &&
2222 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2223 pCurrent->uFmt == uFmt)
2225 if (pCurrent == pInstance->links[side])
2227 pInstance->links[side] = pCurrent->next;
2231 pPrev->next = pCurrent->next;
2234 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2235 HeapFree(GetProcessHeap(), 0, pCurrent);
2240 pCurrent = pCurrent->next;
2244 /* this function is called to remove all links related to the conv.
2245 It should be called from both client and server when terminating
2248 /******************************************************************
2249 * WDML_RemoveAllLinks
2253 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2255 WDML_LINK* pPrev = NULL;
2256 WDML_LINK* pCurrent = NULL;
2257 WDML_LINK* pNext = NULL;
2259 pCurrent = pInstance->links[side];
2261 while (pCurrent != NULL)
2263 if (pCurrent->hConv == (HCONV)pConv)
2265 if (pCurrent == pInstance->links[side])
2267 pInstance->links[side] = pCurrent->next;
2268 pNext = pCurrent->next;
2272 pPrev->next = pCurrent->next;
2273 pNext = pCurrent->next;
2276 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2278 HeapFree(GetProcessHeap(), 0, pCurrent);
2285 pCurrent = pCurrent->next;
2294 /******************************************************************
2299 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2300 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2302 WDML_LINK* pCurrent = NULL;
2304 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2306 /* we don't need to check for transaction type as it can be altered */
2308 if (pCurrent->hConv == hConv &&
2309 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2310 (!use_fmt || pCurrent->uFmt == uFmt))
2320 /* ================================================================
2322 * Transaction management
2324 * ================================================================ */
2326 /******************************************************************
2327 * WDML_AllocTransaction
2329 * Alloc a transaction structure for handling the message ddeMsg
2331 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2332 UINT wFmt, HSZ hszItem)
2335 static WORD tid = 1; /* FIXME: wrap around */
2337 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2340 pInstance->lastError = DMLERR_MEMORY_ERROR;
2344 pXAct->xActID = tid++;
2345 pXAct->ddeMsg = ddeMsg;
2346 pXAct->hDdeData = 0;
2351 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2359 /******************************************************************
2360 * WDML_QueueTransaction
2362 * Adds a transaction to the list of transaction
2364 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2368 /* advance to last in queue */
2369 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2373 /******************************************************************
2374 * WDML_UnQueueTransaction
2378 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2382 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2393 /******************************************************************
2394 * WDML_FreeTransaction
2398 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2400 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2401 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
2403 GlobalFree(pXAct->hMem);
2405 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2407 HeapFree(GetProcessHeap(), 0, pXAct);
2410 /******************************************************************
2411 * WDML_FindTransaction
2415 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2420 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2422 if (pXAct->xActID == tid)
2428 /* ================================================================
2430 * Information broadcast across DDEML implementations
2432 * ================================================================ */
2434 struct tagWDML_BroadcastPmt
2442 /******************************************************************
2443 * WDML_BroadcastEnumProc
2447 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2449 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2452 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2453 lstrcmpiW(buffer, s->clsName) == 0)
2455 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2460 /******************************************************************
2461 * WDML_BroadcastDDEWindows
2465 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2467 struct tagWDML_BroadcastPmt s;
2469 s.clsName = clsName;
2473 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);