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