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
38 #include "wine/debug.h"
39 #include "dde/dde_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
43 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
44 const char WDML_szClientConvClassA[] = "DdeClientAnsi";
45 const WCHAR WDML_szClientConvClassW[] = {'D','d','e','C','l','i','e','n','t','U','n','i','c','o','d','e',0};
47 /******************************************************************************
48 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
51 * idInst [I] Instance identifier
52 * hszService [I] Handle to service name string
53 * hszTopic [I] Handle to topic name string
54 * hConvList [I] Handle to conversation list
55 * pCC [I] Pointer to structure with context data
58 * Success: Handle to new conversation list
61 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
62 HCONVLIST hConvList, PCONVCONTEXT pCC)
64 FIXME("(%ld,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
68 /*****************************************************************
69 * DdeQueryNextServer [USER32.@]
71 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
73 FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
77 /******************************************************************************
78 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
82 * hConvList [I] Handle to conversation list
88 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
90 FIXME("(%p): stub\n", hConvList);
94 /*****************************************************************
95 * DdeConnect (USER32.@)
97 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
101 WDML_INSTANCE* pInstance;
102 WDML_CONV* pConv = NULL;
103 ATOM aSrv = 0, aTpc = 0;
105 TRACE("(0x%lx,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
107 EnterCriticalSection(&WDML_CritSect);
109 pInstance = WDML_GetInstance(idInst);
115 /* make sure this conv is never created */
116 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
119 ERR("This Conv already exists: (%p)\n", pConv);
123 /* we need to establish a conversation with
124 server, so create a window for it */
126 if (pInstance->unicode)
128 WNDCLASSEXW wndclass;
130 wndclass.cbSize = sizeof(wndclass);
132 wndclass.lpfnWndProc = WDML_ClientProc;
133 wndclass.cbClsExtra = 0;
134 wndclass.cbWndExtra = 2 * sizeof(DWORD);
135 wndclass.hInstance = 0;
137 wndclass.hCursor = 0;
138 wndclass.hbrBackground = 0;
139 wndclass.lpszMenuName = NULL;
140 wndclass.lpszClassName = WDML_szClientConvClassW;
141 wndclass.hIconSm = 0;
143 RegisterClassExW(&wndclass);
145 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
149 WNDCLASSEXA wndclass;
151 wndclass.cbSize = sizeof(wndclass);
153 wndclass.lpfnWndProc = WDML_ClientProc;
154 wndclass.cbClsExtra = 0;
155 wndclass.cbWndExtra = 2 * sizeof(DWORD);
156 wndclass.hInstance = 0;
158 wndclass.hCursor = 0;
159 wndclass.hbrBackground = 0;
160 wndclass.lpszMenuName = NULL;
161 wndclass.lpszClassName = WDML_szClientConvClassA;
162 wndclass.hIconSm = 0;
164 RegisterClassExA(&wndclass);
166 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
169 SetWindowLongA(hwndClient, GWL_WDML_INSTANCE, (DWORD)pInstance);
173 aSrv = WDML_MakeAtomFromHsz(hszService);
174 if (!aSrv) goto theEnd;
178 aTpc = WDML_MakeAtomFromHsz(hszTopic);
179 if (!aTpc) goto theEnd;
182 LeaveCriticalSection(&WDML_CritSect);
184 /* note: sent messages shall not use packing */
185 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
186 SMTO_ABORTIFHUNG, 2000, NULL );
188 EnterCriticalSection(&WDML_CritSect);
190 pInstance = WDML_GetInstance(idInst);
196 /* At this point, Client WM_DDE_ACK should have saved hwndServer
197 for this instance id and hwndClient if server responds.
198 So get HCONV and return it. And add it to conv list */
199 pConv = WDML_GetConvFromWnd(hwndClient);
200 if (pConv == NULL || pConv->hwndServer == 0)
202 ERR("Done with INITIATE, but no Server window available\n");
206 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
207 pConv->wConvst = XST_CONNECTED;
209 /* finish init of pConv */
212 pConv->convContext = *pCC;
216 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
217 pConv->convContext.cb = sizeof(pConv->convContext);
218 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
222 LeaveCriticalSection(&WDML_CritSect);
224 if (aSrv) GlobalDeleteAtom(aSrv);
225 if (aTpc) GlobalDeleteAtom(aTpc);
229 /*****************************************************************
230 * DdeReconnect (DDEML.37)
231 * DdeReconnect (USER32.@)
233 HCONV WINAPI DdeReconnect(HCONV hConv)
236 WDML_CONV* pNewConv = NULL;
237 ATOM aSrv = 0, aTpc = 0;
239 TRACE("(%p)\n", hConv);
241 EnterCriticalSection(&WDML_CritSect);
242 pConv = WDML_GetConv(hConv, FALSE);
243 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
247 /* to reestablist a connection, we have to make sure that:
248 * 1/ pConv is the converstation attached to the client window (it wouldn't be
249 * if a call to DdeReconnect would have already been done...)
250 * FIXME: is this really an error ???
251 * 2/ the pConv conversation had really been deconnected
253 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
254 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
256 HWND hwndClient = pConv->hwndClient;
257 HWND hwndServer = pConv->hwndServer;
260 SetWindowLongA(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
262 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
263 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
264 if (!aSrv || !aTpc) goto theEnd;
266 LeaveCriticalSection(&WDML_CritSect);
268 /* note: sent messages shall not use packing */
269 ret = SendMessageA(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
270 MAKELPARAM(aSrv, aTpc));
272 EnterCriticalSection(&WDML_CritSect);
274 pConv = WDML_GetConv(hConv, FALSE);
277 FIXME("Should fail reconnection\n");
281 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
283 /* re-establish all links... */
286 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
288 if (pLink->hConv == hConv)
290 /* try to reestablish the links... */
291 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
292 pLink->transactionType, 1000, NULL);
298 /* reset the conversation as it was */
299 SetWindowLongA(pConv->hwndClient, GWL_WDML_CONVERSATION, (DWORD)pConv);
305 LeaveCriticalSection(&WDML_CritSect);
307 if (aSrv) GlobalDeleteAtom(aSrv);
308 if (aTpc) GlobalDeleteAtom(aTpc);
309 return (HCONV)pNewConv;
312 /******************************************************************
313 * WDML_ClientQueueAdvise
315 * Creates and queue an WM_DDE_ADVISE transaction
317 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
319 DDEADVISE* pDdeAdvise;
323 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
325 atom = WDML_MakeAtomFromHsz(hszItem);
326 if (!atom) return NULL;
328 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
331 GlobalDeleteAtom(atom);
335 pXAct->wType = wType & ~0x0F;
336 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
337 /* FIXME: hMem is unfreed for now... should be deleted in server */
340 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
341 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
342 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
343 pDdeAdvise->cfFormat = wFmt;
344 GlobalUnlock(pXAct->hMem);
346 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
351 /******************************************************************
352 * WDML_HandleAdviseReply
354 * handles the reply to an advise request
356 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
362 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
367 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
368 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
370 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
373 GlobalDeleteAtom(uiHi);
374 FreeDDElParam(WM_DDE_ACK, msg->lParam);
376 WDML_ExtractAck(uiLo, &ddeAck);
382 /* billx: first to see if the link is already created. */
383 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
384 pXAct->hszItem, TRUE, pXAct->wFmt);
387 /* we found a link, and only need to modify it in case it changes */
388 pLink->transactionType = pXAct->wType;
392 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
393 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
395 pXAct->hDdeData = (HDDEDATA)1;
399 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
400 GlobalFree(pXAct->hMem);
401 pXAct->hDdeData = NULL;
404 return WDML_QS_HANDLED;
407 /******************************************************************
408 * WDML_ClientQueueUnadvise
410 * queues an unadvise transaction
412 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
417 TRACE("XTYP_ADVSTOP transaction\n");
419 atom = WDML_MakeAtomFromHsz(hszItem);
420 if (!atom) return NULL;
422 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
425 GlobalDeleteAtom(atom);
429 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
430 * on the specified item.
432 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
436 /******************************************************************
437 * WDML_HandleUnadviseReply
441 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
447 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
452 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
453 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
455 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
458 FreeDDElParam(WM_DDE_ACK, msg->lParam);
459 GlobalDeleteAtom(uiHi);
461 WDML_ExtractAck(uiLo, &ddeAck);
463 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
467 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
468 pXAct->hDdeData = NULL;
472 /* billx: remove the link */
473 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
474 pXAct->hszItem, pXAct->wFmt);
475 pXAct->hDdeData = (HDDEDATA)1;
477 return WDML_QS_HANDLED;
480 /******************************************************************
481 * WDML_ClientQueueRequest
485 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
490 TRACE("XTYP_REQUEST transaction\n");
492 atom = WDML_MakeAtomFromHsz(hszItem);
493 if (!atom) return NULL;
495 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
498 GlobalDeleteAtom(atom);
502 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
507 /******************************************************************
508 * WDML_HandleRequestReply
512 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
519 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
522 switch (msg->message)
525 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
526 FreeDDElParam(WM_DDE_ACK, msg->lParam);
527 GlobalDeleteAtom(uiHi);
528 WDML_ExtractAck(uiLo, &ddeAck);
531 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
532 TRACE("Negative answer...\n");
536 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
537 TRACE("Got the result (%08x)\n", uiLo);
539 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
541 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
544 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
547 GlobalFree((HGLOBAL)uiLo);
551 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
555 GlobalDeleteAtom(uiHi);
556 FreeDDElParam(WM_DDE_ACK, msg->lParam);
561 FreeDDElParam(msg->message, msg->lParam);
565 return WDML_QS_HANDLED;
568 /******************************************************************
569 * WDML_BuildExecuteCommand
571 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
572 * It also takes care of string conversion between the two window procedures
574 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
577 BOOL clientUnicode, serverUnicode;
580 clientUnicode = IsWindowUnicode(pConv->hwndClient);
581 serverUnicode = IsWindowUnicode(pConv->hwndServer);
583 if (clientUnicode == serverUnicode)
591 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
595 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0);
599 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
605 pDst = GlobalLock(hMem);
608 if (clientUnicode == serverUnicode)
610 memcpy(pDst, pData, cbData);
616 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
620 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize);
635 /******************************************************************
636 * WDML_ClientQueueExecute
640 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
644 TRACE("XTYP_EXECUTE transaction\n");
646 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
650 if (cbData == (DWORD)-1)
652 HDDEDATA hDdeData = (HDDEDATA)pData;
654 pData = DdeAccessData(hDdeData, &cbData);
657 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
658 DdeUnaccessData(hDdeData);
663 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
666 pXAct->lParam = (LPARAM)pXAct->hMem;
671 /******************************************************************
672 * WDML_HandleExecuteReply
676 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
681 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
686 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
687 FreeDDElParam(WM_DDE_ACK, msg->lParam);
689 if ((HANDLE)uiHi != pXAct->hMem)
694 WDML_ExtractAck(uiLo, &ddeAck);
695 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
697 return WDML_QS_HANDLED;
700 /******************************************************************
701 * WDML_ClientQueuePoke
705 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
706 UINT wFmt, HSZ hszItem)
711 TRACE("XTYP_POKE transaction\n");
713 atom = WDML_MakeAtomFromHsz(hszItem);
714 if (!atom) return NULL;
716 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
719 GlobalDeleteAtom(atom);
723 if (cbData == (DWORD)-1)
725 pXAct->hMem = (HDDEDATA)pData;
731 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
732 ddePoke = GlobalLock(pXAct->hMem);
735 memcpy(ddePoke->Value, pData, cbData);
736 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
737 ddePoke->cfFormat = wFmt;
738 GlobalUnlock(pXAct->hMem);
742 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
747 /******************************************************************
748 * WDML_HandlePokeReply
752 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
758 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
763 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
764 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
765 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
769 FreeDDElParam(WM_DDE_ACK, msg->lParam);
770 GlobalDeleteAtom(uiHi);
772 WDML_ExtractAck(uiLo, &ddeAck);
773 GlobalFree(pXAct->hMem);
775 pXAct->hDdeData = (HDDEDATA)TRUE;
779 /******************************************************************
780 * WDML_ClientQueueTerminate
782 * Creates and queue an WM_DDE_TERMINATE transaction
784 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
788 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
793 pConv->wStatus &= ~ST_CONNECTED;
798 /******************************************************************
799 * WDML_HandleTerminateReply
801 * handles the reply to a terminate request
803 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
805 if (msg->message != WM_DDE_TERMINATE)
807 /* FIXME: should delete data passed here */
808 return WDML_QS_SWALLOWED;
811 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
813 FIXME("hmmm shouldn't happen\n");
816 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
818 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
819 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
821 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
822 return WDML_QS_HANDLED;
825 /******************************************************************
826 * WDML_HandleReplyData
830 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
833 HDDEDATA hDdeDataIn, hDdeDataOut;
838 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
839 /* wParam -- sending window handle */
840 /* lParam -- hDdeData & item HSZ */
842 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
843 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
845 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
848 * For hot link, data should be passed to its callback with
849 * XTYP_ADVDATA and callback should return the proper status.
851 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
852 uiLo ? TRUE : FALSE, wdh.cfFormat);
855 WDML_DecHSZ(pConv->instance, hsz);
856 DdeFreeDataHandle(hDdeDataIn);
860 if (hDdeDataIn != 0 && wdh.fAckReq)
862 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
868 GlobalDeleteAtom(uiHi);
871 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
872 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
874 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
876 if (uiLo) GlobalFree((HANDLE)uiLo);
879 DdeFreeDataHandle(hDdeDataIn);
881 WDML_DecHSZ(pConv->instance, hsz);
883 FreeDDElParam(WM_DDE_DATA, msg->lParam);
885 return WDML_QS_HANDLED;
888 /******************************************************************
889 * WDML_HandleIncomingTerminate
893 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
895 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
898 pConv->wStatus |= ST_TERMINATED;
899 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
901 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
902 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
904 if (pConv->wStatus & ST_CONNECTED)
906 /* don't care about result code (if server exists or not) */
907 PostMessageA(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
908 pConv->wStatus &= ~ST_CONNECTED;
910 /* have to keep connection around to allow reconnection */
911 return WDML_QS_HANDLED;
914 /******************************************************************
917 * handles any incoming reply, and try to match to an already sent request
919 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
921 WDML_XACT* pXAct = pConv->transactions;
924 if (pConv->transactions)
926 /* first check message against a pending transaction, if any */
927 switch (pXAct->ddeMsg)
930 qs = WDML_HandleAdviseReply(pConv, msg, pXAct);
932 case WM_DDE_UNADVISE:
933 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct);
936 qs = WDML_HandleExecuteReply(pConv, msg, pXAct);
939 qs = WDML_HandleRequestReply(pConv, msg, pXAct);
942 qs = WDML_HandlePokeReply(pConv, msg, pXAct);
944 case WM_DDE_TERMINATE:
945 qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
957 /* now check the results */
961 case WDML_QS_SWALLOWED:
964 case WDML_QS_HANDLED:
965 /* ok, we have resolved a pending transaction
966 * notify callback if asynchronous, and remove it in any case
968 WDML_UnQueueTransaction(pConv, pXAct);
969 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
971 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
972 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
973 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
978 *hdd = pXAct->hDdeData;
980 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
983 /* no pending transaction found, try a warm/hot link or a termination request */
984 switch (msg->message)
987 qs = WDML_HandleIncomingData(pConv, msg, hdd);
989 case WM_DDE_TERMINATE:
990 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
995 FIXME("shouldn't be used on client side\n");
1002 /******************************************************************
1003 * WDML_SyncWaitTransactionReply
1005 * waits until an answer for a sent request is received
1006 * time out is also handled. only used for synchronous transactions
1008 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct)
1014 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
1016 /* FIXME: time 32 bit wrap around */
1017 dwTimeout += GetCurrentTime();
1019 while ((dwTime = GetCurrentTime()) < dwTimeout)
1021 /* we cannot be in the crit sect all the time because when client and server run in a
1022 * single process they need to share the access to the internal data
1024 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1025 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1032 EnterCriticalSection(&WDML_CritSect);
1034 pConv = WDML_GetConv(hConv, FALSE);
1037 LeaveCriticalSection(&WDML_CritSect);
1038 /* conversation no longer available... return failure */
1041 while (PeekMessageA(&msg, pConv->hwndClient, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1043 /* check that either pXAct has been processed or no more xActions are pending */
1044 ret = (pConv->transactions == pXAct);
1045 ret = WDML_HandleReply(pConv, &msg, &hdd) == WDML_QS_HANDLED &&
1046 (pConv->transactions == NULL || ret);
1049 LeaveCriticalSection(&WDML_CritSect);
1057 TRACE("Timeout !!\n");
1059 EnterCriticalSection(&WDML_CritSect);
1061 pConv = WDML_GetConv(hConv, FALSE);
1064 if (pConv->transactions)
1066 switch (pConv->transactions->ddeMsg)
1068 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1069 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1070 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1071 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1072 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1073 default: err = DMLERR_INVALIDPARAMETER; break;
1076 pConv->instance->lastError = err;
1079 LeaveCriticalSection(&WDML_CritSect);
1084 /*****************************************************************
1085 * DdeClientTransaction (USER32.@)
1087 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1088 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1092 HDDEDATA hDdeData = 0;
1094 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1095 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1099 ERR("Invalid conversation handle\n");
1103 EnterCriticalSection(&WDML_CritSect);
1105 pConv = WDML_GetConv(hConv, TRUE);
1108 /* cannot set error... cannot get back to DDE instance */
1115 if (hszItem != 0 || wFmt != 0)
1117 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1120 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1123 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1125 case XTYP_ADVSTART|XTYPF_NODATA:
1126 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1128 case XTYP_ADVSTART|XTYPF_ACKREQ:
1131 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1134 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1139 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1142 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1147 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1150 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1153 FIXME("Unknown transation\n");
1154 /* unknown transaction type */
1155 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1161 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1165 WDML_QueueTransaction(pConv, pXAct);
1167 if (!PostMessageA(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1169 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1170 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1171 pConv->wStatus &= ~ST_CONNECTED;
1172 WDML_UnQueueTransaction(pConv, pXAct);
1173 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1176 pXAct->dwTimeout = dwTimeout;
1177 /* FIXME: should set the app bits on *pdwResult */
1179 if (dwTimeout == TIMEOUT_ASYNC)
1183 *pdwResult = MAKELONG(0, pXAct->xActID);
1185 hDdeData = (HDDEDATA)1;
1195 count = WDML_CritSect.RecursionCount;
1196 for (i = 0; i < count; i++)
1197 LeaveCriticalSection(&WDML_CritSect);
1198 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct);
1199 for (i = 0; i < count; i++)
1200 EnterCriticalSection(&WDML_CritSect);
1202 LeaveCriticalSection(&WDML_CritSect);
1206 LeaveCriticalSection(&WDML_CritSect);
1210 /*****************************************************************
1211 * DdeAbandonTransaction (USER32.@)
1213 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1215 WDML_INSTANCE* pInstance;
1219 TRACE("(%08lx,%p,%08ld);\n", idInst, hConv, idTransaction);
1221 EnterCriticalSection(&WDML_CritSect);
1222 if ((pInstance = WDML_GetInstance(idInst)))
1226 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1228 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1230 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1231 (idTransaction == 0 || pXAct->xActID == idTransaction))
1233 WDML_UnQueueTransaction(pConv, pXAct);
1234 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1241 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1243 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1244 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1246 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1248 WDML_UnQueueTransaction(pConv, pXAct);
1249 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1255 LeaveCriticalSection(&WDML_CritSect);
1260 /******************************************************************
1263 * Window Proc created on client side for each conversation
1265 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1268 WDML_CONV* pConv = NULL;
1271 if (iMsg == WM_DDE_ACK &&
1272 /* in the initial WM_INITIATE sendmessage */
1273 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1275 /* In response to WM_DDE_INITIATE, save server window */
1277 WDML_INSTANCE* pInstance;
1279 /* note: sent messages do not need packing */
1280 uiLo = LOWORD(lParam);
1281 uiHi = HIWORD(lParam);
1283 /* FIXME: convlist should be handled here */
1286 /* we already have started the conv with a server, drop other replies */
1287 GlobalDeleteAtom(uiLo);
1288 GlobalDeleteAtom(uiHi);
1289 PostMessageA((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1293 pInstance = WDML_GetInstanceFromWnd(hwnd);
1295 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1296 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1298 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1300 SetWindowLongA(hwnd, GWL_WDML_CONVERSATION, (DWORD)pConv);
1301 pConv->wStatus |= ST_CONNECTED;
1302 pConv->wConvst = XST_INIT1;
1304 /* check if server is handled by DDEML */
1305 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1306 strcmp(buf, WDML_szServerConvClassA) == 0) ||
1307 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1308 lstrcmpW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1310 pConv->wStatus |= ST_ISLOCAL;
1313 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1315 GlobalDeleteAtom(uiLo);
1316 GlobalDeleteAtom(uiHi);
1318 /* accept conversation */
1322 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1324 EnterCriticalSection(&WDML_CritSect);
1326 pConv = WDML_GetConvFromWnd(hwnd);
1335 msg.wParam = wParam;
1336 msg.lParam = lParam;
1338 WDML_HandleReply(pConv, &msg, &hdd);
1341 LeaveCriticalSection(&WDML_CritSect);
1345 return (IsWindowUnicode(hwnd)) ?
1346 DefWindowProcW(hwnd, iMsg, wParam, lParam) : DefWindowProcA(hwnd, iMsg, wParam, lParam);
1349 /*****************************************************************
1350 * DdeDisconnect (USER32.@)
1352 BOOL WINAPI DdeDisconnect(HCONV hConv)
1354 WDML_CONV* pConv = NULL;
1359 TRACE("(%p)\n", hConv);
1363 ERR("DdeDisconnect(): hConv = 0\n");
1367 EnterCriticalSection(&WDML_CritSect);
1368 pConv = WDML_GetConv(hConv, TRUE);
1371 if (pConv->wStatus & ST_CLIENT)
1373 /* FIXME: should abandon all pending transactions */
1374 pXAct = WDML_ClientQueueTerminate(pConv);
1377 count = WDML_CritSect.RecursionCount;
1378 for (i = 0; i < count; i++)
1379 LeaveCriticalSection(&WDML_CritSect);
1380 if (PostMessageA(pConv->hwndServer, pXAct->ddeMsg,
1381 (WPARAM)pConv->hwndClient, pXAct->lParam))
1382 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct);
1383 for (i = 0; i < count; i++)
1384 EnterCriticalSection(&WDML_CritSect);
1386 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1387 /* still have to destroy data assosiated with conversation */
1388 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1392 FIXME("Not implemented yet for a server side conversation\n");
1396 LeaveCriticalSection(&WDML_CritSect);
1401 /*****************************************************************
1402 * DdeImpersonateClient (USER32.@)
1404 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1409 TRACE("(%p)\n", hConv);
1411 EnterCriticalSection(&WDML_CritSect);
1412 pConv = WDML_GetConv(hConv, TRUE);
1415 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1417 LeaveCriticalSection(&WDML_CritSect);