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 static WDML_INSTANCE* WDML_InstanceList = NULL;
46 static DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
47 const char WDML_szEventClass[] = "DdeEventClass";
48 CRITICAL_SECTION WDML_CritSect = CRITICAL_SECTION_INIT("WDML_CritSect");
50 /* ================================================================
52 * Pure DDE (non DDEML) management
54 * ================================================================ */
57 /*****************************************************************
58 * PackDDElParam (USER32.@)
63 LPARAM WINAPI PackDDElParam(UINT msg, UINT uiLo, UINT uiHi)
74 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT) * 2)))
76 ERR("GlobalAlloc failed\n");
79 if (!(params = GlobalLock(hMem)))
81 ERR("GlobalLock failed (%x)\n", hMem);
93 return MAKELONG(uiLo, uiHi);
98 /*****************************************************************
99 * UnpackDDElParam (USER32.@)
105 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
106 PUINT uiLo, PUINT uiHi)
116 if (!lParam) return FALSE;
117 if (!(params = GlobalLock( (HGLOBAL)lParam )))
119 ERR("GlobalLock failed (%lx)\n", lParam);
122 if (uiLo) *uiLo = params[0];
123 if (uiHi) *uiHi = params[1];
124 GlobalUnlock( (HGLOBAL)lParam );
129 if (uiHi) *uiHi = lParam;
133 if (uiLo) *uiLo = LOWORD(lParam);
134 if (uiHi) *uiHi = HIWORD(lParam);
140 /*****************************************************************
141 * FreeDDElParam (USER32.@)
147 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
155 /* first check if it's a global handle */
156 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
157 return !GlobalFree( (HGLOBAL)lParam );
165 /*****************************************************************
166 * ReuseDDElParam (USER32.@)
171 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
172 UINT uiLo, UINT uiHi)
188 if (!lParam) return 0;
189 if (!(params = GlobalLock( (HGLOBAL)lParam )))
191 ERR("GlobalLock failed\n");
196 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
197 GlobalLock( (HGLOBAL)lParam );
201 FreeDDElParam( msgIn, lParam );
205 FreeDDElParam( msgIn, lParam );
206 return MAKELONG(uiLo, uiHi);
210 return PackDDElParam( msgOut, uiLo, uiHi );
214 /*****************************************************************
215 * ImpersonateDdeClientWindow (USER32.@)
218 * hWndClient [I] handle to DDE client window
219 * hWndServer [I] handle to DDE server window
221 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
223 FIXME("(%04x %04x): stub\n", hWndClient, hWndServer);
227 /*****************************************************************
228 * DdeSetQualityOfService (USER32.@)
231 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
232 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
234 FIXME("(%04x %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
238 /* ================================================================
240 * Instance management
242 * ================================================================ */
244 /******************************************************************************
245 * IncrementInstanceId
247 * generic routine to increment the max instance Id and allocate a new application instance
249 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
251 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
253 pInstance->instanceID = id;
254 TRACE("New instance id %ld allocated\n", id);
257 /******************************************************************
262 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
264 WDML_INSTANCE* pInstance;
269 case WM_WDML_REGISTER:
270 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
271 /* try calling the Callback */
272 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
274 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
275 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
276 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
277 WDML_DecHSZ(pInstance, hsz1);
278 WDML_DecHSZ(pInstance, hsz2);
282 case WM_WDML_UNREGISTER:
283 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
284 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
286 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
287 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
288 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
289 WDML_DecHSZ(pInstance, hsz1);
290 WDML_DecHSZ(pInstance, hsz2);
294 case WM_WDML_CONNECT_CONFIRM:
295 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
296 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
299 /* confirm connection...
300 * lookup for this conv handle
302 HWND client = WIN_GetFullHandle( (HWND)wParam );
303 HWND server = WIN_GetFullHandle( (HWND)lParam );
304 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
306 if (pConv->hwndClient == client && pConv->hwndServer == server)
311 pConv->wStatus |= ST_ISLOCAL;
313 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
314 pConv->hszTopic, pConv->hszService, 0, 0,
315 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
320 return DefWindowProcA(hwndEvent, uMsg, wParam, lParam);
325 /******************************************************************
330 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
331 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
333 WDML_INSTANCE* pInstance;
334 WDML_INSTANCE* reference_inst;
336 WNDCLASSEXA wndclass;
338 TRACE("(%p,%p,0x%lx,%ld)\n",
339 pidInst, pfnCallback, afCmd, ulRes);
343 ERR("Reserved value not zero? What does this mean?\n");
344 /* trap this and no more until we know more */
345 return DMLERR_NO_ERROR;
348 /* grab enough heap for one control struct - not really necessary for re-initialise
349 * but allows us to use same validation routines */
350 pInstance = (WDML_INSTANCE*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
351 if (pInstance == NULL)
353 /* catastrophe !! warn user & abort */
354 ERR("Instance create failed - out of memory\n");
355 return DMLERR_SYS_ERROR;
357 pInstance->next = NULL;
358 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
360 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
362 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
363 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
364 pInstance->threadID = GetCurrentThreadId();
365 pInstance->callback = *pfnCallback;
366 pInstance->unicode = bUnicode;
367 pInstance->win16 = b16;
368 pInstance->nodeList = NULL; /* node will be added later */
369 pInstance->monitorFlags = afCmd & MF_MASK;
370 pInstance->servers = NULL;
371 pInstance->convs[0] = NULL;
372 pInstance->convs[1] = NULL;
373 pInstance->links[0] = NULL;
374 pInstance->links[1] = NULL;
376 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
378 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
380 if (!pInstance->clientOnly)
382 /* Check for other way of setting Client-only !! */
383 pInstance->clientOnly =
384 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
387 TRACE("instance created - checking validity \n");
391 /* Initialisation of new Instance Identifier */
392 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
394 EnterCriticalSection(&WDML_CritSect);
396 if (WDML_InstanceList == NULL)
398 /* can't be another instance in this case, assign to the base pointer */
399 WDML_InstanceList = pInstance;
401 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
403 * ------------------------------- NOTE NOTE NOTE --------------------------
405 * the manual is not clear if this condition
406 * applies to the first call to DdeInitialize from an application, or the
407 * first call for a given callback !!!
410 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
411 TRACE("First application instance detected OK\n");
412 /* allocate new instance ID */
413 WDML_IncrementInstanceId(pInstance);
417 /* really need to chain the new one in to the latest here, but after checking conditions
418 * such as trying to start a conversation from an application trying to monitor */
419 reference_inst = WDML_InstanceList;
420 TRACE("Subsequent application instance - starting checks\n");
421 while (reference_inst->next != NULL)
424 * This set of tests will work if application uses same instance Id
425 * at application level once allocated - which is what manual implies
426 * should happen. If someone tries to be
427 * clever (lazy ?) it will fail to pick up that later calls are for
428 * the same application - should we trust them ?
430 if (pInstance->instanceID == reference_inst->instanceID)
432 /* Check 1 - must be same Client-only state */
434 if (pInstance->clientOnly != reference_inst->clientOnly)
436 ret = DMLERR_DLL_USAGE;
440 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
442 if (pInstance->monitor != reference_inst->monitor)
444 ret = DMLERR_INVALIDPARAMETER;
448 /* Check 3 - must supply different callback address */
450 if (pInstance->callback == reference_inst->callback)
452 ret = DMLERR_DLL_USAGE;
456 reference_inst = reference_inst->next;
458 /* All cleared, add to chain */
460 TRACE("Application Instance checks finished\n");
461 WDML_IncrementInstanceId(pInstance);
462 reference_inst->next = pInstance;
464 LeaveCriticalSection(&WDML_CritSect);
466 *pidInst = pInstance->instanceID;
468 /* for deadlock issues, windows must always be created when outside the critical section */
469 wndclass.cbSize = sizeof(wndclass);
471 wndclass.lpfnWndProc = WDML_EventProc;
472 wndclass.cbClsExtra = 0;
473 wndclass.cbWndExtra = sizeof(DWORD);
474 wndclass.hInstance = 0;
476 wndclass.hCursor = 0;
477 wndclass.hbrBackground = 0;
478 wndclass.lpszMenuName = NULL;
479 wndclass.lpszClassName = WDML_szEventClass;
480 wndclass.hIconSm = 0;
482 RegisterClassExA(&wndclass);
484 pInstance->hwndEvent = CreateWindowA(WDML_szEventClass, NULL,
485 WS_POPUP, 0, 0, 0, 0,
488 SetWindowLongA(pInstance->hwndEvent, GWL_WDML_INSTANCE, (DWORD)pInstance);
490 TRACE("New application instance processing finished OK\n");
494 /* Reinitialisation situation --- FIX */
495 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
497 EnterCriticalSection(&WDML_CritSect);
499 if (WDML_InstanceList == NULL)
501 ret = DMLERR_DLL_USAGE;
504 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
505 /* can't reinitialise if we have initialised nothing !! */
506 reference_inst = WDML_InstanceList;
507 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
509 * MS allows initialisation without specifying a callback, should we allow addition of the
510 * callback by a later call to initialise ? - if so this lot will have to change
512 while (reference_inst->next != NULL)
514 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
516 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
518 if (reference_inst->clientOnly)
520 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
522 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
524 if (!(afCmd & APPCMD_CLIENTONLY))
526 ret = DMLERR_DLL_USAGE;
531 /* Check 2 - cannot change monitor modes */
533 if (pInstance->monitor != reference_inst->monitor)
535 ret = DMLERR_DLL_USAGE;
539 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
541 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
543 ret = DMLERR_DLL_USAGE;
548 reference_inst = reference_inst->next;
550 if (reference_inst->next == NULL)
552 /* Crazy situation - trying to re-initialize something that has not beeen initialized !!
554 * Manual does not say what we do, cannot return DMLERR_NOT_INITIALIZED so what ?
556 ret = DMLERR_INVALIDPARAMETER;
559 /* All checked - change relevant flags */
561 reference_inst->CBFflags = pInstance->CBFflags;
562 reference_inst->clientOnly = pInstance->clientOnly;
563 reference_inst->monitorFlags = pInstance->monitorFlags;
564 LeaveCriticalSection(&WDML_CritSect);
567 return DMLERR_NO_ERROR;
569 HeapFree(GetProcessHeap(), 0, pInstance);
570 LeaveCriticalSection(&WDML_CritSect);
574 /******************************************************************************
575 * DdeInitializeA (USER32.@)
577 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
578 DWORD afCmd, DWORD ulRes)
580 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
583 /******************************************************************************
584 * DdeInitializeW [USER32.@]
585 * Registers an application with the DDEML
588 * pidInst [I] Pointer to instance identifier
589 * pfnCallback [I] Pointer to callback function
590 * afCmd [I] Set of command and filter flags
594 * Success: DMLERR_NO_ERROR
595 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
597 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
598 DWORD afCmd, DWORD ulRes)
600 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
603 /*****************************************************************
604 * DdeUninitialize [USER32.@] Frees DDEML resources
607 * idInst [I] Instance identifier
614 BOOL WINAPI DdeUninitialize(DWORD idInst)
616 /* Stage one - check if we have a handle for this instance
618 WDML_INSTANCE* pInstance;
620 WDML_CONV* pConvNext;
622 EnterCriticalSection(&WDML_CritSect);
624 /* First check instance
626 pInstance = WDML_GetInstance(idInst);
627 if (pInstance == NULL)
629 LeaveCriticalSection(&WDML_CritSect);
631 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
636 /* first terminate all conversations client side
637 * this shall close existing links...
639 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
641 pConvNext = pConv->next;
642 DdeDisconnect((HCONV)pConv);
644 if (pInstance->convs[WDML_CLIENT_SIDE])
645 FIXME("still pending conversations\n");
647 /* then unregister all known service names */
648 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
650 /* Free the nodes that were not freed by this instance
651 * and remove the nodes from the list of HSZ nodes.
653 WDML_FreeAllHSZ(pInstance);
655 DestroyWindow(pInstance->hwndEvent);
657 /* OK now delete the instance handle itself */
659 if (WDML_InstanceList == pInstance)
661 /* special case - the first/only entry */
662 WDML_InstanceList = pInstance->next;
666 /* general case, remove entry */
669 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
670 inst->next = pInstance->next;
672 /* leave crit sect and release the heap entry
674 HeapFree(GetProcessHeap(), 0, pInstance);
675 LeaveCriticalSection(&WDML_CritSect);
679 /******************************************************************
680 * WDML_NotifyThreadExit
684 void WDML_NotifyThreadDetach(void)
686 WDML_INSTANCE* pInstance;
688 DWORD tid = GetCurrentThreadId();
690 EnterCriticalSection(&WDML_CritSect);
691 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
693 next = pInstance->next;
694 if (pInstance->threadID == tid)
696 DdeUninitialize(pInstance->instanceID);
699 LeaveCriticalSection(&WDML_CritSect);
702 /******************************************************************
703 * WDML_InvokeCallback
707 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
708 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
709 DWORD dwData1, DWORD dwData2)
713 if (pInstance == NULL)
715 TRACE("invoking CB%d[%08lx] (%u %u %08lx 0x%x 0x%x %u %lu %lu)\n",
716 pInstance->win16 ? 16 : 32, (DWORD)pInstance->callback, uType, uFmt,
717 (DWORD)hConv, hsz1, hsz2, hdata, dwData1, dwData2);
718 if (pInstance->win16)
720 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
721 hsz1, hsz2, hdata, dwData1, dwData2);
725 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
727 TRACE("done => %08lx\n", (DWORD)ret);
731 /*****************************************************************************
734 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
735 * for an instance Id, or NULL if the entry does not exist
738 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
740 WDML_INSTANCE* pInstance;
742 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
744 if (pInstance->instanceID == instId)
746 if (GetCurrentThreadId() != pInstance->threadID)
748 FIXME("Tried to get instance from wrong thread\n");
754 TRACE("Instance entry missing\n");
758 /******************************************************************
759 * WDML_GetInstanceFromWnd
763 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
765 return (WDML_INSTANCE*)GetWindowLongA(hWnd, GWL_WDML_INSTANCE);
768 /******************************************************************************
769 * DdeGetLastError [USER32.@] Gets most recent error code
772 * idInst [I] Instance identifier
777 UINT WINAPI DdeGetLastError(DWORD idInst)
780 WDML_INSTANCE* pInstance;
782 FIXME("(%ld): error reporting is weakly implemented\n", idInst);
784 EnterCriticalSection(&WDML_CritSect);
786 /* First check instance
788 pInstance = WDML_GetInstance(idInst);
789 if (pInstance == NULL)
791 error_code = DMLERR_DLL_NOT_INITIALIZED;
795 error_code = pInstance->lastError;
796 pInstance->lastError = 0;
799 LeaveCriticalSection(&WDML_CritSect);
803 /* ================================================================
807 * ================================================================ */
810 /******************************************************************
815 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
819 if (pInstance == NULL) return NULL;
821 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
823 if (pNode->hsz == hsz) break;
825 if (!pNode) WARN("HSZ 0x%x not found\n", hsz);
829 /******************************************************************
830 * WDML_MakeAtomFromHsz
832 * Creates a global atom from an existing HSZ
833 * Generally used before sending an HSZ as an atom to a remote app
835 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
837 WCHAR nameBuffer[MAX_BUFFER_LEN];
839 if (GetAtomNameW((ATOM)hsz, nameBuffer, MAX_BUFFER_LEN))
840 return GlobalAddAtomW(nameBuffer);
841 WARN("HSZ 0x%x not found\n", hsz);
845 /******************************************************************
846 * WDML_MakeHszFromAtom
848 * Creates a HSZ from an existing global atom
849 * Generally used while receiving a global atom and transforming it
852 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
854 WCHAR nameBuffer[MAX_BUFFER_LEN];
856 if (!atom) return (HSZ)0;
858 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
860 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
861 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
863 WARN("ATOM 0x%x not found\n", atom);
867 /******************************************************************
872 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
876 pNode = WDML_FindNode(pInstance, hsz);
877 if (!pNode) return FALSE;
883 /******************************************************************************
884 * WDML_DecHSZ (INTERNAL)
886 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
888 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
890 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
892 HSZNode* pPrev = NULL;
895 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
897 /* If we found the node we were looking for and its ref count is one,
900 if (pCurrent->hsz == hsz)
902 if (--pCurrent->refCount == 0)
904 if (pCurrent == pInstance->nodeList)
906 pInstance->nodeList = pCurrent->next;
910 pPrev->next = pCurrent->next;
912 HeapFree(GetProcessHeap(), 0, pCurrent);
913 DeleteAtom((ATOM)hsz);
918 WARN("HSZ 0x%x not found\n", hsz);
923 /******************************************************************************
924 * WDML_FreeAllHSZ (INTERNAL)
926 * Frees up all the strings still allocated in the list and
927 * remove all the nodes from the list of HSZ nodes.
929 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
931 /* Free any strings created in this instance.
933 while (pInstance->nodeList != NULL)
935 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
939 /******************************************************************************
940 * InsertHSZNode (INTERNAL)
942 * Insert a node to the head of the list.
944 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
948 HSZNode* pNew = NULL;
949 /* Create a new node for this HSZ.
951 pNew = (HSZNode*)HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
955 pNew->next = pInstance->nodeList;
957 pInstance->nodeList = pNew;
961 ERR("Primary HSZ Node allocation failed - out of memory\n");
966 /******************************************************************
971 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
974 WCHAR pString[MAX_BUFFER_LEN];
976 /* If psz is null, we have to return only the length
982 cchMax = MAX_BUFFER_LEN;
988 ret = GetAtomNameA((ATOM)hsz, ptr, cchMax);
991 ret = GetAtomNameW((ATOM)hsz, ptr, cchMax);
993 ERR("Unknown code page %d\n", codepage);
999 /*****************************************************************
1000 * DdeQueryStringA [USER32.@]
1002 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1005 WDML_INSTANCE* pInstance;
1007 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1009 EnterCriticalSection(&WDML_CritSect);
1011 /* First check instance
1013 pInstance = WDML_GetInstance(idInst);
1014 if (pInstance != NULL)
1016 if (iCodePage == 0) iCodePage = CP_WINANSI;
1017 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1019 LeaveCriticalSection(&WDML_CritSect);
1021 TRACE("returning %s\n", debugstr_a(psz));
1025 /*****************************************************************
1026 * DdeQueryStringW [USER32.@]
1029 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1032 WDML_INSTANCE* pInstance;
1034 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1036 EnterCriticalSection(&WDML_CritSect);
1038 /* First check instance
1040 pInstance = WDML_GetInstance(idInst);
1041 if (pInstance != NULL)
1043 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1044 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1046 LeaveCriticalSection(&WDML_CritSect);
1048 TRACE("returning %s\n", debugstr_w(psz));
1052 /******************************************************************
1057 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1064 hsz = (HSZ)AddAtomA(ptr);
1065 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_a(ptr), hsz);
1068 hsz = (HSZ)AddAtomW(ptr);
1069 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_w(ptr), hsz);
1072 ERR("Unknown code page %d\n", codepage);
1075 WDML_InsertHSZNode(pInstance, hsz);
1079 /*****************************************************************
1080 * DdeCreateStringHandleA [USER32.@]
1083 * Success: String handle
1086 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1089 WDML_INSTANCE* pInstance;
1091 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1093 EnterCriticalSection(&WDML_CritSect);
1095 pInstance = WDML_GetInstance(idInst);
1098 if (codepage == 0) codepage = CP_WINANSI;
1099 hsz = WDML_CreateString(pInstance, psz, codepage);
1102 LeaveCriticalSection(&WDML_CritSect);
1107 /******************************************************************************
1108 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1111 * idInst [I] Instance identifier
1112 * psz [I] Pointer to string
1113 * codepage [I] Code page identifier
1115 * Success: String handle
1118 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1120 WDML_INSTANCE* pInstance;
1123 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1125 EnterCriticalSection(&WDML_CritSect);
1127 pInstance = WDML_GetInstance(idInst);
1130 if (codepage == 0) codepage = CP_WINUNICODE;
1131 hsz = WDML_CreateString(pInstance, psz, codepage);
1133 LeaveCriticalSection(&WDML_CritSect);
1138 /*****************************************************************
1139 * DdeFreeStringHandle (USER32.@)
1140 * RETURNS: success: nonzero
1143 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1145 WDML_INSTANCE* pInstance;
1148 TRACE("(%ld,0x%x): \n", idInst, hsz);
1150 EnterCriticalSection(&WDML_CritSect);
1152 /* First check instance
1154 pInstance = WDML_GetInstance(idInst);
1156 ret = WDML_DecHSZ(pInstance, hsz);
1158 LeaveCriticalSection(&WDML_CritSect);
1163 /*****************************************************************
1164 * DdeKeepStringHandle (USER32.@)
1166 * RETURNS: success: nonzero
1169 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1171 WDML_INSTANCE* pInstance;
1174 TRACE("(%ld,0x%x): \n", idInst, hsz);
1176 EnterCriticalSection(&WDML_CritSect);
1178 /* First check instance
1180 pInstance = WDML_GetInstance(idInst);
1182 ret = WDML_IncHSZ(pInstance, hsz);
1184 LeaveCriticalSection(&WDML_CritSect);
1188 /*****************************************************************
1189 * DdeCmpStringHandles (USER32.@)
1191 * Compares the value of two string handles. This comparison is
1192 * not case sensitive.
1195 * -1 The value of hsz1 is zero or less than hsz2
1196 * 0 The values of hsz 1 and 2 are the same or both zero.
1197 * 1 The value of hsz2 is zero of less than hsz1
1199 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1201 WCHAR psz1[MAX_BUFFER_LEN];
1202 WCHAR psz2[MAX_BUFFER_LEN];
1206 ret1 = GetAtomNameW((ATOM)hsz1, psz1, MAX_BUFFER_LEN);
1207 ret2 = GetAtomNameW((ATOM)hsz2, psz2, MAX_BUFFER_LEN);
1209 TRACE("(%x<%s> %x<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1211 /* Make sure we found both strings. */
1212 if (ret1 == 0 && ret2 == 0)
1214 /* If both are not found, return both "zero strings". */
1219 /* If hsz1 is a not found, return hsz1 is "zero string". */
1224 /* If hsz2 is a not found, return hsz2 is "zero string". */
1229 /* Compare the two strings we got (case insensitive). */
1230 ret = lstrcmpiW(psz1, psz2);
1231 /* Since strcmp returns any number smaller than
1232 * 0 when the first string is found to be less than
1233 * the second one we must make sure we are returning
1234 * the proper values.
1249 /* ================================================================
1251 * Data handle management
1253 * ================================================================ */
1255 /*****************************************************************
1256 * DdeCreateDataHandle (USER32.@)
1258 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1259 HSZ hszItem, UINT wFmt, UINT afCmd)
1261 /* For now, we ignore idInst, hszItem.
1262 * The purpose of these arguments still need to be investigated.
1267 DDE_DATAHANDLE_HEAD* pDdh;
1269 TRACE("(%ld,%p,%ld,%ld,0x%lx,%d,%d)\n",
1270 idInst, pSrc, cb, cbOff, (DWORD)hszItem, wFmt, afCmd);
1272 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1275 /* we use the first 4 bytes to store the size */
1276 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + sizeof(DDE_DATAHANDLE_HEAD))))
1278 ERR("GlobalAlloc failed\n");
1282 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1289 pDdh->cfFormat = wFmt;
1290 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1292 pByte = (LPBYTE)(pDdh + 1);
1295 memcpy(pByte, pSrc + cbOff, cb);
1299 return (HDDEDATA)hMem;
1302 /*****************************************************************
1304 * DdeAddData (USER32.@)
1306 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1308 DWORD old_sz, new_sz;
1311 pDst = DdeAccessData(hData, &old_sz);
1312 if (!pDst) return 0;
1314 new_sz = cb + cbOff;
1315 if (new_sz > old_sz)
1317 DdeUnaccessData(hData);
1318 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1319 GMEM_MOVEABLE | GMEM_DDESHARE);
1320 pDst = DdeAccessData(hData, &old_sz);
1323 if (!pDst) return 0;
1325 memcpy(pDst + cbOff, pSrc, cb);
1326 DdeUnaccessData(hData);
1330 /******************************************************************************
1331 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1335 * hData [I] Handle to DDE object
1336 * pDst [I] Pointer to destination buffer
1337 * cbMax [I] Amount of data to copy
1338 * cbOff [I] Offset to beginning of data
1341 * Size of memory object associated with handle
1343 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1345 DWORD dwSize, dwRet;
1348 TRACE("(%08lx,%p,%ld,%ld)\n",(DWORD)hData, pDst, cbMax, cbOff);
1350 pByte = DdeAccessData(hData, &dwSize);
1358 else if (cbOff + cbMax < dwSize)
1362 else if (cbOff < dwSize)
1364 dwRet = dwSize - cbOff;
1370 if (pDst && dwRet != 0)
1372 memcpy(pDst, pByte + cbOff, dwRet);
1374 DdeUnaccessData(hData);
1383 /*****************************************************************
1384 * DdeAccessData (USER32.@)
1386 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1388 HGLOBAL hMem = (HGLOBAL)hData;
1389 DDE_DATAHANDLE_HEAD* pDdh;
1391 TRACE("(%08lx,%p)\n", (DWORD)hData, pcbDataSize);
1393 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1396 ERR("Failed on GlobalLock(%04x)\n", hMem);
1400 if (pcbDataSize != NULL)
1402 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1404 TRACE("=> %08lx (%lu)\n", (DWORD)(pDdh + 1), GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD));
1405 return (LPBYTE)(pDdh + 1);
1408 /*****************************************************************
1409 * DdeUnaccessData (USER32.@)
1411 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1413 HGLOBAL hMem = (HGLOBAL)hData;
1415 TRACE("(0x%lx)\n", (DWORD)hData);
1422 /*****************************************************************
1423 * DdeFreeDataHandle (USER32.@)
1425 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1427 return GlobalFree((HGLOBAL)hData) == 0;
1430 /******************************************************************
1435 BOOL WDML_IsAppOwned(HDDEDATA hData)
1437 DDE_DATAHANDLE_HEAD* pDdh;
1440 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1443 ret = pDdh->bAppOwned;
1444 GlobalUnlock((HGLOBAL)hData);
1449 /* ================================================================
1451 * Global <=> Data handle management
1453 * ================================================================ */
1455 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1457 * (bytes) (bits) comment
1458 * 0 16 bit fields for options (release, ackreq, response...)
1459 * 2 16 clipboard format
1460 * 4 ? data to be used
1462 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1470 pDd = GlobalLock(hMem);
1471 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1474 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1475 switch (pDd->cfFormat)
1478 FIXME("Unsupported format (%d) for data... assuming raw information\n",
1483 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1486 if (size >= sizeof(BITMAP))
1488 BITMAP* bmp = (BITMAP*)pDd->Value;
1489 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1490 if (size >= sizeof(BITMAP) + count)
1494 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1495 bmp->bmPlanes, bmp->bmBitsPixel,
1496 pDd->Value + sizeof(BITMAP))))
1498 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1499 0, 0, CF_BITMAP, 0);
1501 else ERR("Can't create bmp\n");
1505 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1507 } else ERR("No bitmap header\n");
1516 /******************************************************************
1517 * WDML_DataHandle2Global
1521 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1522 BOOL fDeferUpd, BOOL fAckReq)
1524 DDE_DATAHANDLE_HEAD* pDdh;
1528 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1529 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1532 WINE_DDEHEAD* wdh = NULL;
1534 switch (pDdh->cfFormat)
1537 FIXME("Unsupported format (%d) for data... passing raw information\n", pDdh->cfFormat);
1541 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1542 if (hMem && (wdh = GlobalLock(hMem)))
1544 memcpy(wdh + 1, pDdh + 1, dwSize);
1548 if (dwSize >= sizeof(HBITMAP))
1552 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1554 if (GetObjectA(hbmp, sizeof(bmp), &bmp))
1556 count = bmp.bmWidthBytes * bmp.bmHeight;
1557 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1558 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1559 if (hMem && (wdh = GlobalLock(hMem)))
1561 memcpy(wdh + 1, &bmp, sizeof(bmp));
1562 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1570 wdh->fResponse = fResponse;
1571 wdh->fRelease = fRelease;
1572 wdh->fDeferUpd = fDeferUpd;
1573 wdh->fAckReq = fAckReq;
1574 wdh->cfFormat = pDdh->cfFormat;
1577 GlobalUnlock((HGLOBAL)hDdeData);
1583 /* ================================================================
1587 * ================================================================ */
1589 /******************************************************************
1594 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1596 WDML_SERVER* pServer;
1600 pServer = (WDML_SERVER*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1601 if (pServer == NULL) return NULL;
1603 WDML_IncHSZ(pInstance, pServer->hszService = hszService);
1605 DdeQueryStringA(pInstance->instanceID, hszService, buf1, sizeof(buf1), CP_WINANSI);
1606 snprintf(buf2, sizeof(buf2), "%s(0x%08lx)", buf1, GetCurrentProcessId());
1607 pServer->hszServiceSpec = DdeCreateStringHandleA(pInstance->instanceID, buf2, CP_WINANSI);
1609 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1610 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1612 pServer->filterOn = TRUE;
1614 pServer->next = pInstance->servers;
1615 pInstance->servers = pServer;
1619 /******************************************************************
1624 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1626 WDML_SERVER* pPrev = NULL;
1627 WDML_SERVER* pServer = NULL;
1629 WDML_CONV* pConvNext;
1631 pServer = pInstance->servers;
1633 while (pServer != NULL)
1635 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1637 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1638 pServer->atomService, pServer->atomServiceSpec);
1639 /* terminate all conversations for given topic */
1640 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1642 pConvNext = pConv->next;
1643 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1645 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1646 /* don't care about return code (whether client window is present or not) */
1647 PostMessageA(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0L);
1650 if (pServer == pInstance->servers)
1652 pInstance->servers = pServer->next;
1656 pPrev->next = pServer->next;
1659 DestroyWindow(pServer->hwndServer);
1660 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1661 WDML_DecHSZ(pInstance, pServer->hszService);
1663 GlobalDeleteAtom(pServer->atomService);
1664 GlobalDeleteAtom(pServer->atomServiceSpec);
1666 HeapFree(GetProcessHeap(), 0, pServer);
1671 pServer = pServer->next;
1675 /*****************************************************************************
1678 * generic routine to return a pointer to the relevant ServiceNode
1679 * for a given service name, or NULL if the entry does not exist
1682 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1684 WDML_SERVER* pServer;
1686 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1688 if (hszService == pServer->hszService)
1693 TRACE("Service name missing\n");
1697 /* ================================================================
1699 * Conversation management
1701 * ================================================================ */
1703 /******************************************************************
1708 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1709 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1713 /* no converstation yet, add it */
1714 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1715 if (!pConv) return NULL;
1717 pConv->instance = pInstance;
1718 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1719 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1720 pConv->hwndServer = hwndServer;
1721 pConv->hwndClient = hwndClient;
1722 pConv->transactions = NULL;
1724 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1725 /* check if both side of the conversation are of the same instance */
1726 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1727 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1729 pConv->wStatus |= ST_ISSELF;
1731 pConv->wConvst = XST_NULL;
1733 pConv->next = pInstance->convs[side];
1734 pInstance->convs[side] = pConv;
1739 /******************************************************************
1744 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1745 HSZ hszService, HSZ hszTopic)
1747 WDML_CONV* pCurrent = NULL;
1749 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1751 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1752 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1761 /******************************************************************
1766 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1768 WDML_CONV* pPrev = NULL;
1769 WDML_CONV* pCurrent;
1771 WDML_XACT* pXActNext;
1777 /* remove any pending transaction */
1778 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1780 pXActNext = pXAct->next;
1781 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1784 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1786 /* FIXME: should we keep the window around ? it seems so (at least on client side
1787 * to let QueryConvInfo work after conv termination, but also to implement
1790 /* destroy conversation window, but first remove pConv from hWnd.
1791 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1793 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1794 SetWindowLongA(hWnd, GWL_WDML_CONVERSATION, 0);
1796 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1798 WDML_DecHSZ(pRef->instance, pRef->hszService);
1799 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1801 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1803 if (pCurrent == pRef)
1805 if (pCurrent == pRef->instance->convs[side])
1807 pRef->instance->convs[side] = pCurrent->next;
1811 pPrev->next = pCurrent->next;
1814 HeapFree(GetProcessHeap(), 0, pCurrent);
1820 /*****************************************************************
1821 * DdeEnableCallback (USER32.@)
1823 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1825 FIXME("(%ld, 0x%x, %d) stub\n", idInst, hConv, wCmd);
1830 /******************************************************************
1835 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1837 WDML_CONV* pConv = (WDML_CONV*)hConv;
1839 /* FIXME: should do better checking */
1841 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1843 FIXME("found conv but ain't connected\n");
1846 if (GetCurrentThreadId() != pConv->instance->threadID)
1848 FIXME("wrong thread ID\n");
1855 /******************************************************************
1856 * WDML_GetConvFromWnd
1860 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1862 return (WDML_CONV*)GetWindowLongA(hWnd, GWL_WDML_CONVERSATION);
1865 /******************************************************************
1870 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1871 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1876 if (side == WDML_SERVER_SIDE)
1878 from = pConv->hwndServer;
1879 to = pConv->hwndClient;
1883 to = pConv->hwndServer;
1884 from = pConv->hwndClient;
1887 ddeAck.bAppReturnCode = appRetCode;
1888 ddeAck.reserved = 0;
1889 ddeAck.fBusy = fBusy;
1892 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1894 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1895 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1896 if (!PostMessageA(to, WM_DDE_ACK, (WPARAM)from, lParam))
1898 pConv->wStatus &= ~ST_CONNECTED;
1899 FreeDDElParam(WM_DDE_ACK, lParam);
1905 /*****************************************************************
1906 * DdeSetUserHandle (USER32.@)
1908 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1913 EnterCriticalSection(&WDML_CritSect);
1915 pConv = WDML_GetConv(hConv, FALSE);
1923 pConv->hUser = hUser;
1929 pXAct = WDML_FindTransaction(pConv, id);
1932 pXAct->hUser = hUser;
1936 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
1941 LeaveCriticalSection(&WDML_CritSect);
1945 /******************************************************************
1946 * WDML_GetLocalConvInfo
1950 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
1956 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((DWORD)pConv | 1) : 0;
1957 ci->hszSvcPartner = pConv->hszService;
1958 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
1959 ci->hszTopic = pConv->hszTopic;
1960 ci->wStatus = pConv->wStatus;
1962 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
1964 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
1966 if (pLink->hConv == (HCONV)pConv)
1968 ci->wStatus |= ST_ADVISE;
1973 /* FIXME: non handled status flags:
1979 ci->wConvst = pConv->wConvst; /* FIXME */
1981 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
1983 ci->ConvCtxt = pConv->convContext;
1984 if (ci->wStatus & ST_CLIENT)
1986 ci->hwnd = pConv->hwndClient;
1987 ci->hwndPartner = pConv->hwndServer;
1991 ci->hwnd = pConv->hwndServer;
1992 ci->hwndPartner = pConv->hwndClient;
1996 ci->hUser = pConv->hUser;
2005 pXAct = WDML_FindTransaction(pConv, id);
2008 ci->hUser = pXAct->hUser;
2009 ci->hszItem = pXAct->hszItem;
2010 ci->wFmt = pXAct->wFmt;
2011 ci->wType = pXAct->wType;
2016 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2022 /******************************************************************
2023 * DdeQueryConvInfo (USER32.@)
2026 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, LPCONVINFO lpConvInfo)
2028 UINT ret = lpConvInfo->cb;
2035 FIXME("hConv is NULL\n");
2039 EnterCriticalSection(&WDML_CritSect);
2041 pConv = WDML_GetConv(hConv, FALSE);
2042 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2046 else if ((DWORD)hConv & 1)
2048 pConv = WDML_GetConv((DWORD)hConv & ~1, FALSE);
2051 FIXME("Request on remote conversation information is not implemented yet\n");
2055 LeaveCriticalSection(&WDML_CritSect);
2057 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2061 /* ================================================================
2063 * Link (hot & warm) management
2065 * ================================================================ */
2067 /******************************************************************
2072 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2073 UINT wType, HSZ hszItem, UINT wFmt)
2077 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2084 pLink->hConv = hConv;
2085 pLink->transactionType = wType;
2086 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2088 pLink->next = pInstance->links[side];
2089 pInstance->links[side] = pLink;
2092 /******************************************************************
2097 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2098 HSZ hszItem, UINT uFmt)
2100 WDML_LINK* pPrev = NULL;
2101 WDML_LINK* pCurrent = NULL;
2103 pCurrent = pInstance->links[side];
2105 while (pCurrent != NULL)
2107 if (pCurrent->hConv == hConv &&
2108 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2109 pCurrent->uFmt == uFmt)
2111 if (pCurrent == pInstance->links[side])
2113 pInstance->links[side] = pCurrent->next;
2117 pPrev->next = pCurrent->next;
2120 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2121 HeapFree(GetProcessHeap(), 0, pCurrent);
2126 pCurrent = pCurrent->next;
2130 /* this function is called to remove all links related to the conv.
2131 It should be called from both client and server when terminating
2134 /******************************************************************
2135 * WDML_RemoveAllLinks
2139 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2141 WDML_LINK* pPrev = NULL;
2142 WDML_LINK* pCurrent = NULL;
2143 WDML_LINK* pNext = NULL;
2145 pCurrent = pInstance->links[side];
2147 while (pCurrent != NULL)
2149 if (pCurrent->hConv == (HCONV)pConv)
2151 if (pCurrent == pInstance->links[side])
2153 pInstance->links[side] = pCurrent->next;
2154 pNext = pCurrent->next;
2158 pPrev->next = pCurrent->next;
2159 pNext = pCurrent->next;
2162 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2164 HeapFree(GetProcessHeap(), 0, pCurrent);
2171 pCurrent = pCurrent->next;
2180 /******************************************************************
2185 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2186 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2188 WDML_LINK* pCurrent = NULL;
2190 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2192 /* we don't need to check for transaction type as it can be altered */
2194 if (pCurrent->hConv == hConv &&
2195 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2196 (!use_fmt || pCurrent->uFmt == uFmt))
2206 /* ================================================================
2208 * Transaction management
2210 * ================================================================ */
2212 /******************************************************************
2213 * WDML_AllocTransaction
2215 * Alloc a transaction structure for handling the message ddeMsg
2217 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2218 UINT wFmt, HSZ hszItem)
2221 static WORD tid = 1; /* FIXME: wrap around */
2223 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2226 pInstance->lastError = DMLERR_MEMORY_ERROR;
2230 pXAct->xActID = tid++;
2231 pXAct->ddeMsg = ddeMsg;
2232 pXAct->hDdeData = 0;
2237 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2245 /******************************************************************
2246 * WDML_QueueTransaction
2248 * Adds a transaction to the list of transaction
2250 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2254 /* advance to last in queue */
2255 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2259 /******************************************************************
2260 * WDML_UnQueueTransaction
2264 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2268 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2279 /******************************************************************
2280 * WDML_FreeTransaction
2284 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2286 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2287 if (doFreePmt && (DWORD)pXAct->hMem > 1)
2289 GlobalFree(pXAct->hMem);
2291 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2293 HeapFree(GetProcessHeap(), 0, pXAct);
2296 /******************************************************************
2297 * WDML_FindTransaction
2301 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2306 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2308 if (pXAct->xActID == tid)
2314 /* ================================================================
2316 * Information broadcast across DDEML implementations
2318 * ================================================================ */
2320 struct tagWDML_BroadcastPmt
2328 /******************************************************************
2329 * WDML_BroadcastEnumProc
2333 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2335 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2338 if (GetClassNameA(hWnd, buffer, sizeof(buffer)) > 0 &&
2339 strcmp(buffer, s->clsName) == 0)
2341 PostMessageA(hWnd, s->uMsg, s->wParam, s->lParam);
2346 /******************************************************************
2347 * WDML_BroadcastDDEWindows
2351 void WDML_BroadcastDDEWindows(const char* clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2353 struct tagWDML_BroadcastPmt s;
2355 s.clsName = clsName;
2359 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);