The TEB doesn't need execute permissions.
[wine] / dlls / user / dde_client.c
1 /*
2  * DDEML library
3  *
4  * Copyright 1997 Alexandre Julliard
5  * Copyright 1997 Len White
6  * Copyright 1999 Keith Matthews
7  * Copyright 2000 Corel
8  * Copyright 2001 Eric Pouech
9  * Copyright 2004, 2005 Dmitry Timoshkov
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winnls.h"
34 #include "dde.h"
35 #include "ddeml.h"
36 #include "win.h"
37 #include "wine/debug.h"
38 #include "dde_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
41
42 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM);    /* only for one client, not conv list */
43 const WCHAR WDML_szClientConvClass[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t',0};
44
45 /******************************************************************************
46  * DdeConnectList [USER32.@]  Establishes conversation with DDE servers
47  *
48  * PARAMS
49  *    idInst     [I] Instance identifier
50  *    hszService [I] Handle to service name string
51  *    hszTopic   [I] Handle to topic name string
52  *    hConvList  [I] Handle to conversation list
53  *    pCC        [I] Pointer to structure with context data
54  *
55  * RETURNS
56  *    Success: Handle to new conversation list
57  *    Failure: 0
58  */
59 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
60                                 HCONVLIST hConvList, PCONVCONTEXT pCC)
61 {
62     FIXME("(%ld,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
63     return (HCONVLIST)1;
64 }
65
66 /*****************************************************************
67  * DdeQueryNextServer [USER32.@]
68  */
69 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
70 {
71     FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
72     return 0;
73 }
74
75 /******************************************************************************
76  * DdeDisconnectList [USER32.@]  Destroys list and terminates conversations
77  *
78  *
79  * PARAMS
80  *    hConvList  [I] Handle to conversation list
81  *
82  * RETURNS
83  *    Success: TRUE
84  *    Failure: FALSE
85  */
86 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
87 {
88     FIXME("(%p): stub\n", hConvList);
89     return TRUE;
90 }
91
92 /*****************************************************************
93  *            DdeConnect   (USER32.@)
94  */
95 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
96                         PCONVCONTEXT pCC)
97 {
98     HWND                hwndClient;
99     WDML_INSTANCE*      pInstance;
100     WDML_CONV*          pConv = NULL;
101     ATOM                aSrv = 0, aTpc = 0;
102     WNDCLASSEXW         wndclass;
103
104     TRACE("(0x%lx,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
105
106     EnterCriticalSection(&WDML_CritSect);
107
108     pInstance = WDML_GetInstance(idInst);
109     if (!pInstance)
110     {
111         goto theEnd;
112     }
113
114     /* make sure this conv is never created */
115     pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
116     if (pConv != NULL)
117     {
118         ERR("This Conv already exists: (%p)\n", pConv);
119         goto theEnd;
120     }
121
122     /* we need to establish a conversation with
123        server, so create a window for it       */
124
125     wndclass.cbSize        = sizeof(wndclass);
126     wndclass.style         = 0;
127     wndclass.lpfnWndProc   = WDML_ClientProc;
128     wndclass.cbClsExtra    = 0;
129     wndclass.cbWndExtra    = 2 * sizeof(ULONG_PTR);
130     wndclass.hInstance     = 0;
131     wndclass.hIcon         = 0;
132     wndclass.hCursor       = 0;
133     wndclass.hbrBackground = 0;
134     wndclass.lpszMenuName  = NULL;
135     wndclass.lpszClassName = WDML_szClientConvClass;
136     wndclass.hIconSm       = 0;
137
138     RegisterClassExW(&wndclass);
139
140     hwndClient = CreateWindowW(WDML_szClientConvClass, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
141
142     SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
143
144     if (hszService)
145     {
146         aSrv = WDML_MakeAtomFromHsz(hszService);
147         if (!aSrv) goto theEnd;
148     }
149     if (hszTopic)
150     {
151         aTpc = WDML_MakeAtomFromHsz(hszTopic);
152         if (!aTpc) goto theEnd;
153     }
154
155     LeaveCriticalSection(&WDML_CritSect);
156
157     /* note: sent messages shall not use packing */
158     SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
159                          SMTO_ABORTIFHUNG, 2000, NULL );
160
161     EnterCriticalSection(&WDML_CritSect);
162
163     pInstance = WDML_GetInstance(idInst);
164     if (!pInstance)
165     {
166         goto theEnd;
167     }
168
169     /* At this point, Client WM_DDE_ACK should have saved hwndServer
170        for this instance id and hwndClient if server responds.
171        So get HCONV and return it. And add it to conv list */
172     pConv = WDML_GetConvFromWnd(hwndClient);
173     if (pConv == NULL || pConv->hwndServer == 0)
174     {
175         ERR("Done with INITIATE, but no Server window available\n");
176         pConv = NULL;
177         goto theEnd;
178     }
179     TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
180     pConv->wConvst = XST_CONNECTED;
181
182     /* finish init of pConv */
183     if (pCC != NULL)
184     {
185         pConv->convContext = *pCC;
186     }
187     else
188     {
189         memset(&pConv->convContext, 0, sizeof(pConv->convContext));
190         pConv->convContext.cb = sizeof(pConv->convContext);
191         pConv->convContext.iCodePage = CP_WINUNICODE;
192     }
193
194  theEnd:
195     LeaveCriticalSection(&WDML_CritSect);
196
197     if (aSrv) GlobalDeleteAtom(aSrv);
198     if (aTpc) GlobalDeleteAtom(aTpc);
199     return (HCONV)pConv;
200 }
201
202 /*****************************************************************
203  *            DdeReconnect   (DDEML.37)
204  *            DdeReconnect   (USER32.@)
205  */
206 HCONV WINAPI DdeReconnect(HCONV hConv)
207 {
208     WDML_CONV*  pConv;
209     WDML_CONV*  pNewConv = NULL;
210     ATOM        aSrv = 0, aTpc = 0;
211
212     TRACE("(%p)\n", hConv);
213
214     EnterCriticalSection(&WDML_CritSect);
215     pConv = WDML_GetConv(hConv, FALSE);
216     if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
217     {
218         BOOL    ret;
219
220         /* to reestablist a connection, we have to make sure that:
221          * 1/ pConv is the converstation attached to the client window (it wouldn't be
222          *    if a call to DdeReconnect would have already been done...)
223          *    FIXME: is this really an error ???
224          * 2/ the pConv conversation had really been deconnected
225          */
226         if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
227             (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
228         {
229             HWND        hwndClient = pConv->hwndClient;
230             HWND        hwndServer = pConv->hwndServer;
231             ATOM        aSrv, aTpc;
232
233             SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
234
235             aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
236             aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
237             if (!aSrv || !aTpc) goto theEnd;
238
239             LeaveCriticalSection(&WDML_CritSect);
240
241             /* note: sent messages shall not use packing */
242             ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
243                                MAKELPARAM(aSrv, aTpc));
244
245             EnterCriticalSection(&WDML_CritSect);
246
247             pConv = WDML_GetConv(hConv, FALSE);
248             if (pConv == NULL)
249             {
250                 FIXME("Should fail reconnection\n");
251                 goto theEnd;
252             }
253
254             if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
255             {
256                 /* re-establish all links... */
257                 WDML_LINK* pLink;
258
259                 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
260                 {
261                     if (pLink->hConv == hConv)
262                     {
263                         /* try to reestablish the links... */
264                         DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
265                                              pLink->transactionType, 1000, NULL);
266                     }
267                 }
268             }
269             else
270             {
271                 /* reset the conversation as it was */
272                 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
273             }
274         }
275     }
276
277  theEnd:
278     LeaveCriticalSection(&WDML_CritSect);
279
280     if (aSrv) GlobalDeleteAtom(aSrv);
281     if (aTpc) GlobalDeleteAtom(aTpc);
282     return (HCONV)pNewConv;
283 }
284
285 /******************************************************************
286  *              WDML_ClientQueueAdvise
287  *
288  * Creates and queue an WM_DDE_ADVISE transaction
289  */
290 static WDML_XACT*       WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
291 {
292     DDEADVISE*          pDdeAdvise;
293     WDML_XACT*          pXAct;
294     ATOM                atom;
295
296     TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
297
298     atom = WDML_MakeAtomFromHsz(hszItem);
299     if (!atom) return NULL;
300
301     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
302     if (!pXAct)
303     {
304         GlobalDeleteAtom(atom);
305         return NULL;
306     }
307
308     pXAct->wType = wType & ~0x0F;
309     pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
310     /* FIXME: hMem is unfreed for now... should be deleted in server */
311
312     /* pack DdeAdvise   */
313     pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
314     pDdeAdvise->fAckReq   = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
315     pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
316     pDdeAdvise->cfFormat  = wFmt;
317     GlobalUnlock(pXAct->hMem);
318
319     pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
320
321     return pXAct;
322 }
323
324 /******************************************************************
325  *              WDML_HandleAdviseReply
326  *
327  * handles the reply to an advise request
328  */
329 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
330 {
331     DDEACK              ddeAck;
332     UINT_PTR            uiLo, uiHi;
333     HSZ                 hsz;
334
335     if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
336     {
337         return WDML_QS_PASS;
338     }
339
340     UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
341     hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
342
343     if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
344         return WDML_QS_PASS;
345
346     GlobalDeleteAtom(uiHi);
347     FreeDDElParam(WM_DDE_ACK, msg->lParam);
348
349     WDML_ExtractAck(uiLo, &ddeAck);
350
351     if (ddeAck.fAck)
352     {
353         WDML_LINK*      pLink;
354
355         /* billx: first to see if the link is already created. */
356         pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
357                               pXAct->hszItem, TRUE, pXAct->wFmt);
358         if (pLink != NULL)
359         {
360             /* we found a link, and only need to modify it in case it changes */
361             pLink->transactionType = pXAct->wType;
362         }
363         else
364         {
365             WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
366                          pXAct->wType, pXAct->hszItem, pXAct->wFmt);
367         }
368         pXAct->hDdeData = (HDDEDATA)1;
369     }
370     else
371     {
372         TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
373         GlobalFree(pXAct->hMem);
374         pXAct->hDdeData = NULL;
375     }
376
377     return WDML_QS_HANDLED;
378 }
379
380 /******************************************************************
381  *              WDML_ClientQueueUnadvise
382  *
383  * queues an unadvise transaction
384  */
385 static WDML_XACT*       WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
386 {
387     WDML_XACT*  pXAct;
388     ATOM        atom;
389
390     TRACE("XTYP_ADVSTOP transaction\n");
391
392     atom = WDML_MakeAtomFromHsz(hszItem);
393     if (!atom) return NULL;
394
395     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
396     if (!pXAct)
397     {
398         GlobalDeleteAtom(atom);
399         return NULL;
400     }
401
402     /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
403      * on the specified item.
404      */
405     pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
406     return pXAct;
407 }
408
409 /******************************************************************
410  *              WDML_HandleUnadviseReply
411  *
412  *
413  */
414 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
415 {
416     DDEACK      ddeAck;
417     UINT_PTR    uiLo, uiHi;
418     HSZ         hsz;
419
420     if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
421     {
422         return WDML_QS_PASS;
423     }
424
425     UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
426     hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
427
428     if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
429         return WDML_QS_PASS;
430
431     FreeDDElParam(WM_DDE_ACK, msg->lParam);
432     GlobalDeleteAtom(uiHi);
433
434     WDML_ExtractAck(uiLo, &ddeAck);
435
436     TRACE("WM_DDE_ACK received while waiting for a timeout\n");
437
438     if (!ddeAck.fAck)
439     {
440         TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
441         pXAct->hDdeData = NULL;
442     }
443     else
444     {
445         /* billx: remove the link */
446         WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
447                         pXAct->hszItem, pXAct->wFmt);
448         pXAct->hDdeData = (HDDEDATA)1;
449     }
450     return WDML_QS_HANDLED;
451 }
452
453 /******************************************************************
454  *              WDML_ClientQueueRequest
455  *
456  *
457  */
458 static WDML_XACT*       WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
459 {
460     WDML_XACT*  pXAct;
461     ATOM        atom;
462
463     TRACE("XTYP_REQUEST transaction\n");
464
465     atom = WDML_MakeAtomFromHsz(hszItem);
466     if (!atom) return NULL;
467
468     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
469     if (!pXAct)
470     {
471         GlobalDeleteAtom(atom);
472         return NULL;
473     }
474
475     pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
476
477     return pXAct;
478 }
479
480 /******************************************************************
481  *              WDML_HandleRequestReply
482  *
483  *
484  */
485 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
486 {
487     DDEACK              ddeAck;
488     WINE_DDEHEAD        wdh;
489     UINT_PTR            uiLo, uiHi;
490     HSZ                 hsz;
491
492     if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
493         return WDML_QS_PASS;
494
495     switch (msg->message)
496     {
497     case WM_DDE_ACK:
498         UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
499         FreeDDElParam(WM_DDE_ACK, msg->lParam);
500         GlobalDeleteAtom(uiHi);
501         WDML_ExtractAck(uiLo, &ddeAck);
502         pXAct->hDdeData = 0;
503         if (ddeAck.fAck)
504             ERR("Positive answer should appear in NACK for a request, assuming negative\n");
505         TRACE("Negative answer...\n");
506         break;
507
508     case WM_DDE_DATA:
509         UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
510         TRACE("Got the result (%08x)\n", uiLo);
511
512         hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
513
514         if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
515             return WDML_QS_PASS;
516
517         pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
518         if (wdh.fRelease)
519         {
520             GlobalFree((HGLOBAL)uiLo);
521         }
522         if (wdh.fAckReq)
523         {
524             WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
525         }
526         else
527         {
528             GlobalDeleteAtom(uiHi);
529             FreeDDElParam(WM_DDE_ACK, msg->lParam);
530         }
531         break;
532
533     default:
534         FreeDDElParam(msg->message, msg->lParam);
535         return WDML_QS_PASS;
536     }
537
538     return WDML_QS_HANDLED;
539 }
540
541 /******************************************************************
542  *              WDML_BuildExecuteCommand
543  *
544  * Creates a DDE block suitable for sending in WM_DDE_COMMAND
545  * It also takes care of string conversion between the two window procedures
546  */
547 static  HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
548 {
549     HGLOBAL     hMem;
550     BOOL        clientUnicode, serverUnicode;
551     DWORD       memSize;
552
553     clientUnicode = IsWindowUnicode(pConv->hwndClient);
554     serverUnicode = IsWindowUnicode(pConv->hwndServer);
555
556     if (clientUnicode == serverUnicode)
557     {
558         memSize = cbData;
559     }
560     else
561     {
562         if (clientUnicode)
563         {
564             memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
565         }
566         else
567         {
568             memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
569         }
570     }
571
572     hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
573
574     if (hMem)
575     {
576         LPBYTE  pDst;
577
578         pDst = GlobalLock(hMem);
579         if (pDst)
580         {
581             if (clientUnicode == serverUnicode)
582             {
583                 memcpy(pDst, pData, cbData);
584             }
585             else
586             {
587                 if (clientUnicode)
588                 {
589                     WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
590                 }
591                 else
592                 {
593                     MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
594                 }
595             }
596
597             GlobalUnlock(hMem);
598         }
599         else
600         {
601             GlobalFree(hMem);
602             hMem = 0;
603         }
604     }
605     return hMem;
606 }
607
608 /******************************************************************
609  *              WDML_ClientQueueExecute
610  *
611  *
612  */
613 static WDML_XACT*       WDML_ClientQueueExecute(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
614 {
615     WDML_XACT*  pXAct;
616
617     TRACE("XTYP_EXECUTE transaction\n");
618
619     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
620     if (!pXAct)
621         return NULL;
622
623     if (cbData == (DWORD)-1)
624     {
625         HDDEDATA        hDdeData = (HDDEDATA)pData;
626
627         pData = DdeAccessData(hDdeData, &cbData);
628         if (pData)
629         {
630             pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
631             DdeUnaccessData(hDdeData);
632         }
633     }
634     else
635     {
636         pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
637     }
638
639     pXAct->lParam = (LPARAM)pXAct->hMem;
640
641     return pXAct;
642 }
643
644 /******************************************************************
645  *              WDML_HandleExecuteReply
646  *
647  *
648  */
649 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
650 {
651     DDEACK      ddeAck;
652     UINT_PTR    uiLo, uiHi;
653
654     if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
655     {
656         return WDML_QS_PASS;
657     }
658
659     UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
660     FreeDDElParam(WM_DDE_ACK, msg->lParam);
661
662     if ((HANDLE)uiHi != pXAct->hMem)
663     {
664         return WDML_QS_PASS;
665     }
666
667     WDML_ExtractAck(uiLo, &ddeAck);
668     pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
669
670     TRACE("hDdeData = %p\n", pXAct->hDdeData);
671     pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
672
673     return WDML_QS_HANDLED;
674 }
675
676 /******************************************************************
677  *              WDML_ClientQueuePoke
678  *
679  *
680  */
681 static WDML_XACT*       WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
682                                              UINT wFmt, HSZ hszItem)
683 {
684     WDML_XACT*  pXAct;
685     ATOM        atom;
686
687     TRACE("XTYP_POKE transaction\n");
688
689     atom = WDML_MakeAtomFromHsz(hszItem);
690     if (!atom) return NULL;
691
692     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
693     if (!pXAct)
694     {
695         GlobalDeleteAtom(atom);
696         return NULL;
697     }
698
699     if (cbData == (DWORD)-1)
700     {
701         pXAct->hMem = (HDDEDATA)pData;
702     }
703     else
704     {
705         DDEPOKE*        ddePoke;
706
707         pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
708         ddePoke = GlobalLock(pXAct->hMem);
709         if (ddePoke)
710         {
711             memcpy(ddePoke->Value, pData, cbData);
712             ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
713             ddePoke->cfFormat = wFmt;
714             GlobalUnlock(pXAct->hMem);
715         }
716     }
717
718     pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
719
720     return pXAct;
721 }
722
723 /******************************************************************
724  *              WDML_HandlePokeReply
725  *
726  *
727  */
728 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
729 {
730     DDEACK      ddeAck;
731     UINT_PTR    uiLo, uiHi;
732     HSZ         hsz;
733
734     if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
735     {
736         return WDML_QS_PASS;
737     }
738
739     UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
740     hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
741     if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
742     {
743         return WDML_QS_PASS;
744     }
745     FreeDDElParam(WM_DDE_ACK, msg->lParam);
746     GlobalDeleteAtom(uiHi);
747
748     WDML_ExtractAck(uiLo, &ddeAck);
749     GlobalFree(pXAct->hMem);
750
751     pXAct->hDdeData = (HDDEDATA)TRUE;
752     return TRUE;
753 }
754
755 /******************************************************************
756  *              WDML_ClientQueueTerminate
757  *
758  * Creates and queue an WM_DDE_TERMINATE transaction
759  */
760 static WDML_XACT*       WDML_ClientQueueTerminate(WDML_CONV* pConv)
761 {
762     WDML_XACT*          pXAct;
763
764     pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
765     if (!pXAct)
766         return NULL;
767
768     pXAct->lParam = 0;
769     pConv->wStatus &= ~ST_CONNECTED;
770
771     return pXAct;
772 }
773
774 /******************************************************************
775  *              WDML_HandleTerminateReply
776  *
777  * handles the reply to a terminate request
778  */
779 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
780 {
781     if (msg->message != WM_DDE_TERMINATE)
782     {
783         /* FIXME: should delete data passed here */
784         return WDML_QS_SWALLOWED;
785     }
786
787     if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
788     {
789         FIXME("hmmm shouldn't happen\n");
790         return WDML_QS_PASS;
791     }
792     if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
793     {
794         WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
795                             0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
796     }
797     WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
798     return WDML_QS_HANDLED;
799 }
800
801 /******************************************************************
802  *              WDML_HandleReplyData
803  *
804  *
805  */
806 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
807 {
808     UINT_PTR            uiLo, uiHi;
809     HDDEDATA            hDdeDataIn, hDdeDataOut;
810     WDML_LINK*          pLink;
811     WINE_DDEHEAD        wdh;
812     HSZ                 hsz;
813
814     TRACE("WM_DDE_DATA message received in the Client Proc!\n");
815     /* wParam -- sending window handle  */
816     /* lParam -- hDdeData & item HSZ    */
817
818     UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
819     hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
820
821     hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
822
823     /* billx:
824      *  For hot link, data should be passed to its callback with
825      * XTYP_ADVDATA and callback should return the proper status.
826      */
827     pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
828                           uiLo ? TRUE : FALSE, wdh.cfFormat);
829     if (!pLink)
830     {
831         WDML_DecHSZ(pConv->instance, hsz);
832         DdeFreeDataHandle(hDdeDataIn);
833         return WDML_QS_PASS;
834     }
835
836     if (hDdeDataIn != 0 && wdh.fAckReq)
837     {
838         WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
839         if (msg->lParam)
840             msg->lParam = 0;
841     }
842     else
843     {
844         GlobalDeleteAtom(uiHi);
845     }
846
847     hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
848                                       pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
849
850     if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
851     {
852         if (uiLo) GlobalFree((HANDLE)uiLo);
853     }
854
855     DdeFreeDataHandle(hDdeDataIn);
856
857     WDML_DecHSZ(pConv->instance, hsz);
858     if (msg->lParam)
859         FreeDDElParam(WM_DDE_DATA, msg->lParam);
860
861     return WDML_QS_HANDLED;
862 }
863
864 /******************************************************************
865  *              WDML_HandleIncomingTerminate
866  *
867  *
868  */
869 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
870 {
871     if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
872         return WDML_QS_PASS;
873
874     pConv->wStatus |= ST_TERMINATED;
875     if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
876     {
877         WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
878                             0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
879     }
880     if (pConv->wStatus & ST_CONNECTED)
881     {
882         /* don't care about result code (if server exists or not) */
883         PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
884         pConv->wStatus &= ~ST_CONNECTED;
885     }
886     /* have to keep connection around to allow reconnection */
887     return WDML_QS_HANDLED;
888 }
889
890 /******************************************************************
891  *              WDML_HandleReply
892  *
893  * handles any incoming reply, and try to match to an already sent request
894  */
895 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
896 {
897     WDML_XACT*          pXAct = pConv->transactions;
898     WDML_QUEUE_STATE    qs;
899
900     if (pConv->transactions)
901     {
902         /* first check message against a pending transaction, if any */
903         switch (pXAct->ddeMsg)
904         {
905         case WM_DDE_ADVISE:
906             qs = WDML_HandleAdviseReply(pConv, msg, pXAct);
907             break;
908         case WM_DDE_UNADVISE:
909             qs = WDML_HandleUnadviseReply(pConv, msg, pXAct);
910             break;
911         case WM_DDE_EXECUTE:
912             qs = WDML_HandleExecuteReply(pConv, msg, pXAct);
913             break;
914         case WM_DDE_REQUEST:
915             qs = WDML_HandleRequestReply(pConv, msg, pXAct);
916             break;
917         case WM_DDE_POKE:
918             qs = WDML_HandlePokeReply(pConv, msg, pXAct);
919             break;
920         case WM_DDE_TERMINATE:
921             qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
922             break;
923         default:
924             qs = WDML_QS_ERROR;
925             FIXME("oooch\n");
926         }
927     }
928     else
929     {
930         qs = WDML_QS_PASS;
931     }
932
933     /* now check the results */
934     switch (qs)
935     {
936     case WDML_QS_ERROR:
937     case WDML_QS_SWALLOWED:
938         *hdd = 0;
939         break;
940     case WDML_QS_HANDLED:
941         /* ok, we have resolved a pending transaction
942          * notify callback if asynchronous, and remove it in any case
943          */
944         WDML_UnQueueTransaction(pConv, pXAct);
945         if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
946         {
947             WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
948                                 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
949                                 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
950             qs = WDML_QS_PASS;
951         }
952         else
953         {
954             *hdd = pXAct->hDdeData;
955         }
956         WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
957         break;
958     case WDML_QS_PASS:
959         /* no pending transaction found, try a warm/hot link or a termination request */
960         switch (msg->message)
961         {
962         case WM_DDE_DATA:
963             qs = WDML_HandleIncomingData(pConv, msg, hdd);
964             break;
965         case WM_DDE_TERMINATE:
966             qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
967             break;
968         }
969         break;
970     case WDML_QS_BLOCK:
971         FIXME("shouldn't be used on client side\n");
972         break;
973     }
974
975     return qs;
976 }
977
978 /******************************************************************
979  *              WDML_SyncWaitTransactionReply
980  *
981  * waits until an answer for a sent request is received
982  * time out is also handled. only used for synchronous transactions
983  */
984 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct)
985 {
986     DWORD       dwTime;
987     DWORD       err;
988     WDML_CONV*  pConv;
989
990     TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
991
992     /* FIXME: time 32 bit wrap around */
993     dwTimeout += GetCurrentTime();
994
995     while ((dwTime = GetCurrentTime()) < dwTimeout)
996     {
997         /* we cannot be in the crit sect all the time because when client and server run in a
998          * single process they need to share the access to the internal data
999          */
1000         if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1001                                       dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1002         {
1003             BOOL        ret = FALSE;
1004             MSG         msg;
1005             WDML_CONV*  pConv;
1006             HDDEDATA    hdd;
1007
1008             EnterCriticalSection(&WDML_CritSect);
1009
1010             pConv = WDML_GetConv(hConv, FALSE);
1011             if (pConv == NULL)
1012             {
1013                 LeaveCriticalSection(&WDML_CritSect);
1014                 /* conversation no longer available... return failure */
1015                 break;
1016             }
1017             while (PeekMessageW(&msg, pConv->hwndClient, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1018             {
1019                 /* check that either pXAct has been processed or no more xActions are pending */
1020                 ret = (pConv->transactions == pXAct);
1021                 ret = WDML_HandleReply(pConv, &msg, &hdd) == WDML_QS_HANDLED &&
1022                     (pConv->transactions == NULL || ret);
1023                 if (ret) break;
1024             }
1025             LeaveCriticalSection(&WDML_CritSect);
1026             if (ret)
1027             {
1028                 return hdd;
1029             }
1030         }
1031     }
1032
1033     TRACE("Timeout !!\n");
1034
1035     EnterCriticalSection(&WDML_CritSect);
1036
1037     pConv = WDML_GetConv(hConv, FALSE);
1038     if (pConv != NULL)
1039     {
1040         if (pConv->transactions)
1041         {
1042             switch (pConv->transactions->ddeMsg)
1043             {
1044             case WM_DDE_ADVISE:         err = DMLERR_ADVACKTIMEOUT;     break;
1045             case WM_DDE_REQUEST:        err = DMLERR_DATAACKTIMEOUT;    break;
1046             case WM_DDE_EXECUTE:        err = DMLERR_EXECACKTIMEOUT;    break;
1047             case WM_DDE_POKE:           err = DMLERR_POKEACKTIMEOUT;    break;
1048             case WM_DDE_UNADVISE:       err = DMLERR_UNADVACKTIMEOUT;   break;
1049             default:                    err = DMLERR_INVALIDPARAMETER;  break;
1050             }
1051
1052             pConv->instance->lastError = err;
1053         }
1054     }
1055     LeaveCriticalSection(&WDML_CritSect);
1056
1057     return 0;
1058 }
1059
1060 /*****************************************************************
1061  *            DdeClientTransaction  (USER32.@)
1062  */
1063 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1064                                      UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1065 {
1066     WDML_CONV*          pConv;
1067     WDML_XACT*          pXAct;
1068     HDDEDATA            hDdeData = 0;
1069
1070     TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1071           pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1072
1073     if (hConv == 0)
1074     {
1075         ERR("Invalid conversation handle\n");
1076         return 0;
1077     }
1078
1079     EnterCriticalSection(&WDML_CritSect);
1080
1081     pConv = WDML_GetConv(hConv, TRUE);
1082     if (pConv == NULL)
1083     {
1084         /* cannot set error... cannot get back to DDE instance */
1085         goto theError;
1086     }
1087
1088     switch (wType)
1089     {
1090     case XTYP_EXECUTE:
1091         if (hszItem != 0 || wFmt != 0)
1092         {
1093             pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1094             goto theError;
1095         }
1096         pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1097         break;
1098     case XTYP_POKE:
1099         pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1100         break;
1101     case XTYP_ADVSTART|XTYPF_NODATA:
1102     case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1103     case XTYP_ADVSTART:
1104     case XTYP_ADVSTART|XTYPF_ACKREQ:
1105         if (pData)
1106         {
1107             pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1108             goto theError;
1109         }
1110         pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1111         break;
1112     case XTYP_ADVSTOP:
1113         if (pData)
1114         {
1115             pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1116             goto theError;
1117         }
1118         pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1119         break;
1120     case XTYP_REQUEST:
1121         if (pData)
1122         {
1123             pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1124             goto theError;
1125         }
1126         pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1127         break;
1128     default:
1129         FIXME("Unknown transation\n");
1130         /* unknown transaction type */
1131         pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1132         goto theError;
1133     }
1134
1135     if (pXAct == NULL)
1136     {
1137         pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1138         goto theError;
1139     }
1140
1141     WDML_QueueTransaction(pConv, pXAct);
1142
1143     if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1144     {
1145         WARN("Failed posting message %x to %p (error=0x%lx)\n",
1146               pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1147         pConv->wStatus &= ~ST_CONNECTED;
1148         WDML_UnQueueTransaction(pConv, pXAct);
1149         WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1150         goto theError;
1151     }
1152     pXAct->dwTimeout = dwTimeout;
1153     /* FIXME: should set the app bits on *pdwResult */
1154
1155     if (dwTimeout == TIMEOUT_ASYNC)
1156     {
1157         if (pdwResult)
1158         {
1159             *pdwResult = MAKELONG(0, pXAct->xActID);
1160         }
1161         hDdeData = (HDDEDATA)1;
1162     }
1163     else
1164     {
1165         DWORD   count, i;
1166
1167         if (pdwResult)
1168         {
1169             *pdwResult = 0L;
1170         }
1171         count = WDML_CritSect.RecursionCount;
1172         for (i = 0; i < count; i++)
1173             LeaveCriticalSection(&WDML_CritSect);
1174         hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct);
1175         for (i = 0; i < count; i++)
1176             EnterCriticalSection(&WDML_CritSect);
1177     }
1178     LeaveCriticalSection(&WDML_CritSect);
1179
1180     return hDdeData;
1181  theError:
1182     LeaveCriticalSection(&WDML_CritSect);
1183     return 0;
1184 }
1185
1186 /*****************************************************************
1187  *            DdeAbandonTransaction (USER32.@)
1188  */
1189 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1190 {
1191     WDML_INSTANCE*      pInstance;
1192     WDML_CONV*          pConv;
1193     WDML_XACT*          pXAct;
1194
1195     TRACE("(%08lx,%p,%08ld);\n", idInst, hConv, idTransaction);
1196
1197     EnterCriticalSection(&WDML_CritSect);
1198     if ((pInstance = WDML_GetInstance(idInst)))
1199     {
1200         if (hConv)
1201         {
1202             if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1203             {
1204                 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1205                 {
1206                     if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1207                         (idTransaction == 0 || pXAct->xActID == idTransaction))
1208                     {
1209                         WDML_UnQueueTransaction(pConv, pXAct);
1210                         WDML_FreeTransaction(pInstance, pXAct, TRUE);
1211                     }
1212                 }
1213             }
1214         }
1215         else
1216         {
1217             for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1218             {
1219                 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1220                 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1221                 {
1222                     if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1223                     {
1224                         WDML_UnQueueTransaction(pConv, pXAct);
1225                         WDML_FreeTransaction(pInstance, pXAct, TRUE);
1226                     }
1227                 }
1228             }
1229         }
1230     }
1231     LeaveCriticalSection(&WDML_CritSect);
1232
1233     return TRUE;
1234 }
1235
1236 /******************************************************************
1237  *              WDML_ClientProc
1238  *
1239  * Window Proc created on client side for each conversation
1240  */
1241 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1242 {
1243     UINT        uiLo, uiHi;
1244     WDML_CONV*  pConv = NULL;
1245     HSZ         hszSrv, hszTpc;
1246
1247     TRACE("%p %04x %08x %08lx\n", hwnd, iMsg, wParam , lParam);
1248
1249     if (iMsg == WM_DDE_ACK &&
1250         /* in the initial WM_INITIATE sendmessage */
1251         ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1252     {
1253         /* In response to WM_DDE_INITIATE, save server window  */
1254         WCHAR           buf[256];
1255         WDML_INSTANCE*  pInstance;
1256
1257         /* note: sent messages do not need packing */
1258         uiLo = LOWORD(lParam);
1259         uiHi = HIWORD(lParam);
1260
1261         /* FIXME: convlist should be handled here */
1262         if (pConv)
1263         {
1264             /* we already have started the conv with a server, drop other replies */
1265             GlobalDeleteAtom(uiLo);
1266             GlobalDeleteAtom(uiHi);
1267             PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1268             return 0;
1269         }
1270
1271         pInstance = WDML_GetInstanceFromWnd(hwnd);
1272
1273         hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1274         hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1275
1276         pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1277
1278         SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1279         pConv->wStatus |= ST_CONNECTED;
1280         pConv->wConvst = XST_INIT1;
1281
1282         /* check if server is handled by DDEML */
1283         if (GetClassNameW((HWND)wParam, buf, sizeof(buf)/sizeof(WCHAR)) &&
1284              lstrcmpiW(buf, WDML_szServerConvClass) == 0)
1285         {
1286             pConv->wStatus |= ST_ISLOCAL;
1287         }
1288
1289         WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1290
1291         GlobalDeleteAtom(uiLo);
1292         GlobalDeleteAtom(uiHi);
1293
1294         /* accept conversation */
1295         return 1;
1296     }
1297
1298     if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1299     {
1300         EnterCriticalSection(&WDML_CritSect);
1301
1302         pConv = WDML_GetConvFromWnd(hwnd);
1303
1304         if (pConv)
1305         {
1306             MSG         msg;
1307             HDDEDATA    hdd;
1308
1309             msg.hwnd = hwnd;
1310             msg.message = iMsg;
1311             msg.wParam = wParam;
1312             msg.lParam = lParam;
1313
1314             WDML_HandleReply(pConv, &msg, &hdd);
1315         }
1316
1317         LeaveCriticalSection(&WDML_CritSect);
1318         return 0;
1319     }
1320
1321     return DefWindowProcW(hwnd, iMsg, wParam, lParam);
1322 }
1323
1324 /*****************************************************************
1325  *            DdeDisconnect   (USER32.@)
1326  */
1327 BOOL WINAPI DdeDisconnect(HCONV hConv)
1328 {
1329     WDML_CONV*  pConv = NULL;
1330     WDML_XACT*  pXAct;
1331     DWORD       count, i;
1332     BOOL        ret = FALSE;
1333
1334     TRACE("(%p)\n", hConv);
1335
1336     if (hConv == 0)
1337     {
1338         ERR("DdeDisconnect(): hConv = 0\n");
1339         return FALSE;
1340     }
1341
1342     EnterCriticalSection(&WDML_CritSect);
1343     pConv = WDML_GetConv(hConv, TRUE);
1344     if (pConv != NULL)
1345     {
1346         if (pConv->wStatus & ST_CLIENT)
1347         {
1348             /* FIXME: should abandon all pending transactions */
1349             pXAct = WDML_ClientQueueTerminate(pConv);
1350             if (pXAct != NULL)
1351             {
1352                 count = WDML_CritSect.RecursionCount;
1353                 for (i = 0; i < count; i++)
1354                     LeaveCriticalSection(&WDML_CritSect);
1355                 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1356                                  (WPARAM)pConv->hwndClient, pXAct->lParam))
1357                     WDML_SyncWaitTransactionReply(hConv, 10000, pXAct);
1358                 for (i = 0; i < count; i++)
1359                     EnterCriticalSection(&WDML_CritSect);
1360                 ret = TRUE;
1361                 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1362                 /* still have to destroy data assosiated with conversation */
1363                 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1364             }
1365             else
1366             {
1367                 FIXME("Not implemented yet for a server side conversation\n");
1368             }
1369         }
1370     }
1371     LeaveCriticalSection(&WDML_CritSect);
1372
1373     return ret;
1374 }
1375
1376 /*****************************************************************
1377  *            DdeImpersonateClient (USER32.@)
1378  */
1379 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1380 {
1381     WDML_CONV*  pConv;
1382     BOOL        ret = FALSE;
1383
1384     TRACE("(%p)\n", hConv);
1385
1386     EnterCriticalSection(&WDML_CritSect);
1387     pConv = WDML_GetConv(hConv, TRUE);
1388     if (pConv)
1389     {
1390         ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1391     }
1392     LeaveCriticalSection(&WDML_CritSect);
1393     return ret;
1394 }