[User32]
[reactos.git] / reactos / win32ss / user / user32 / misc / ddeclient.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 <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[] = {'D','D','E','M','L','U','n','i','c','o','d','e','C','l','i','e','n','t',0};
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 = NULL;
92 ATOM aSrv = 0, aTpc = 0;
93
94 ERR("(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 ERR("Done with INITIATE, but no Server window available %p\n", (pConv ? pConv->hwndServer : NULL));
184 pConv = NULL;
185 pInstance->lastError = DMLERR_NO_CONV_ESTABLISHED;
186 goto theEnd;
187 }
188 ERR("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 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 ERR("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 ERR("conversation no longer available\n");
1036 /* conversation no longer available... return failure */
1037 return 0;
1038 }
1039 ERR("Msg hWnd %p & Client %p\n",msg.hwnd,pConv->hwndClient);
1040 if (msg.hwnd == pConv->hwndClient)
1041 {
1042 /* check that either pXAct has been processed or no more xActions are pending */
1043 BOOL ret = (pConv->transactions == pXAct);
1044 if (WDML_HandleReply(pConv, &msg, &hdd, ack) == WDML_QS_HANDLED)
1045 {
1046 ERR("WDML_HandleReply returned WDML_QS_HANDLED\n");
1047 ret = TRUE;
1048 }
1049 else
1050 ret = (pConv->transactions == NULL || ret);
1051
1052 if (ret)
1053 {
1054 pConv->instance->lastError = hdd ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
1055 return hdd;
1056 }
1057 }
1058 else
1059 {
1060 ERR("Dispatching message\n");
1061 DispatchMessageW(&msg);
1062 }
1063 }
1064 }
1065 }
1066
1067 ERR("Timeout !!\n");
1068
1069 pConv = WDML_GetConv(hConv, FALSE);
1070 if (pConv != NULL)
1071 {
1072 if (pConv->transactions)
1073 {
1074 switch (pConv->transactions->ddeMsg)
1075 {
1076 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1077 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1078 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1079 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1080 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1081 default: err = DMLERR_INVALIDPARAMETER; break;
1082 }
1083
1084 pConv->instance->lastError = err;
1085 }
1086 }
1087
1088 return 0;
1089 }
1090
1091
1092 /*****************************************************************
1093 * WDML_ClientHandle
1094 */
1095 HDDEDATA WDML_ClientHandle(WDML_CONV *pConv, WDML_XACT *pXAct, DWORD dwTimeout, LPDWORD pdwResult)
1096 {
1097 HDDEDATA hDdeData;
1098
1099 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1100 {
1101 WARN("Failed posting message %x to %p (error=0x%x)\n",
1102 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1103 pConv->wStatus &= ~ST_CONNECTED;
1104 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1105 return 0;
1106 }
1107 pXAct->dwTimeout = dwTimeout;
1108 /* FIXME: should set the app bits on *pdwResult */
1109
1110 if (dwTimeout == TIMEOUT_ASYNC)
1111 {
1112 if (pdwResult)
1113 *pdwResult = MAKELONG(0, pXAct->xActID);
1114
1115 hDdeData = (HDDEDATA)1;
1116 }
1117 else
1118 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct, pdwResult);
1119
1120 return hDdeData;
1121 }
1122
1123
1124 /*****************************************************************
1125 * DdeClientTransaction (USER32.@)
1126 */
1127 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1128 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1129 {
1130 WDML_CONV* pConv;
1131 WDML_XACT* pXAct;
1132 HDDEDATA hDdeData = 0;
1133
1134 TRACE("(%p,%d,%p,%p,%x,%x,%d,%p)\n",
1135 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1136
1137 if (hConv == 0)
1138 {
1139 WARN("Invalid conversation handle NULL\n");
1140 return 0;
1141 }
1142
1143 pConv = WDML_GetConv(hConv, TRUE);
1144 if (pConv == NULL)
1145 {
1146 /* cannot set error... cannot get back to DDE instance */
1147 ERR("No Conv!\n");
1148 return 0;
1149 }
1150
1151 switch (wType)
1152 {
1153 case XTYP_EXECUTE:
1154 /* Windows simply ignores hszItem and wFmt in this case */
1155 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1156 if (pXAct == NULL)
1157 return 0;
1158 break;
1159 case XTYP_POKE:
1160 if (!hszItem)
1161 {
1162 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1163 return 0;
1164 }
1165 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1166 break;
1167 case XTYP_ADVSTART|XTYPF_NODATA:
1168 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1169 case XTYP_ADVSTART:
1170 case XTYP_ADVSTART|XTYPF_ACKREQ:
1171 if (pData)
1172 {
1173 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1174 return 0;
1175 }
1176 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1177 break;
1178 case XTYP_ADVSTOP:
1179 if (pData)
1180 {
1181 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1182 return 0;
1183 }
1184 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1185 break;
1186 case XTYP_REQUEST:
1187 if (pData || !hszItem)
1188 {
1189 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1190 return 0;
1191 }
1192 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1193 break;
1194 default:
1195 FIXME("Unknown transaction type %04x\n", wType);
1196 /* unknown transaction type */
1197 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1198 return 0;
1199 }
1200
1201 if (pXAct == NULL)
1202 {
1203 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1204 return 0;
1205 }
1206
1207 WDML_QueueTransaction(pConv, pXAct);
1208
1209 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1210
1211 if (pConv->wStatus & ST_BLOCKED)
1212 {
1213 TRACE("Transactions are blocked, add to the queue and exit\n");
1214 return (HDDEDATA)1;
1215 }
1216
1217 hDdeData = WDML_ClientHandle(pConv, pXAct, dwTimeout, pdwResult);
1218 if (dwTimeout != TIMEOUT_ASYNC)
1219 {
1220 WDML_UnQueueTransaction(pConv, pXAct);
1221 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1222 }
1223
1224 return hDdeData;
1225 }
1226
1227 /*****************************************************************
1228 * DdeAbandonTransaction (USER32.@)
1229 */
1230 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1231 {
1232 WDML_INSTANCE* pInstance;
1233 WDML_CONV* pConv;
1234 WDML_XACT* pXAct;
1235
1236 if ((pInstance = WDML_GetInstance(idInst)))
1237 {
1238 if (hConv)
1239 {
1240 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1241 {
1242
1243 pXAct = pConv->transactions;
1244 while (pXAct) {
1245 WDML_XACT *nextXAct = pXAct->next;
1246
1247 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1248 (idTransaction == 0 || pXAct->xActID == idTransaction))
1249 {
1250 WDML_UnQueueTransaction(pConv, pXAct);
1251 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1252 }
1253 pXAct = nextXAct;
1254 }
1255 }
1256 }
1257 else
1258 {
1259 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1260 {
1261 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1262 pXAct = pConv->transactions;
1263 while (pXAct) {
1264 WDML_XACT *nextXAct = pXAct->next;
1265
1266 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1267 {
1268 WDML_UnQueueTransaction(pConv, pXAct);
1269 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1270 }
1271 pXAct = nextXAct;
1272 }
1273 }
1274 }
1275 }
1276
1277 return TRUE;
1278 }
1279
1280 /******************************************************************
1281 * WDML_ClientProc
1282 *
1283 * Window Proc created on client side for each conversation
1284 */
1285 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1286 {
1287 UINT uiLo, uiHi;
1288 WDML_CONV* pConv = NULL;
1289 HSZ hszSrv, hszTpc;
1290 char buf[256];
1291 WDML_INSTANCE* pInstance;
1292
1293 ERR("%p %04x %08lx %08lx\n", hwnd, iMsg, wParam , lParam);
1294
1295 /* in the initial WM_INITIATE sendmessage */
1296 if (iMsg == WM_DDE_ACK &&
1297 (!(pConv = WDML_GetConvFromWnd(hwnd)) || pConv->wStatus == XST_INIT1))
1298 {
1299
1300 ERR("WM_DDE_ACK\n");
1301 /* In response to WM_DDE_INITIATE, save server window */
1302
1303 /* note: sent messages do not need packing */
1304 uiLo = LOWORD(lParam);
1305 uiHi = HIWORD(lParam);
1306
1307 /* FIXME: convlist should be handled here */
1308 if (pConv)
1309 {
1310 /* we already have started the conv with a server, drop other replies */
1311 GlobalDeleteAtom(uiLo);
1312 GlobalDeleteAtom(uiHi);
1313 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1314 return 0;
1315 }
1316
1317 pInstance = WDML_GetInstanceFromWnd(hwnd);
1318
1319 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1320 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1321
1322 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1323
1324 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1325 pConv->wStatus |= ST_CONNECTED;
1326 pConv->wConvst = XST_INIT1;
1327
1328 /* check if server is handled by DDEML */
1329 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1330 lstrcmpiA(buf, WDML_szServerConvClassA) == 0) ||
1331 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1332 lstrcmpiW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1333 {
1334 pConv->wStatus |= ST_ISLOCAL;
1335 }
1336
1337 GlobalDeleteAtom(uiLo);
1338 GlobalDeleteAtom(uiHi);
1339
1340 /* accept conversation */
1341 return 1;
1342 }
1343
1344 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1345 {
1346 pConv = WDML_GetConvFromWnd(hwnd);
1347
1348 if (pConv)
1349 {
1350 MSG msg;
1351 HDDEDATA hdd;
1352
1353 msg.hwnd = hwnd;
1354 msg.message = iMsg;
1355 msg.wParam = wParam;
1356 msg.lParam = lParam;
1357
1358 WDML_HandleReply(pConv, &msg, &hdd, NULL);
1359 }
1360
1361 return 0;
1362 }
1363
1364 return IsWindowUnicode(hwnd) ? DefWindowProcW(hwnd, iMsg, wParam, lParam) :
1365 DefWindowProcA(hwnd, iMsg, wParam, lParam);
1366 }
1367
1368 /*****************************************************************
1369 * DdeDisconnect (USER32.@)
1370 */
1371 BOOL WINAPI DdeDisconnect(HCONV hConv)
1372 {
1373 WDML_CONV* pConv = NULL;
1374 WDML_XACT* pXAct;
1375 BOOL ret = FALSE;
1376
1377 TRACE("(%p)\n", hConv);
1378
1379 if (hConv == 0)
1380 {
1381 WARN("DdeDisconnect(): hConv = 0\n");
1382 return FALSE;
1383 }
1384
1385 pConv = WDML_GetConv(hConv, TRUE);
1386 if (pConv != NULL)
1387 {
1388 if (pConv->wStatus & ST_CLIENT)
1389 {
1390 /* FIXME: should abandon all pending transactions */
1391 pXAct = WDML_ClientQueueTerminate(pConv);
1392 if (pXAct != NULL)
1393 {
1394 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1395 (WPARAM)pConv->hwndClient, pXAct->lParam))
1396 {
1397 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct, NULL);
1398 ret = TRUE;
1399 }
1400 else
1401 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1402
1403 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1404 /* still have to destroy data associated with conversation */
1405 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1406 }
1407 else
1408 {
1409 FIXME("Not implemented yet for a server side conversation\n");
1410 }
1411 }
1412 }
1413
1414 return ret;
1415 }
1416
1417 /*****************************************************************
1418 * DdeImpersonateClient (USER32.@)
1419 */
1420 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1421 {
1422 WDML_CONV* pConv;
1423 BOOL ret = FALSE;
1424
1425 TRACE("(%p)\n", hConv);
1426
1427 pConv = WDML_GetConv(hConv, TRUE);
1428 if (pConv)
1429 {
1430 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1431 }
1432 return ret;
1433 }