4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
8 * Copyright 2001 Eric Pouech
9 * Copyright 2004, 2005 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
37 #include "dde_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
41 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
42 const char WDML_szClientConvClassA[] = "WineDdeClientA";
43 const WCHAR WDML_szClientConvClassW[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t','W',0};
45 /******************************************************************************
46 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
49 * idInst [I] Instance identifier
50 * hszService [I] Handle to service name string
51 * hszTopic [I] Handle to topic name string
52 * hConvList [I] Handle to conversation list
53 * pCC [I] Pointer to structure with context data
56 * Success: Handle to new conversation list
59 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
60 HCONVLIST hConvList, PCONVCONTEXT pCC)
62 FIXME("(%d,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
66 /*****************************************************************
67 * DdeQueryNextServer [USER32.@]
69 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
71 FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
75 /******************************************************************************
76 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
80 * hConvList [I] Handle to conversation list
86 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
88 FIXME("(%p): stub\n", hConvList);
92 /*****************************************************************
93 * DdeConnect (USER32.@)
95 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
99 WDML_INSTANCE* pInstance;
100 WDML_CONV* pConv = NULL;
101 ATOM aSrv = 0, aTpc = 0;
103 TRACE("(0x%x,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
105 pInstance = WDML_GetInstance(idInst);
109 /* make sure this conv is never created */
110 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
113 ERR("This Conv already exists: (%p)\n", pConv);
117 /* we need to establish a conversation with
118 server, so create a window for it */
120 if (pInstance->unicode)
122 WNDCLASSEXW wndclass;
124 wndclass.cbSize = sizeof(wndclass);
126 wndclass.lpfnWndProc = WDML_ClientProc;
127 wndclass.cbClsExtra = 0;
128 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
129 wndclass.hInstance = 0;
131 wndclass.hCursor = 0;
132 wndclass.hbrBackground = 0;
133 wndclass.lpszMenuName = NULL;
134 wndclass.lpszClassName = WDML_szClientConvClassW;
135 wndclass.hIconSm = 0;
137 RegisterClassExW(&wndclass);
139 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
143 WNDCLASSEXA wndclass;
145 wndclass.cbSize = sizeof(wndclass);
147 wndclass.lpfnWndProc = WDML_ClientProc;
148 wndclass.cbClsExtra = 0;
149 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
150 wndclass.hInstance = 0;
152 wndclass.hCursor = 0;
153 wndclass.hbrBackground = 0;
154 wndclass.lpszMenuName = NULL;
155 wndclass.lpszClassName = WDML_szClientConvClassA;
156 wndclass.hIconSm = 0;
158 RegisterClassExA(&wndclass);
160 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
163 SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
167 aSrv = WDML_MakeAtomFromHsz(hszService);
168 if (!aSrv) goto theEnd;
172 aTpc = WDML_MakeAtomFromHsz(hszTopic);
173 if (!aTpc) goto theEnd;
176 /* note: sent messages shall not use packing */
177 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
178 SMTO_ABORTIFHUNG, 2000, NULL );
180 pInstance = WDML_GetInstance(idInst);
186 /* At this point, Client WM_DDE_ACK should have saved hwndServer
187 for this instance id and hwndClient if server responds.
188 So get HCONV and return it. And add it to conv list */
189 pConv = WDML_GetConvFromWnd(hwndClient);
190 if (pConv == NULL || pConv->hwndServer == 0)
192 WARN("Done with INITIATE, but no Server window available\n");
196 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
197 pConv->wConvst = XST_CONNECTED;
199 /* finish init of pConv */
202 pConv->convContext = *pCC;
206 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
207 pConv->convContext.cb = sizeof(pConv->convContext);
208 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
213 if (aSrv) GlobalDeleteAtom(aSrv);
214 if (aTpc) GlobalDeleteAtom(aTpc);
218 /*****************************************************************
219 * DdeReconnect (DDEML.37)
220 * DdeReconnect (USER32.@)
222 HCONV WINAPI DdeReconnect(HCONV hConv)
225 WDML_CONV* pNewConv = NULL;
226 ATOM aSrv = 0, aTpc = 0;
228 TRACE("(%p)\n", hConv);
230 pConv = WDML_GetConv(hConv, FALSE);
231 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
235 /* to reestablist a connection, we have to make sure that:
236 * 1/ pConv is the converstation attached to the client window (it wouldn't be
237 * if a call to DdeReconnect would have already been done...)
238 * FIXME: is this really an error ???
239 * 2/ the pConv conversation had really been deconnected
241 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
242 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
244 HWND hwndClient = pConv->hwndClient;
245 HWND hwndServer = pConv->hwndServer;
247 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
249 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
250 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
251 if (!aSrv || !aTpc) goto theEnd;
253 /* note: sent messages shall not use packing */
254 ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
255 MAKELPARAM(aSrv, aTpc));
257 pConv = WDML_GetConv(hConv, FALSE);
260 FIXME("Should fail reconnection\n");
264 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
266 /* re-establish all links... */
269 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
271 if (pLink->hConv == hConv)
273 /* try to reestablish the links... */
274 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
275 pLink->transactionType, 1000, NULL);
281 /* reset the conversation as it was */
282 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
289 if (aSrv) GlobalDeleteAtom(aSrv);
290 if (aTpc) GlobalDeleteAtom(aTpc);
291 return (HCONV)pNewConv;
294 /******************************************************************
295 * WDML_ClientQueueAdvise
297 * Creates and queue an WM_DDE_ADVISE transaction
299 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
301 DDEADVISE* pDdeAdvise;
305 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
307 atom = WDML_MakeAtomFromHsz(hszItem);
308 if (!atom) return NULL;
310 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
313 GlobalDeleteAtom(atom);
317 pXAct->wType = wType & ~0x0F;
318 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
319 /* FIXME: hMem is unfreed for now... should be deleted in server */
322 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
323 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
324 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
325 pDdeAdvise->cfFormat = wFmt;
326 GlobalUnlock(pXAct->hMem);
328 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
333 /******************************************************************
334 * WDML_HandleAdviseReply
336 * handles the reply to an advise request
338 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
344 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
349 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
350 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
352 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
355 GlobalDeleteAtom(uiHi);
356 FreeDDElParam(WM_DDE_ACK, msg->lParam);
358 if (ack) *ack = uiLo;
359 WDML_ExtractAck(uiLo, &ddeAck);
365 /* billx: first to see if the link is already created. */
366 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
367 pXAct->hszItem, TRUE, pXAct->wFmt);
370 /* we found a link, and only need to modify it in case it changes */
371 pLink->transactionType = pXAct->wType;
375 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
376 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
378 pXAct->hDdeData = (HDDEDATA)1;
382 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
383 GlobalFree(pXAct->hMem);
384 pXAct->hDdeData = NULL;
387 return WDML_QS_HANDLED;
390 /******************************************************************
391 * WDML_ClientQueueUnadvise
393 * queues an unadvise transaction
395 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
400 TRACE("XTYP_ADVSTOP transaction\n");
402 atom = WDML_MakeAtomFromHsz(hszItem);
403 if (!atom) return NULL;
405 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
408 GlobalDeleteAtom(atom);
412 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
413 * on the specified item.
415 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
419 /******************************************************************
420 * WDML_HandleUnadviseReply
424 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
430 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
435 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
436 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
438 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
441 FreeDDElParam(WM_DDE_ACK, msg->lParam);
442 GlobalDeleteAtom(uiHi);
444 if (ack) *ack = uiLo;
445 WDML_ExtractAck(uiLo, &ddeAck);
447 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
451 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
452 pXAct->hDdeData = NULL;
456 /* billx: remove the link */
457 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
458 pXAct->hszItem, pXAct->wFmt);
459 pXAct->hDdeData = (HDDEDATA)1;
461 return WDML_QS_HANDLED;
464 /******************************************************************
465 * WDML_ClientQueueRequest
469 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
474 TRACE("XTYP_REQUEST transaction\n");
476 atom = WDML_MakeAtomFromHsz(hszItem);
477 if (!atom) return NULL;
479 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
482 GlobalDeleteAtom(atom);
486 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
491 /******************************************************************
492 * WDML_HandleRequestReply
496 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
503 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
506 switch (msg->message)
509 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
510 FreeDDElParam(WM_DDE_ACK, msg->lParam);
511 GlobalDeleteAtom(uiHi);
512 if (ack) *ack = uiLo;
513 WDML_ExtractAck(uiLo, &ddeAck);
516 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
517 TRACE("Negative answer...\n");
521 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
522 TRACE("Got the result (%08lx)\n", uiLo);
524 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
526 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
529 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
532 GlobalFree((HGLOBAL)uiLo);
536 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
540 GlobalDeleteAtom(uiHi);
541 FreeDDElParam(WM_DDE_ACK, msg->lParam);
546 FreeDDElParam(msg->message, msg->lParam);
550 return WDML_QS_HANDLED;
553 /******************************************************************
554 * WDML_BuildExecuteCommand
556 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
557 * It also takes care of string conversion between the two window procedures
559 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
562 BOOL clientUnicode, serverUnicode;
565 clientUnicode = pConv->instance->unicode;
566 TRACE("client %p uses unicode = %d\n", pConv->hwndClient, clientUnicode);
567 /* FIXME: how exactly Windows determines what to use for the server side? */
568 serverUnicode = IsWindowUnicode(pConv->hwndServer) && IsWindowUnicode(pConv->hwndClient);
569 TRACE("server %p uses unicode = %d\n", pConv->hwndServer, serverUnicode);
571 if (clientUnicode == serverUnicode)
579 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
583 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
587 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
593 pDst = GlobalLock(hMem);
596 if (clientUnicode == serverUnicode)
598 memcpy(pDst, pData, cbData);
604 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
608 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
623 /******************************************************************
624 * WDML_ClientQueueExecute
628 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPVOID pData, DWORD cbData)
632 TRACE("XTYP_EXECUTE transaction\n");
634 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
638 if (cbData == (DWORD)-1)
640 HDDEDATA hDdeData = pData;
642 pData = DdeAccessData(hDdeData, &cbData);
645 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
646 DdeUnaccessData(hDdeData);
651 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
654 pXAct->lParam = (LPARAM)pXAct->hMem;
659 /******************************************************************
660 * WDML_HandleExecuteReply
664 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
669 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
674 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
675 FreeDDElParam(WM_DDE_ACK, msg->lParam);
677 if ((HANDLE)uiHi != pXAct->hMem)
682 if (ack) *ack = uiLo;
683 WDML_ExtractAck(uiLo, &ddeAck);
684 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
686 TRACE("hDdeData = %p\n", pXAct->hDdeData);
687 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
689 return WDML_QS_HANDLED;
692 /******************************************************************
693 * WDML_ClientQueuePoke
697 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPVOID pData, DWORD cbData,
698 UINT wFmt, HSZ hszItem)
700 DDE_DATAHANDLE_HEAD *dh;
706 TRACE("XTYP_POKE transaction\n");
708 atom = WDML_MakeAtomFromHsz(hszItem);
709 if (!atom) return NULL;
711 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
714 GlobalDeleteAtom(atom);
718 if (cbData == (DWORD)-1)
720 hglobal = (HGLOBAL)pData;
721 dh = (DDE_DATAHANDLE_HEAD *)GlobalLock(hglobal);
722 cbData = GlobalSize(hglobal) - sizeof(DDE_DATAHANDLE_HEAD);
723 pData = (LPVOID)(dh + 1);
724 GlobalUnlock(hglobal);
727 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
728 ddePoke = GlobalLock(pXAct->hMem);
731 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
736 ddePoke->fRelease = TRUE;
737 ddePoke->cfFormat = wFmt;
738 memcpy(ddePoke->Value, pData, cbData);
739 GlobalUnlock(pXAct->hMem);
741 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
746 /******************************************************************
747 * WDML_HandlePokeReply
751 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
756 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
761 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
762 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
763 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
767 FreeDDElParam(WM_DDE_ACK, msg->lParam);
768 GlobalDeleteAtom(uiHi);
770 if (ack) *ack = uiLo;
771 GlobalFree(pXAct->hMem);
773 pXAct->hDdeData = (HDDEDATA)TRUE;
777 /******************************************************************
778 * WDML_ClientQueueTerminate
780 * Creates and queue an WM_DDE_TERMINATE transaction
782 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
786 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
791 pConv->wStatus &= ~ST_CONNECTED;
796 /******************************************************************
797 * WDML_HandleTerminateReply
799 * handles the reply to a terminate request
801 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg)
803 if (msg->message != WM_DDE_TERMINATE)
805 /* FIXME: should delete data passed here */
806 return WDML_QS_SWALLOWED;
809 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
811 FIXME("hmmm shouldn't happen\n");
814 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
816 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
817 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
819 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
820 return WDML_QS_HANDLED;
823 /******************************************************************
824 * WDML_HandleIncomingData
828 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
831 HDDEDATA hDdeDataIn, hDdeDataOut;
836 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
837 /* wParam -- sending window handle */
838 /* lParam -- hDdeData & item HSZ */
840 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
841 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
843 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
846 * For hot link, data should be passed to its callback with
847 * XTYP_ADVDATA and callback should return the proper status.
849 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
850 uiLo ? TRUE : FALSE, wdh.cfFormat);
853 WDML_DecHSZ(pConv->instance, hsz);
854 DdeFreeDataHandle(hDdeDataIn);
858 if (hDdeDataIn != 0 && wdh.fAckReq)
860 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
866 GlobalDeleteAtom(uiHi);
869 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
870 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
872 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
874 if (uiLo) GlobalFree((HANDLE)uiLo);
877 DdeFreeDataHandle(hDdeDataIn);
879 WDML_DecHSZ(pConv->instance, hsz);
881 FreeDDElParam(WM_DDE_DATA, msg->lParam);
883 return WDML_QS_HANDLED;
886 /******************************************************************
887 * WDML_HandleIncomingTerminate
891 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
893 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
896 pConv->wStatus |= ST_TERMINATED;
897 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
899 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
900 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
902 if (pConv->wStatus & ST_CONNECTED)
904 /* don't care about result code (if server exists or not) */
905 PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
906 pConv->wStatus &= ~ST_CONNECTED;
908 /* have to keep connection around to allow reconnection */
909 return WDML_QS_HANDLED;
912 /******************************************************************
915 * handles any incoming reply, and try to match to an already sent request
917 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd, DWORD *ack)
919 WDML_XACT* pXAct = pConv->transactions;
922 if (pConv->transactions)
924 /* first check message against a pending transaction, if any */
925 switch (pXAct->ddeMsg)
928 qs = WDML_HandleAdviseReply(pConv, msg, pXAct, ack);
930 case WM_DDE_UNADVISE:
931 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct, ack);
934 qs = WDML_HandleExecuteReply(pConv, msg, pXAct, ack);
937 qs = WDML_HandleRequestReply(pConv, msg, pXAct, ack);
940 qs = WDML_HandlePokeReply(pConv, msg, pXAct, ack);
942 case WM_DDE_TERMINATE:
943 qs = WDML_HandleTerminateReply(pConv, msg);
955 /* now check the results */
959 case WDML_QS_SWALLOWED:
962 case WDML_QS_HANDLED:
963 /* ok, we have resolved a pending transaction
964 * notify callback if asynchronous.
966 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
968 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
969 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
970 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
975 *hdd = pXAct->hDdeData;
979 /* no pending transaction found, try a warm/hot link or a termination request */
980 switch (msg->message)
983 qs = WDML_HandleIncomingData(pConv, msg, hdd);
985 case WM_DDE_TERMINATE:
986 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
989 /* This happens at end of DdeClientTransaction XTYP_EXECUTE
990 * Without this assignment, DdeClientTransaction's return value is undefined
991 * See also http://support.microsoft.com/kb/102574
993 *hdd = (HDDEDATA)TRUE;
1000 FIXME("shouldn't be used on client side\n");
1007 /******************************************************************
1008 * WDML_SyncWaitTransactionReply
1010 * waits until an answer for a sent request is received
1011 * time out is also handled. only used for synchronous transactions
1013 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, const WDML_XACT* pXAct, DWORD *ack)
1019 TRACE("Starting wait for a timeout of %d ms\n", dwTimeout);
1021 /* FIXME: time 32 bit wrap around */
1022 dwTimeout += GetCurrentTime();
1024 while ((dwTime = GetCurrentTime()) < dwTimeout)
1026 /* we cannot be in the crit sect all the time because when client and server run in a
1027 * single process they need to share the access to the internal data
1029 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1030 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1034 while (PeekMessageW(&msg, 0, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1038 pConv = WDML_GetConv(hConv, FALSE);
1041 /* conversation no longer available... return failure */
1044 if (msg.hwnd == pConv->hwndClient)
1046 /* check that either pXAct has been processed or no more xActions are pending */
1047 BOOL ret = (pConv->transactions == pXAct);
1048 if (WDML_HandleReply(pConv, &msg, &hdd, ack) == WDML_QS_HANDLED)
1050 TRACE("WDML_HandleReply returned WDML_QS_HANDLED\n");
1054 ret = (pConv->transactions == NULL || ret);
1058 pConv->instance->lastError = hdd ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
1064 DispatchMessageW(&msg);
1070 TRACE("Timeout !!\n");
1072 pConv = WDML_GetConv(hConv, FALSE);
1075 if (pConv->transactions)
1077 switch (pConv->transactions->ddeMsg)
1079 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1080 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1081 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1082 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1083 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1084 default: err = DMLERR_INVALIDPARAMETER; break;
1087 pConv->instance->lastError = err;
1095 /*****************************************************************
1098 HDDEDATA WDML_ClientHandle(WDML_CONV *pConv, WDML_XACT *pXAct, DWORD dwTimeout, LPDWORD pdwResult)
1102 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1104 WARN("Failed posting message %x to %p (error=0x%x)\n",
1105 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1106 pConv->wStatus &= ~ST_CONNECTED;
1107 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1110 pXAct->dwTimeout = dwTimeout;
1111 /* FIXME: should set the app bits on *pdwResult */
1113 if (dwTimeout == TIMEOUT_ASYNC)
1116 *pdwResult = MAKELONG(0, pXAct->xActID);
1118 hDdeData = (HDDEDATA)1;
1121 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct, pdwResult);
1127 /*****************************************************************
1128 * DdeClientTransaction (USER32.@)
1130 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1131 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1135 HDDEDATA hDdeData = 0;
1137 TRACE("(%p,%d,%p,%p,%x,%x,%d,%p)\n",
1138 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1142 WARN("Invalid conversation handle NULL\n");
1146 pConv = WDML_GetConv(hConv, TRUE);
1149 /* cannot set error... cannot get back to DDE instance */
1156 /* Windows simply ignores hszItem and wFmt in this case */
1157 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1162 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1165 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1167 case XTYP_ADVSTART|XTYPF_NODATA:
1168 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1170 case XTYP_ADVSTART|XTYPF_ACKREQ:
1173 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1176 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1181 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1184 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1187 if (pData || !hszItem)
1189 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1192 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1195 FIXME("Unknown transaction type %04x\n", wType);
1196 /* unknown transaction type */
1197 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1203 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1207 WDML_QueueTransaction(pConv, pXAct);
1209 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1211 if (pConv->wStatus & ST_BLOCKED)
1213 TRACE("Transactions are blocked, add to the queue and exit\n");
1217 hDdeData = WDML_ClientHandle(pConv, pXAct, dwTimeout, pdwResult);
1218 if (dwTimeout != TIMEOUT_ASYNC)
1220 WDML_UnQueueTransaction(pConv, pXAct);
1221 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1227 /*****************************************************************
1228 * DdeAbandonTransaction (USER32.@)
1230 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1232 WDML_INSTANCE* pInstance;
1236 if ((pInstance = WDML_GetInstance(idInst)))
1240 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1242 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1244 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1245 (idTransaction == 0 || pXAct->xActID == idTransaction))
1247 WDML_UnQueueTransaction(pConv, pXAct);
1248 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1255 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1257 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1258 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1260 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1262 WDML_UnQueueTransaction(pConv, pXAct);
1263 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1273 /******************************************************************
1276 * Window Proc created on client side for each conversation
1278 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1281 WDML_CONV* pConv = NULL;
1284 TRACE("%p %04x %08lx %08lx\n", hwnd, iMsg, wParam , lParam);
1286 if (iMsg == WM_DDE_ACK &&
1287 /* in the initial WM_INITIATE sendmessage */
1288 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1290 /* In response to WM_DDE_INITIATE, save server window */
1292 WDML_INSTANCE* pInstance;
1294 /* note: sent messages do not need packing */
1295 uiLo = LOWORD(lParam);
1296 uiHi = HIWORD(lParam);
1298 /* FIXME: convlist should be handled here */
1301 /* we already have started the conv with a server, drop other replies */
1302 GlobalDeleteAtom(uiLo);
1303 GlobalDeleteAtom(uiHi);
1304 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1308 pInstance = WDML_GetInstanceFromWnd(hwnd);
1310 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1311 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1313 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1315 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1316 pConv->wStatus |= ST_CONNECTED;
1317 pConv->wConvst = XST_INIT1;
1319 /* check if server is handled by DDEML */
1320 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1321 lstrcmpiA(buf, WDML_szServerConvClassA) == 0) ||
1322 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1323 lstrcmpiW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1325 pConv->wStatus |= ST_ISLOCAL;
1328 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1330 GlobalDeleteAtom(uiLo);
1331 GlobalDeleteAtom(uiHi);
1333 /* accept conversation */
1337 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1339 pConv = WDML_GetConvFromWnd(hwnd);
1348 msg.wParam = wParam;
1349 msg.lParam = lParam;
1351 WDML_HandleReply(pConv, &msg, &hdd, NULL);
1357 return IsWindowUnicode(hwnd) ? DefWindowProcW(hwnd, iMsg, wParam, lParam) :
1358 DefWindowProcA(hwnd, iMsg, wParam, lParam);
1361 /*****************************************************************
1362 * DdeDisconnect (USER32.@)
1364 BOOL WINAPI DdeDisconnect(HCONV hConv)
1366 WDML_CONV* pConv = NULL;
1370 TRACE("(%p)\n", hConv);
1374 WARN("DdeDisconnect(): hConv = 0\n");
1378 pConv = WDML_GetConv(hConv, TRUE);
1381 if (pConv->wStatus & ST_CLIENT)
1383 /* FIXME: should abandon all pending transactions */
1384 pXAct = WDML_ClientQueueTerminate(pConv);
1387 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1388 (WPARAM)pConv->hwndClient, pXAct->lParam))
1390 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct, NULL);
1394 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1396 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1397 /* still have to destroy data assosiated with conversation */
1398 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1402 FIXME("Not implemented yet for a server side conversation\n");
1410 /*****************************************************************
1411 * DdeImpersonateClient (USER32.@)
1413 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1418 TRACE("(%p)\n", hConv);
1420 pConv = WDML_GetConv(hConv, TRUE);
1423 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);