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