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