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) * sizeof(WCHAR);
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/sizeof(WCHAR));
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 TRACE("hDdeData = %p\n", pXAct->hDdeData);
698 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
700 return WDML_QS_HANDLED;
703 /******************************************************************
704 * WDML_ClientQueuePoke
708 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
709 UINT wFmt, HSZ hszItem)
714 TRACE("XTYP_POKE transaction\n");
716 atom = WDML_MakeAtomFromHsz(hszItem);
717 if (!atom) return NULL;
719 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
722 GlobalDeleteAtom(atom);
726 if (cbData == (DWORD)-1)
728 pXAct->hMem = (HDDEDATA)pData;
734 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
735 ddePoke = GlobalLock(pXAct->hMem);
738 memcpy(ddePoke->Value, pData, cbData);
739 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
740 ddePoke->cfFormat = wFmt;
741 GlobalUnlock(pXAct->hMem);
745 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
750 /******************************************************************
751 * WDML_HandlePokeReply
755 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
761 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
766 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
767 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
768 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
772 FreeDDElParam(WM_DDE_ACK, msg->lParam);
773 GlobalDeleteAtom(uiHi);
775 WDML_ExtractAck(uiLo, &ddeAck);
776 GlobalFree(pXAct->hMem);
778 pXAct->hDdeData = (HDDEDATA)TRUE;
782 /******************************************************************
783 * WDML_ClientQueueTerminate
785 * Creates and queue an WM_DDE_TERMINATE transaction
787 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
791 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
796 pConv->wStatus &= ~ST_CONNECTED;
801 /******************************************************************
802 * WDML_HandleTerminateReply
804 * handles the reply to a terminate request
806 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
808 if (msg->message != WM_DDE_TERMINATE)
810 /* FIXME: should delete data passed here */
811 return WDML_QS_SWALLOWED;
814 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
816 FIXME("hmmm shouldn't happen\n");
819 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
821 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
822 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
824 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
825 return WDML_QS_HANDLED;
828 /******************************************************************
829 * WDML_HandleReplyData
833 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
836 HDDEDATA hDdeDataIn, hDdeDataOut;
841 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
842 /* wParam -- sending window handle */
843 /* lParam -- hDdeData & item HSZ */
845 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
846 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
848 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
851 * For hot link, data should be passed to its callback with
852 * XTYP_ADVDATA and callback should return the proper status.
854 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
855 uiLo ? TRUE : FALSE, wdh.cfFormat);
858 WDML_DecHSZ(pConv->instance, hsz);
859 DdeFreeDataHandle(hDdeDataIn);
863 if (hDdeDataIn != 0 && wdh.fAckReq)
865 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
871 GlobalDeleteAtom(uiHi);
874 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
875 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
877 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
879 if (uiLo) GlobalFree((HANDLE)uiLo);
882 DdeFreeDataHandle(hDdeDataIn);
884 WDML_DecHSZ(pConv->instance, hsz);
886 FreeDDElParam(WM_DDE_DATA, msg->lParam);
888 return WDML_QS_HANDLED;
891 /******************************************************************
892 * WDML_HandleIncomingTerminate
896 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
898 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
901 pConv->wStatus |= ST_TERMINATED;
902 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
904 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
905 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
907 if (pConv->wStatus & ST_CONNECTED)
909 /* don't care about result code (if server exists or not) */
910 PostMessageA(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
911 pConv->wStatus &= ~ST_CONNECTED;
913 /* have to keep connection around to allow reconnection */
914 return WDML_QS_HANDLED;
917 /******************************************************************
920 * handles any incoming reply, and try to match to an already sent request
922 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
924 WDML_XACT* pXAct = pConv->transactions;
927 if (pConv->transactions)
929 /* first check message against a pending transaction, if any */
930 switch (pXAct->ddeMsg)
933 qs = WDML_HandleAdviseReply(pConv, msg, pXAct);
935 case WM_DDE_UNADVISE:
936 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct);
939 qs = WDML_HandleExecuteReply(pConv, msg, pXAct);
942 qs = WDML_HandleRequestReply(pConv, msg, pXAct);
945 qs = WDML_HandlePokeReply(pConv, msg, pXAct);
947 case WM_DDE_TERMINATE:
948 qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
960 /* now check the results */
964 case WDML_QS_SWALLOWED:
967 case WDML_QS_HANDLED:
968 /* ok, we have resolved a pending transaction
969 * notify callback if asynchronous, and remove it in any case
971 WDML_UnQueueTransaction(pConv, pXAct);
972 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
974 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
975 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
976 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
981 *hdd = pXAct->hDdeData;
983 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
986 /* no pending transaction found, try a warm/hot link or a termination request */
987 switch (msg->message)
990 qs = WDML_HandleIncomingData(pConv, msg, hdd);
992 case WM_DDE_TERMINATE:
993 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
998 FIXME("shouldn't be used on client side\n");
1005 /******************************************************************
1006 * WDML_SyncWaitTransactionReply
1008 * waits until an answer for a sent request is received
1009 * time out is also handled. only used for synchronous transactions
1011 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct)
1017 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
1019 /* FIXME: time 32 bit wrap around */
1020 dwTimeout += GetCurrentTime();
1022 while ((dwTime = GetCurrentTime()) < dwTimeout)
1024 /* we cannot be in the crit sect all the time because when client and server run in a
1025 * single process they need to share the access to the internal data
1027 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1028 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1035 EnterCriticalSection(&WDML_CritSect);
1037 pConv = WDML_GetConv(hConv, FALSE);
1040 LeaveCriticalSection(&WDML_CritSect);
1041 /* conversation no longer available... return failure */
1044 while (PeekMessageA(&msg, pConv->hwndClient, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1046 /* check that either pXAct has been processed or no more xActions are pending */
1047 ret = (pConv->transactions == pXAct);
1048 ret = WDML_HandleReply(pConv, &msg, &hdd) == WDML_QS_HANDLED &&
1049 (pConv->transactions == NULL || ret);
1052 LeaveCriticalSection(&WDML_CritSect);
1060 TRACE("Timeout !!\n");
1062 EnterCriticalSection(&WDML_CritSect);
1064 pConv = WDML_GetConv(hConv, FALSE);
1067 if (pConv->transactions)
1069 switch (pConv->transactions->ddeMsg)
1071 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1072 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1073 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1074 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1075 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1076 default: err = DMLERR_INVALIDPARAMETER; break;
1079 pConv->instance->lastError = err;
1082 LeaveCriticalSection(&WDML_CritSect);
1087 /*****************************************************************
1088 * DdeClientTransaction (USER32.@)
1090 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1091 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1095 HDDEDATA hDdeData = 0;
1097 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1098 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1102 ERR("Invalid conversation handle\n");
1106 EnterCriticalSection(&WDML_CritSect);
1108 pConv = WDML_GetConv(hConv, TRUE);
1111 /* cannot set error... cannot get back to DDE instance */
1118 if (hszItem != 0 || wFmt != 0)
1120 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1123 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1126 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1128 case XTYP_ADVSTART|XTYPF_NODATA:
1129 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1131 case XTYP_ADVSTART|XTYPF_ACKREQ:
1134 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1137 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1142 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1145 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1150 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1153 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1156 FIXME("Unknown transation\n");
1157 /* unknown transaction type */
1158 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1164 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1168 WDML_QueueTransaction(pConv, pXAct);
1170 if (!PostMessageA(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1172 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1173 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1174 pConv->wStatus &= ~ST_CONNECTED;
1175 WDML_UnQueueTransaction(pConv, pXAct);
1176 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1179 pXAct->dwTimeout = dwTimeout;
1180 /* FIXME: should set the app bits on *pdwResult */
1182 if (dwTimeout == TIMEOUT_ASYNC)
1186 *pdwResult = MAKELONG(0, pXAct->xActID);
1188 hDdeData = (HDDEDATA)1;
1198 count = WDML_CritSect.RecursionCount;
1199 for (i = 0; i < count; i++)
1200 LeaveCriticalSection(&WDML_CritSect);
1201 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct);
1202 for (i = 0; i < count; i++)
1203 EnterCriticalSection(&WDML_CritSect);
1205 LeaveCriticalSection(&WDML_CritSect);
1209 LeaveCriticalSection(&WDML_CritSect);
1213 /*****************************************************************
1214 * DdeAbandonTransaction (USER32.@)
1216 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1218 WDML_INSTANCE* pInstance;
1222 TRACE("(%08lx,%p,%08ld);\n", idInst, hConv, idTransaction);
1224 EnterCriticalSection(&WDML_CritSect);
1225 if ((pInstance = WDML_GetInstance(idInst)))
1229 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1231 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1233 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1234 (idTransaction == 0 || pXAct->xActID == idTransaction))
1236 WDML_UnQueueTransaction(pConv, pXAct);
1237 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1244 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1246 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1247 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1249 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1251 WDML_UnQueueTransaction(pConv, pXAct);
1252 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1258 LeaveCriticalSection(&WDML_CritSect);
1263 /******************************************************************
1266 * Window Proc created on client side for each conversation
1268 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1271 WDML_CONV* pConv = NULL;
1274 if (iMsg == WM_DDE_ACK &&
1275 /* in the initial WM_INITIATE sendmessage */
1276 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1278 /* In response to WM_DDE_INITIATE, save server window */
1280 WDML_INSTANCE* pInstance;
1282 /* note: sent messages do not need packing */
1283 uiLo = LOWORD(lParam);
1284 uiHi = HIWORD(lParam);
1286 /* FIXME: convlist should be handled here */
1289 /* we already have started the conv with a server, drop other replies */
1290 GlobalDeleteAtom(uiLo);
1291 GlobalDeleteAtom(uiHi);
1292 PostMessageA((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1296 pInstance = WDML_GetInstanceFromWnd(hwnd);
1298 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1299 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1301 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1303 SetWindowLongA(hwnd, GWL_WDML_CONVERSATION, (DWORD)pConv);
1304 pConv->wStatus |= ST_CONNECTED;
1305 pConv->wConvst = XST_INIT1;
1307 /* check if server is handled by DDEML */
1308 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1309 strcmp(buf, WDML_szServerConvClassA) == 0) ||
1310 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1311 lstrcmpW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1313 pConv->wStatus |= ST_ISLOCAL;
1316 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1318 GlobalDeleteAtom(uiLo);
1319 GlobalDeleteAtom(uiHi);
1321 /* accept conversation */
1325 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1327 EnterCriticalSection(&WDML_CritSect);
1329 pConv = WDML_GetConvFromWnd(hwnd);
1338 msg.wParam = wParam;
1339 msg.lParam = lParam;
1341 WDML_HandleReply(pConv, &msg, &hdd);
1344 LeaveCriticalSection(&WDML_CritSect);
1348 return (IsWindowUnicode(hwnd)) ?
1349 DefWindowProcW(hwnd, iMsg, wParam, lParam) : DefWindowProcA(hwnd, iMsg, wParam, lParam);
1352 /*****************************************************************
1353 * DdeDisconnect (USER32.@)
1355 BOOL WINAPI DdeDisconnect(HCONV hConv)
1357 WDML_CONV* pConv = NULL;
1362 TRACE("(%p)\n", hConv);
1366 ERR("DdeDisconnect(): hConv = 0\n");
1370 EnterCriticalSection(&WDML_CritSect);
1371 pConv = WDML_GetConv(hConv, TRUE);
1374 if (pConv->wStatus & ST_CLIENT)
1376 /* FIXME: should abandon all pending transactions */
1377 pXAct = WDML_ClientQueueTerminate(pConv);
1380 count = WDML_CritSect.RecursionCount;
1381 for (i = 0; i < count; i++)
1382 LeaveCriticalSection(&WDML_CritSect);
1383 if (PostMessageA(pConv->hwndServer, pXAct->ddeMsg,
1384 (WPARAM)pConv->hwndClient, pXAct->lParam))
1385 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct);
1386 for (i = 0; i < count; i++)
1387 EnterCriticalSection(&WDML_CritSect);
1389 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1390 /* still have to destroy data assosiated with conversation */
1391 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1395 FIXME("Not implemented yet for a server side conversation\n");
1399 LeaveCriticalSection(&WDML_CritSect);
1404 /*****************************************************************
1405 * DdeImpersonateClient (USER32.@)
1407 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1412 TRACE("(%p)\n", hConv);
1414 EnterCriticalSection(&WDML_CritSect);
1415 pConv = WDML_GetConv(hConv, TRUE);
1418 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1420 LeaveCriticalSection(&WDML_CritSect);