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
28 #include "wine/port.h"
40 #include "wine/debug.h"
41 #include "dde/dde_private.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 DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
51 const char WDML_szEventClass[] = "DdeEventClass";
52 CRITICAL_SECTION WDML_CritSect = CRITICAL_SECTION_INIT("WDML_CritSect");
54 /* ================================================================
56 * Pure DDE (non DDEML) management
58 * ================================================================ */
61 /*****************************************************************
62 * PackDDElParam (USER32.@)
67 LPARAM WINAPI PackDDElParam(UINT msg, UINT uiLo, UINT uiHi)
78 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT) * 2)))
80 ERR("GlobalAlloc failed\n");
83 if (!(params = GlobalLock(hMem)))
85 ERR("GlobalLock failed (%p)\n", hMem);
97 return MAKELONG(uiLo, uiHi);
102 /*****************************************************************
103 * UnpackDDElParam (USER32.@)
109 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
110 PUINT uiLo, PUINT uiHi)
120 if (!lParam) return FALSE;
121 if (!(params = GlobalLock( (HGLOBAL)lParam )))
123 ERR("GlobalLock failed (%lx)\n", lParam);
126 if (uiLo) *uiLo = params[0];
127 if (uiHi) *uiHi = params[1];
128 GlobalUnlock( (HGLOBAL)lParam );
133 if (uiHi) *uiHi = lParam;
137 if (uiLo) *uiLo = LOWORD(lParam);
138 if (uiHi) *uiHi = HIWORD(lParam);
144 /*****************************************************************
145 * FreeDDElParam (USER32.@)
151 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
159 /* first check if it's a global handle */
160 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
161 return !GlobalFree( (HGLOBAL)lParam );
169 /*****************************************************************
170 * ReuseDDElParam (USER32.@)
175 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
176 UINT uiLo, UINT uiHi)
192 if (!lParam) return 0;
193 if (!(params = GlobalLock( (HGLOBAL)lParam )))
195 ERR("GlobalLock failed\n");
200 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
201 GlobalLock( (HGLOBAL)lParam );
205 FreeDDElParam( msgIn, lParam );
209 FreeDDElParam( msgIn, lParam );
210 return MAKELONG(uiLo, uiHi);
214 return PackDDElParam( msgOut, uiLo, uiHi );
218 /*****************************************************************
219 * ImpersonateDdeClientWindow (USER32.@)
222 * hWndClient [I] handle to DDE client window
223 * hWndServer [I] handle to DDE server window
225 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
227 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
231 /*****************************************************************
232 * DdeSetQualityOfService (USER32.@)
235 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
236 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
238 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
242 /* ================================================================
244 * Instance management
246 * ================================================================ */
248 /******************************************************************************
249 * IncrementInstanceId
251 * generic routine to increment the max instance Id and allocate a new application instance
253 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
255 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
257 pInstance->instanceID = id;
258 TRACE("New instance id %ld allocated\n", id);
261 /******************************************************************
266 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
268 WDML_INSTANCE* pInstance;
273 case WM_WDML_REGISTER:
274 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
275 /* try calling the Callback */
276 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
278 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
279 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
280 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
281 WDML_DecHSZ(pInstance, hsz1);
282 WDML_DecHSZ(pInstance, hsz2);
286 case WM_WDML_UNREGISTER:
287 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
288 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
290 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
291 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
292 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
293 WDML_DecHSZ(pInstance, hsz1);
294 WDML_DecHSZ(pInstance, hsz2);
298 case WM_WDML_CONNECT_CONFIRM:
299 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
300 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
303 /* confirm connection...
304 * lookup for this conv handle
306 HWND client = WIN_GetFullHandle( (HWND)wParam );
307 HWND server = WIN_GetFullHandle( (HWND)lParam );
308 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
310 if (pConv->hwndClient == client && pConv->hwndServer == server)
315 pConv->wStatus |= ST_ISLOCAL;
317 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
318 pConv->hszTopic, pConv->hszService, 0, 0,
319 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
324 return DefWindowProcA(hwndEvent, uMsg, wParam, lParam);
329 /******************************************************************
334 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
335 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
337 WDML_INSTANCE* pInstance;
338 WDML_INSTANCE* reference_inst;
340 WNDCLASSEXA wndclass;
342 TRACE("(%p,%p,0x%lx,%ld)\n",
343 pidInst, pfnCallback, afCmd, ulRes);
347 ERR("Reserved value not zero? What does this mean?\n");
348 /* trap this and no more until we know more */
349 return DMLERR_NO_ERROR;
352 /* grab enough heap for one control struct - not really necessary for re-initialise
353 * but allows us to use same validation routines */
354 pInstance = (WDML_INSTANCE*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
355 if (pInstance == NULL)
357 /* catastrophe !! warn user & abort */
358 ERR("Instance create failed - out of memory\n");
359 return DMLERR_SYS_ERROR;
361 pInstance->next = NULL;
362 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
364 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
366 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
367 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
368 pInstance->threadID = GetCurrentThreadId();
369 pInstance->callback = *pfnCallback;
370 pInstance->unicode = bUnicode;
371 pInstance->win16 = b16;
372 pInstance->nodeList = NULL; /* node will be added later */
373 pInstance->monitorFlags = afCmd & MF_MASK;
374 pInstance->servers = NULL;
375 pInstance->convs[0] = NULL;
376 pInstance->convs[1] = NULL;
377 pInstance->links[0] = NULL;
378 pInstance->links[1] = NULL;
380 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
382 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
384 if (!pInstance->clientOnly)
386 /* Check for other way of setting Client-only !! */
387 pInstance->clientOnly =
388 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
391 TRACE("instance created - checking validity \n");
395 /* Initialisation of new Instance Identifier */
396 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
398 EnterCriticalSection(&WDML_CritSect);
400 if (WDML_InstanceList == NULL)
402 /* can't be another instance in this case, assign to the base pointer */
403 WDML_InstanceList = pInstance;
405 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
407 * ------------------------------- NOTE NOTE NOTE --------------------------
409 * the manual is not clear if this condition
410 * applies to the first call to DdeInitialize from an application, or the
411 * first call for a given callback !!!
414 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
415 TRACE("First application instance detected OK\n");
416 /* allocate new instance ID */
417 WDML_IncrementInstanceId(pInstance);
421 /* really need to chain the new one in to the latest here, but after checking conditions
422 * such as trying to start a conversation from an application trying to monitor */
423 reference_inst = WDML_InstanceList;
424 TRACE("Subsequent application instance - starting checks\n");
425 while (reference_inst->next != NULL)
428 * This set of tests will work if application uses same instance Id
429 * at application level once allocated - which is what manual implies
430 * should happen. If someone tries to be
431 * clever (lazy ?) it will fail to pick up that later calls are for
432 * the same application - should we trust them ?
434 if (pInstance->instanceID == reference_inst->instanceID)
436 /* Check 1 - must be same Client-only state */
438 if (pInstance->clientOnly != reference_inst->clientOnly)
440 ret = DMLERR_DLL_USAGE;
444 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
446 if (pInstance->monitor != reference_inst->monitor)
448 ret = DMLERR_INVALIDPARAMETER;
452 /* Check 3 - must supply different callback address */
454 if (pInstance->callback == reference_inst->callback)
456 ret = DMLERR_DLL_USAGE;
460 reference_inst = reference_inst->next;
462 /* All cleared, add to chain */
464 TRACE("Application Instance checks finished\n");
465 WDML_IncrementInstanceId(pInstance);
466 reference_inst->next = pInstance;
468 LeaveCriticalSection(&WDML_CritSect);
470 *pidInst = pInstance->instanceID;
472 /* for deadlock issues, windows must always be created when outside the critical section */
473 wndclass.cbSize = sizeof(wndclass);
475 wndclass.lpfnWndProc = WDML_EventProc;
476 wndclass.cbClsExtra = 0;
477 wndclass.cbWndExtra = sizeof(DWORD);
478 wndclass.hInstance = 0;
480 wndclass.hCursor = 0;
481 wndclass.hbrBackground = 0;
482 wndclass.lpszMenuName = NULL;
483 wndclass.lpszClassName = WDML_szEventClass;
484 wndclass.hIconSm = 0;
486 RegisterClassExA(&wndclass);
488 pInstance->hwndEvent = CreateWindowA(WDML_szEventClass, NULL,
489 WS_POPUP, 0, 0, 0, 0,
492 SetWindowLongA(pInstance->hwndEvent, GWL_WDML_INSTANCE, (DWORD)pInstance);
494 TRACE("New application instance processing finished OK\n");
498 /* Reinitialisation situation --- FIX */
499 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
501 EnterCriticalSection(&WDML_CritSect);
503 if (WDML_InstanceList == NULL)
505 ret = DMLERR_DLL_USAGE;
508 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
509 /* can't reinitialise if we have initialised nothing !! */
510 reference_inst = WDML_InstanceList;
511 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
513 * MS allows initialisation without specifying a callback, should we allow addition of the
514 * callback by a later call to initialise ? - if so this lot will have to change
516 while (reference_inst->next != NULL)
518 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
520 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
522 if (reference_inst->clientOnly)
524 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
526 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
528 if (!(afCmd & APPCMD_CLIENTONLY))
530 ret = DMLERR_DLL_USAGE;
535 /* Check 2 - cannot change monitor modes */
537 if (pInstance->monitor != reference_inst->monitor)
539 ret = DMLERR_DLL_USAGE;
543 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
545 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
547 ret = DMLERR_DLL_USAGE;
552 reference_inst = reference_inst->next;
554 if (reference_inst->next == NULL)
556 /* Crazy situation - trying to re-initialize something that has not beeen initialized !!
558 * Manual does not say what we do, cannot return DMLERR_NOT_INITIALIZED so what ?
560 ret = DMLERR_INVALIDPARAMETER;
563 /* All checked - change relevant flags */
565 reference_inst->CBFflags = pInstance->CBFflags;
566 reference_inst->clientOnly = pInstance->clientOnly;
567 reference_inst->monitorFlags = pInstance->monitorFlags;
568 LeaveCriticalSection(&WDML_CritSect);
571 return DMLERR_NO_ERROR;
573 HeapFree(GetProcessHeap(), 0, pInstance);
574 LeaveCriticalSection(&WDML_CritSect);
578 /******************************************************************************
579 * DdeInitializeA (USER32.@)
581 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
582 DWORD afCmd, DWORD ulRes)
584 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
587 /******************************************************************************
588 * DdeInitializeW [USER32.@]
589 * Registers an application with the DDEML
592 * pidInst [I] Pointer to instance identifier
593 * pfnCallback [I] Pointer to callback function
594 * afCmd [I] Set of command and filter flags
598 * Success: DMLERR_NO_ERROR
599 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
601 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
602 DWORD afCmd, DWORD ulRes)
604 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
607 /*****************************************************************
608 * DdeUninitialize [USER32.@] Frees DDEML resources
611 * idInst [I] Instance identifier
618 BOOL WINAPI DdeUninitialize(DWORD idInst)
620 /* Stage one - check if we have a handle for this instance
622 WDML_INSTANCE* pInstance;
624 WDML_CONV* pConvNext;
626 EnterCriticalSection(&WDML_CritSect);
628 /* First check instance
630 pInstance = WDML_GetInstance(idInst);
631 if (pInstance == NULL)
633 LeaveCriticalSection(&WDML_CritSect);
635 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
640 /* first terminate all conversations client side
641 * this shall close existing links...
643 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
645 pConvNext = pConv->next;
646 DdeDisconnect((HCONV)pConv);
648 if (pInstance->convs[WDML_CLIENT_SIDE])
649 FIXME("still pending conversations\n");
651 /* then unregister all known service names */
652 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
654 /* Free the nodes that were not freed by this instance
655 * and remove the nodes from the list of HSZ nodes.
657 WDML_FreeAllHSZ(pInstance);
659 DestroyWindow(pInstance->hwndEvent);
661 /* OK now delete the instance handle itself */
663 if (WDML_InstanceList == pInstance)
665 /* special case - the first/only entry */
666 WDML_InstanceList = pInstance->next;
670 /* general case, remove entry */
673 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
674 inst->next = pInstance->next;
676 /* leave crit sect and release the heap entry
678 HeapFree(GetProcessHeap(), 0, pInstance);
679 LeaveCriticalSection(&WDML_CritSect);
683 /******************************************************************
684 * WDML_NotifyThreadExit
688 void WDML_NotifyThreadDetach(void)
690 WDML_INSTANCE* pInstance;
692 DWORD tid = GetCurrentThreadId();
694 EnterCriticalSection(&WDML_CritSect);
695 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
697 next = pInstance->next;
698 if (pInstance->threadID == tid)
700 DdeUninitialize(pInstance->instanceID);
703 LeaveCriticalSection(&WDML_CritSect);
706 /******************************************************************
707 * WDML_InvokeCallback
711 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
712 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
713 DWORD dwData1, DWORD dwData2)
717 if (pInstance == NULL)
719 TRACE("invoking CB%d[%p] (%u %u %p %p %p %p %lu %lu)\n",
720 pInstance->win16 ? 16 : 32, pInstance->callback, uType, uFmt,
721 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
722 if (pInstance->win16)
724 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
725 hsz1, hsz2, hdata, dwData1, dwData2);
729 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
731 TRACE("done => %08lx\n", (DWORD)ret);
735 /*****************************************************************************
738 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
739 * for an instance Id, or NULL if the entry does not exist
742 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
744 WDML_INSTANCE* pInstance;
746 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
748 if (pInstance->instanceID == instId)
750 if (GetCurrentThreadId() != pInstance->threadID)
752 FIXME("Tried to get instance from wrong thread\n");
758 TRACE("Instance entry missing\n");
762 /******************************************************************
763 * WDML_GetInstanceFromWnd
767 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
769 return (WDML_INSTANCE*)GetWindowLongA(hWnd, GWL_WDML_INSTANCE);
772 /******************************************************************************
773 * DdeGetLastError [USER32.@] Gets most recent error code
776 * idInst [I] Instance identifier
781 UINT WINAPI DdeGetLastError(DWORD idInst)
784 WDML_INSTANCE* pInstance;
786 FIXME("(%ld): error reporting is weakly implemented\n", idInst);
788 EnterCriticalSection(&WDML_CritSect);
790 /* First check instance
792 pInstance = WDML_GetInstance(idInst);
793 if (pInstance == NULL)
795 error_code = DMLERR_DLL_NOT_INITIALIZED;
799 error_code = pInstance->lastError;
800 pInstance->lastError = 0;
803 LeaveCriticalSection(&WDML_CritSect);
807 /* ================================================================
811 * ================================================================ */
814 /******************************************************************
819 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
823 if (pInstance == NULL) return NULL;
825 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
827 if (pNode->hsz == hsz) break;
829 if (!pNode) WARN("HSZ %p not found\n", hsz);
833 /******************************************************************
834 * WDML_MakeAtomFromHsz
836 * Creates a global atom from an existing HSZ
837 * Generally used before sending an HSZ as an atom to a remote app
839 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
841 WCHAR nameBuffer[MAX_BUFFER_LEN];
843 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
844 return GlobalAddAtomW(nameBuffer);
845 WARN("HSZ %p not found\n", hsz);
849 /******************************************************************
850 * WDML_MakeHszFromAtom
852 * Creates a HSZ from an existing global atom
853 * Generally used while receiving a global atom and transforming it
856 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
858 WCHAR nameBuffer[MAX_BUFFER_LEN];
860 if (!atom) return NULL;
862 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
864 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
865 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
867 WARN("ATOM 0x%x not found\n", atom);
871 /******************************************************************
876 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
880 pNode = WDML_FindNode(pInstance, hsz);
881 if (!pNode) return FALSE;
887 /******************************************************************************
888 * WDML_DecHSZ (INTERNAL)
890 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
892 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
894 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
896 HSZNode* pPrev = NULL;
899 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
901 /* If we found the node we were looking for and its ref count is one,
904 if (pCurrent->hsz == hsz)
906 if (--pCurrent->refCount == 0)
908 if (pCurrent == pInstance->nodeList)
910 pInstance->nodeList = pCurrent->next;
914 pPrev->next = pCurrent->next;
916 HeapFree(GetProcessHeap(), 0, pCurrent);
917 DeleteAtom(HSZ2ATOM(hsz));
922 WARN("HSZ %p not found\n", hsz);
927 /******************************************************************************
928 * WDML_FreeAllHSZ (INTERNAL)
930 * Frees up all the strings still allocated in the list and
931 * remove all the nodes from the list of HSZ nodes.
933 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
935 /* Free any strings created in this instance.
937 while (pInstance->nodeList != NULL)
939 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
943 /******************************************************************************
944 * InsertHSZNode (INTERNAL)
946 * Insert a node to the head of the list.
948 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
952 HSZNode* pNew = NULL;
953 /* Create a new node for this HSZ.
955 pNew = (HSZNode*)HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
959 pNew->next = pInstance->nodeList;
961 pInstance->nodeList = pNew;
965 ERR("Primary HSZ Node allocation failed - out of memory\n");
970 /******************************************************************
975 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
978 WCHAR pString[MAX_BUFFER_LEN];
980 /* If psz is null, we have to return only the length
986 cchMax = MAX_BUFFER_LEN;
992 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
995 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
997 ERR("Unknown code page %d\n", codepage);
1003 /*****************************************************************
1004 * DdeQueryStringA [USER32.@]
1006 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1009 WDML_INSTANCE* pInstance;
1011 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1013 EnterCriticalSection(&WDML_CritSect);
1015 /* First check instance
1017 pInstance = WDML_GetInstance(idInst);
1018 if (pInstance != NULL)
1020 if (iCodePage == 0) iCodePage = CP_WINANSI;
1021 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1023 LeaveCriticalSection(&WDML_CritSect);
1025 TRACE("returning %s\n", debugstr_a(psz));
1029 /*****************************************************************
1030 * DdeQueryStringW [USER32.@]
1033 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1036 WDML_INSTANCE* pInstance;
1038 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1040 EnterCriticalSection(&WDML_CritSect);
1042 /* First check instance
1044 pInstance = WDML_GetInstance(idInst);
1045 if (pInstance != NULL)
1047 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1048 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1050 LeaveCriticalSection(&WDML_CritSect);
1052 TRACE("returning %s\n", debugstr_w(psz));
1056 /******************************************************************
1061 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1068 hsz = ATOM2HSZ(AddAtomA(ptr));
1069 TRACE("added atom %s with HSZ %p, \n", debugstr_a(ptr), hsz);
1072 hsz = ATOM2HSZ(AddAtomW(ptr));
1073 TRACE("added atom %s with HSZ %p, \n", debugstr_w(ptr), hsz);
1076 ERR("Unknown code page %d\n", codepage);
1079 WDML_InsertHSZNode(pInstance, hsz);
1083 /*****************************************************************
1084 * DdeCreateStringHandleA [USER32.@]
1087 * Success: String handle
1090 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1093 WDML_INSTANCE* pInstance;
1095 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1097 EnterCriticalSection(&WDML_CritSect);
1099 pInstance = WDML_GetInstance(idInst);
1102 if (codepage == 0) codepage = CP_WINANSI;
1103 hsz = WDML_CreateString(pInstance, psz, codepage);
1106 LeaveCriticalSection(&WDML_CritSect);
1111 /******************************************************************************
1112 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1115 * idInst [I] Instance identifier
1116 * psz [I] Pointer to string
1117 * codepage [I] Code page identifier
1119 * Success: String handle
1122 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1124 WDML_INSTANCE* pInstance;
1127 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1129 EnterCriticalSection(&WDML_CritSect);
1131 pInstance = WDML_GetInstance(idInst);
1134 if (codepage == 0) codepage = CP_WINUNICODE;
1135 hsz = WDML_CreateString(pInstance, psz, codepage);
1137 LeaveCriticalSection(&WDML_CritSect);
1142 /*****************************************************************
1143 * DdeFreeStringHandle (USER32.@)
1144 * RETURNS: success: nonzero
1147 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1149 WDML_INSTANCE* pInstance;
1152 TRACE("(%ld,%p): \n", idInst, hsz);
1154 EnterCriticalSection(&WDML_CritSect);
1156 /* First check instance
1158 pInstance = WDML_GetInstance(idInst);
1160 ret = WDML_DecHSZ(pInstance, hsz);
1162 LeaveCriticalSection(&WDML_CritSect);
1167 /*****************************************************************
1168 * DdeKeepStringHandle (USER32.@)
1170 * RETURNS: success: nonzero
1173 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1175 WDML_INSTANCE* pInstance;
1178 TRACE("(%ld,%p): \n", idInst, hsz);
1180 EnterCriticalSection(&WDML_CritSect);
1182 /* First check instance
1184 pInstance = WDML_GetInstance(idInst);
1186 ret = WDML_IncHSZ(pInstance, hsz);
1188 LeaveCriticalSection(&WDML_CritSect);
1192 /*****************************************************************
1193 * DdeCmpStringHandles (USER32.@)
1195 * Compares the value of two string handles. This comparison is
1196 * not case sensitive.
1199 * -1 The value of hsz1 is zero or less than hsz2
1200 * 0 The values of hsz 1 and 2 are the same or both zero.
1201 * 1 The value of hsz2 is zero of less than hsz1
1203 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1205 WCHAR psz1[MAX_BUFFER_LEN];
1206 WCHAR psz2[MAX_BUFFER_LEN];
1210 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
1211 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
1213 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1215 /* Make sure we found both strings. */
1216 if (ret1 == 0 && ret2 == 0)
1218 /* If both are not found, return both "zero strings". */
1223 /* If hsz1 is a not found, return hsz1 is "zero string". */
1228 /* If hsz2 is a not found, return hsz2 is "zero string". */
1233 /* Compare the two strings we got (case insensitive). */
1234 ret = lstrcmpiW(psz1, psz2);
1235 /* Since strcmp returns any number smaller than
1236 * 0 when the first string is found to be less than
1237 * the second one we must make sure we are returning
1238 * the proper values.
1253 /* ================================================================
1255 * Data handle management
1257 * ================================================================ */
1259 /*****************************************************************
1260 * DdeCreateDataHandle (USER32.@)
1262 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1263 HSZ hszItem, UINT wFmt, UINT afCmd)
1265 /* For now, we ignore idInst, hszItem.
1266 * The purpose of these arguments still need to be investigated.
1271 DDE_DATAHANDLE_HEAD* pDdh;
1273 TRACE("(%ld,%p,%ld,%ld,0x%lx,%d,%d)\n",
1274 idInst, pSrc, cb, cbOff, (DWORD)hszItem, wFmt, afCmd);
1276 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1279 /* we use the first 4 bytes to store the size */
1280 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + sizeof(DDE_DATAHANDLE_HEAD))))
1282 ERR("GlobalAlloc failed\n");
1286 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1293 pDdh->cfFormat = wFmt;
1294 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1296 pByte = (LPBYTE)(pDdh + 1);
1299 memcpy(pByte, pSrc + cbOff, cb);
1303 return (HDDEDATA)hMem;
1306 /*****************************************************************
1308 * DdeAddData (USER32.@)
1310 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1312 DWORD old_sz, new_sz;
1315 pDst = DdeAccessData(hData, &old_sz);
1316 if (!pDst) return 0;
1318 new_sz = cb + cbOff;
1319 if (new_sz > old_sz)
1321 DdeUnaccessData(hData);
1322 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1323 GMEM_MOVEABLE | GMEM_DDESHARE);
1324 pDst = DdeAccessData(hData, &old_sz);
1327 if (!pDst) return 0;
1329 memcpy(pDst + cbOff, pSrc, cb);
1330 DdeUnaccessData(hData);
1334 /******************************************************************************
1335 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1339 * hData [I] Handle to DDE object
1340 * pDst [I] Pointer to destination buffer
1341 * cbMax [I] Amount of data to copy
1342 * cbOff [I] Offset to beginning of data
1345 * Size of memory object associated with handle
1347 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1349 DWORD dwSize, dwRet;
1352 TRACE("(%08lx,%p,%ld,%ld)\n",(DWORD)hData, pDst, cbMax, cbOff);
1354 pByte = DdeAccessData(hData, &dwSize);
1362 else if (cbOff + cbMax < dwSize)
1366 else if (cbOff < dwSize)
1368 dwRet = dwSize - cbOff;
1374 if (pDst && dwRet != 0)
1376 memcpy(pDst, pByte + cbOff, dwRet);
1378 DdeUnaccessData(hData);
1387 /*****************************************************************
1388 * DdeAccessData (USER32.@)
1390 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1392 HGLOBAL hMem = (HGLOBAL)hData;
1393 DDE_DATAHANDLE_HEAD* pDdh;
1395 TRACE("(%08lx,%p)\n", (DWORD)hData, pcbDataSize);
1397 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1400 ERR("Failed on GlobalLock(%p)\n", hMem);
1404 if (pcbDataSize != NULL)
1406 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1408 TRACE("=> %08lx (%lu)\n", (DWORD)(pDdh + 1), GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD));
1409 return (LPBYTE)(pDdh + 1);
1412 /*****************************************************************
1413 * DdeUnaccessData (USER32.@)
1415 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1417 HGLOBAL hMem = (HGLOBAL)hData;
1419 TRACE("(0x%lx)\n", (DWORD)hData);
1426 /*****************************************************************
1427 * DdeFreeDataHandle (USER32.@)
1429 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1431 return GlobalFree((HGLOBAL)hData) == 0;
1434 /******************************************************************
1439 BOOL WDML_IsAppOwned(HDDEDATA hData)
1441 DDE_DATAHANDLE_HEAD* pDdh;
1444 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1447 ret = pDdh->bAppOwned;
1448 GlobalUnlock((HGLOBAL)hData);
1453 /* ================================================================
1455 * Global <=> Data handle management
1457 * ================================================================ */
1459 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1461 * (bytes) (bits) comment
1462 * 0 16 bit fields for options (release, ackreq, response...)
1463 * 2 16 clipboard format
1464 * 4 ? data to be used
1466 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1474 pDd = GlobalLock(hMem);
1475 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1478 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1479 switch (pDd->cfFormat)
1482 FIXME("Unsupported format (%d) for data... assuming raw information\n",
1487 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1490 if (size >= sizeof(BITMAP))
1492 BITMAP* bmp = (BITMAP*)pDd->Value;
1493 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1494 if (size >= sizeof(BITMAP) + count)
1498 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1499 bmp->bmPlanes, bmp->bmBitsPixel,
1500 pDd->Value + sizeof(BITMAP))))
1502 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1503 0, 0, CF_BITMAP, 0);
1505 else ERR("Can't create bmp\n");
1509 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1511 } else ERR("No bitmap header\n");
1520 /******************************************************************
1521 * WDML_DataHandle2Global
1525 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1526 BOOL fDeferUpd, BOOL fAckReq)
1528 DDE_DATAHANDLE_HEAD* pDdh;
1532 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1533 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1536 WINE_DDEHEAD* wdh = NULL;
1538 switch (pDdh->cfFormat)
1541 FIXME("Unsupported format (%d) for data... passing raw information\n", pDdh->cfFormat);
1545 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1546 if (hMem && (wdh = GlobalLock(hMem)))
1548 memcpy(wdh + 1, pDdh + 1, dwSize);
1552 if (dwSize >= sizeof(HBITMAP))
1556 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1558 if (GetObjectA(hbmp, sizeof(bmp), &bmp))
1560 count = bmp.bmWidthBytes * bmp.bmHeight;
1561 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1562 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1563 if (hMem && (wdh = GlobalLock(hMem)))
1565 memcpy(wdh + 1, &bmp, sizeof(bmp));
1566 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1574 wdh->fResponse = fResponse;
1575 wdh->fRelease = fRelease;
1576 wdh->fDeferUpd = fDeferUpd;
1577 wdh->fAckReq = fAckReq;
1578 wdh->cfFormat = pDdh->cfFormat;
1581 GlobalUnlock((HGLOBAL)hDdeData);
1587 /* ================================================================
1591 * ================================================================ */
1593 /******************************************************************
1598 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1600 WDML_SERVER* pServer;
1604 pServer = (WDML_SERVER*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1605 if (pServer == NULL) return NULL;
1607 WDML_IncHSZ(pInstance, pServer->hszService = hszService);
1609 DdeQueryStringA(pInstance->instanceID, hszService, buf1, sizeof(buf1), CP_WINANSI);
1610 snprintf(buf2, sizeof(buf2), "%s(0x%08lx)", buf1, GetCurrentProcessId());
1611 pServer->hszServiceSpec = DdeCreateStringHandleA(pInstance->instanceID, buf2, CP_WINANSI);
1613 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1614 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1616 pServer->filterOn = TRUE;
1618 pServer->next = pInstance->servers;
1619 pInstance->servers = pServer;
1623 /******************************************************************
1628 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1630 WDML_SERVER* pPrev = NULL;
1631 WDML_SERVER* pServer = NULL;
1633 WDML_CONV* pConvNext;
1635 pServer = pInstance->servers;
1637 while (pServer != NULL)
1639 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1641 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1642 pServer->atomService, pServer->atomServiceSpec);
1643 /* terminate all conversations for given topic */
1644 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1646 pConvNext = pConv->next;
1647 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1649 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1650 /* don't care about return code (whether client window is present or not) */
1651 PostMessageA(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0L);
1654 if (pServer == pInstance->servers)
1656 pInstance->servers = pServer->next;
1660 pPrev->next = pServer->next;
1663 DestroyWindow(pServer->hwndServer);
1664 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1665 WDML_DecHSZ(pInstance, pServer->hszService);
1667 GlobalDeleteAtom(pServer->atomService);
1668 GlobalDeleteAtom(pServer->atomServiceSpec);
1670 HeapFree(GetProcessHeap(), 0, pServer);
1675 pServer = pServer->next;
1679 /*****************************************************************************
1682 * generic routine to return a pointer to the relevant ServiceNode
1683 * for a given service name, or NULL if the entry does not exist
1686 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1688 WDML_SERVER* pServer;
1690 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1692 if (hszService == pServer->hszService)
1697 TRACE("Service name missing\n");
1701 /* ================================================================
1703 * Conversation management
1705 * ================================================================ */
1707 /******************************************************************
1712 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1713 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1717 /* no converstation yet, add it */
1718 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1719 if (!pConv) return NULL;
1721 pConv->instance = pInstance;
1722 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1723 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1724 pConv->hwndServer = hwndServer;
1725 pConv->hwndClient = hwndClient;
1726 pConv->transactions = NULL;
1728 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1729 /* check if both side of the conversation are of the same instance */
1730 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1731 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1733 pConv->wStatus |= ST_ISSELF;
1735 pConv->wConvst = XST_NULL;
1737 pConv->next = pInstance->convs[side];
1738 pInstance->convs[side] = pConv;
1743 /******************************************************************
1748 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1749 HSZ hszService, HSZ hszTopic)
1751 WDML_CONV* pCurrent = NULL;
1753 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1755 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1756 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1765 /******************************************************************
1770 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1772 WDML_CONV* pPrev = NULL;
1773 WDML_CONV* pCurrent;
1775 WDML_XACT* pXActNext;
1781 /* remove any pending transaction */
1782 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1784 pXActNext = pXAct->next;
1785 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1788 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1790 /* FIXME: should we keep the window around ? it seems so (at least on client side
1791 * to let QueryConvInfo work after conv termination, but also to implement
1794 /* destroy conversation window, but first remove pConv from hWnd.
1795 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1797 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1798 SetWindowLongA(hWnd, GWL_WDML_CONVERSATION, 0);
1800 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1802 WDML_DecHSZ(pRef->instance, pRef->hszService);
1803 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1805 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1807 if (pCurrent == pRef)
1809 if (pCurrent == pRef->instance->convs[side])
1811 pRef->instance->convs[side] = pCurrent->next;
1815 pPrev->next = pCurrent->next;
1818 HeapFree(GetProcessHeap(), 0, pCurrent);
1824 /*****************************************************************
1825 * DdeEnableCallback (USER32.@)
1827 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1829 FIXME("(%ld, %p, %d) stub\n", idInst, hConv, wCmd);
1834 /******************************************************************
1839 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1841 WDML_CONV* pConv = (WDML_CONV*)hConv;
1843 /* FIXME: should do better checking */
1845 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1847 FIXME("found conv but ain't connected\n");
1850 if (GetCurrentThreadId() != pConv->instance->threadID)
1852 FIXME("wrong thread ID\n");
1859 /******************************************************************
1860 * WDML_GetConvFromWnd
1864 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1866 return (WDML_CONV*)GetWindowLongA(hWnd, GWL_WDML_CONVERSATION);
1869 /******************************************************************
1874 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1875 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1880 if (side == WDML_SERVER_SIDE)
1882 from = pConv->hwndServer;
1883 to = pConv->hwndClient;
1887 to = pConv->hwndServer;
1888 from = pConv->hwndClient;
1891 ddeAck.bAppReturnCode = appRetCode;
1892 ddeAck.reserved = 0;
1893 ddeAck.fBusy = fBusy;
1896 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1898 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1899 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1900 if (!PostMessageA(to, WM_DDE_ACK, (WPARAM)from, lParam))
1902 pConv->wStatus &= ~ST_CONNECTED;
1903 FreeDDElParam(WM_DDE_ACK, lParam);
1909 /*****************************************************************
1910 * DdeSetUserHandle (USER32.@)
1912 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1917 EnterCriticalSection(&WDML_CritSect);
1919 pConv = WDML_GetConv(hConv, FALSE);
1927 pConv->hUser = hUser;
1933 pXAct = WDML_FindTransaction(pConv, id);
1936 pXAct->hUser = hUser;
1940 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
1945 LeaveCriticalSection(&WDML_CritSect);
1949 /******************************************************************
1950 * WDML_GetLocalConvInfo
1954 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
1960 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((DWORD)pConv | 1) : 0;
1961 ci->hszSvcPartner = pConv->hszService;
1962 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
1963 ci->hszTopic = pConv->hszTopic;
1964 ci->wStatus = pConv->wStatus;
1966 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
1968 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
1970 if (pLink->hConv == (HCONV)pConv)
1972 ci->wStatus |= ST_ADVISE;
1977 /* FIXME: non handled status flags:
1983 ci->wConvst = pConv->wConvst; /* FIXME */
1985 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
1987 ci->ConvCtxt = pConv->convContext;
1988 if (ci->wStatus & ST_CLIENT)
1990 ci->hwnd = pConv->hwndClient;
1991 ci->hwndPartner = pConv->hwndServer;
1995 ci->hwnd = pConv->hwndServer;
1996 ci->hwndPartner = pConv->hwndClient;
2000 ci->hUser = pConv->hUser;
2009 pXAct = WDML_FindTransaction(pConv, id);
2012 ci->hUser = pXAct->hUser;
2013 ci->hszItem = pXAct->hszItem;
2014 ci->wFmt = pXAct->wFmt;
2015 ci->wType = pXAct->wType;
2020 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2026 /******************************************************************
2027 * DdeQueryConvInfo (USER32.@)
2030 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2032 UINT ret = lpConvInfo->cb;
2039 FIXME("hConv is NULL\n");
2043 EnterCriticalSection(&WDML_CritSect);
2045 pConv = WDML_GetConv(hConv, FALSE);
2046 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2050 else if ((DWORD)hConv & 1)
2052 pConv = WDML_GetConv((HCONV)((DWORD)hConv & ~1), FALSE);
2055 FIXME("Request on remote conversation information is not implemented yet\n");
2059 LeaveCriticalSection(&WDML_CritSect);
2061 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2065 /* ================================================================
2067 * Link (hot & warm) management
2069 * ================================================================ */
2071 /******************************************************************
2076 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2077 UINT wType, HSZ hszItem, UINT wFmt)
2081 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2088 pLink->hConv = hConv;
2089 pLink->transactionType = wType;
2090 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2092 pLink->next = pInstance->links[side];
2093 pInstance->links[side] = pLink;
2096 /******************************************************************
2101 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2102 HSZ hszItem, UINT uFmt)
2104 WDML_LINK* pPrev = NULL;
2105 WDML_LINK* pCurrent = NULL;
2107 pCurrent = pInstance->links[side];
2109 while (pCurrent != NULL)
2111 if (pCurrent->hConv == hConv &&
2112 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2113 pCurrent->uFmt == uFmt)
2115 if (pCurrent == pInstance->links[side])
2117 pInstance->links[side] = pCurrent->next;
2121 pPrev->next = pCurrent->next;
2124 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2125 HeapFree(GetProcessHeap(), 0, pCurrent);
2130 pCurrent = pCurrent->next;
2134 /* this function is called to remove all links related to the conv.
2135 It should be called from both client and server when terminating
2138 /******************************************************************
2139 * WDML_RemoveAllLinks
2143 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2145 WDML_LINK* pPrev = NULL;
2146 WDML_LINK* pCurrent = NULL;
2147 WDML_LINK* pNext = NULL;
2149 pCurrent = pInstance->links[side];
2151 while (pCurrent != NULL)
2153 if (pCurrent->hConv == (HCONV)pConv)
2155 if (pCurrent == pInstance->links[side])
2157 pInstance->links[side] = pCurrent->next;
2158 pNext = pCurrent->next;
2162 pPrev->next = pCurrent->next;
2163 pNext = pCurrent->next;
2166 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2168 HeapFree(GetProcessHeap(), 0, pCurrent);
2175 pCurrent = pCurrent->next;
2184 /******************************************************************
2189 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2190 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2192 WDML_LINK* pCurrent = NULL;
2194 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2196 /* we don't need to check for transaction type as it can be altered */
2198 if (pCurrent->hConv == hConv &&
2199 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2200 (!use_fmt || pCurrent->uFmt == uFmt))
2210 /* ================================================================
2212 * Transaction management
2214 * ================================================================ */
2216 /******************************************************************
2217 * WDML_AllocTransaction
2219 * Alloc a transaction structure for handling the message ddeMsg
2221 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2222 UINT wFmt, HSZ hszItem)
2225 static WORD tid = 1; /* FIXME: wrap around */
2227 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2230 pInstance->lastError = DMLERR_MEMORY_ERROR;
2234 pXAct->xActID = tid++;
2235 pXAct->ddeMsg = ddeMsg;
2236 pXAct->hDdeData = 0;
2241 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2249 /******************************************************************
2250 * WDML_QueueTransaction
2252 * Adds a transaction to the list of transaction
2254 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2258 /* advance to last in queue */
2259 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2263 /******************************************************************
2264 * WDML_UnQueueTransaction
2268 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2272 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2283 /******************************************************************
2284 * WDML_FreeTransaction
2288 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2290 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2291 if (doFreePmt && (DWORD)pXAct->hMem > 1)
2293 GlobalFree(pXAct->hMem);
2295 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2297 HeapFree(GetProcessHeap(), 0, pXAct);
2300 /******************************************************************
2301 * WDML_FindTransaction
2305 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2310 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2312 if (pXAct->xActID == tid)
2318 /* ================================================================
2320 * Information broadcast across DDEML implementations
2322 * ================================================================ */
2324 struct tagWDML_BroadcastPmt
2332 /******************************************************************
2333 * WDML_BroadcastEnumProc
2337 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2339 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2342 if (GetClassNameA(hWnd, buffer, sizeof(buffer)) > 0 &&
2343 strcmp(buffer, s->clsName) == 0)
2345 PostMessageA(hWnd, s->uMsg, s->wParam, s->lParam);
2350 /******************************************************************
2351 * WDML_BroadcastDDEWindows
2355 void WDML_BroadcastDDEWindows(const char* clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2357 struct tagWDML_BroadcastPmt s;
2359 s.clsName = clsName;
2363 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);