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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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("(%ld,%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%lx,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
106 EnterCriticalSection(&WDML_CritSect);
108 pInstance = WDML_GetInstance(idInst);
114 /* make sure this conv is never created */
115 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
118 ERR("This Conv already exists: (%p)\n", pConv);
122 /* we need to establish a conversation with
123 server, so create a window for it */
125 if (pInstance->unicode)
127 WNDCLASSEXW wndclass;
129 wndclass.cbSize = sizeof(wndclass);
131 wndclass.lpfnWndProc = WDML_ClientProc;
132 wndclass.cbClsExtra = 0;
133 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
134 wndclass.hInstance = 0;
136 wndclass.hCursor = 0;
137 wndclass.hbrBackground = 0;
138 wndclass.lpszMenuName = NULL;
139 wndclass.lpszClassName = WDML_szClientConvClassW;
140 wndclass.hIconSm = 0;
142 RegisterClassExW(&wndclass);
144 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
148 WNDCLASSEXA wndclass;
150 wndclass.cbSize = sizeof(wndclass);
152 wndclass.lpfnWndProc = WDML_ClientProc;
153 wndclass.cbClsExtra = 0;
154 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
155 wndclass.hInstance = 0;
157 wndclass.hCursor = 0;
158 wndclass.hbrBackground = 0;
159 wndclass.lpszMenuName = NULL;
160 wndclass.lpszClassName = WDML_szClientConvClassA;
161 wndclass.hIconSm = 0;
163 RegisterClassExA(&wndclass);
165 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
168 SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
172 aSrv = WDML_MakeAtomFromHsz(hszService);
173 if (!aSrv) goto theEnd;
177 aTpc = WDML_MakeAtomFromHsz(hszTopic);
178 if (!aTpc) goto theEnd;
181 LeaveCriticalSection(&WDML_CritSect);
183 /* note: sent messages shall not use packing */
184 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
185 SMTO_ABORTIFHUNG, 2000, NULL );
187 EnterCriticalSection(&WDML_CritSect);
189 pInstance = WDML_GetInstance(idInst);
195 /* At this point, Client WM_DDE_ACK should have saved hwndServer
196 for this instance id and hwndClient if server responds.
197 So get HCONV and return it. And add it to conv list */
198 pConv = WDML_GetConvFromWnd(hwndClient);
199 if (pConv == NULL || pConv->hwndServer == 0)
201 WARN("Done with INITIATE, but no Server window available\n");
205 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
206 pConv->wConvst = XST_CONNECTED;
208 /* finish init of pConv */
211 pConv->convContext = *pCC;
215 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
216 pConv->convContext.cb = sizeof(pConv->convContext);
217 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
221 LeaveCriticalSection(&WDML_CritSect);
223 if (aSrv) GlobalDeleteAtom(aSrv);
224 if (aTpc) GlobalDeleteAtom(aTpc);
228 /*****************************************************************
229 * DdeReconnect (DDEML.37)
230 * DdeReconnect (USER32.@)
232 HCONV WINAPI DdeReconnect(HCONV hConv)
235 WDML_CONV* pNewConv = NULL;
236 ATOM aSrv = 0, aTpc = 0;
238 TRACE("(%p)\n", hConv);
240 EnterCriticalSection(&WDML_CritSect);
241 pConv = WDML_GetConv(hConv, FALSE);
242 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
246 /* to reestablist a connection, we have to make sure that:
247 * 1/ pConv is the converstation attached to the client window (it wouldn't be
248 * if a call to DdeReconnect would have already been done...)
249 * FIXME: is this really an error ???
250 * 2/ the pConv conversation had really been deconnected
252 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
253 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
255 HWND hwndClient = pConv->hwndClient;
256 HWND hwndServer = pConv->hwndServer;
259 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
261 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
262 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
263 if (!aSrv || !aTpc) goto theEnd;
265 LeaveCriticalSection(&WDML_CritSect);
267 /* note: sent messages shall not use packing */
268 ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
269 MAKELPARAM(aSrv, aTpc));
271 EnterCriticalSection(&WDML_CritSect);
273 pConv = WDML_GetConv(hConv, FALSE);
276 FIXME("Should fail reconnection\n");
280 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
282 /* re-establish all links... */
285 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
287 if (pLink->hConv == hConv)
289 /* try to reestablish the links... */
290 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
291 pLink->transactionType, 1000, NULL);
297 /* reset the conversation as it was */
298 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
304 LeaveCriticalSection(&WDML_CritSect);
306 if (aSrv) GlobalDeleteAtom(aSrv);
307 if (aTpc) GlobalDeleteAtom(aTpc);
308 return (HCONV)pNewConv;
311 /******************************************************************
312 * WDML_ClientQueueAdvise
314 * Creates and queue an WM_DDE_ADVISE transaction
316 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
318 DDEADVISE* pDdeAdvise;
322 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
324 atom = WDML_MakeAtomFromHsz(hszItem);
325 if (!atom) return NULL;
327 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
330 GlobalDeleteAtom(atom);
334 pXAct->wType = wType & ~0x0F;
335 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
336 /* FIXME: hMem is unfreed for now... should be deleted in server */
339 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
340 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
341 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
342 pDdeAdvise->cfFormat = wFmt;
343 GlobalUnlock(pXAct->hMem);
345 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
350 /******************************************************************
351 * WDML_HandleAdviseReply
353 * handles the reply to an advise request
355 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
361 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
366 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
367 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
369 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
372 GlobalDeleteAtom(uiHi);
373 FreeDDElParam(WM_DDE_ACK, msg->lParam);
375 if (ack) *ack = uiLo;
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, DWORD *ack)
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 if (ack) *ack = uiLo;
462 WDML_ExtractAck(uiLo, &ddeAck);
464 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
468 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
469 pXAct->hDdeData = NULL;
473 /* billx: remove the link */
474 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
475 pXAct->hszItem, pXAct->wFmt);
476 pXAct->hDdeData = (HDDEDATA)1;
478 return WDML_QS_HANDLED;
481 /******************************************************************
482 * WDML_ClientQueueRequest
486 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
491 TRACE("XTYP_REQUEST transaction\n");
493 atom = WDML_MakeAtomFromHsz(hszItem);
494 if (!atom) return NULL;
496 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
499 GlobalDeleteAtom(atom);
503 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
508 /******************************************************************
509 * WDML_HandleRequestReply
513 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
520 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
523 switch (msg->message)
526 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
527 FreeDDElParam(WM_DDE_ACK, msg->lParam);
528 GlobalDeleteAtom(uiHi);
529 if (ack) *ack = uiLo;
530 WDML_ExtractAck(uiLo, &ddeAck);
533 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
534 TRACE("Negative answer...\n");
538 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
539 TRACE("Got the result (%08x)\n", uiLo);
541 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
543 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
546 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
549 GlobalFree((HGLOBAL)uiLo);
553 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
557 GlobalDeleteAtom(uiHi);
558 FreeDDElParam(WM_DDE_ACK, msg->lParam);
563 FreeDDElParam(msg->message, msg->lParam);
567 return WDML_QS_HANDLED;
570 /******************************************************************
571 * WDML_BuildExecuteCommand
573 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
574 * It also takes care of string conversion between the two window procedures
576 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
579 BOOL clientUnicode, serverUnicode;
582 clientUnicode = pConv->instance->unicode;
583 TRACE("client %p uses unicode = %d\n", pConv->hwndClient, clientUnicode);
584 /* FIXME: how exactly Windows determines what to use for the server side? */
585 serverUnicode = IsWindowUnicode(pConv->hwndServer) && IsWindowUnicode(pConv->hwndClient);
586 TRACE("server %p uses unicode = %d\n", pConv->hwndServer, serverUnicode);
588 if (clientUnicode == serverUnicode)
596 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
600 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
604 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
610 pDst = GlobalLock(hMem);
613 if (clientUnicode == serverUnicode)
615 memcpy(pDst, pData, cbData);
621 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
625 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
640 /******************************************************************
641 * WDML_ClientQueueExecute
645 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
649 TRACE("XTYP_EXECUTE transaction\n");
651 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
655 if (cbData == (DWORD)-1)
657 HDDEDATA hDdeData = (HDDEDATA)pData;
659 pData = DdeAccessData(hDdeData, &cbData);
662 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
663 DdeUnaccessData(hDdeData);
668 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
671 pXAct->lParam = (LPARAM)pXAct->hMem;
676 /******************************************************************
677 * WDML_HandleExecuteReply
681 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
686 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
691 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
692 FreeDDElParam(WM_DDE_ACK, msg->lParam);
694 if ((HANDLE)uiHi != pXAct->hMem)
699 if (ack) *ack = uiLo;
700 WDML_ExtractAck(uiLo, &ddeAck);
701 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
703 TRACE("hDdeData = %p\n", pXAct->hDdeData);
704 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
706 return WDML_QS_HANDLED;
709 /******************************************************************
710 * WDML_ClientQueuePoke
714 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
715 UINT wFmt, HSZ hszItem)
720 TRACE("XTYP_POKE transaction\n");
722 atom = WDML_MakeAtomFromHsz(hszItem);
723 if (!atom) return NULL;
725 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
728 GlobalDeleteAtom(atom);
732 if (cbData == (DWORD)-1)
734 pXAct->hMem = (HDDEDATA)pData;
740 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
741 ddePoke = GlobalLock(pXAct->hMem);
744 memcpy(ddePoke->Value, pData, cbData);
745 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
746 ddePoke->cfFormat = wFmt;
747 GlobalUnlock(pXAct->hMem);
751 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
756 /******************************************************************
757 * WDML_HandlePokeReply
761 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
766 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
771 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
772 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
773 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
777 FreeDDElParam(WM_DDE_ACK, msg->lParam);
778 GlobalDeleteAtom(uiHi);
780 if (ack) *ack = uiLo;
781 GlobalFree(pXAct->hMem);
783 pXAct->hDdeData = (HDDEDATA)TRUE;
787 /******************************************************************
788 * WDML_ClientQueueTerminate
790 * Creates and queue an WM_DDE_TERMINATE transaction
792 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
796 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
801 pConv->wStatus &= ~ST_CONNECTED;
806 /******************************************************************
807 * WDML_HandleTerminateReply
809 * handles the reply to a terminate request
811 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
813 if (msg->message != WM_DDE_TERMINATE)
815 /* FIXME: should delete data passed here */
816 return WDML_QS_SWALLOWED;
819 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
821 FIXME("hmmm shouldn't happen\n");
824 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
826 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
827 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
829 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
830 return WDML_QS_HANDLED;
833 /******************************************************************
834 * WDML_HandleReplyData
838 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
841 HDDEDATA hDdeDataIn, hDdeDataOut;
846 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
847 /* wParam -- sending window handle */
848 /* lParam -- hDdeData & item HSZ */
850 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
851 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
853 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
856 * For hot link, data should be passed to its callback with
857 * XTYP_ADVDATA and callback should return the proper status.
859 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
860 uiLo ? TRUE : FALSE, wdh.cfFormat);
863 WDML_DecHSZ(pConv->instance, hsz);
864 DdeFreeDataHandle(hDdeDataIn);
868 if (hDdeDataIn != 0 && wdh.fAckReq)
870 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
876 GlobalDeleteAtom(uiHi);
879 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
880 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
882 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
884 if (uiLo) GlobalFree((HANDLE)uiLo);
887 DdeFreeDataHandle(hDdeDataIn);
889 WDML_DecHSZ(pConv->instance, hsz);
891 FreeDDElParam(WM_DDE_DATA, msg->lParam);
893 return WDML_QS_HANDLED;
896 /******************************************************************
897 * WDML_HandleIncomingTerminate
901 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
903 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
906 pConv->wStatus |= ST_TERMINATED;
907 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
909 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
910 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
912 if (pConv->wStatus & ST_CONNECTED)
914 /* don't care about result code (if server exists or not) */
915 PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
916 pConv->wStatus &= ~ST_CONNECTED;
918 /* have to keep connection around to allow reconnection */
919 return WDML_QS_HANDLED;
922 /******************************************************************
925 * handles any incoming reply, and try to match to an already sent request
927 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd, DWORD *ack)
929 WDML_XACT* pXAct = pConv->transactions;
932 if (pConv->transactions)
934 /* first check message against a pending transaction, if any */
935 switch (pXAct->ddeMsg)
938 qs = WDML_HandleAdviseReply(pConv, msg, pXAct, ack);
940 case WM_DDE_UNADVISE:
941 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct, ack);
944 qs = WDML_HandleExecuteReply(pConv, msg, pXAct, ack);
947 qs = WDML_HandleRequestReply(pConv, msg, pXAct, ack);
950 qs = WDML_HandlePokeReply(pConv, msg, pXAct, ack);
952 case WM_DDE_TERMINATE:
953 qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
965 /* now check the results */
969 case WDML_QS_SWALLOWED:
972 case WDML_QS_HANDLED:
973 /* ok, we have resolved a pending transaction
974 * notify callback if asynchronous, and remove it in any case
976 WDML_UnQueueTransaction(pConv, pXAct);
977 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
979 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
980 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
981 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
986 *hdd = pXAct->hDdeData;
988 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
991 /* no pending transaction found, try a warm/hot link or a termination request */
992 switch (msg->message)
995 qs = WDML_HandleIncomingData(pConv, msg, hdd);
997 case WM_DDE_TERMINATE:
998 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
1003 FIXME("shouldn't be used on client side\n");
1010 /******************************************************************
1011 * WDML_SyncWaitTransactionReply
1013 * waits until an answer for a sent request is received
1014 * time out is also handled. only used for synchronous transactions
1016 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct, DWORD *ack)
1022 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
1024 /* FIXME: time 32 bit wrap around */
1025 dwTimeout += GetCurrentTime();
1027 while ((dwTime = GetCurrentTime()) < dwTimeout)
1029 /* we cannot be in the crit sect all the time because when client and server run in a
1030 * single process they need to share the access to the internal data
1032 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1033 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1037 while (PeekMessageW(&msg, 0, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1042 EnterCriticalSection(&WDML_CritSect);
1044 pConv = WDML_GetConv(hConv, FALSE);
1047 LeaveCriticalSection(&WDML_CritSect);
1048 /* conversation no longer available... return failure */
1051 if (msg.hwnd == pConv->hwndClient)
1053 /* check that either pXAct has been processed or no more xActions are pending */
1054 BOOL ret = (pConv->transactions == pXAct);
1055 if (WDML_HandleReply(pConv, &msg, &hdd, ack) == WDML_QS_HANDLED)
1057 TRACE("WDML_HandleReply returned WDML_QS_HANDLED\n");
1061 ret = (pConv->transactions == NULL || ret);
1065 pConv->instance->lastError = hdd ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
1066 LeaveCriticalSection(&WDML_CritSect);
1072 LeaveCriticalSection(&WDML_CritSect);
1073 DispatchMessageW(&msg);
1079 TRACE("Timeout !!\n");
1081 EnterCriticalSection(&WDML_CritSect);
1083 pConv = WDML_GetConv(hConv, FALSE);
1086 if (pConv->transactions)
1088 switch (pConv->transactions->ddeMsg)
1090 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1091 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1092 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1093 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1094 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1095 default: err = DMLERR_INVALIDPARAMETER; break;
1098 pConv->instance->lastError = err;
1101 LeaveCriticalSection(&WDML_CritSect);
1106 /*****************************************************************
1107 * DdeClientTransaction (USER32.@)
1109 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1110 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1114 HDDEDATA hDdeData = 0;
1116 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1117 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1121 WARN("Invalid conversation handle NULL\n");
1125 EnterCriticalSection(&WDML_CritSect);
1127 pConv = WDML_GetConv(hConv, TRUE);
1130 /* cannot set error... cannot get back to DDE instance */
1137 /* Windows simply ignores hszItem and wFmt in this case */
1138 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1141 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1143 case XTYP_ADVSTART|XTYPF_NODATA:
1144 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1146 case XTYP_ADVSTART|XTYPF_ACKREQ:
1149 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1152 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1157 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1160 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1165 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1168 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1171 FIXME("Unknown transation\n");
1172 /* unknown transaction type */
1173 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1179 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1183 WDML_QueueTransaction(pConv, pXAct);
1185 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1187 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1188 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1189 pConv->wStatus &= ~ST_CONNECTED;
1190 WDML_UnQueueTransaction(pConv, pXAct);
1191 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1194 pXAct->dwTimeout = dwTimeout;
1195 /* FIXME: should set the app bits on *pdwResult */
1197 if (dwTimeout == TIMEOUT_ASYNC)
1201 *pdwResult = MAKELONG(0, pXAct->xActID);
1203 hDdeData = (HDDEDATA)1;
1209 count = WDML_CritSect.RecursionCount;
1210 for (i = 0; i < count; i++)
1211 LeaveCriticalSection(&WDML_CritSect);
1212 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct, pdwResult);
1213 for (i = 0; i < count; i++)
1214 EnterCriticalSection(&WDML_CritSect);
1216 LeaveCriticalSection(&WDML_CritSect);
1220 LeaveCriticalSection(&WDML_CritSect);
1224 /*****************************************************************
1225 * DdeAbandonTransaction (USER32.@)
1227 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1229 WDML_INSTANCE* pInstance;
1233 TRACE("(%08lx,%p,%08lx);\n", idInst, hConv, idTransaction);
1235 EnterCriticalSection(&WDML_CritSect);
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);
1269 LeaveCriticalSection(&WDML_CritSect);
1274 /******************************************************************
1277 * Window Proc created on client side for each conversation
1279 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1282 WDML_CONV* pConv = NULL;
1285 TRACE("%p %04x %08x %08lx\n", hwnd, iMsg, wParam , lParam);
1287 if (iMsg == WM_DDE_ACK &&
1288 /* in the initial WM_INITIATE sendmessage */
1289 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1291 /* In response to WM_DDE_INITIATE, save server window */
1293 WDML_INSTANCE* pInstance;
1295 /* note: sent messages do not need packing */
1296 uiLo = LOWORD(lParam);
1297 uiHi = HIWORD(lParam);
1299 /* FIXME: convlist should be handled here */
1302 /* we already have started the conv with a server, drop other replies */
1303 GlobalDeleteAtom(uiLo);
1304 GlobalDeleteAtom(uiHi);
1305 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1309 pInstance = WDML_GetInstanceFromWnd(hwnd);
1311 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1312 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1314 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1316 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1317 pConv->wStatus |= ST_CONNECTED;
1318 pConv->wConvst = XST_INIT1;
1320 /* check if server is handled by DDEML */
1321 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1322 lstrcmpiA(buf, WDML_szServerConvClassA) == 0) ||
1323 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1324 lstrcmpiW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1326 pConv->wStatus |= ST_ISLOCAL;
1329 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1331 GlobalDeleteAtom(uiLo);
1332 GlobalDeleteAtom(uiHi);
1334 /* accept conversation */
1338 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1340 EnterCriticalSection(&WDML_CritSect);
1342 pConv = WDML_GetConvFromWnd(hwnd);
1351 msg.wParam = wParam;
1352 msg.lParam = lParam;
1354 WDML_HandleReply(pConv, &msg, &hdd, NULL);
1357 LeaveCriticalSection(&WDML_CritSect);
1361 return IsWindowUnicode(hwnd) ? DefWindowProcW(hwnd, iMsg, wParam, lParam) :
1362 DefWindowProcA(hwnd, iMsg, wParam, lParam);
1365 /*****************************************************************
1366 * DdeDisconnect (USER32.@)
1368 BOOL WINAPI DdeDisconnect(HCONV hConv)
1370 WDML_CONV* pConv = NULL;
1375 TRACE("(%p)\n", hConv);
1379 WARN("DdeDisconnect(): hConv = 0\n");
1383 EnterCriticalSection(&WDML_CritSect);
1384 pConv = WDML_GetConv(hConv, TRUE);
1387 if (pConv->wStatus & ST_CLIENT)
1389 /* FIXME: should abandon all pending transactions */
1390 pXAct = WDML_ClientQueueTerminate(pConv);
1393 count = WDML_CritSect.RecursionCount;
1394 for (i = 0; i < count; i++)
1395 LeaveCriticalSection(&WDML_CritSect);
1396 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1397 (WPARAM)pConv->hwndClient, pXAct->lParam))
1398 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct, NULL);
1399 for (i = 0; i < count; i++)
1400 EnterCriticalSection(&WDML_CritSect);
1402 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1403 /* still have to destroy data assosiated with conversation */
1404 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1408 FIXME("Not implemented yet for a server side conversation\n");
1412 LeaveCriticalSection(&WDML_CritSect);
1417 /*****************************************************************
1418 * DdeImpersonateClient (USER32.@)
1420 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1425 TRACE("(%p)\n", hConv);
1427 EnterCriticalSection(&WDML_CritSect);
1428 pConv = WDML_GetConv(hConv, TRUE);
1431 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1433 LeaveCriticalSection(&WDML_CritSect);