Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / dlls / user / dde / client.c
blob077b2b9387d2b9b2e30e5786b0936521b08a2901
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * DDEML library
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
9 * Copyright 2000 Corel
10 * Copyright 2001 Eric Pouech
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <string.h>
28 #include "winbase.h"
29 #include "windef.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/dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
43 const char WDML_szClientConvClassA[] = "DdeClientAnsi";
44 const WCHAR WDML_szClientConvClassW[] = {'D','d','e','C','l','i','e','n','t','U','n','i','c','o','d','e',0};
46 /******************************************************************************
47 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
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
56 * RETURNS
57 * Success: Handle to new conversation list
58 * Failure: 0
60 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
61 HCONVLIST hConvList, LPCONVCONTEXT pCC)
63 FIXME("(%ld,%d,%d,%d,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
64 return (HCONVLIST)1;
67 /*****************************************************************
68 * DdeQueryNextServer [USER32.@]
70 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
72 FIXME("(%d,%d): stub\n", hConvList, hConvPrev);
73 return 0;
76 /******************************************************************************
77 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
80 * PARAMS
81 * hConvList [I] Handle to conversation list
83 * RETURNS
84 * Success: TRUE
85 * Failure: FALSE
87 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
89 FIXME("(%d): stub\n", hConvList);
90 return TRUE;
93 /*****************************************************************
94 * DdeConnect (USER32.@)
96 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
97 LPCONVCONTEXT pCC)
99 HWND hwndClient;
100 WDML_INSTANCE* pInstance;
101 WDML_CONV* pConv = NULL;
102 ATOM aSrv = 0, aTpc = 0;
104 TRACE("(0x%lx,0x%x,0x%x,%p)\n", idInst, hszService, hszTopic, pCC);
106 EnterCriticalSection(&WDML_CritSect);
108 pInstance = WDML_GetInstance(idInst);
109 if (!pInstance)
111 goto theEnd;
114 /* make sure this conv is never created */
115 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
116 if (pConv != NULL)
118 ERR("This Conv already exists: (0x%lx)\n", (DWORD)pConv);
119 goto theEnd;
122 /* we need to establish a conversation with
123 server, so create a window for it */
125 if (pInstance->unicode)
127 WNDCLASSEXW wndclass;
129 wndclass.cbSize = sizeof(wndclass);
130 wndclass.style = 0;
131 wndclass.lpfnWndProc = WDML_ClientProc;
132 wndclass.cbClsExtra = 0;
133 wndclass.cbWndExtra = 2 * sizeof(DWORD);
134 wndclass.hInstance = 0;
135 wndclass.hIcon = 0;
136 wndclass.hCursor = 0;
137 wndclass.hbrBackground = 0;
138 wndclass.lpszMenuName = NULL;
139 wndclass.lpszClassName = WDML_szClientConvClassW;
140 wndclass.hIconSm = 0;
142 RegisterClassExW(&wndclass);
144 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
146 else
148 WNDCLASSEXA wndclass;
150 wndclass.cbSize = sizeof(wndclass);
151 wndclass.style = 0;
152 wndclass.lpfnWndProc = WDML_ClientProc;
153 wndclass.cbClsExtra = 0;
154 wndclass.cbWndExtra = 2 * sizeof(DWORD);
155 wndclass.hInstance = 0;
156 wndclass.hIcon = 0;
157 wndclass.hCursor = 0;
158 wndclass.hbrBackground = 0;
159 wndclass.lpszMenuName = NULL;
160 wndclass.lpszClassName = WDML_szClientConvClassA;
161 wndclass.hIconSm = 0;
163 RegisterClassExA(&wndclass);
165 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
168 SetWindowLongA(hwndClient, GWL_WDML_INSTANCE, (DWORD)pInstance);
170 if (hszService)
172 aSrv = WDML_MakeAtomFromHsz(hszService);
173 if (!aSrv) goto theEnd;
175 if (hszTopic)
177 aTpc = WDML_MakeAtomFromHsz(hszTopic);
178 if (!aTpc) goto theEnd;
181 LeaveCriticalSection(&WDML_CritSect);
183 /* note: sent messages shall not use packing */
184 SendMessageA(HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc));
186 EnterCriticalSection(&WDML_CritSect);
188 pInstance = WDML_GetInstance(idInst);
189 if (!pInstance)
191 goto theEnd;
194 /* At this point, Client WM_DDE_ACK should have saved hwndServer
195 for this instance id and hwndClient if server responds.
196 So get HCONV and return it. And add it to conv list */
197 pConv = WDML_GetConvFromWnd(hwndClient);
198 if (pConv == NULL || pConv->hwndServer == 0)
200 ERR("Done with INITIATE, but no Server window available\n");
201 pConv = NULL;
202 goto theEnd;
204 TRACE("Connected to Server window (%x)\n", pConv->hwndServer);
205 pConv->wConvst = XST_CONNECTED;
207 /* finish init of pConv */
208 if (pCC != NULL)
210 pConv->convContext = *pCC;
212 else
214 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
215 pConv->convContext.cb = sizeof(pConv->convContext);
216 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
219 theEnd:
220 LeaveCriticalSection(&WDML_CritSect);
222 if (aSrv) GlobalDeleteAtom(aSrv);
223 if (aTpc) GlobalDeleteAtom(aTpc);
224 return (HCONV)pConv;
227 /*****************************************************************
228 * DdeReconnect (DDEML.37)
229 * DdeReconnect (USER32.@)
231 HCONV WINAPI DdeReconnect(HCONV hConv)
233 WDML_CONV* pConv;
234 WDML_CONV* pNewConv = NULL;
235 ATOM aSrv = 0, aTpc = 0;
237 EnterCriticalSection(&WDML_CritSect);
238 pConv = WDML_GetConv(hConv, FALSE);
239 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
241 BOOL ret;
243 /* to reestablist a connection, we have to make sure that:
244 * 1/ pConv is the converstation attached to the client window (it wouldn't be
245 * if a call to DdeReconnect would have already been done...)
246 * FIXME: is this really an error ???
247 * 2/ the pConv conversation had really been deconnected
249 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
250 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
252 HWND hwndClient = pConv->hwndClient;
253 HWND hwndServer = pConv->hwndServer;
254 ATOM aSrv, aTpc;
256 SetWindowLongA(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
258 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
259 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
260 if (!aSrv || !aTpc) goto theEnd;
262 LeaveCriticalSection(&WDML_CritSect);
264 /* note: sent messages shall not use packing */
265 ret = SendMessageA(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
266 MAKELPARAM(aSrv, aTpc));
268 EnterCriticalSection(&WDML_CritSect);
270 pConv = WDML_GetConv(hConv, FALSE);
271 if (pConv == NULL)
273 FIXME("Should fail reconnection\n");
274 goto theEnd;
277 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
279 /* re-establish all links... */
280 WDML_LINK* pLink;
282 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
284 if (pLink->hConv == hConv)
286 /* try to reestablish the links... */
287 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
288 pLink->transactionType, 1000, NULL);
292 else
294 /* reset the conversation as it was */
295 SetWindowLongA(pConv->hwndClient, GWL_WDML_CONVERSATION, (DWORD)pConv);
300 theEnd:
301 LeaveCriticalSection(&WDML_CritSect);
303 if (aSrv) GlobalDeleteAtom(aSrv);
304 if (aTpc) GlobalDeleteAtom(aTpc);
305 return (HCONV)pNewConv;
308 /******************************************************************
309 * WDML_ClientQueueAdvise
311 * Creates and queue an WM_DDE_ADVISE transaction
313 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
315 DDEADVISE* pDdeAdvise;
316 WDML_XACT* pXAct;
317 ATOM atom;
319 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
321 atom = WDML_MakeAtomFromHsz(hszItem);
322 if (!atom) return NULL;
324 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
325 if (!pXAct)
327 GlobalDeleteAtom(atom);
328 return NULL;
331 pXAct->wType = wType & ~0x0F;
332 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
333 /* FIXME: hMem is unfreed for now... should be deleted in server */
335 /* pack DdeAdvise */
336 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
337 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) ? TRUE : FALSE;
338 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) ? TRUE : FALSE;
339 pDdeAdvise->cfFormat = wFmt;
340 GlobalUnlock(pXAct->hMem);
342 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT)pXAct->hMem, atom);
344 return pXAct;
347 /******************************************************************
348 * WDML_HandleAdviseReply
350 * handles the reply to an advise request
352 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
354 DDEACK ddeAck;
355 UINT uiLo, uiHi;
356 HSZ hsz;
358 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
360 return WDML_QS_PASS;
363 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
364 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
366 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
367 return WDML_QS_PASS;
369 GlobalDeleteAtom(uiHi);
370 FreeDDElParam(WM_DDE_ACK, msg->lParam);
372 WDML_ExtractAck(uiLo, &ddeAck);
374 if (ddeAck.fAck)
376 WDML_LINK* pLink;
378 /* billx: first to see if the link is already created. */
379 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
380 pXAct->hszItem, TRUE, pXAct->wFmt);
381 if (pLink != NULL)
383 /* we found a link, and only need to modify it in case it changes */
384 pLink->transactionType = pXAct->wType;
386 else
388 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
389 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
391 pXAct->hDdeData = (HDDEDATA)1;
393 else
395 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
396 GlobalFree(pXAct->hMem);
397 pXAct->hDdeData = (HDDEDATA)0;
400 return WDML_QS_HANDLED;
403 /******************************************************************
404 * WDML_ClientQueueUnadvise
406 * queues an unadvise transaction
408 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
410 WDML_XACT* pXAct;
411 ATOM atom;
413 TRACE("XTYP_ADVSTOP transaction\n");
415 atom = WDML_MakeAtomFromHsz(hszItem);
416 if (!atom) return NULL;
418 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
419 if (!pXAct)
421 GlobalDeleteAtom(atom);
422 return NULL;
425 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
426 * on the specified item.
428 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
429 return pXAct;
432 /******************************************************************
433 * WDML_HandleUnadviseReply
437 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
439 DDEACK ddeAck;
440 UINT uiLo, uiHi;
441 HSZ hsz;
443 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
445 return WDML_QS_PASS;
448 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
449 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
451 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
452 return WDML_QS_PASS;
454 FreeDDElParam(WM_DDE_ACK, msg->lParam);
455 GlobalDeleteAtom(uiHi);
457 WDML_ExtractAck(uiLo, &ddeAck);
459 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
461 if (!ddeAck.fAck)
463 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
464 pXAct->hDdeData = (HDDEDATA)0;
466 else
468 /* billx: remove the link */
469 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
470 pXAct->hszItem, pXAct->wFmt);
471 pXAct->hDdeData = (HDDEDATA)1;
473 return WDML_QS_HANDLED;
476 /******************************************************************
477 * WDML_ClientQueueRequest
481 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
483 WDML_XACT* pXAct;
484 ATOM atom;
486 TRACE("XTYP_REQUEST transaction\n");
488 atom = WDML_MakeAtomFromHsz(hszItem);
489 if (!atom) return NULL;
491 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
492 if (!pXAct)
494 GlobalDeleteAtom(atom);
495 return NULL;
498 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
500 return pXAct;
503 /******************************************************************
504 * WDML_HandleRequestReply
508 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
510 DDEACK ddeAck;
511 WINE_DDEHEAD wdh;
512 UINT uiLo, uiHi;
513 HSZ hsz;
515 if (WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
516 return WDML_QS_PASS;
518 switch (msg->message)
520 case WM_DDE_ACK:
521 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
522 FreeDDElParam(WM_DDE_ACK, msg->lParam);
523 GlobalDeleteAtom(uiHi);
524 WDML_ExtractAck(uiLo, &ddeAck);
525 pXAct->hDdeData = 0;
526 if (ddeAck.fAck)
527 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
528 TRACE("Negative answer...\n");
529 break;
531 case WM_DDE_DATA:
532 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
533 TRACE("Got the result (%08lx)\n", (DWORD)uiLo);
535 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
537 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
538 return WDML_QS_PASS;
540 pXAct->hDdeData = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
541 if (wdh.fRelease)
543 GlobalFree((HGLOBAL)uiLo);
545 if (wdh.fAckReq)
547 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
549 else
551 GlobalDeleteAtom(uiHi);
552 FreeDDElParam(WM_DDE_ACK, msg->lParam);
554 break;
556 default:
557 FreeDDElParam(msg->message, msg->lParam);
558 return WDML_QS_PASS;
561 return WDML_QS_HANDLED;
564 /******************************************************************
565 * WDML_BuildExecuteCommand
567 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
568 * It also takes care of string conversion between the two window procedures
570 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
572 HGLOBAL hMem;
573 BOOL clientUnicode, serverUnicode;
574 DWORD memSize;
576 clientUnicode = IsWindowUnicode(pConv->hwndClient);
577 serverUnicode = IsWindowUnicode(pConv->hwndServer);
579 if (clientUnicode == serverUnicode)
581 memSize = cbData;
583 else
585 if (clientUnicode)
587 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData, NULL, 0, NULL, NULL);
589 else
591 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0);
595 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
597 if (hMem)
599 LPBYTE pDst;
601 pDst = GlobalLock(hMem);
602 if (pDst)
604 if (clientUnicode == serverUnicode)
606 memcpy(pDst, pData, cbData);
608 else
610 if (clientUnicode)
612 WideCharToMultiByte( CP_ACP, 0, pData, cbData, pDst, memSize, NULL, NULL);
614 else
616 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize);
620 GlobalUnlock(hMem);
622 else
624 GlobalFree(hMem);
625 hMem = 0;
628 return hMem;
631 /******************************************************************
632 * WDML_ClientQueueExecute
636 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
638 WDML_XACT* pXAct;
640 TRACE("XTYP_EXECUTE transaction\n");
642 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
643 if (!pXAct)
644 return NULL;
646 if (cbData == (DWORD)-1)
648 HDDEDATA hDdeData = (HDDEDATA)pData;
650 pData = DdeAccessData(hDdeData, &cbData);
651 if (pData)
653 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
654 DdeUnaccessData(hDdeData);
657 else
659 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
662 pXAct->lParam = pXAct->hMem;
664 return pXAct;
667 /******************************************************************
668 * WDML_HandleExecuteReply
672 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
674 DDEACK ddeAck;
675 UINT uiLo, uiHi;
677 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
679 return WDML_QS_PASS;
682 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
683 FreeDDElParam(WM_DDE_ACK, msg->lParam);
685 if (uiHi != pXAct->hMem)
687 return WDML_QS_PASS;
690 WDML_ExtractAck(uiLo, &ddeAck);
691 pXAct->hDdeData = (HDDEDATA)ddeAck.fAck;
693 return WDML_QS_HANDLED;
696 /******************************************************************
697 * WDML_ClientQueuePoke
701 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData,
702 UINT wFmt, HSZ hszItem)
704 WDML_XACT* pXAct;
705 ATOM atom;
707 TRACE("XTYP_POKE transaction\n");
709 atom = WDML_MakeAtomFromHsz(hszItem);
710 if (!atom) return NULL;
712 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
713 if (!pXAct)
715 GlobalDeleteAtom(atom);
716 return NULL;
719 if (cbData == (DWORD)-1)
721 pXAct->hMem = (HDDEDATA)pData;
723 else
725 DDEPOKE* ddePoke;
727 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData);
728 ddePoke = GlobalLock(pXAct->hMem);
729 if (ddePoke)
731 memcpy(ddePoke->Value, pData, cbData);
732 ddePoke->fRelease = FALSE; /* FIXME: app owned ? */
733 ddePoke->cfFormat = wFmt;
734 GlobalUnlock(pXAct->hMem);
738 pXAct->lParam = PackDDElParam(WM_DDE_POKE, pXAct->hMem, atom);
740 return pXAct;
743 /******************************************************************
744 * WDML_HandlePokeReply
748 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
750 DDEACK ddeAck;
751 UINT uiLo, uiHi;
752 HSZ hsz;
754 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
756 return WDML_QS_PASS;
759 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
760 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
761 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
763 return WDML_QS_PASS;
765 FreeDDElParam(WM_DDE_ACK, msg->lParam);
766 GlobalDeleteAtom(uiHi);
768 WDML_ExtractAck(uiLo, &ddeAck);
769 GlobalFree(pXAct->hMem);
771 pXAct->hDdeData = (HDDEDATA)TRUE;
772 return TRUE;
775 /******************************************************************
776 * WDML_ClientQueueTerminate
778 * Creates and queue an WM_DDE_TERMINATE transaction
780 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
782 WDML_XACT* pXAct;
784 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
785 if (!pXAct)
786 return NULL;
788 pXAct->lParam = 0;
789 pConv->wStatus &= ~ST_CONNECTED;
791 return pXAct;
794 /******************************************************************
795 * WDML_HandleTerminateReply
797 * handles the reply to a terminate request
799 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct)
801 if (msg->message != WM_DDE_TERMINATE)
803 /* FIXME: should delete data passed here */
804 return WDML_QS_SWALLOWED;
807 if (WIN_GetFullHandle(msg->wParam) != pConv->hwndServer)
809 FIXME("hmmm shouldn't happen\n");
810 return WDML_QS_PASS;
812 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
814 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
815 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
817 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
818 return WDML_QS_HANDLED;
821 /******************************************************************
822 * WDML_HandleReplyData
826 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
828 UINT uiLo, uiHi;
829 HDDEDATA hDdeDataIn, hDdeDataOut;
830 WDML_LINK* pLink;
831 WINE_DDEHEAD wdh;
832 HSZ hsz;
834 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
835 /* wParam -- sending window handle */
836 /* lParam -- hDdeData & item HSZ */
838 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
839 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
841 hDdeDataIn = WDML_Global2DataHandle((HGLOBAL)uiLo, &wdh);
843 /* billx:
844 * For hot link, data should be passed to its callback with
845 * XTYP_ADVDATA and callback should return the proper status.
847 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
848 uiLo ? TRUE : FALSE, wdh.cfFormat);
849 if (!pLink)
851 WDML_DecHSZ(pConv->instance, hsz);
852 DdeFreeDataHandle(hDdeDataIn);
853 return WDML_QS_PASS;
856 if (hDdeDataIn != 0 && wdh.fAckReq)
858 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
859 if (msg->lParam)
860 msg->lParam = 0;
862 else
864 GlobalDeleteAtom(uiHi);
867 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
868 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
870 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
872 if (uiLo)
874 GlobalFree(uiLo);
878 DdeFreeDataHandle(hDdeDataIn);
880 WDML_DecHSZ(pConv->instance, hsz);
881 if (msg->lParam)
882 FreeDDElParam(WM_DDE_DATA, msg->lParam);
884 return WDML_QS_HANDLED;
887 /******************************************************************
888 * WDML_HandleIncomingTerminate
892 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
894 if (pConv->hwndServer != WIN_GetFullHandle(msg->wParam))
895 return WDML_QS_PASS;
897 pConv->wStatus |= ST_TERMINATED;
898 if (!pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS)
900 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
901 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
903 if (pConv->wStatus & ST_CONNECTED)
905 /* don't care about result code (if server exists or not) */
906 PostMessageA(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
907 pConv->wStatus &= ~ST_CONNECTED;
909 /* have to keep connection around to allow reconnection */
910 return WDML_QS_HANDLED;
913 /******************************************************************
914 * WDML_HandleReply
916 * handles any incoming reply, and try to match to an already sent request
918 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
920 WDML_XACT* pXAct = pConv->transactions;
921 WDML_QUEUE_STATE qs;
923 if (pConv->transactions)
925 /* first check message against a pending transaction, if any */
926 switch (pXAct->ddeMsg)
928 case WM_DDE_ADVISE:
929 qs = WDML_HandleAdviseReply(pConv, msg, pXAct);
930 break;
931 case WM_DDE_UNADVISE:
932 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct);
933 break;
934 case WM_DDE_EXECUTE:
935 qs = WDML_HandleExecuteReply(pConv, msg, pXAct);
936 break;
937 case WM_DDE_REQUEST:
938 qs = WDML_HandleRequestReply(pConv, msg, pXAct);
939 break;
940 case WM_DDE_POKE:
941 qs = WDML_HandlePokeReply(pConv, msg, pXAct);
942 break;
943 case WM_DDE_TERMINATE:
944 qs = WDML_HandleTerminateReply(pConv, msg, pXAct);
945 break;
946 default:
947 qs = WDML_QS_ERROR;
948 FIXME("oooch\n");
951 else
953 qs = WDML_QS_PASS;
956 /* now check the results */
957 switch (qs)
959 case WDML_QS_ERROR:
960 case WDML_QS_SWALLOWED:
961 *hdd = 0;
962 break;
963 case WDML_QS_HANDLED:
964 /* ok, we have resolved a pending transaction
965 * notify callback if asynchronous, and remove it in any case
967 WDML_UnQueueTransaction(pConv, pXAct);
968 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
970 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
971 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
972 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
973 qs = WDML_QS_PASS;
975 else
977 *hdd = pXAct->hDdeData;
979 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
980 break;
981 case WDML_QS_PASS:
982 /* no pending transaction found, try a warm/hot link or a termination request */
983 switch (msg->message)
985 case WM_DDE_DATA:
986 qs = WDML_HandleIncomingData(pConv, msg, hdd);
987 break;
988 case WM_DDE_TERMINATE:
989 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
990 break;
992 break;
993 case WDML_QS_BLOCK:
994 FIXME("shouldn't be used on client side\n");
995 break;
998 return qs;
1001 /******************************************************************
1002 * WDML_SyncWaitTransactionReply
1004 * waits until an answer for a sent request is received
1005 * time out is also handled. only used for synchronous transactions
1007 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, WDML_XACT* pXAct)
1009 DWORD dwTime;
1010 DWORD err;
1011 WDML_CONV* pConv;
1013 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout);
1015 /* FIXME: time 32 bit wrap around */
1016 dwTimeout += GetCurrentTime();
1018 while ((dwTime = GetCurrentTime()) < dwTimeout)
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
1023 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1024 dwTimeout - dwTime, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1026 BOOL ret = FALSE;
1027 MSG msg;
1028 WDML_CONV* pConv;
1029 HDDEDATA hdd;
1031 EnterCriticalSection(&WDML_CritSect);
1033 pConv = WDML_GetConv(hConv, FALSE);
1034 if (pConv == NULL)
1036 LeaveCriticalSection(&WDML_CritSect);
1037 /* conversation no longer available... return failure */
1038 break;
1040 while (PeekMessageA(&msg, pConv->hwndClient, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1042 /* check that either pXAct has been processed or no more xActions are pending */
1043 ret = (pConv->transactions == pXAct);
1044 ret = WDML_HandleReply(pConv, &msg, &hdd) == WDML_QS_HANDLED &&
1045 (pConv->transactions == NULL || ret);
1046 if (ret) break;
1048 LeaveCriticalSection(&WDML_CritSect);
1049 if (ret)
1051 return hdd;
1056 TRACE("Timeout !!\n");
1058 EnterCriticalSection(&WDML_CritSect);
1060 pConv = WDML_GetConv(hConv, FALSE);
1061 if (pConv != NULL)
1063 if (pConv->transactions)
1065 switch (pConv->transactions->ddeMsg)
1067 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1068 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1069 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1070 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1071 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1072 default: err = DMLERR_INVALIDPARAMETER; break;
1075 pConv->instance->lastError = err;
1078 LeaveCriticalSection(&WDML_CritSect);
1080 return 0;
1083 /*****************************************************************
1084 * DdeClientTransaction (USER32.@)
1086 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1087 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1089 WDML_CONV* pConv;
1090 WDML_XACT* pXAct;
1091 HDDEDATA hDdeData = 0;
1093 TRACE("(%p,%ld,0x%lx,0x%x,%d,%d,%ld,%p)\n",
1094 pData, cbData, (DWORD)hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1096 if (hConv == 0)
1098 ERR("Invalid conversation handle\n");
1099 return 0;
1102 EnterCriticalSection(&WDML_CritSect);
1104 pConv = WDML_GetConv(hConv, TRUE);
1105 if (pConv == NULL)
1107 /* cannot set error... cannot get back to DDE instance */
1108 goto theError;
1111 switch (wType)
1113 case XTYP_EXECUTE:
1114 if (hszItem != 0 || wFmt != 0)
1116 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1117 goto theError;
1119 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1120 break;
1121 case XTYP_POKE:
1122 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1123 break;
1124 case XTYP_ADVSTART|XTYPF_NODATA:
1125 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1126 case XTYP_ADVSTART:
1127 case XTYP_ADVSTART|XTYPF_ACKREQ:
1128 if (pData)
1130 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1131 goto theError;
1133 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1134 break;
1135 case XTYP_ADVSTOP:
1136 if (pData)
1138 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1139 goto theError;
1141 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1142 break;
1143 case XTYP_REQUEST:
1144 if (pData)
1146 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1147 goto theError;
1149 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1150 break;
1151 default:
1152 FIXME("Unknown transation\n");
1153 /* unknown transaction type */
1154 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1155 goto theError;
1158 if (pXAct == NULL)
1160 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1161 goto theError;
1164 WDML_QueueTransaction(pConv, pXAct);
1166 if (!PostMessageA(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1168 TRACE("Failed posting message %d to 0x%04x (error=0x%lx)\n",
1169 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1170 pConv->wStatus &= ~ST_CONNECTED;
1171 WDML_UnQueueTransaction(pConv, pXAct);
1172 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1173 goto theError;
1175 pXAct->dwTimeout = dwTimeout;
1176 /* FIXME: should set the app bits on *pdwResult */
1178 if (dwTimeout == TIMEOUT_ASYNC)
1180 if (pdwResult)
1182 *pdwResult = MAKELONG(0, pXAct->xActID);
1184 hDdeData = (HDDEDATA)1;
1186 else
1188 DWORD count, i;
1190 if (pdwResult)
1192 *pdwResult = 0L;
1194 count = WDML_CritSect.RecursionCount;
1195 for (i = 0; i < count; i++)
1196 LeaveCriticalSection(&WDML_CritSect);
1197 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct);
1198 for (i = 0; i < count; i++)
1199 EnterCriticalSection(&WDML_CritSect);
1201 LeaveCriticalSection(&WDML_CritSect);
1203 return hDdeData;
1204 theError:
1205 LeaveCriticalSection(&WDML_CritSect);
1206 return 0;
1209 /*****************************************************************
1210 * DdeAbandonTransaction (USER32.@)
1212 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1214 WDML_INSTANCE* pInstance;
1215 WDML_CONV* pConv;
1216 WDML_XACT* pXAct;
1218 TRACE("(%08lx,%08lx,%08ld);\n", idInst, (DWORD)hConv, idTransaction);
1220 EnterCriticalSection(&WDML_CritSect);
1221 if ((pInstance = WDML_GetInstance(idInst)))
1223 if (hConv)
1225 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1227 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1229 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1230 (idTransaction == 0 || pXAct->xActID == idTransaction))
1232 WDML_UnQueueTransaction(pConv, pXAct);
1233 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1238 else
1240 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1242 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1243 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1245 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1247 WDML_UnQueueTransaction(pConv, pXAct);
1248 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1254 LeaveCriticalSection(&WDML_CritSect);
1256 return TRUE;
1259 /******************************************************************
1260 * WDML_ClientProc
1262 * Window Proc created on client side for each conversation
1264 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1266 UINT uiLo, uiHi;
1267 WDML_CONV* pConv = NULL;
1268 HSZ hszSrv, hszTpc;
1270 if (iMsg == WM_DDE_ACK &&
1271 /* in the initial WM_INITIATE sendmessage */
1272 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1274 /* In response to WM_DDE_INITIATE, save server window */
1275 char buf[256];
1276 WDML_INSTANCE* pInstance;
1278 /* note: sent messages do not need packing */
1279 uiLo = LOWORD(lParam);
1280 uiHi = HIWORD(lParam);
1282 /* FIXME: convlist should be handled here */
1283 if (pConv)
1285 /* we already have started the conv with a server, drop other replies */
1286 GlobalDeleteAtom(uiLo);
1287 GlobalDeleteAtom(uiHi);
1288 PostMessageA((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1289 return 0;
1292 pInstance = WDML_GetInstanceFromWnd(hwnd);
1294 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1295 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1297 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1299 SetWindowLongA(hwnd, GWL_WDML_CONVERSATION, (DWORD)pConv);
1300 pConv->wStatus |= ST_CONNECTED;
1301 pConv->wConvst = XST_INIT1;
1303 /* check if server is handled by DDEML */
1304 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1305 strcmp(buf, WDML_szServerConvClassA) == 0) ||
1306 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1307 lstrcmpW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1309 pConv->wStatus |= ST_ISLOCAL;
1312 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_CONNECT_CONFIRM, (WPARAM)hwnd, wParam);
1314 GlobalDeleteAtom(uiLo);
1315 GlobalDeleteAtom(uiHi);
1317 /* accept conversation */
1318 return 1;
1321 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1323 EnterCriticalSection(&WDML_CritSect);
1325 pConv = WDML_GetConvFromWnd(hwnd);
1327 if (pConv)
1329 MSG msg;
1330 HDDEDATA hdd;
1332 msg.hwnd = hwnd;
1333 msg.message = iMsg;
1334 msg.wParam = wParam;
1335 msg.lParam = lParam;
1337 WDML_HandleReply(pConv, &msg, &hdd);
1340 LeaveCriticalSection(&WDML_CritSect);
1341 return 0;
1344 return (IsWindowUnicode(hwnd)) ?
1345 DefWindowProcA(hwnd, iMsg, wParam, lParam) : DefWindowProcW(hwnd, iMsg, wParam, lParam);
1348 /*****************************************************************
1349 * DdeDisconnect (USER32.@)
1351 BOOL WINAPI DdeDisconnect(HCONV hConv)
1353 WDML_CONV* pConv = NULL;
1354 WDML_XACT* pXAct;
1355 DWORD count, i;
1356 BOOL ret = FALSE;
1358 TRACE("(%ld)\n", (DWORD)hConv);
1360 if (hConv == 0)
1362 ERR("DdeDisconnect(): hConv = 0\n");
1363 return FALSE;
1366 EnterCriticalSection(&WDML_CritSect);
1367 pConv = WDML_GetConv(hConv, TRUE);
1368 if (pConv != NULL)
1370 if (pConv->wStatus & ST_CLIENT)
1372 /* FIXME: should abandon all pending transactions */
1373 pXAct = WDML_ClientQueueTerminate(pConv);
1374 if (pXAct != NULL)
1376 count = WDML_CritSect.RecursionCount;
1377 for (i = 0; i < count; i++)
1378 LeaveCriticalSection(&WDML_CritSect);
1379 if (PostMessageA(pConv->hwndServer, pXAct->ddeMsg,
1380 (WPARAM)pConv->hwndClient, pXAct->lParam))
1381 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct);
1382 for (i = 0; i < count; i++)
1383 EnterCriticalSection(&WDML_CritSect);
1384 ret = TRUE;
1385 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1386 /* still have to destroy data assosiated with conversation */
1387 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1389 else
1391 FIXME("Not implemented yet for a server side conversation\n");
1395 LeaveCriticalSection(&WDML_CritSect);
1397 return ret;
1400 /*****************************************************************
1401 * DdeImpersonateClient (USER32.@)
1403 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1405 WDML_CONV* pConv;
1406 BOOL ret = FALSE;
1408 EnterCriticalSection(&WDML_CritSect);
1409 pConv = WDML_GetConv(hConv, TRUE);
1410 if (pConv)
1412 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1414 LeaveCriticalSection(&WDML_CritSect);
1415 return ret;