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