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