1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
10 * Copyright 2001 Eric Pouech
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "wine/debug.h"
38 #include "dde/dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static WDML_INSTANCE* WDML_InstanceList = NULL;
43 static DWORD WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
44 const char WDML_szEventClass[] = "DdeEventClass";
45 CRITICAL_SECTION WDML_CritSect = CRITICAL_SECTION_INIT("WDML_CritSect");
47 /* ================================================================
49 * Pure DDE (non DDEML) management
51 * ================================================================ */
54 /*****************************************************************
55 * PackDDElParam (USER32.@)
60 LPARAM WINAPI PackDDElParam(UINT msg, UINT uiLo, UINT uiHi)
71 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT) * 2)))
73 ERR("GlobalAlloc failed\n");
76 if (!(params = GlobalLock(hMem)))
78 ERR("GlobalLock failed (%x)\n", hMem);
90 return MAKELONG(uiLo, uiHi);
95 /*****************************************************************
96 * UnpackDDElParam (USER32.@)
102 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
103 PUINT uiLo, PUINT uiHi)
113 if (!lParam) return FALSE;
114 if (!(params = GlobalLock( (HGLOBAL)lParam )))
116 ERR("GlobalLock failed (%lx)\n", lParam);
119 if (uiLo) *uiLo = params[0];
120 if (uiHi) *uiHi = params[1];
121 GlobalUnlock( (HGLOBAL)lParam );
126 if (uiHi) *uiHi = lParam;
130 if (uiLo) *uiLo = LOWORD(lParam);
131 if (uiHi) *uiHi = HIWORD(lParam);
137 /*****************************************************************
138 * FreeDDElParam (USER32.@)
144 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
152 /* first check if it's a global handle */
153 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
154 return !GlobalFree( (HGLOBAL)lParam );
162 /*****************************************************************
163 * ReuseDDElParam (USER32.@)
168 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
169 UINT uiLo, UINT uiHi)
185 if (!lParam) return 0;
186 if (!(params = GlobalLock( (HGLOBAL)lParam )))
188 ERR("GlobalLock failed\n");
193 TRACE("Reusing pack %08x %08x\n", uiLo, uiHi);
194 GlobalLock( (HGLOBAL)lParam );
198 FreeDDElParam( msgIn, lParam );
202 FreeDDElParam( msgIn, lParam );
203 return MAKELONG(uiLo, uiHi);
207 return PackDDElParam( msgOut, uiLo, uiHi );
211 /*****************************************************************
212 * ImpersonateDdeClientWindow (USER32.@)
215 * hWndClient [I] handle to DDE client window
216 * hWndServer [I] handle to DDE server window
218 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
220 FIXME("(%04x %04x): stub\n", hWndClient, hWndServer);
224 /*****************************************************************
225 * DdeSetQualityOfService (USER32.@)
228 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, CONST SECURITY_QUALITY_OF_SERVICE *pqosNew,
229 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
231 FIXME("(%04x %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
235 /* ================================================================
237 * Instance management
239 * ================================================================ */
241 /******************************************************************************
242 * IncrementInstanceId
244 * generic routine to increment the max instance Id and allocate a new application instance
246 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
248 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
250 pInstance->instanceID = id;
251 TRACE("New instance id %ld allocated\n", id);
254 /******************************************************************
259 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
261 WDML_INSTANCE* pInstance;
266 case WM_WDML_REGISTER:
267 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
268 /* try calling the Callback */
269 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
271 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
272 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
273 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
274 WDML_DecHSZ(pInstance, hsz1);
275 WDML_DecHSZ(pInstance, hsz2);
279 case WM_WDML_UNREGISTER:
280 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
281 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
283 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
284 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
285 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
286 WDML_DecHSZ(pInstance, hsz1);
287 WDML_DecHSZ(pInstance, hsz2);
291 case WM_WDML_CONNECT_CONFIRM:
292 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
293 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
296 /* confirm connection...
297 * lookup for this conv handle
299 HWND client = WIN_GetFullHandle( (HWND)wParam );
300 HWND server = WIN_GetFullHandle( (HWND)lParam );
301 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
303 if (pConv->hwndClient == client && pConv->hwndServer == server)
308 pConv->wStatus |= ST_ISLOCAL;
310 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
311 pConv->hszTopic, pConv->hszService, 0, 0,
312 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
317 return DefWindowProcA(hwndEvent, uMsg, wParam, lParam);
322 /******************************************************************
327 UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
328 DWORD afCmd, DWORD ulRes, BOOL bUnicode, BOOL b16)
330 WDML_INSTANCE* pInstance;
331 WDML_INSTANCE* reference_inst;
333 WNDCLASSEXA wndclass;
335 TRACE("(%p,%p,0x%lx,%ld)\n",
336 pidInst, pfnCallback, afCmd, ulRes);
340 ERR("Reserved value not zero? What does this mean?\n");
341 /* trap this and no more until we know more */
342 return DMLERR_NO_ERROR;
345 /* grab enough heap for one control struct - not really necessary for re-initialise
346 * but allows us to use same validation routines */
347 pInstance = (WDML_INSTANCE*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
348 if (pInstance == NULL)
350 /* catastrophe !! warn user & abort */
351 ERR("Instance create failed - out of memory\n");
352 return DMLERR_SYS_ERROR;
354 pInstance->next = NULL;
355 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
357 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
359 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
360 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
361 pInstance->threadID = GetCurrentThreadId();
362 pInstance->callback = *pfnCallback;
363 pInstance->unicode = bUnicode;
364 pInstance->win16 = b16;
365 pInstance->nodeList = NULL; /* node will be added later */
366 pInstance->monitorFlags = afCmd & MF_MASK;
367 pInstance->servers = NULL;
368 pInstance->convs[0] = NULL;
369 pInstance->convs[1] = NULL;
370 pInstance->links[0] = NULL;
371 pInstance->links[1] = NULL;
373 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
375 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
377 if (!pInstance->clientOnly)
379 /* Check for other way of setting Client-only !! */
380 pInstance->clientOnly =
381 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
384 TRACE("instance created - checking validity \n");
388 /* Initialisation of new Instance Identifier */
389 TRACE("new instance, callback %p flags %lX\n",pfnCallback,afCmd);
391 EnterCriticalSection(&WDML_CritSect);
393 if (WDML_InstanceList == NULL)
395 /* can't be another instance in this case, assign to the base pointer */
396 WDML_InstanceList = pInstance;
398 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
400 * ------------------------------- NOTE NOTE NOTE --------------------------
402 * the manual is not clear if this condition
403 * applies to the first call to DdeInitialize from an application, or the
404 * first call for a given callback !!!
407 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
408 TRACE("First application instance detected OK\n");
409 /* allocate new instance ID */
410 WDML_IncrementInstanceId(pInstance);
414 /* really need to chain the new one in to the latest here, but after checking conditions
415 * such as trying to start a conversation from an application trying to monitor */
416 reference_inst = WDML_InstanceList;
417 TRACE("Subsequent application instance - starting checks\n");
418 while (reference_inst->next != NULL)
421 * This set of tests will work if application uses same instance Id
422 * at application level once allocated - which is what manual implies
423 * should happen. If someone tries to be
424 * clever (lazy ?) it will fail to pick up that later calls are for
425 * the same application - should we trust them ?
427 if (pInstance->instanceID == reference_inst->instanceID)
429 /* Check 1 - must be same Client-only state */
431 if (pInstance->clientOnly != reference_inst->clientOnly)
433 ret = DMLERR_DLL_USAGE;
437 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
439 if (pInstance->monitor != reference_inst->monitor)
441 ret = DMLERR_INVALIDPARAMETER;
445 /* Check 3 - must supply different callback address */
447 if (pInstance->callback == reference_inst->callback)
449 ret = DMLERR_DLL_USAGE;
453 reference_inst = reference_inst->next;
455 /* All cleared, add to chain */
457 TRACE("Application Instance checks finished\n");
458 WDML_IncrementInstanceId(pInstance);
459 reference_inst->next = pInstance;
461 LeaveCriticalSection(&WDML_CritSect);
463 *pidInst = pInstance->instanceID;
465 /* for deadlock issues, windows must always be created when outside the critical section */
466 wndclass.cbSize = sizeof(wndclass);
468 wndclass.lpfnWndProc = WDML_EventProc;
469 wndclass.cbClsExtra = 0;
470 wndclass.cbWndExtra = sizeof(DWORD);
471 wndclass.hInstance = 0;
473 wndclass.hCursor = 0;
474 wndclass.hbrBackground = 0;
475 wndclass.lpszMenuName = NULL;
476 wndclass.lpszClassName = WDML_szEventClass;
477 wndclass.hIconSm = 0;
479 RegisterClassExA(&wndclass);
481 pInstance->hwndEvent = CreateWindowA(WDML_szEventClass, NULL,
482 WS_POPUP, 0, 0, 0, 0,
485 SetWindowLongA(pInstance->hwndEvent, GWL_WDML_INSTANCE, (DWORD)pInstance);
487 TRACE("New application instance processing finished OK\n");
491 /* Reinitialisation situation --- FIX */
492 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst, pfnCallback, afCmd, ulRes);
494 EnterCriticalSection(&WDML_CritSect);
496 if (WDML_InstanceList == NULL)
498 ret = DMLERR_DLL_USAGE;
501 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
502 /* can't reinitialise if we have initialised nothing !! */
503 reference_inst = WDML_InstanceList;
504 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
506 * MS allows initialisation without specifying a callback, should we allow addition of the
507 * callback by a later call to initialise ? - if so this lot will have to change
509 while (reference_inst->next != NULL)
511 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
513 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
515 if (reference_inst->clientOnly)
517 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
519 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
521 if (!(afCmd & APPCMD_CLIENTONLY))
523 ret = DMLERR_DLL_USAGE;
528 /* Check 2 - cannot change monitor modes */
530 if (pInstance->monitor != reference_inst->monitor)
532 ret = DMLERR_DLL_USAGE;
536 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
538 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
540 ret = DMLERR_DLL_USAGE;
545 reference_inst = reference_inst->next;
547 if (reference_inst->next == NULL)
549 /* Crazy situation - trying to re-initialize something that has not beeen initialized !!
551 * Manual does not say what we do, cannot return DMLERR_NOT_INITIALIZED so what ?
553 ret = DMLERR_INVALIDPARAMETER;
556 /* All checked - change relevant flags */
558 reference_inst->CBFflags = pInstance->CBFflags;
559 reference_inst->clientOnly = pInstance->clientOnly;
560 reference_inst->monitorFlags = pInstance->monitorFlags;
561 LeaveCriticalSection(&WDML_CritSect);
564 return DMLERR_NO_ERROR;
566 HeapFree(GetProcessHeap(), 0, pInstance);
567 LeaveCriticalSection(&WDML_CritSect);
571 /******************************************************************************
572 * DdeInitializeA (USER32.@)
574 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
575 DWORD afCmd, DWORD ulRes)
577 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE, FALSE);
580 /******************************************************************************
581 * DdeInitializeW [USER32.@]
582 * Registers an application with the DDEML
585 * pidInst [I] Pointer to instance identifier
586 * pfnCallback [I] Pointer to callback function
587 * afCmd [I] Set of command and filter flags
591 * Success: DMLERR_NO_ERROR
592 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
594 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
595 DWORD afCmd, DWORD ulRes)
597 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE, FALSE);
600 /*****************************************************************
601 * DdeUninitialize [USER32.@] Frees DDEML resources
604 * idInst [I] Instance identifier
611 BOOL WINAPI DdeUninitialize(DWORD idInst)
613 /* Stage one - check if we have a handle for this instance
615 WDML_INSTANCE* pInstance;
617 WDML_CONV* pConvNext;
619 EnterCriticalSection(&WDML_CritSect);
621 /* First check instance
623 pInstance = WDML_GetInstance(idInst);
624 if (pInstance == NULL)
626 LeaveCriticalSection(&WDML_CritSect);
628 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
633 /* first terminate all conversations client side
634 * this shall close existing links...
636 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
638 pConvNext = pConv->next;
639 DdeDisconnect((HCONV)pConv);
641 if (pInstance->convs[WDML_CLIENT_SIDE])
642 FIXME("still pending conversations\n");
644 /* then unregister all known service names */
645 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
647 /* Free the nodes that were not freed by this instance
648 * and remove the nodes from the list of HSZ nodes.
650 WDML_FreeAllHSZ(pInstance);
652 DestroyWindow(pInstance->hwndEvent);
654 /* OK now delete the instance handle itself */
656 if (WDML_InstanceList == pInstance)
658 /* special case - the first/only entry */
659 WDML_InstanceList = pInstance->next;
663 /* general case, remove entry */
666 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
667 inst->next = pInstance->next;
669 /* leave crit sect and release the heap entry
671 HeapFree(GetProcessHeap(), 0, pInstance);
672 LeaveCriticalSection(&WDML_CritSect);
676 /******************************************************************
677 * WDML_NotifyThreadExit
681 void WDML_NotifyThreadDetach(void)
683 WDML_INSTANCE* pInstance;
685 DWORD tid = GetCurrentThreadId();
687 EnterCriticalSection(&WDML_CritSect);
688 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
690 next = pInstance->next;
691 if (pInstance->threadID == tid)
693 DdeUninitialize(pInstance->instanceID);
696 LeaveCriticalSection(&WDML_CritSect);
699 /******************************************************************
700 * WDML_InvokeCallback
704 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
705 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
706 DWORD dwData1, DWORD dwData2)
710 if (pInstance == NULL)
712 TRACE("invoking CB%d[%08lx] (%u %u %08lx 0x%x 0x%x %u %lu %lu)\n",
713 pInstance->win16 ? 16 : 32, (DWORD)pInstance->callback, uType, uFmt,
714 (DWORD)hConv, hsz1, hsz2, hdata, dwData1, dwData2);
715 if (pInstance->win16)
717 ret = WDML_InvokeCallback16(pInstance->callback, uType, uFmt, hConv,
718 hsz1, hsz2, hdata, dwData1, dwData2);
722 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
724 TRACE("done => %08lx\n", (DWORD)ret);
728 /*****************************************************************************
731 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
732 * for an instance Id, or NULL if the entry does not exist
735 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
737 WDML_INSTANCE* pInstance;
739 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
741 if (pInstance->instanceID == instId)
743 if (GetCurrentThreadId() != pInstance->threadID)
745 FIXME("Tried to get instance from wrong thread\n");
751 TRACE("Instance entry missing\n");
755 /******************************************************************
756 * WDML_GetInstanceFromWnd
760 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
762 return (WDML_INSTANCE*)GetWindowLongA(hWnd, GWL_WDML_INSTANCE);
765 /******************************************************************************
766 * DdeGetLastError [USER32.@] Gets most recent error code
769 * idInst [I] Instance identifier
774 UINT WINAPI DdeGetLastError(DWORD idInst)
777 WDML_INSTANCE* pInstance;
779 FIXME("(%ld): error reporting is weakly implemented\n", idInst);
781 EnterCriticalSection(&WDML_CritSect);
783 /* First check instance
785 pInstance = WDML_GetInstance(idInst);
786 if (pInstance == NULL)
788 error_code = DMLERR_DLL_NOT_INITIALIZED;
792 error_code = pInstance->lastError;
793 pInstance->lastError = 0;
796 LeaveCriticalSection(&WDML_CritSect);
800 /* ================================================================
804 * ================================================================ */
807 /******************************************************************
812 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
816 if (pInstance == NULL) return NULL;
818 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
820 if (pNode->hsz == hsz) break;
822 if (!pNode) WARN("HSZ 0x%x not found\n", hsz);
826 /******************************************************************
827 * WDML_MakeAtomFromHsz
829 * Creates a global atom from an existing HSZ
830 * Generally used before sending an HSZ as an atom to a remote app
832 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
834 WCHAR nameBuffer[MAX_BUFFER_LEN];
836 if (GetAtomNameW((ATOM)hsz, nameBuffer, MAX_BUFFER_LEN))
837 return GlobalAddAtomW(nameBuffer);
838 WARN("HSZ 0x%x not found\n", hsz);
842 /******************************************************************
843 * WDML_MakeHszFromAtom
845 * Creates a HSZ from an existing global atom
846 * Generally used while receiving a global atom and transforming it
849 HSZ WDML_MakeHszFromAtom(WDML_INSTANCE* pInstance, ATOM atom)
851 WCHAR nameBuffer[MAX_BUFFER_LEN];
853 if (!atom) return (HSZ)0;
855 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
857 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
858 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
860 WARN("ATOM 0x%x not found\n", atom);
864 /******************************************************************
869 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
873 pNode = WDML_FindNode(pInstance, hsz);
874 if (!pNode) return FALSE;
880 /******************************************************************************
881 * WDML_DecHSZ (INTERNAL)
883 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
885 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
887 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
889 HSZNode* pPrev = NULL;
892 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
894 /* If we found the node we were looking for and its ref count is one,
897 if (pCurrent->hsz == hsz)
899 if (--pCurrent->refCount == 0)
901 if (pCurrent == pInstance->nodeList)
903 pInstance->nodeList = pCurrent->next;
907 pPrev->next = pCurrent->next;
909 HeapFree(GetProcessHeap(), 0, pCurrent);
910 DeleteAtom((ATOM)hsz);
915 WARN("HSZ 0x%x not found\n", hsz);
920 /******************************************************************************
921 * WDML_FreeAllHSZ (INTERNAL)
923 * Frees up all the strings still allocated in the list and
924 * remove all the nodes from the list of HSZ nodes.
926 void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
928 /* Free any strings created in this instance.
930 while (pInstance->nodeList != NULL)
932 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
936 /******************************************************************************
937 * InsertHSZNode (INTERNAL)
939 * Insert a node to the head of the list.
941 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
945 HSZNode* pNew = NULL;
946 /* Create a new node for this HSZ.
948 pNew = (HSZNode*)HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
952 pNew->next = pInstance->nodeList;
954 pInstance->nodeList = pNew;
958 ERR("Primary HSZ Node allocation failed - out of memory\n");
963 /******************************************************************
968 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
971 WCHAR pString[MAX_BUFFER_LEN];
973 /* If psz is null, we have to return only the length
979 cchMax = MAX_BUFFER_LEN;
985 ret = GetAtomNameA((ATOM)hsz, ptr, cchMax);
988 ret = GetAtomNameW((ATOM)hsz, ptr, cchMax);
990 ERR("Unknown code page %d\n", codepage);
996 /*****************************************************************
997 * DdeQueryStringA [USER32.@]
999 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
1002 WDML_INSTANCE* pInstance;
1004 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1006 EnterCriticalSection(&WDML_CritSect);
1008 /* First check instance
1010 pInstance = WDML_GetInstance(idInst);
1011 if (pInstance != NULL)
1013 if (iCodePage == 0) iCodePage = CP_WINANSI;
1014 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1016 LeaveCriticalSection(&WDML_CritSect);
1018 TRACE("returning %s\n", debugstr_a(psz));
1022 /*****************************************************************
1023 * DdeQueryStringW [USER32.@]
1026 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
1029 WDML_INSTANCE* pInstance;
1031 TRACE("(%ld, 0x%x, %p, %ld, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
1033 EnterCriticalSection(&WDML_CritSect);
1035 /* First check instance
1037 pInstance = WDML_GetInstance(idInst);
1038 if (pInstance != NULL)
1040 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
1041 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
1043 LeaveCriticalSection(&WDML_CritSect);
1045 TRACE("returning %s\n", debugstr_w(psz));
1049 /******************************************************************
1054 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
1061 hsz = (HSZ)AddAtomA(ptr);
1062 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_a(ptr), hsz);
1065 hsz = (HSZ)AddAtomW(ptr);
1066 TRACE("added atom %s with HSZ 0x%x, \n", debugstr_w(ptr), hsz);
1069 ERR("Unknown code page %d\n", codepage);
1072 WDML_InsertHSZNode(pInstance, hsz);
1076 /*****************************************************************
1077 * DdeCreateStringHandleA [USER32.@]
1080 * Success: String handle
1083 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
1086 WDML_INSTANCE* pInstance;
1088 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1090 EnterCriticalSection(&WDML_CritSect);
1092 pInstance = WDML_GetInstance(idInst);
1095 if (codepage == 0) codepage = CP_WINANSI;
1096 hsz = WDML_CreateString(pInstance, psz, codepage);
1099 LeaveCriticalSection(&WDML_CritSect);
1104 /******************************************************************************
1105 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1108 * idInst [I] Instance identifier
1109 * psz [I] Pointer to string
1110 * codepage [I] Code page identifier
1112 * Success: String handle
1115 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
1117 WDML_INSTANCE* pInstance;
1120 TRACE("(%ld,%p,%d)\n", idInst, psz, codepage);
1122 EnterCriticalSection(&WDML_CritSect);
1124 pInstance = WDML_GetInstance(idInst);
1127 if (codepage == 0) codepage = CP_WINUNICODE;
1128 hsz = WDML_CreateString(pInstance, psz, codepage);
1130 LeaveCriticalSection(&WDML_CritSect);
1135 /*****************************************************************
1136 * DdeFreeStringHandle (USER32.@)
1137 * RETURNS: success: nonzero
1140 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
1142 WDML_INSTANCE* pInstance;
1145 TRACE("(%ld,0x%x): \n", idInst, hsz);
1147 EnterCriticalSection(&WDML_CritSect);
1149 /* First check instance
1151 pInstance = WDML_GetInstance(idInst);
1153 ret = WDML_DecHSZ(pInstance, hsz);
1155 LeaveCriticalSection(&WDML_CritSect);
1160 /*****************************************************************
1161 * DdeKeepStringHandle (USER32.@)
1163 * RETURNS: success: nonzero
1166 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
1168 WDML_INSTANCE* pInstance;
1171 TRACE("(%ld,0x%x): \n", idInst, hsz);
1173 EnterCriticalSection(&WDML_CritSect);
1175 /* First check instance
1177 pInstance = WDML_GetInstance(idInst);
1179 ret = WDML_IncHSZ(pInstance, hsz);
1181 LeaveCriticalSection(&WDML_CritSect);
1185 /*****************************************************************
1186 * DdeCmpStringHandles (USER32.@)
1188 * Compares the value of two string handles. This comparison is
1189 * not case sensitive.
1192 * -1 The value of hsz1 is zero or less than hsz2
1193 * 0 The values of hsz 1 and 2 are the same or both zero.
1194 * 1 The value of hsz2 is zero of less than hsz1
1196 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
1198 WCHAR psz1[MAX_BUFFER_LEN];
1199 WCHAR psz2[MAX_BUFFER_LEN];
1203 ret1 = GetAtomNameW((ATOM)hsz1, psz1, MAX_BUFFER_LEN);
1204 ret2 = GetAtomNameW((ATOM)hsz2, psz2, MAX_BUFFER_LEN);
1206 TRACE("(%x<%s> %x<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
1208 /* Make sure we found both strings. */
1209 if (ret1 == 0 && ret2 == 0)
1211 /* If both are not found, return both "zero strings". */
1216 /* If hsz1 is a not found, return hsz1 is "zero string". */
1221 /* If hsz2 is a not found, return hsz2 is "zero string". */
1226 /* Compare the two strings we got (case insensitive). */
1227 ret = lstrcmpiW(psz1, psz2);
1228 /* Since strcmp returns any number smaller than
1229 * 0 when the first string is found to be less than
1230 * the second one we must make sure we are returning
1231 * the proper values.
1246 /* ================================================================
1248 * Data handle management
1250 * ================================================================ */
1252 /*****************************************************************
1253 * DdeCreateDataHandle (USER32.@)
1255 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1256 HSZ hszItem, UINT wFmt, UINT afCmd)
1258 /* For now, we ignore idInst, hszItem.
1259 * The purpose of these arguments still need to be investigated.
1264 DDE_DATAHANDLE_HEAD* pDdh;
1266 TRACE("(%ld,%p,%ld,%ld,0x%lx,%d,%d)\n",
1267 idInst, pSrc, cb, cbOff, (DWORD)hszItem, wFmt, afCmd);
1269 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1272 /* we use the first 4 bytes to store the size */
1273 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + sizeof(DDE_DATAHANDLE_HEAD))))
1275 ERR("GlobalAlloc failed\n");
1279 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1286 pDdh->cfFormat = wFmt;
1287 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1289 pByte = (LPBYTE)(pDdh + 1);
1292 memcpy(pByte, pSrc + cbOff, cb);
1296 return (HDDEDATA)hMem;
1299 /*****************************************************************
1301 * DdeAddData (USER32.@)
1303 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1305 DWORD old_sz, new_sz;
1308 pDst = DdeAccessData(hData, &old_sz);
1309 if (!pDst) return 0;
1311 new_sz = cb + cbOff;
1312 if (new_sz > old_sz)
1314 DdeUnaccessData(hData);
1315 hData = GlobalReAlloc((HGLOBAL)hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1316 GMEM_MOVEABLE | GMEM_DDESHARE);
1317 pDst = DdeAccessData(hData, &old_sz);
1320 if (!pDst) return 0;
1322 memcpy(pDst + cbOff, pSrc, cb);
1323 DdeUnaccessData(hData);
1327 /******************************************************************************
1328 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1332 * hData [I] Handle to DDE object
1333 * pDst [I] Pointer to destination buffer
1334 * cbMax [I] Amount of data to copy
1335 * cbOff [I] Offset to beginning of data
1338 * Size of memory object associated with handle
1340 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1342 DWORD dwSize, dwRet;
1345 TRACE("(%08lx,%p,%ld,%ld)\n",(DWORD)hData, pDst, cbMax, cbOff);
1347 pByte = DdeAccessData(hData, &dwSize);
1355 else if (cbOff + cbMax < dwSize)
1359 else if (cbOff < dwSize)
1361 dwRet = dwSize - cbOff;
1367 if (pDst && dwRet != 0)
1369 memcpy(pDst, pByte + cbOff, dwRet);
1371 DdeUnaccessData(hData);
1380 /*****************************************************************
1381 * DdeAccessData (USER32.@)
1383 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1385 HGLOBAL hMem = (HGLOBAL)hData;
1386 DDE_DATAHANDLE_HEAD* pDdh;
1388 TRACE("(%08lx,%p)\n", (DWORD)hData, pcbDataSize);
1390 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock(hMem);
1393 ERR("Failed on GlobalLock(%04x)\n", hMem);
1397 if (pcbDataSize != NULL)
1399 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1401 TRACE("=> %08lx (%lu)\n", (DWORD)(pDdh + 1), GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD));
1402 return (LPBYTE)(pDdh + 1);
1405 /*****************************************************************
1406 * DdeUnaccessData (USER32.@)
1408 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1410 HGLOBAL hMem = (HGLOBAL)hData;
1412 TRACE("(0x%lx)\n", (DWORD)hData);
1419 /*****************************************************************
1420 * DdeFreeDataHandle (USER32.@)
1422 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1424 return GlobalFree((HGLOBAL)hData) == 0;
1427 /******************************************************************
1432 BOOL WDML_IsAppOwned(HDDEDATA hData)
1434 DDE_DATAHANDLE_HEAD* pDdh;
1437 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hData);
1440 ret = pDdh->bAppOwned;
1441 GlobalUnlock((HGLOBAL)hData);
1446 /* ================================================================
1448 * Global <=> Data handle management
1450 * ================================================================ */
1452 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1454 * (bytes) (bits) comment
1455 * 0 16 bit fields for options (release, ackreq, response...)
1456 * 2 16 clipboard format
1457 * 4 ? data to be used
1459 HDDEDATA WDML_Global2DataHandle(HGLOBAL hMem, WINE_DDEHEAD* p)
1467 pDd = GlobalLock(hMem);
1468 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1471 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1472 switch (pDd->cfFormat)
1475 FIXME("Unsupported format (%d) for data... assuming raw information\n",
1480 ret = DdeCreateDataHandle(0, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1483 if (size >= sizeof(BITMAP))
1485 BITMAP* bmp = (BITMAP*)pDd->Value;
1486 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1487 if (size >= sizeof(BITMAP) + count)
1491 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1492 bmp->bmPlanes, bmp->bmBitsPixel,
1493 pDd->Value + sizeof(BITMAP))))
1495 ret = DdeCreateDataHandle(0, (LPBYTE)&hbmp, sizeof(hbmp),
1496 0, 0, CF_BITMAP, 0);
1498 else ERR("Can't create bmp\n");
1502 ERR("Wrong count: %lu / %d\n", size, sizeof(BITMAP) + count);
1504 } else ERR("No bitmap header\n");
1513 /******************************************************************
1514 * WDML_DataHandle2Global
1518 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1519 BOOL fDeferUpd, BOOL fAckReq)
1521 DDE_DATAHANDLE_HEAD* pDdh;
1525 dwSize = GlobalSize((HGLOBAL)hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1526 pDdh = (DDE_DATAHANDLE_HEAD*)GlobalLock((HGLOBAL)hDdeData);
1529 WINE_DDEHEAD* wdh = NULL;
1531 switch (pDdh->cfFormat)
1534 FIXME("Unsupported format (%d) for data... passing raw information\n", pDdh->cfFormat);
1538 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1539 if (hMem && (wdh = GlobalLock(hMem)))
1541 memcpy(wdh + 1, pDdh + 1, dwSize);
1545 if (dwSize >= sizeof(HBITMAP))
1549 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1551 if (GetObjectA(hbmp, sizeof(bmp), &bmp))
1553 count = bmp.bmWidthBytes * bmp.bmHeight;
1554 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1555 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1556 if (hMem && (wdh = GlobalLock(hMem)))
1558 memcpy(wdh + 1, &bmp, sizeof(bmp));
1559 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1567 wdh->fResponse = fResponse;
1568 wdh->fRelease = fRelease;
1569 wdh->fDeferUpd = fDeferUpd;
1570 wdh->fAckReq = fAckReq;
1571 wdh->cfFormat = pDdh->cfFormat;
1574 GlobalUnlock((HGLOBAL)hDdeData);
1580 /* ================================================================
1584 * ================================================================ */
1586 /******************************************************************
1591 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1593 WDML_SERVER* pServer;
1597 pServer = (WDML_SERVER*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1598 if (pServer == NULL) return NULL;
1600 WDML_IncHSZ(pInstance, pServer->hszService = hszService);
1602 DdeQueryStringA(pInstance->instanceID, hszService, buf1, sizeof(buf1), CP_WINANSI);
1603 snprintf(buf2, sizeof(buf2), "%s(0x%08lx)", buf1, GetCurrentProcessId());
1604 pServer->hszServiceSpec = DdeCreateStringHandleA(pInstance->instanceID, buf2, CP_WINANSI);
1606 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1607 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1609 pServer->filterOn = TRUE;
1611 pServer->next = pInstance->servers;
1612 pInstance->servers = pServer;
1616 /******************************************************************
1621 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1623 WDML_SERVER* pPrev = NULL;
1624 WDML_SERVER* pServer = NULL;
1626 WDML_CONV* pConvNext;
1628 pServer = pInstance->servers;
1630 while (pServer != NULL)
1632 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1634 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1635 pServer->atomService, pServer->atomServiceSpec);
1636 /* terminate all conversations for given topic */
1637 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1639 pConvNext = pConv->next;
1640 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1642 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1643 /* don't care about return code (whether client window is present or not) */
1644 PostMessageA(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0L);
1647 if (pServer == pInstance->servers)
1649 pInstance->servers = pServer->next;
1653 pPrev->next = pServer->next;
1656 DestroyWindow(pServer->hwndServer);
1657 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1658 WDML_DecHSZ(pInstance, pServer->hszService);
1660 GlobalDeleteAtom(pServer->atomService);
1661 GlobalDeleteAtom(pServer->atomServiceSpec);
1663 HeapFree(GetProcessHeap(), 0, pServer);
1668 pServer = pServer->next;
1672 /*****************************************************************************
1675 * generic routine to return a pointer to the relevant ServiceNode
1676 * for a given service name, or NULL if the entry does not exist
1679 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1681 WDML_SERVER* pServer;
1683 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1685 if (hszService == pServer->hszService)
1690 TRACE("Service name missing\n");
1694 /* ================================================================
1696 * Conversation management
1698 * ================================================================ */
1700 /******************************************************************
1705 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1706 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
1710 /* no converstation yet, add it */
1711 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
1712 if (!pConv) return NULL;
1714 pConv->instance = pInstance;
1715 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
1716 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
1717 pConv->hwndServer = hwndServer;
1718 pConv->hwndClient = hwndClient;
1719 pConv->transactions = NULL;
1721 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
1722 /* check if both side of the conversation are of the same instance */
1723 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
1724 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
1726 pConv->wStatus |= ST_ISSELF;
1728 pConv->wConvst = XST_NULL;
1730 pConv->next = pInstance->convs[side];
1731 pInstance->convs[side] = pConv;
1736 /******************************************************************
1741 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
1742 HSZ hszService, HSZ hszTopic)
1744 WDML_CONV* pCurrent = NULL;
1746 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1748 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
1749 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
1758 /******************************************************************
1763 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
1765 WDML_CONV* pPrev = NULL;
1766 WDML_CONV* pCurrent;
1768 WDML_XACT* pXActNext;
1774 /* remove any pending transaction */
1775 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
1777 pXActNext = pXAct->next;
1778 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
1781 WDML_RemoveAllLinks(pRef->instance, pRef, side);
1783 /* FIXME: should we keep the window around ? it seems so (at least on client side
1784 * to let QueryConvInfo work after conv termination, but also to implement
1787 /* destroy conversation window, but first remove pConv from hWnd.
1788 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1790 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
1791 SetWindowLongA(hWnd, GWL_WDML_CONVERSATION, 0);
1793 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
1795 WDML_DecHSZ(pRef->instance, pRef->hszService);
1796 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
1798 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
1800 if (pCurrent == pRef)
1802 if (pCurrent == pRef->instance->convs[side])
1804 pRef->instance->convs[side] = pCurrent->next;
1808 pPrev->next = pCurrent->next;
1811 HeapFree(GetProcessHeap(), 0, pCurrent);
1817 /*****************************************************************
1818 * DdeEnableCallback (USER32.@)
1820 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
1822 FIXME("(%ld, 0x%x, %d) stub\n", idInst, hConv, wCmd);
1827 /******************************************************************
1832 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
1834 WDML_CONV* pConv = (WDML_CONV*)hConv;
1836 /* FIXME: should do better checking */
1838 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
1840 FIXME("found conv but ain't connected\n");
1843 if (GetCurrentThreadId() != pConv->instance->threadID)
1845 FIXME("wrong thread ID\n");
1852 /******************************************************************
1853 * WDML_GetConvFromWnd
1857 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
1859 return (WDML_CONV*)GetWindowLongA(hWnd, GWL_WDML_CONVERSATION);
1862 /******************************************************************
1867 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
1868 BOOL fBusy, BOOL fAck, UINT pmt, LPARAM lParam, UINT oldMsg)
1873 if (side == WDML_SERVER_SIDE)
1875 from = pConv->hwndServer;
1876 to = pConv->hwndClient;
1880 to = pConv->hwndServer;
1881 from = pConv->hwndClient;
1884 ddeAck.bAppReturnCode = appRetCode;
1885 ddeAck.reserved = 0;
1886 ddeAck.fBusy = fBusy;
1889 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
1891 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
1892 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
1893 if (!PostMessageA(to, WM_DDE_ACK, (WPARAM)from, lParam))
1895 pConv->wStatus &= ~ST_CONNECTED;
1896 FreeDDElParam(WM_DDE_ACK, lParam);
1902 /*****************************************************************
1903 * DdeSetUserHandle (USER32.@)
1905 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
1910 EnterCriticalSection(&WDML_CritSect);
1912 pConv = WDML_GetConv(hConv, FALSE);
1920 pConv->hUser = hUser;
1926 pXAct = WDML_FindTransaction(pConv, id);
1929 pXAct->hUser = hUser;
1933 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
1938 LeaveCriticalSection(&WDML_CritSect);
1942 /******************************************************************
1943 * WDML_GetLocalConvInfo
1947 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
1953 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((DWORD)pConv | 1) : 0;
1954 ci->hszSvcPartner = pConv->hszService;
1955 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
1956 ci->hszTopic = pConv->hszTopic;
1957 ci->wStatus = pConv->wStatus;
1959 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
1961 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
1963 if (pLink->hConv == (HCONV)pConv)
1965 ci->wStatus |= ST_ADVISE;
1970 /* FIXME: non handled status flags:
1976 ci->wConvst = pConv->wConvst; /* FIXME */
1978 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
1980 ci->ConvCtxt = pConv->convContext;
1981 if (ci->wStatus & ST_CLIENT)
1983 ci->hwnd = pConv->hwndClient;
1984 ci->hwndPartner = pConv->hwndServer;
1988 ci->hwnd = pConv->hwndServer;
1989 ci->hwndPartner = pConv->hwndClient;
1993 ci->hUser = pConv->hUser;
2002 pXAct = WDML_FindTransaction(pConv, id);
2005 ci->hUser = pXAct->hUser;
2006 ci->hszItem = pXAct->hszItem;
2007 ci->wFmt = pXAct->wFmt;
2008 ci->wType = pXAct->wType;
2013 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2019 /******************************************************************
2020 * DdeQueryConvInfo (USER32.@)
2023 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, LPCONVINFO lpConvInfo)
2025 UINT ret = lpConvInfo->cb;
2032 FIXME("hConv is NULL\n");
2036 EnterCriticalSection(&WDML_CritSect);
2038 pConv = WDML_GetConv(hConv, FALSE);
2039 if (pConv != NULL && !WDML_GetLocalConvInfo(pConv, &ci, id))
2043 else if ((DWORD)hConv & 1)
2045 pConv = WDML_GetConv((DWORD)hConv & ~1, FALSE);
2048 FIXME("Request on remote conversation information is not implemented yet\n");
2052 LeaveCriticalSection(&WDML_CritSect);
2054 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2058 /* ================================================================
2060 * Link (hot & warm) management
2062 * ================================================================ */
2064 /******************************************************************
2069 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2070 UINT wType, HSZ hszItem, UINT wFmt)
2074 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
2081 pLink->hConv = hConv;
2082 pLink->transactionType = wType;
2083 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
2085 pLink->next = pInstance->links[side];
2086 pInstance->links[side] = pLink;
2089 /******************************************************************
2094 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2095 HSZ hszItem, UINT uFmt)
2097 WDML_LINK* pPrev = NULL;
2098 WDML_LINK* pCurrent = NULL;
2100 pCurrent = pInstance->links[side];
2102 while (pCurrent != NULL)
2104 if (pCurrent->hConv == hConv &&
2105 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2106 pCurrent->uFmt == uFmt)
2108 if (pCurrent == pInstance->links[side])
2110 pInstance->links[side] = pCurrent->next;
2114 pPrev->next = pCurrent->next;
2117 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2118 HeapFree(GetProcessHeap(), 0, pCurrent);
2123 pCurrent = pCurrent->next;
2127 /* this function is called to remove all links related to the conv.
2128 It should be called from both client and server when terminating
2131 /******************************************************************
2132 * WDML_RemoveAllLinks
2136 void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
2138 WDML_LINK* pPrev = NULL;
2139 WDML_LINK* pCurrent = NULL;
2140 WDML_LINK* pNext = NULL;
2142 pCurrent = pInstance->links[side];
2144 while (pCurrent != NULL)
2146 if (pCurrent->hConv == (HCONV)pConv)
2148 if (pCurrent == pInstance->links[side])
2150 pInstance->links[side] = pCurrent->next;
2151 pNext = pCurrent->next;
2155 pPrev->next = pCurrent->next;
2156 pNext = pCurrent->next;
2159 WDML_DecHSZ(pInstance, pCurrent->hszItem);
2161 HeapFree(GetProcessHeap(), 0, pCurrent);
2168 pCurrent = pCurrent->next;
2177 /******************************************************************
2182 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
2183 HSZ hszItem, BOOL use_fmt, UINT uFmt)
2185 WDML_LINK* pCurrent = NULL;
2187 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2189 /* we don't need to check for transaction type as it can be altered */
2191 if (pCurrent->hConv == hConv &&
2192 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
2193 (!use_fmt || pCurrent->uFmt == uFmt))
2203 /* ================================================================
2205 * Transaction management
2207 * ================================================================ */
2209 /******************************************************************
2210 * WDML_AllocTransaction
2212 * Alloc a transaction structure for handling the message ddeMsg
2214 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
2215 UINT wFmt, HSZ hszItem)
2218 static WORD tid = 1; /* FIXME: wrap around */
2220 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
2223 pInstance->lastError = DMLERR_MEMORY_ERROR;
2227 pXAct->xActID = tid++;
2228 pXAct->ddeMsg = ddeMsg;
2229 pXAct->hDdeData = 0;
2234 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
2242 /******************************************************************
2243 * WDML_QueueTransaction
2245 * Adds a transaction to the list of transaction
2247 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2251 /* advance to last in queue */
2252 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
2256 /******************************************************************
2257 * WDML_UnQueueTransaction
2261 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
2265 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
2276 /******************************************************************
2277 * WDML_FreeTransaction
2281 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
2283 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2284 if (doFreePmt && (DWORD)pXAct->hMem > 1)
2286 GlobalFree(pXAct->hMem);
2288 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
2290 HeapFree(GetProcessHeap(), 0, pXAct);
2293 /******************************************************************
2294 * WDML_FindTransaction
2298 WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
2303 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
2305 if (pXAct->xActID == tid)
2311 /* ================================================================
2313 * Information broadcast across DDEML implementations
2315 * ================================================================ */
2317 struct tagWDML_BroadcastPmt
2325 /******************************************************************
2326 * WDML_BroadcastEnumProc
2330 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2332 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2335 if (GetClassNameA(hWnd, buffer, sizeof(buffer)) > 0 &&
2336 strcmp(buffer, s->clsName) == 0)
2338 PostMessageA(hWnd, s->uMsg, s->wParam, s->lParam);
2343 /******************************************************************
2344 * WDML_BroadcastDDEWindows
2348 void WDML_BroadcastDDEWindows(const char* clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2350 struct tagWDML_BroadcastPmt s;
2352 s.clsName = clsName;
2356 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);