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
37 #include "wine/debug.h"
38 #include "dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
43 const char WDML_szClientConvClassA[] = "WineDdeClientA";
44 const WCHAR WDML_szClientConvClassW[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t','W',0};
46 /******************************************************************************
47 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
50 * idInst [I] Instance identifier
51 * hszService [I] Handle to service name string
52 * hszTopic [I] Handle to topic name string
53 * hConvList [I] Handle to conversation list
54 * pCC [I] Pointer to structure with context data
57 * Success: Handle to new conversation list
60 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
61 HCONVLIST hConvList, PCONVCONTEXT pCC)
63 FIXME("(%d,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
67 /*****************************************************************
68 * DdeQueryNextServer [USER32.@]
70 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
72 FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
76 /******************************************************************************
77 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
81 * hConvList [I] Handle to conversation list
87 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
89 FIXME("(%p): stub\n", hConvList);
93 /*****************************************************************
94 * DdeConnect (USER32.@)
96 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
100 WDML_INSTANCE* pInstance;
101 WDML_CONV* pConv = NULL;
102 ATOM aSrv = 0, aTpc = 0;
104 TRACE("(0x%x,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
106 pInstance = WDML_GetInstance(idInst);
110 /* make sure this conv is never created */
111 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
114 ERR("This Conv already exists: (%p)\n", pConv);
118 /* we need to establish a conversation with
119 server, so create a window for it */
121 if (pInstance->unicode)
123 WNDCLASSEXW wndclass;
125 wndclass.cbSize = sizeof(wndclass);
127 wndclass.lpfnWndProc = WDML_ClientProc;
128 wndclass.cbClsExtra = 0;
129 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
130 wndclass.hInstance = 0;
132 wndclass.hCursor = 0;
133 wndclass.hbrBackground = 0;
134 wndclass.lpszMenuName = NULL;
135 wndclass.lpszClassName = WDML_szClientConvClassW;
136 wndclass.hIconSm = 0;
138 RegisterClassExW(&wndclass);
140 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
144 WNDCLASSEXA wndclass;
146 wndclass.cbSize = sizeof(wndclass);
148 wndclass.lpfnWndProc = WDML_ClientProc;
149 wndclass.cbClsExtra = 0;
150 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
151 wndclass.hInstance = 0;
153 wndclass.hCursor = 0;
154 wndclass.hbrBackground = 0;
155 wndclass.lpszMenuName = NULL;
156 wndclass.lpszClassName = WDML_szClientConvClassA;
157 wndclass.hIconSm = 0;
159 RegisterClassExA(&wndclass);
161 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
164 SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
168 aSrv = WDML_MakeAtomFromHsz(hszService);
169 if (!aSrv) goto theEnd;
173 aTpc = WDML_MakeAtomFromHsz(hszTopic);
174 if (!aTpc) goto theEnd;
177 /* note: sent messages shall not use packing */
178 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
179 SMTO_ABORTIFHUNG, 2000, NULL );
181 pInstance = WDML_GetInstance(idInst);
187 /* At this point, Client WM_DDE_ACK should have saved hwndServer
188 for this instance id and hwndClient if server responds.
189 So get HCONV and return it. And add it to conv list */
190 pConv = WDML_GetConvFromWnd(hwndClient);
191 if (pConv == NULL || pConv->hwndServer == 0)
193 WARN("Done with INITIATE, but no Server window available\n");
197 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
198 pConv->wConvst = XST_CONNECTED;
200 /* finish init of pConv */
203 pConv->convContext = *pCC;
207 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
208 pConv->convContext.cb = sizeof(pConv->convContext);
209 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
214 if (aSrv) GlobalDeleteAtom(aSrv);
215 if (aTpc) GlobalDeleteAtom(aTpc);
219 /*****************************************************************
220 * DdeReconnect (DDEML.37)
221 * DdeReconnect (USER32.@)
223 HCONV WINAPI DdeReconnect(HCONV hConv)
226 WDML_CONV* pNewConv = NULL;
227 ATOM aSrv = 0, aTpc = 0;
229 TRACE("(%p)\n", hConv);
231 pConv = WDML_GetConv(hConv, FALSE);
232 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
236 /* to reestablist a connection, we have to make sure that:
237 * 1/ pConv is the converstation attached to the client window (it wouldn't be
238 * if a call to DdeReconnect would have already been done...)
239 * FIXME: is this really an error ???
240 * 2/ the pConv conversation had really been deconnected
242 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
243 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
245 HWND hwndClient = pConv->hwndClient;
246 HWND hwndServer = pConv->hwndServer;
248 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
250 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
251 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
252 if (!aSrv || !aTpc) goto theEnd;
254 /* note: sent messages shall not use packing */
255 ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
256 MAKELPARAM(aSrv, aTpc));
258 pConv = WDML_GetConv(hConv, FALSE);
261 FIXME("Should fail reconnection\n");
265 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
267 /* re-establish all links... */
270 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
272 if (pLink->hConv == hConv)
274 /* try to reestablish the links... */
275 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
276 pLink->transactionType, 1000, NULL);
282 /* reset the conversation as it was */
283 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
290 if (aSrv) GlobalDeleteAtom(aSrv);
291 if (aTpc) GlobalDeleteAtom(aTpc);
292 return (HCONV)pNewConv;
295 /******************************************************************
296 * WDML_ClientQueueAdvise
298 * Creates and queue an WM_DDE_ADVISE transaction
300 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
302 DDEADVISE* pDdeAdvise;
306 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
308 atom = WDML_MakeAtomFromHsz(hszItem);
309 if (!atom) return NULL;
311 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
314 GlobalDeleteAtom(atom);
318 pXAct->wType = wType & ~0x0F;
319 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
320 /* FIXME: hMem is unfreed for now... should be deleted in server */
323 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
324 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
325 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
326 pDdeAdvise->cfFormat = wFmt;
327 GlobalUnlock(pXAct->hMem);
329 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
334 /******************************************************************
335 * WDML_HandleAdviseReply
337 * handles the reply to an advise request
339 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
345 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
350 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
351 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
353 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
356 GlobalDeleteAtom(uiHi);
357 FreeDDElParam(WM_DDE_ACK, msg->lParam);
359 if (ack) *ack = uiLo;
360 WDML_ExtractAck(uiLo, &ddeAck);
366 /* billx: first to see if the link is already created. */
367 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
368 pXAct->hszItem, TRUE, pXAct->wFmt);
371 /* we found a link, and only need to modify it in case it changes */
372 pLink->transactionType = pXAct->wType;
376 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
377 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
379 pXAct->hDdeData = (HDDEDATA)1;
383 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
384 GlobalFree(pXAct->hMem);
385 pXAct->hDdeData = NULL;
388 return WDML_QS_HANDLED;
391 /******************************************************************
392 * WDML_ClientQueueUnadvise
394 * queues an unadvise transaction
396 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
401 TRACE("XTYP_ADVSTOP transaction\n");
403 atom = WDML_MakeAtomFromHsz(hszItem);
404 if (!atom) return NULL;
406 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
409 GlobalDeleteAtom(atom);
413 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
414 * on the specified item.
416 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
420 /******************************************************************
421 * WDML_HandleUnadviseReply
425 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
431 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
436 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
437 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
439 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
442 FreeDDElParam(WM_DDE_ACK, msg->lParam);
443 GlobalDeleteAtom(uiHi);
445 if (ack) *ack = uiLo;
446 WDML_ExtractAck(uiLo, &ddeAck);
448 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
452 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
453 pXAct->hDdeData = NULL;
457 /* billx: remove the link */
458 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
459 pXAct->hszItem, pXAct->wFmt);
460 pXAct->hDdeData = (HDDEDATA)1;
462 return WDML_QS_HANDLED;
465 /******************************************************************
466 * WDML_ClientQueueRequest
470 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
475 TRACE("XTYP_REQUEST transaction\n");
477 atom = WDML_MakeAtomFromHsz(hszItem);
478 if (!atom) return NULL;
480 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
483 GlobalDeleteAtom(atom);
487 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
492 /******************************************************************
493 * WDML_HandleRequestReply
497 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
504 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
507 switch (msg->message)
510 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
511 FreeDDElParam(WM_DDE_ACK, msg->lParam);
512 GlobalDeleteAtom(uiHi);
513 if (ack) *ack = uiLo;
514 WDML_ExtractAck(uiLo, &ddeAck);
517 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
518 TRACE("Negative answer...\n");
522 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
523 TRACE("Got the result (%08x)\n", uiLo);
525 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
527 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
530 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
533 GlobalFree((HGLOBAL)uiLo);
537 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
541 GlobalDeleteAtom(uiHi);
542 FreeDDElParam(WM_DDE_ACK, msg->lParam);
547 FreeDDElParam(msg->message, msg->lParam);
551 return WDML_QS_HANDLED;
554 /******************************************************************
555 * WDML_BuildExecuteCommand
557 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
558 * It also takes care of string conversion between the two window procedures
560 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
563 BOOL clientUnicode, serverUnicode;
566 clientUnicode = pConv->instance->unicode;
567 TRACE("client %p uses unicode = %d\n", pConv->hwndClient, clientUnicode);
568 /* FIXME: how exactly Windows determines what to use for the server side? */
569 serverUnicode = IsWindowUnicode(pConv->hwndServer) && IsWindowUnicode(pConv->hwndClient);
570 TRACE("server %p uses unicode = %d\n", pConv->hwndServer, serverUnicode);
572 if (clientUnicode == serverUnicode)
580 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
584 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
588 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
594 pDst = GlobalLock(hMem);
597 if (clientUnicode == serverUnicode)
599 memcpy(pDst, pData, cbData);
605 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
609 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
624 /******************************************************************
625 * WDML_ClientQueueExecute
629 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPVOID pData, DWORD cbData)
633 TRACE("XTYP_EXECUTE transaction\n");
635 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
639 if (cbData == (DWORD)-1)
641 HDDEDATA hDdeData = pData;
643 pData = DdeAccessData(hDdeData, &cbData);
646 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
647 DdeUnaccessData(hDdeData);
652 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
655 pXAct->lParam = (LPARAM)pXAct->hMem;
660 /******************************************************************
661 * WDML_HandleExecuteReply
665 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
670 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
675 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
676 FreeDDElParam(WM_DDE_ACK, msg->lParam);
678 if ((HANDLE)uiHi != pXAct->hMem)
683 if (ack) *ack = uiLo;
684 WDML_ExtractAck(uiLo, &ddeAck);
685 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
687 TRACE("hDdeData = %p\n", pXAct->hDdeData);
688 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
690 return WDML_QS_HANDLED;
693 /******************************************************************
694 * WDML_ClientQueuePoke
698 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPVOID pData, DWORD cbData,
699 UINT wFmt, HSZ hszItem)
704 TRACE("XTYP_POKE transaction\n");
706 atom = WDML_MakeAtomFromHsz(hszItem);
707 if (!atom) return NULL;
709 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
712 GlobalDeleteAtom(atom);
716 if (cbData == (DWORD)-1)
718 pXAct->hMem = (HDDEDATA)pData;
724 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
725 ddePoke = GlobalLock(pXAct->hMem);
728 memcpy(ddePoke->Value, pData, cbData);
729 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
730 ddePoke->cfFormat = wFmt;
731 GlobalUnlock(pXAct->hMem);
735 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
740 /******************************************************************
741 * WDML_HandlePokeReply
745 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
750 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
755 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
756 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
757 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
761 FreeDDElParam(WM_DDE_ACK, msg->lParam);
762 GlobalDeleteAtom(uiHi);
764 if (ack) *ack = uiLo;
765 GlobalFree(pXAct->hMem);
767 pXAct->hDdeData = (HDDEDATA)TRUE;
771 /******************************************************************
772 * WDML_ClientQueueTerminate
774 * Creates and queue an WM_DDE_TERMINATE transaction
776 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
780 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
785 pConv->wStatus &= ~ST_CONNECTED;
790 /******************************************************************
791 * WDML_HandleTerminateReply
793 * handles the reply to a terminate request
795 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg)
797 if (msg->message != WM_DDE_TERMINATE)
799 /* FIXME: should delete data passed here */
800 return WDML_QS_SWALLOWED;
803 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
805 FIXME("hmmm shouldn't happen\n");
808 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
810 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
811 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
813 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
814 return WDML_QS_HANDLED;
817 /******************************************************************
818 * WDML_HandleIncomingData
822 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
825 HDDEDATA hDdeDataIn, hDdeDataOut;
830 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
831 /* wParam -- sending window handle */
832 /* lParam -- hDdeData & item HSZ */
834 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
835 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
837 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
840 * For hot link, data should be passed to its callback with
841 * XTYP_ADVDATA and callback should return the proper status.
843 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
844 uiLo ? TRUE : FALSE, wdh.cfFormat);
847 WDML_DecHSZ(pConv->instance, hsz);
848 DdeFreeDataHandle(hDdeDataIn);
852 if (hDdeDataIn != 0 && wdh.fAckReq)
854 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
860 GlobalDeleteAtom(uiHi);
863 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
864 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
866 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
868 if (uiLo) GlobalFree((HANDLE)uiLo);
871 DdeFreeDataHandle(hDdeDataIn);
873 WDML_DecHSZ(pConv->instance, hsz);
875 FreeDDElParam(WM_DDE_DATA, msg->lParam);
877 return WDML_QS_HANDLED;
880 /******************************************************************
881 * WDML_HandleIncomingTerminate
885 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
887 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
890 pConv->wStatus |= ST_TERMINATED;
891 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
893 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
894 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
896 if (pConv->wStatus & ST_CONNECTED)
898 /* don't care about result code (if server exists or not) */
899 PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
900 pConv->wStatus &= ~ST_CONNECTED;
902 /* have to keep connection around to allow reconnection */
903 return WDML_QS_HANDLED;
906 /******************************************************************
909 * handles any incoming reply, and try to match to an already sent request
911 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd, DWORD *ack)
913 WDML_XACT* pXAct = pConv->transactions;
916 if (pConv->transactions)
918 /* first check message against a pending transaction, if any */
919 switch (pXAct->ddeMsg)
922 qs = WDML_HandleAdviseReply(pConv, msg, pXAct, ack);
924 case WM_DDE_UNADVISE:
925 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct, ack);
928 qs = WDML_HandleExecuteReply(pConv, msg, pXAct, ack);
931 qs = WDML_HandleRequestReply(pConv, msg, pXAct, ack);
934 qs = WDML_HandlePokeReply(pConv, msg, pXAct, ack);
936 case WM_DDE_TERMINATE:
937 qs = WDML_HandleTerminateReply(pConv, msg);
949 /* now check the results */
953 case WDML_QS_SWALLOWED:
956 case WDML_QS_HANDLED:
957 /* ok, we have resolved a pending transaction
958 * notify callback if asynchronous.
960 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
962 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
963 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
964 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
969 *hdd = pXAct->hDdeData;
973 /* no pending transaction found, try a warm/hot link or a termination request */
974 switch (msg->message)
977 qs = WDML_HandleIncomingData(pConv, msg, hdd);
979 case WM_DDE_TERMINATE:
980 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
985 FIXME("shouldn't be used on client side\n");
992 /******************************************************************
993 * WDML_SyncWaitTransactionReply
995 * waits until an answer for a sent request is received
996 * time out is also handled. only used for synchronous transactions
998 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct, DWORD *ack)
1004 TRACE("Starting wait for a timeout of %d ms\n", dwTimeout);
1006 /* FIXME: time 32 bit wrap around */
1007 dwTimeout += GetCurrentTime();
1009 while ((dwTime = GetCurrentTime()) < dwTimeout)
1011 /* we cannot be in the crit sect all the time because when client and server run in a
1012 * single process they need to share the access to the internal data
1014 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1015 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1019 while (PeekMessageW(&msg, 0, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1023 pConv = WDML_GetConv(hConv, FALSE);
1026 /* conversation no longer available... return failure */
1029 if (msg.hwnd == pConv->hwndClient)
1031 /* check that either pXAct has been processed or no more xActions are pending */
1032 BOOL ret = (pConv->transactions == pXAct);
1033 if (WDML_HandleReply(pConv, &msg, &hdd, ack) == WDML_QS_HANDLED)
1035 TRACE("WDML_HandleReply returned WDML_QS_HANDLED\n");
1039 ret = (pConv->transactions == NULL || ret);
1043 pConv->instance->lastError = hdd ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
1049 DispatchMessageW(&msg);
1055 TRACE("Timeout !!\n");
1057 pConv = WDML_GetConv(hConv, FALSE);
1060 if (pConv->transactions)
1062 switch (pConv->transactions->ddeMsg)
1064 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1065 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1066 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1067 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1068 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1069 default: err = DMLERR_INVALIDPARAMETER; break;
1072 pConv->instance->lastError = err;
1080 /*****************************************************************
1083 HDDEDATA WDML_ClientHandle(WDML_CONV *pConv, WDML_XACT *pXAct, DWORD dwTimeout, LPDWORD pdwResult)
1087 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1089 WARN("Failed posting message %x to %p (error=0x%x)\n",
1090 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1091 pConv->wStatus &= ~ST_CONNECTED;
1092 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1095 pXAct->dwTimeout = dwTimeout;
1096 /* FIXME: should set the app bits on *pdwResult */
1098 if (dwTimeout == TIMEOUT_ASYNC)
1101 *pdwResult = MAKELONG(0, pXAct->xActID);
1103 hDdeData = (HDDEDATA)1;
1106 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct, pdwResult);
1112 /*****************************************************************
1113 * DdeClientTransaction (USER32.@)
1115 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1116 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1120 HDDEDATA hDdeData = 0;
1122 TRACE("(%p,%d,%p,%p,%x,%x,%d,%p)\n",
1123 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1127 WARN("Invalid conversation handle NULL\n");
1131 pConv = WDML_GetConv(hConv, TRUE);
1134 /* cannot set error... cannot get back to DDE instance */
1141 /* Windows simply ignores hszItem and wFmt in this case */
1142 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1145 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1147 case XTYP_ADVSTART|XTYPF_NODATA:
1148 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1150 case XTYP_ADVSTART|XTYPF_ACKREQ:
1153 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1156 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1161 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1164 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1169 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1172 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1175 FIXME("Unknown transaction type %04x\n", wType);
1176 /* unknown transaction type */
1177 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1183 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1187 WDML_QueueTransaction(pConv, pXAct);
1189 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1191 if (pConv->wStatus & ST_BLOCKED)
1193 TRACE("Transactions are blocked, add to the queue and exit\n");
1197 hDdeData = WDML_ClientHandle(pConv, pXAct, dwTimeout, pdwResult);
1198 if (dwTimeout != TIMEOUT_ASYNC)
1200 WDML_UnQueueTransaction(pConv, pXAct);
1201 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1207 /*****************************************************************
1208 * DdeAbandonTransaction (USER32.@)
1210 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1212 WDML_INSTANCE* pInstance;
1216 TRACE("(%08x,%p,%08x);\n", idInst, hConv, idTransaction);
1218 if ((pInstance = WDML_GetInstance(idInst)))
1222 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1224 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1226 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1227 (idTransaction == 0 || pXAct->xActID == idTransaction))
1229 WDML_UnQueueTransaction(pConv, pXAct);
1230 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1237 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1239 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1240 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1242 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1244 WDML_UnQueueTransaction(pConv, pXAct);
1245 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1255 /******************************************************************
1258 * Window Proc created on client side for each conversation
1260 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1263 WDML_CONV* pConv = NULL;
1266 TRACE("%p %04x %08x %08lx\n", hwnd, iMsg, wParam , lParam);
1268 if (iMsg == WM_DDE_ACK &&
1269 /* in the initial WM_INITIATE sendmessage */
1270 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1272 /* In response to WM_DDE_INITIATE, save server window */
1274 WDML_INSTANCE* pInstance;
1276 /* note: sent messages do not need packing */
1277 uiLo = LOWORD(lParam);
1278 uiHi = HIWORD(lParam);
1280 /* FIXME: convlist should be handled here */
1283 /* we already have started the conv with a server, drop other replies */
1284 GlobalDeleteAtom(uiLo);
1285 GlobalDeleteAtom(uiHi);
1286 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1290 pInstance = WDML_GetInstanceFromWnd(hwnd);
1292 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1293 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1295 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1297 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1298 pConv->wStatus |= ST_CONNECTED;
1299 pConv->wConvst = XST_INIT1;
1301 /* check if server is handled by DDEML */
1302 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1303 lstrcmpiA(buf, WDML_szServerConvClassA) == 0) ||
1304 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1305 lstrcmpiW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1307 pConv->wStatus |= ST_ISLOCAL;
1310 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1312 GlobalDeleteAtom(uiLo);
1313 GlobalDeleteAtom(uiHi);
1315 /* accept conversation */
1319 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1321 pConv = WDML_GetConvFromWnd(hwnd);
1330 msg.wParam = wParam;
1331 msg.lParam = lParam;
1333 WDML_HandleReply(pConv, &msg, &hdd, NULL);
1339 return IsWindowUnicode(hwnd) ? DefWindowProcW(hwnd, iMsg, wParam, lParam) :
1340 DefWindowProcA(hwnd, iMsg, wParam, lParam);
1343 /*****************************************************************
1344 * DdeDisconnect (USER32.@)
1346 BOOL WINAPI DdeDisconnect(HCONV hConv)
1348 WDML_CONV* pConv = NULL;
1352 TRACE("(%p)\n", hConv);
1356 WARN("DdeDisconnect(): hConv = 0\n");
1360 pConv = WDML_GetConv(hConv, TRUE);
1363 if (pConv->wStatus & ST_CLIENT)
1365 /* FIXME: should abandon all pending transactions */
1366 pXAct = WDML_ClientQueueTerminate(pConv);
1369 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1370 (WPARAM)pConv->hwndClient, pXAct->lParam))
1372 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct, NULL);
1376 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1378 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1379 /* still have to destroy data assosiated with conversation */
1380 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1384 FIXME("Not implemented yet for a server side conversation\n");
1392 /*****************************************************************
1393 * DdeImpersonateClient (USER32.@)
1395 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1400 TRACE("(%p)\n", hConv);
1402 pConv = WDML_GetConv(hConv, TRUE);
1405 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);