1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
10 * Copyright 2001 Eric Pouech
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "wine/debug.h"
38 #include "dde/dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static WDML_INSTANCE* WDML_InstanceList = NULL;
43 static DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
44 const char WDML_szEventClass[] = "DdeEventClass";
45 CRITICAL_SECTION WDML_CritSect = CRITICAL_SECTION_INIT("WDML_CritSect");
47 /* ================================================================
49 * Pure DDE (non DDEML) management
51 * ================================================================ */
53 static BOOL DDE_RequirePacking(UINT msg)
65 case WM_DDE_EXECUTE: /* strange, NT 2000 (at least) really uses packing here... */
67 case WM_DDE_REQUEST: /* assuming clipboard formats are 16 bit */
68 case WM_DDE_TERMINATE:
69 case WM_DDE_UNADVISE: /* assuming clipboard formats are 16 bit */
73 TRACE("Unknown message %04x\n", msg);
80 /*****************************************************************
81 * PackDDElParam (USER32.@)
86 LPARAM WINAPI PackDDElParam(UINT msg, UINT uiLo, UINT uiHi)
91 if (!DDE_RequirePacking(msg))
92 return MAKELONG(uiLo, uiHi);
94 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(UINT) * 2)))
96 ERR("GlobalAlloc failed\n");
100 params = GlobalLock(hMem);
103 ERR("GlobalLock failed (%x)\n", hMem);
116 /*****************************************************************
117 * UnpackDDElParam (USER32.@)
123 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
124 PUINT uiLo, PUINT uiHi)
129 if (!DDE_RequirePacking(msg))
131 *uiLo = LOWORD(lParam);
132 *uiHi = HIWORD(lParam);
142 hMem = (HGLOBAL)lParam;
144 params = GlobalLock(hMem);
147 ERR("GlobalLock failed (%x)\n", hMem);
160 /*****************************************************************
161 * FreeDDElParam (USER32.@)
167 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
169 HGLOBAL hMem = (HGLOBAL)lParam;
171 if (!DDE_RequirePacking(msg))
178 return GlobalFree(hMem) == (HGLOBAL)NULL;
182 /*****************************************************************
183 * ReuseDDElParam (USER32.@)
188 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
189 UINT uiLo, UINT uiHi)
195 in = DDE_RequirePacking(msgIn);
196 out = DDE_RequirePacking(msgOut);
200 return PackDDElParam(msgOut, uiLo, uiHi);
210 FreeDDElParam(msgIn, lParam);
211 return MAKELONG(uiLo, uiHi);
214 hMem = (HGLOBAL)lParam;
216 params = GlobalLock(hMem);
219 ERR("GlobalLock failed\n");
226 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
232 /*****************************************************************
233 * ImpersonateDdeClientWindow (USER32.@)
236 * hWndClient [I] handle to DDE client window
237 * hWndServer [I] handle to DDE server window
239 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
241 FIXME("(%04x %04x): stub\n", hWndClient, hWndServer);
245 /*****************************************************************
246 * DdeSetQualityOfService (USER32.@)
249 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
250 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
252 FIXME("(%04x %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
256 /* ================================================================
258 * Instance management
260 * ================================================================ */
262 /******************************************************************************
263 * IncrementInstanceId
265 * generic routine to increment the max instance Id and allocate a new application instance
267 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
269 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
271 pInstance->instanceID = id;
272 TRACE("New instance id %ld allocated\n", id);
275 /******************************************************************
280 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
282 WDML_INSTANCE* pInstance;
287 case WM_WDML_REGISTER:
288 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
289 /* try calling the Callback */
290 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
292 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
293 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
294 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
295 WDML_DecHSZ(pInstance, hsz1);
296 WDML_DecHSZ(pInstance, hsz2);
300 case WM_WDML_UNREGISTER:
301 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
302 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
304 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
305 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
306 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
307 WDML_DecHSZ(pInstance, hsz1);
308 WDML_DecHSZ(pInstance, hsz2);
312 case WM_WDML_CONNECT_CONFIRM:
313 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
314 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
317 /* confirm connection...
318 * lookup for this conv handle
320 HWND client = WIN_GetFullHandle( (HWND)wParam );
321 HWND server = WIN_GetFullHandle( (HWND)lParam );
322 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
324 if (pConv->hwndClient == client && pConv->hwndServer == server)
329 pConv->wStatus |= ST_ISLOCAL;
331 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
332 pConv->hszTopic, pConv->hszService, 0, 0,
333 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
338 return DefWindowProcA(hwndEvent, uMsg, wParam, lParam);
343 /******************************************************************
348 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
349 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
351 WDML_INSTANCE* pInstance;
352 WDML_INSTANCE* reference_inst;
354 WNDCLASSEXA wndclass;
356 TRACE("(%p,%p,0x%lx,%ld)\n",
357 pidInst, pfnCallback, afCmd, ulRes);
361 ERR("Reserved value not zero? What does this mean?\n");
362 /* trap this and no more until we know more */
363 return DMLERR_NO_ERROR;
366 /* grab enough heap for one control struct - not really necessary for re-initialise
367 * but allows us to use same validation routines */
368 pInstance = (WDML_INSTANCE*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
369 if (pInstance == NULL)
371 /* catastrophe !! warn user & abort */
372 ERR("Instance create failed - out of memory\n");
373 return DMLERR_SYS_ERROR;
375 pInstance->next = NULL;
376 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
378 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
380 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
381 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
382 pInstance->threadID = GetCurrentThreadId();
383 pInstance->callback = *pfnCallback;
384 pInstance->unicode = bUnicode;
385 pInstance->win16 = b16;
386 pInstance->nodeList = NULL; /* node will be added later */
387 pInstance->monitorFlags = afCmd & MF_MASK;
388 pInstance->servers = NULL;
389 pInstance->convs[0] = NULL;
390 pInstance->convs[1] = NULL;
391 pInstance->links[0] = NULL;
392 pInstance->links[1] = NULL;
394 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
396 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
398 if (!pInstance->clientOnly)
400 /* Check for other way of setting Client-only !! */
401 pInstance->clientOnly =
402 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
405 TRACE("instance created - checking validity \n");
409 /* Initialisation of new Instance Identifier */
410 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
412 EnterCriticalSection(&WDML_CritSect);
414 if (WDML_InstanceList == NULL)
416 /* can't be another instance in this case, assign to the base pointer */
417 WDML_InstanceList = pInstance;
419 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
421 * ------------------------------- NOTE NOTE NOTE --------------------------
423 * the manual is not clear if this condition
424 * applies to the first call to DdeInitialize from an application, or the
425 * first call for a given callback !!!
428 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
429 TRACE("First application instance detected OK\n");
430 /* allocate new instance ID */
431 WDML_IncrementInstanceId(pInstance);
435 /* really need to chain the new one in to the latest here, but after checking conditions
436 * such as trying to start a conversation from an application trying to monitor */
437 reference_inst = WDML_InstanceList;
438 TRACE("Subsequent application instance - starting checks\n");
439 while (reference_inst->next != NULL)
442 * This set of tests will work if application uses same instance Id
443 * at application level once allocated - which is what manual implies
444 * should happen. If someone tries to be
445 * clever (lazy ?) it will fail to pick up that later calls are for
446 * the same application - should we trust them ?
448 if (pInstance->instanceID == reference_inst->instanceID)
450 /* Check 1 - must be same Client-only state */
452 if (pInstance->clientOnly != reference_inst->clientOnly)
454 ret = DMLERR_DLL_USAGE;
458 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
460 if (pInstance->monitor != reference_inst->monitor)
462 ret = DMLERR_INVALIDPARAMETER;
466 /* Check 3 - must supply different callback address */
468 if (pInstance->callback == reference_inst->callback)
470 ret = DMLERR_DLL_USAGE;
474 reference_inst = reference_inst->next;
476 /* All cleared, add to chain */
478 TRACE("Application Instance checks finished\n");
479 WDML_IncrementInstanceId(pInstance);
480 reference_inst->next = pInstance;
482 LeaveCriticalSection(&WDML_CritSect);
484 *pidInst = pInstance->instanceID;
486 /* for deadlock issues, windows must always be created when outside the critical section */
487 wndclass.cbSize = sizeof(wndclass);
489 wndclass.lpfnWndProc = WDML_EventProc;
490 wndclass.cbClsExtra = 0;
491 wndclass.cbWndExtra = sizeof(DWORD);
492 wndclass.hInstance = 0;
494 wndclass.hCursor = 0;
495 wndclass.hbrBackground = 0;
496 wndclass.lpszMenuName = NULL;
497 wndclass.lpszClassName = WDML_szEventClass;
498 wndclass.hIconSm = 0;
500 RegisterClassExA(&wndclass);
502 pInstance->hwndEvent = CreateWindowA(WDML_szEventClass, NULL,
503 WS_POPUP, 0, 0, 0, 0,
506 SetWindowLongA(pInstance->hwndEvent, GWL_WDML_INSTANCE, (DWORD)pInstance);
508 TRACE("New application instance processing finished OK\n");
512 /* Reinitialisation situation --- FIX */
513 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
515 EnterCriticalSection(&WDML_CritSect);
517 if (WDML_InstanceList == NULL)
519 ret = DMLERR_DLL_USAGE;
522 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
523 /* can't reinitialise if we have initialised nothing !! */
524 reference_inst = WDML_InstanceList;
525 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
527 * MS allows initialisation without specifying a callback, should we allow addition of the
528 * callback by a later call to initialise ? - if so this lot will have to change
530 while (reference_inst->next != NULL)
532 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
534 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
536 if (reference_inst->clientOnly)
538 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
540 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
542 if (!(afCmd & APPCMD_CLIENTONLY))
544 ret = DMLERR_DLL_USAGE;
549 /* Check 2 - cannot change monitor modes */
551 if (pInstance->monitor != reference_inst->monitor)
553 ret = DMLERR_DLL_USAGE;
557 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
559 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
561 ret = DMLERR_DLL_USAGE;
566 reference_inst = reference_inst->next;
568 if (reference_inst->next == NULL)
570 /* Crazy situation - trying to re-initialize something that has not beeen initialized !!
572 * Manual does not say what we do, cannot return DMLERR_NOT_INITIALIZED so what ?
574 ret = DMLERR_INVALIDPARAMETER;
577 /* All checked - change relevant flags */
579 reference_inst->CBFflags = pInstance->CBFflags;
580 reference_inst->clientOnly = pInstance->clientOnly;
581 reference_inst->monitorFlags = pInstance->monitorFlags;
582 LeaveCriticalSection(&WDML_CritSect);
585 return DMLERR_NO_ERROR;
587 HeapFree(GetProcessHeap(), 0, pInstance);
588 LeaveCriticalSection(&WDML_CritSect);
592 /******************************************************************************
593 * DdeInitializeA (USER32.@)
595 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
596 DWORD afCmd, DWORD ulRes)
598 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
601 /******************************************************************************
602 * DdeInitializeW [USER32.@]
603 * Registers an application with the DDEML
606 * pidInst [I] Pointer to instance identifier
607 * pfnCallback [I] Pointer to callback function
608 * afCmd [I] Set of command and filter flags
612 * Success: DMLERR_NO_ERROR
613 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
615 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
616 DWORD afCmd, DWORD ulRes)
618 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
621 /*****************************************************************
622 * DdeUninitialize [USER32.@] Frees DDEML resources
625 * idInst [I] Instance identifier
632 BOOL WINAPI DdeUninitialize(DWORD idInst)
634 /* Stage one - check if we have a handle for this instance
636 WDML_INSTANCE* pInstance;
638 WDML_CONV* pConvNext;
640 EnterCriticalSection(&WDML_CritSect);
642 /* First check instance
644 pInstance = WDML_GetInstance(idInst);
645 if (pInstance == NULL)
647 LeaveCriticalSection(&WDML_CritSect);
649 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
654 /* first terminate all conversations client side
655 * this shall close existing links...
657 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
659 pConvNext = pConv->next;
660 DdeDisconnect((HCONV)pConv);
662 if (pInstance->convs[WDML_CLIENT_SIDE])
663 FIXME("still pending conversations\n");
665 /* then unregister all known service names */
666 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
668 /* Free the nodes that were not freed by this instance
669 * and remove the nodes from the list of HSZ nodes.
671 WDML_FreeAllHSZ(pInstance);
673 DestroyWindow(pInstance->hwndEvent);
675 /* OK now delete the instance handle itself */
677 if (WDML_InstanceList == pInstance)
679 /* special case - the first/only entry */
680 WDML_InstanceList = pInstance->next;
684 /* general case, remove entry */
687 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
688 inst->next = pInstance->next;
690 /* leave crit sect and release the heap entry
692 HeapFree(GetProcessHeap(), 0, pInstance);
693 LeaveCriticalSection(&WDML_CritSect);
697 /******************************************************************
698 * WDML_NotifyThreadExit
702 void WDML_NotifyThreadDetach(void)
704 WDML_INSTANCE* pInstance;
706 DWORD tid = GetCurrentThreadId();
708 EnterCriticalSection(&WDML_CritSect);
709 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
711 next = pInstance->next;
712 if (pInstance->threadID == tid)
714 DdeUninitialize(pInstance->instanceID);
717 LeaveCriticalSection(&WDML_CritSect);
720 /******************************************************************
721 * WDML_InvokeCallback
725 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
726 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
727 DWORD dwData1, DWORD dwData2)
731 if (pInstance == NULL)
733 TRACE("invoking CB%d[%08lx] (%u %u %08lx 0x%x 0x%x %u %lu %lu)\n",
734 pInstance->win16 ? 16 : 32, (DWORD)pInstance->callback, uType, uFmt,
735 (DWORD)hConv, hsz1, hsz2, hdata, dwData1, dwData2);
736 if (pInstance->win16)
738 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
739 hsz1, hsz2, hdata, dwData1, dwData2);
743 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
745 TRACE("done => %08lx\n", (DWORD)ret);
749 /*****************************************************************************
752 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
753 * for an instance Id, or NULL if the entry does not exist
756 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
758 WDML_INSTANCE* pInstance;
760 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
762 if (pInstance->instanceID == instId)
764 if (GetCurrentThreadId() != pInstance->threadID)
766 FIXME("Tried to get instance from wrong thread\n");
772 TRACE("Instance entry missing\n");
776 /******************************************************************
777 * WDML_GetInstanceFromWnd
781 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
783 return (WDML_INSTANCE*)GetWindowLongA(hWnd, GWL_WDML_INSTANCE);
786 /******************************************************************************
787 * DdeGetLastError [USER32.@] Gets most recent error code
790 * idInst [I] Instance identifier
795 UINT WINAPI DdeGetLastError(DWORD idInst)
798 WDML_INSTANCE* pInstance;
800 FIXME("(%ld): error reporting is weakly implemented\n", idInst);
802 EnterCriticalSection(&WDML_CritSect);
804 /* First check instance
806 pInstance = WDML_GetInstance(idInst);
807 if (pInstance == NULL)
809 error_code = DMLERR_DLL_NOT_INITIALIZED;
813 error_code = pInstance->lastError;
814 pInstance->lastError = 0;
817 LeaveCriticalSection(&WDML_CritSect);
821 /* ================================================================
825 * ================================================================ */
828 /******************************************************************
833 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
837 if (pInstance == NULL) return NULL;
839 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
841 if (pNode->hsz == hsz) break;
843 if (!pNode) WARN("HSZ 0x%x not found\n", hsz);
847 /******************************************************************
848 * WDML_MakeAtomFromHsz
850 * Creates a global atom from an existing HSZ
851 * Generally used before sending an HSZ as an atom to a remote app
853 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
855 WCHAR nameBuffer[MAX_BUFFER_LEN];
857 if (GetAtomNameW((ATOM)hsz, nameBuffer, MAX_BUFFER_LEN))
858 return GlobalAddAtomW(nameBuffer);
859 WARN("HSZ 0x%x not found\n", hsz);
863 /******************************************************************
864 * WDML_MakeHszFromAtom
866 * Creates a HSZ from an existing global atom
867 * Generally used while receiving a global atom and transforming it
870 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
872 WCHAR nameBuffer[MAX_BUFFER_LEN];
874 if (!atom) return (HSZ)0;
876 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
878 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
879 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
881 WARN("ATOM 0x%x not found\n", atom);
885 /******************************************************************
890 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
894 pNode = WDML_FindNode(pInstance, hsz);
895 if (!pNode) return FALSE;
901 /******************************************************************************
902 * WDML_DecHSZ (INTERNAL)
904 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
906 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
908 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
910 HSZNode* pPrev = NULL;
913 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
915 /* If we found the node we were looking for and its ref count is one,
918 if (pCurrent->hsz == hsz)
920 if (--pCurrent->refCount == 0)
922 if (pCurrent == pInstance->nodeList)
924 pInstance->nodeList = pCurrent->next;
928 pPrev->next = pCurrent->next;
930 HeapFree(GetProcessHeap(), 0, pCurrent);
931 DeleteAtom((ATOM)hsz);
936 WARN("HSZ 0x%x not found\n", hsz);
941 /******************************************************************************
942 * WDML_FreeAllHSZ (INTERNAL)
944 * Frees up all the strings still allocated in the list and
945 * remove all the nodes from the list of HSZ nodes.
947 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
949 /* Free any strings created in this instance.
951 while (pInstance->nodeList != NULL)
953 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
957 /******************************************************************************
958 * InsertHSZNode (INTERNAL)
960 * Insert a node to the head of the list.
962 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
966 HSZNode* pNew = NULL;
967 /* Create a new node for this HSZ.
969 pNew = (HSZNode*)HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
973 pNew->next = pInstance->nodeList;
975 pInstance->nodeList = pNew;
979 ERR("Primary HSZ Node allocation failed - out of memory\n");
984 /******************************************************************
989 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
992 WCHAR pString[MAX_BUFFER_LEN];
994 /* If psz is null, we have to return only the length
1000 cchMax = MAX_BUFFER_LEN;
1006 ret = GetAtomNameA((ATOM)hsz, ptr, cchMax);
1009 ret = GetAtomNameW((ATOM)hsz, ptr, cchMax);
1011 ERR("Unknown code page %d\n", codepage);
1017 /*****************************************************************
1018 * DdeQueryStringA [USER32.@]
1020 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1023 WDML_INSTANCE* pInstance;
1025 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1027 EnterCriticalSection(&WDML_CritSect);
1029 /* First check instance
1031 pInstance = WDML_GetInstance(idInst);
1032 if (pInstance != NULL)
1034 if (iCodePage == 0) iCodePage = CP_WINANSI;
1035 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1037 LeaveCriticalSection(&WDML_CritSect);
1039 TRACE("returning %s\n", debugstr_a(psz));
1043 /*****************************************************************
1044 * DdeQueryStringW [USER32.@]
1047 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1050 WDML_INSTANCE* pInstance;
1052 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1054 EnterCriticalSection(&WDML_CritSect);
1056 /* First check instance
1058 pInstance = WDML_GetInstance(idInst);
1059 if (pInstance != NULL)
1061 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1062 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1064 LeaveCriticalSection(&WDML_CritSect);
1066 TRACE("returning %s\n", debugstr_w(psz));
1070 /******************************************************************
1075 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1082 hsz = (HSZ)AddAtomA(ptr);
1083 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_a(ptr), hsz);
1086 hsz = (HSZ)AddAtomW(ptr);
1087 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_w(ptr), hsz);
1090 ERR("Unknown code page %d\n", codepage);
1093 WDML_InsertHSZNode(pInstance, hsz);
1097 /*****************************************************************
1098 * DdeCreateStringHandleA [USER32.@]
1101 * Success: String handle
1104 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1107 WDML_INSTANCE* pInstance;
1109 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1111 EnterCriticalSection(&WDML_CritSect);
1113 pInstance = WDML_GetInstance(idInst);
1116 if (codepage == 0) codepage = CP_WINANSI;
1117 hsz = WDML_CreateString(pInstance, psz, codepage);
1120 LeaveCriticalSection(&WDML_CritSect);
1125 /******************************************************************************
1126 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1129 * idInst [I] Instance identifier
1130 * psz [I] Pointer to string
1131 * codepage [I] Code page identifier
1133 * Success: String handle
1136 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1138 WDML_INSTANCE* pInstance;
1141 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1143 EnterCriticalSection(&WDML_CritSect);
1145 pInstance = WDML_GetInstance(idInst);
1148 if (codepage == 0) codepage = CP_WINUNICODE;
1149 hsz = WDML_CreateString(pInstance, psz, codepage);
1151 LeaveCriticalSection(&WDML_CritSect);
1156 /*****************************************************************
1157 * DdeFreeStringHandle (USER32.@)
1158 * RETURNS: success: nonzero
1161 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1163 WDML_INSTANCE* pInstance;
1166 TRACE("(%ld,0x%x): \n", idInst, hsz);
1168 EnterCriticalSection(&WDML_CritSect);
1170 /* First check instance
1172 pInstance = WDML_GetInstance(idInst);
1174 ret = WDML_DecHSZ(pInstance, hsz);
1176 LeaveCriticalSection(&WDML_CritSect);
1181 /*****************************************************************
1182 * DdeKeepStringHandle (USER32.@)
1184 * RETURNS: success: nonzero
1187 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1189 WDML_INSTANCE* pInstance;
1192 TRACE("(%ld,0x%x): \n", idInst, hsz);
1194 EnterCriticalSection(&WDML_CritSect);
1196 /* First check instance
1198 pInstance = WDML_GetInstance(idInst);
1200 ret = WDML_IncHSZ(pInstance, hsz);
1202 LeaveCriticalSection(&WDML_CritSect);
1206 /*****************************************************************
1207 * DdeCmpStringHandles (USER32.@)
1209 * Compares the value of two string handles. This comparison is
1210 * not case sensitive.
1213 * -1 The value of hsz1 is zero or less than hsz2
1214 * 0 The values of hsz 1 and 2 are the same or both zero.
1215 * 1 The value of hsz2 is zero of less than hsz1
1217 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1219 WCHAR psz1[MAX_BUFFER_LEN];
1220 WCHAR psz2[MAX_BUFFER_LEN];
1224 ret1 = GetAtomNameW((ATOM)hsz1, psz1, MAX_BUFFER_LEN);
1225 ret2 = GetAtomNameW((ATOM)hsz2, psz2, MAX_BUFFER_LEN);
1227 TRACE("(%x<%s> %x<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1229 /* Make sure we found both strings. */
1230 if (ret1 == 0 && ret2 == 0)
1232 /* If both are not found, return both "zero strings". */
1237 /* If hsz1 is a not found, return hsz1 is "zero string". */
1242 /* If hsz2 is a not found, return hsz2 is "zero string". */
1247 /* Compare the two strings we got (case insensitive). */
1248 ret = lstrcmpiW(psz1, psz2);
1249 /* Since strcmp returns any number smaller than
1250 * 0 when the first string is found to be less than
1251 * the second one we must make sure we are returning
1252 * the proper values.
1267 /* ================================================================
1269 * Data handle management
1271 * ================================================================ */
1273 /*****************************************************************
1274 * DdeCreateDataHandle (USER32.@)
1276 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1277 HSZ hszItem, UINT wFmt, UINT afCmd)
1279 /* For now, we ignore idInst, hszItem.
1280 * The purpose of these arguments still need to be investigated.
1285 DDE_DATAHANDLE_HEAD* pDdh;
1287 TRACE("(%ld,%p,%ld,%ld,0x%lx,%d,%d)\n",
1288 idInst, pSrc, cb, cbOff, (DWORD)hszItem, wFmt, afCmd);
1290 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1293 /* we use the first 4 bytes to store the size */
1294 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + sizeof(DDE_DATAHANDLE_HEAD))))
1296 ERR("GlobalAlloc failed\n");
1300 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1307 pDdh->cfFormat = wFmt;
1308 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1310 pByte = (LPBYTE)(pDdh + 1);
1313 memcpy(pByte, pSrc + cbOff, cb);
1317 return (HDDEDATA)hMem;
1320 /*****************************************************************
1322 * DdeAddData (USER32.@)
1324 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1326 DWORD old_sz, new_sz;
1329 pDst = DdeAccessData(hData, &old_sz);
1330 if (!pDst) return 0;
1332 new_sz = cb + cbOff;
1333 if (new_sz > old_sz)
1335 DdeUnaccessData(hData);
1336 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1337 GMEM_MOVEABLE | GMEM_DDESHARE);
1338 pDst = DdeAccessData(hData, &old_sz);
1341 if (!pDst) return 0;
1343 memcpy(pDst + cbOff, pSrc, cb);
1344 DdeUnaccessData(hData);
1348 /******************************************************************************
1349 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1353 * hData [I] Handle to DDE object
1354 * pDst [I] Pointer to destination buffer
1355 * cbMax [I] Amount of data to copy
1356 * cbOff [I] Offset to beginning of data
1359 * Size of memory object associated with handle
1361 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1363 DWORD dwSize, dwRet;
1366 TRACE("(%08lx,%p,%ld,%ld)\n",(DWORD)hData, pDst, cbMax, cbOff);
1368 pByte = DdeAccessData(hData, &dwSize);
1376 else if (cbOff + cbMax < dwSize)
1380 else if (cbOff < dwSize)
1382 dwRet = dwSize - cbOff;
1388 if (pDst && dwRet != 0)
1390 memcpy(pDst, pByte + cbOff, dwRet);
1392 DdeUnaccessData(hData);
1401 /*****************************************************************
1402 * DdeAccessData (USER32.@)
1404 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1406 HGLOBAL hMem = (HGLOBAL)hData;
1407 DDE_DATAHANDLE_HEAD* pDdh;
1409 TRACE("(%08lx,%p)\n", (DWORD)hData, pcbDataSize);
1411 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1414 ERR("Failed on GlobalLock(%04x)\n", hMem);
1418 if (pcbDataSize != NULL)
1420 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1422 TRACE("=> %08lx (%lu)\n", (DWORD)(pDdh + 1), GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD));
1423 return (LPBYTE)(pDdh + 1);
1426 /*****************************************************************
1427 * DdeUnaccessData (USER32.@)
1429 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1431 HGLOBAL hMem = (HGLOBAL)hData;
1433 TRACE("(0x%lx)\n", (DWORD)hData);
1440 /*****************************************************************
1441 * DdeFreeDataHandle (USER32.@)
1443 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1445 return GlobalFree((HGLOBAL)hData) == 0;
1448 /******************************************************************
1453 BOOL WDML_IsAppOwned(HDDEDATA hData)
1455 DDE_DATAHANDLE_HEAD* pDdh;
1458 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1461 ret = pDdh->bAppOwned;
1462 GlobalUnlock((HGLOBAL)hData);
1467 /* ================================================================
1469 * Global <=> Data handle management
1471 * ================================================================ */
1473 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1475 * (bytes) (bits) comment
1476 * 0 16 bit fields for options (release, ackreq, response...)
1477 * 2 16 clipboard format
1478 * 4 ? data to be used
1480 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1488 pDd = GlobalLock(hMem);
1489 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1492 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1493 switch (pDd->cfFormat)
1496 FIXME("Unsupported format (%d) for data... assuming raw information\n",
1501 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1504 if (size >= sizeof(BITMAP))
1506 BITMAP* bmp = (BITMAP*)pDd->Value;
1507 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1508 if (size >= sizeof(BITMAP) + count)
1512 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1513 bmp->bmPlanes, bmp->bmBitsPixel,
1514 pDd->Value + sizeof(BITMAP))))
1516 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1517 0, 0, CF_BITMAP, 0);
1519 else ERR("Can't create bmp\n");
1523 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1525 } else ERR("No bitmap header\n");
1534 /******************************************************************
1535 * WDML_DataHandle2Global
1539 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1540 BOOL fDeferUpd, BOOL fAckReq)
1542 DDE_DATAHANDLE_HEAD* pDdh;
1546 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1547 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1550 WINE_DDEHEAD* wdh = NULL;
1552 switch (pDdh->cfFormat)
1555 FIXME("Unsupported format (%d) for data... passing raw information\n", pDdh->cfFormat);
1559 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1560 if (hMem && (wdh = GlobalLock(hMem)))
1562 memcpy(wdh + 1, pDdh + 1, dwSize);
1566 if (dwSize >= sizeof(HBITMAP))
1570 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1572 if (GetObjectA(hbmp, sizeof(bmp), &bmp))
1574 count = bmp.bmWidthBytes * bmp.bmHeight;
1575 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1576 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1577 if (hMem && (wdh = GlobalLock(hMem)))
1579 memcpy(wdh + 1, &bmp, sizeof(bmp));
1580 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1588 wdh->fResponse = fResponse;
1589 wdh->fRelease = fRelease;
1590 wdh->fDeferUpd = fDeferUpd;
1591 wdh->fAckReq = fAckReq;
1592 wdh->cfFormat = pDdh->cfFormat;
1595 GlobalUnlock((HGLOBAL)hDdeData);
1601 /* ================================================================
1605 * ================================================================ */
1607 /******************************************************************
1612 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1614 WDML_SERVER* pServer;
1618 pServer = (WDML_SERVER*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1619 if (pServer == NULL) return NULL;
1621 WDML_IncHSZ(pInstance, pServer->hszService = hszService);
1623 DdeQueryStringA(pInstance->instanceID, hszService, buf1, sizeof(buf1), CP_WINANSI);
1624 snprintf(buf2, sizeof(buf2), "%s(0x%08lx)", buf1, GetCurrentProcessId());
1625 pServer->hszServiceSpec = DdeCreateStringHandleA(pInstance->instanceID, buf2, CP_WINANSI);
1627 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1628 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1630 pServer->filterOn = TRUE;
1632 pServer->next = pInstance->servers;
1633 pInstance->servers = pServer;
1637 /******************************************************************
1642 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1644 WDML_SERVER* pPrev = NULL;
1645 WDML_SERVER* pServer = NULL;
1647 WDML_CONV* pConvNext;
1649 pServer = pInstance->servers;
1651 while (pServer != NULL)
1653 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1655 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1656 pServer->atomService, pServer->atomServiceSpec);
1657 /* terminate all conversations for given topic */
1658 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1660 pConvNext = pConv->next;
1661 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1663 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1664 /* don't care about return code (whether client window is present or not) */
1665 PostMessageA(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0L);
1668 if (pServer == pInstance->servers)
1670 pInstance->servers = pServer->next;
1674 pPrev->next = pServer->next;
1677 DestroyWindow(pServer->hwndServer);
1678 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1679 WDML_DecHSZ(pInstance, pServer->hszService);
1681 GlobalDeleteAtom(pServer->atomService);
1682 GlobalDeleteAtom(pServer->atomServiceSpec);
1684 HeapFree(GetProcessHeap(), 0, pServer);
1689 pServer = pServer->next;
1693 /*****************************************************************************
1696 * generic routine to return a pointer to the relevant ServiceNode
1697 * for a given service name, or NULL if the entry does not exist
1700 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1702 WDML_SERVER* pServer;
1704 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1706 if (hszService == pServer->hszService)
1711 TRACE("Service name missing\n");
1715 /* ================================================================
1717 * Conversation management
1719 * ================================================================ */
1721 /******************************************************************
1726 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1727 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1731 /* no converstation yet, add it */
1732 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1733 if (!pConv) return NULL;
1735 pConv->instance = pInstance;
1736 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1737 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1738 pConv->hwndServer = hwndServer;
1739 pConv->hwndClient = hwndClient;
1740 pConv->transactions = NULL;
1742 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1743 /* check if both side of the conversation are of the same instance */
1744 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1745 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1747 pConv->wStatus |= ST_ISSELF;
1749 pConv->wConvst = XST_NULL;
1751 pConv->next = pInstance->convs[side];
1752 pInstance->convs[side] = pConv;
1757 /******************************************************************
1762 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1763 HSZ hszService, HSZ hszTopic)
1765 WDML_CONV* pCurrent = NULL;
1767 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1769 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1770 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1779 /******************************************************************
1784 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1786 WDML_CONV* pPrev = NULL;
1787 WDML_CONV* pCurrent;
1789 WDML_XACT* pXActNext;
1795 /* remove any pending transaction */
1796 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1798 pXActNext = pXAct->next;
1799 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1802 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1804 /* FIXME: should we keep the window around ? it seems so (at least on client side
1805 * to let QueryConvInfo work after conv termination, but also to implement
1808 /* destroy conversation window, but first remove pConv from hWnd.
1809 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1811 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1812 SetWindowLongA(hWnd, GWL_WDML_CONVERSATION, 0);
1814 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1816 WDML_DecHSZ(pRef->instance, pRef->hszService);
1817 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1819 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1821 if (pCurrent == pRef)
1823 if (pCurrent == pRef->instance->convs[side])
1825 pRef->instance->convs[side] = pCurrent->next;
1829 pPrev->next = pCurrent->next;
1832 HeapFree(GetProcessHeap(), 0, pCurrent);
1838 /*****************************************************************
1839 * DdeEnableCallback (USER32.@)
1841 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1843 FIXME("(%ld, 0x%x, %d) stub\n", idInst, hConv, wCmd);
1848 /******************************************************************
1853 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1855 WDML_CONV* pConv = (WDML_CONV*)hConv;
1857 /* FIXME: should do better checking */
1859 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1861 FIXME("found conv but ain't connected\n");
1864 if (GetCurrentThreadId() != pConv->instance->threadID)
1866 FIXME("wrong thread ID\n");
1873 /******************************************************************
1874 * WDML_GetConvFromWnd
1878 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1880 return (WDML_CONV*)GetWindowLongA(hWnd, GWL_WDML_CONVERSATION);
1883 /******************************************************************
1888 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1889 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1894 if (side == WDML_SERVER_SIDE)
1896 from = pConv->hwndServer;
1897 to = pConv->hwndClient;
1901 to = pConv->hwndServer;
1902 from = pConv->hwndClient;
1905 ddeAck.bAppReturnCode = appRetCode;
1906 ddeAck.reserved = 0;
1907 ddeAck.fBusy = fBusy;
1910 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1912 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1913 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1914 if (!PostMessageA(to, WM_DDE_ACK, (WPARAM)from, lParam))
1916 pConv->wStatus &= ~ST_CONNECTED;
1917 FreeDDElParam(WM_DDE_ACK, lParam);
1923 /*****************************************************************
1924 * DdeSetUserHandle (USER32.@)
1926 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1931 EnterCriticalSection(&WDML_CritSect);
1933 pConv = WDML_GetConv(hConv, FALSE);
1941 pConv->hUser = hUser;
1947 pXAct = WDML_FindTransaction(pConv, id);
1950 pXAct->hUser = hUser;
1954 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
1959 LeaveCriticalSection(&WDML_CritSect);
1963 /******************************************************************
1964 * WDML_GetLocalConvInfo
1968 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
1974 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((DWORD)pConv | 1) : 0;
1975 ci->hszSvcPartner = pConv->hszService;
1976 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
1977 ci->hszTopic = pConv->hszTopic;
1978 ci->wStatus = pConv->wStatus;
1980 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
1982 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
1984 if (pLink->hConv == (HCONV)pConv)
1986 ci->wStatus |= ST_ADVISE;
1991 /* FIXME: non handled status flags:
1997 ci->wConvst = pConv->wConvst; /* FIXME */
1999 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2001 ci->ConvCtxt = pConv->convContext;
2002 if (ci->wStatus & ST_CLIENT)
2004 ci->hwnd = pConv->hwndClient;
2005 ci->hwndPartner = pConv->hwndServer;
2009 ci->hwnd = pConv->hwndServer;
2010 ci->hwndPartner = pConv->hwndClient;
2014 ci->hUser = pConv->hUser;
2023 pXAct = WDML_FindTransaction(pConv, id);
2026 ci->hUser = pXAct->hUser;
2027 ci->hszItem = pXAct->hszItem;
2028 ci->wFmt = pXAct->wFmt;
2029 ci->wType = pXAct->wType;
2034 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2040 /******************************************************************
2041 * DdeQueryConvInfo (USER32.@)
2044 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, LPCONVINFO lpConvInfo)
2046 UINT ret = lpConvInfo->cb;
2050 EnterCriticalSection(&WDML_CritSect);
2052 pConv = WDML_GetConv(hConv, FALSE);
2053 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2057 else if ((DWORD)hConv & 1)
2059 pConv = WDML_GetConv((DWORD)hConv & ~1, FALSE);
2062 FIXME("Request on remote conversation information is not implemented yet\n");
2066 LeaveCriticalSection(&WDML_CritSect);
2068 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2072 /* ================================================================
2074 * Link (hot & warm) management
2076 * ================================================================ */
2078 /******************************************************************
2083 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2084 UINT wType, HSZ hszItem, UINT wFmt)
2088 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2095 pLink->hConv = hConv;
2096 pLink->transactionType = wType;
2097 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2099 pLink->next = pInstance->links[side];
2100 pInstance->links[side] = pLink;
2103 /******************************************************************
2108 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2109 HSZ hszItem, UINT uFmt)
2111 WDML_LINK* pPrev = NULL;
2112 WDML_LINK* pCurrent = NULL;
2114 pCurrent = pInstance->links[side];
2116 while (pCurrent != NULL)
2118 if (pCurrent->hConv == hConv &&
2119 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2120 pCurrent->uFmt == uFmt)
2122 if (pCurrent == pInstance->links[side])
2124 pInstance->links[side] = pCurrent->next;
2128 pPrev->next = pCurrent->next;
2131 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2132 HeapFree(GetProcessHeap(), 0, pCurrent);
2137 pCurrent = pCurrent->next;
2141 /* this function is called to remove all links related to the conv.
2142 It should be called from both client and server when terminating
2145 /******************************************************************
2146 * WDML_RemoveAllLinks
2150 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2152 WDML_LINK* pPrev = NULL;
2153 WDML_LINK* pCurrent = NULL;
2154 WDML_LINK* pNext = NULL;
2156 pCurrent = pInstance->links[side];
2158 while (pCurrent != NULL)
2160 if (pCurrent->hConv == (HCONV)pConv)
2162 if (pCurrent == pInstance->links[side])
2164 pInstance->links[side] = pCurrent->next;
2165 pNext = pCurrent->next;
2169 pPrev->next = pCurrent->next;
2170 pNext = pCurrent->next;
2173 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2175 HeapFree(GetProcessHeap(), 0, pCurrent);
2182 pCurrent = pCurrent->next;
2191 /******************************************************************
2196 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2197 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2199 WDML_LINK* pCurrent = NULL;
2201 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2203 /* we don't need to check for transaction type as it can be altered */
2205 if (pCurrent->hConv == hConv &&
2206 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2207 (!use_fmt || pCurrent->uFmt == uFmt))
2217 /* ================================================================
2219 * Transaction management
2221 * ================================================================ */
2223 /******************************************************************
2224 * WDML_AllocTransaction
2226 * Alloc a transaction structure for handling the message ddeMsg
2228 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2229 UINT wFmt, HSZ hszItem)
2232 static WORD tid = 1; /* FIXME: wrap around */
2234 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2237 pInstance->lastError = DMLERR_MEMORY_ERROR;
2241 pXAct->xActID = tid++;
2242 pXAct->ddeMsg = ddeMsg;
2243 pXAct->hDdeData = 0;
2248 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2256 /******************************************************************
2257 * WDML_QueueTransaction
2259 * Adds a transaction to the list of transaction
2261 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2265 /* advance to last in queue */
2266 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2270 /******************************************************************
2271 * WDML_UnQueueTransaction
2275 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2279 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2290 /******************************************************************
2291 * WDML_FreeTransaction
2295 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2297 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2298 if (doFreePmt && (DWORD)pXAct->hMem > 1)
2300 GlobalFree(pXAct->hMem);
2302 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2304 HeapFree(GetProcessHeap(), 0, pXAct);
2307 /******************************************************************
2308 * WDML_FindTransaction
2312 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2317 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2319 if (pXAct->xActID == tid)
2325 /* ================================================================
2327 * Information broadcast across DDEML implementations
2329 * ================================================================ */
2331 struct tagWDML_BroadcastPmt
2339 /******************************************************************
2340 * WDML_BroadcastEnumProc
2344 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2346 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2349 if (GetClassNameA(hWnd, buffer, sizeof(buffer)) > 0 &&
2350 strcmp(buffer, s->clsName) == 0)
2352 PostMessageA(hWnd, s->uMsg, s->wParam, s->lParam);
2357 /******************************************************************
2358 * WDML_BroadcastDDEWindows
2362 void WDML_BroadcastDDEWindows(const char* clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2364 struct tagWDML_BroadcastPmt s;
2366 s.clsName = clsName;
2370 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);