schedsvc/tests: Add the current process id to the test summary line.
[wine.git] / dlls / user32 / dde_client.c
blob989131c66c21e53d2d8febf72d703d28049b8f6c
1 /*
2 * DDEML library
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
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.
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.
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
26 #include <stdarg.h>
27 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "dde.h"
34 #include "ddeml.h"
35 #include "win.h"
36 #include "wine/debug.h"
37 #include "dde_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
41 static LRESULT CALLBACK WDML_ClientProc(HWND, UINT, WPARAM, LPARAM); /* only for one client, not conv list */
42 const char WDML_szClientConvClassA[] = "WineDdeClientA";
43 const WCHAR WDML_szClientConvClassW[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t','W',0};
45 /******************************************************************************
46 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
48 * PARAMS
49 * idInst [I] Instance identifier
50 * hszService [I] Handle to service name string
51 * hszTopic [I] Handle to topic name string
52 * hConvList [I] Handle to conversation list
53 * pCC [I] Pointer to structure with context data
55 * RETURNS
56 * Success: Handle to new conversation list
57 * Failure: 0
59 HCONVLIST WINAPI DdeConnectList(DWORD idInst, HSZ hszService, HSZ hszTopic,
60 HCONVLIST hConvList, PCONVCONTEXT pCC)
62 FIXME("(%d,%p,%p,%p,%p): stub\n", idInst, hszService, hszTopic, hConvList, pCC);
63 return (HCONVLIST)1;
66 /*****************************************************************
67 * DdeQueryNextServer [USER32.@]
69 HCONV WINAPI DdeQueryNextServer(HCONVLIST hConvList, HCONV hConvPrev)
71 FIXME("(%p,%p): stub\n", hConvList, hConvPrev);
72 return 0;
75 /******************************************************************************
76 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
79 * PARAMS
80 * hConvList [I] Handle to conversation list
82 * RETURNS
83 * Success: TRUE
84 * Failure: FALSE
86 BOOL WINAPI DdeDisconnectList(HCONVLIST hConvList)
88 FIXME("(%p): stub\n", hConvList);
89 return TRUE;
92 /*****************************************************************
93 * DdeConnect (USER32.@)
95 HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic,
96 PCONVCONTEXT pCC)
98 HWND hwndClient;
99 WDML_INSTANCE* pInstance;
100 WDML_CONV* pConv;
101 ATOM aSrv = 0, aTpc = 0;
103 TRACE("(0x%x,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC);
105 pInstance = WDML_GetInstance(idInst);
106 if (!pInstance)
107 return NULL;
109 /* make sure this conv is never created */
110 pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic);
111 if (pConv != NULL)
113 ERR("This Conv already exists: (%p)\n", pConv);
114 return NULL;
117 /* we need to establish a conversation with
118 server, so create a window for it */
120 if (pInstance->unicode)
122 WNDCLASSEXW wndclass;
124 wndclass.cbSize = sizeof(wndclass);
125 wndclass.style = 0;
126 wndclass.lpfnWndProc = WDML_ClientProc;
127 wndclass.cbClsExtra = 0;
128 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
129 wndclass.hInstance = 0;
130 wndclass.hIcon = 0;
131 wndclass.hCursor = 0;
132 wndclass.hbrBackground = 0;
133 wndclass.lpszMenuName = NULL;
134 wndclass.lpszClassName = WDML_szClientConvClassW;
135 wndclass.hIconSm = 0;
137 RegisterClassExW(&wndclass);
139 hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
141 else
143 WNDCLASSEXA wndclass;
145 wndclass.cbSize = sizeof(wndclass);
146 wndclass.style = 0;
147 wndclass.lpfnWndProc = WDML_ClientProc;
148 wndclass.cbClsExtra = 0;
149 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
150 wndclass.hInstance = 0;
151 wndclass.hIcon = 0;
152 wndclass.hCursor = 0;
153 wndclass.hbrBackground = 0;
154 wndclass.lpszMenuName = NULL;
155 wndclass.lpszClassName = WDML_szClientConvClassA;
156 wndclass.hIconSm = 0;
158 RegisterClassExA(&wndclass);
160 hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
163 SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
165 if (hszService)
167 aSrv = WDML_MakeAtomFromHsz(hszService);
168 if (!aSrv) goto theEnd;
170 if (hszTopic)
172 aTpc = WDML_MakeAtomFromHsz(hszTopic);
173 if (!aTpc) goto theEnd;
176 /* note: sent messages shall not use packing */
177 SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc),
178 SMTO_ABORTIFHUNG, 0, NULL );
180 pInstance = WDML_GetInstance(idInst);
181 if (!pInstance)
183 goto theEnd;
186 /* At this point, Client WM_DDE_ACK should have saved hwndServer
187 for this instance id and hwndClient if server responds.
188 So get HCONV and return it. And add it to conv list */
189 pConv = WDML_GetConvFromWnd(hwndClient);
190 if (pConv == NULL || pConv->hwndServer == 0)
192 WARN("Done with INITIATE, but no Server window available\n");
193 pConv = NULL;
194 pInstance->lastError = DMLERR_NO_CONV_ESTABLISHED;
195 goto theEnd;
197 TRACE("Connected to Server window (%p)\n", pConv->hwndServer);
198 pConv->wConvst = XST_CONNECTED;
200 /* finish init of pConv */
201 if (pCC != NULL)
203 pConv->convContext = *pCC;
205 else
207 memset(&pConv->convContext, 0, sizeof(pConv->convContext));
208 pConv->convContext.cb = sizeof(pConv->convContext);
209 pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI;
212 theEnd:
214 if (aSrv) GlobalDeleteAtom(aSrv);
215 if (aTpc) GlobalDeleteAtom(aTpc);
216 return (HCONV)pConv;
219 /*****************************************************************
220 * DdeReconnect (DDEML.37)
221 * DdeReconnect (USER32.@)
223 HCONV WINAPI DdeReconnect(HCONV hConv)
225 WDML_CONV* pConv;
226 WDML_CONV* pNewConv = NULL;
227 ATOM aSrv = 0, aTpc = 0;
229 TRACE("(%p)\n", hConv);
231 pConv = WDML_GetConv(hConv, FALSE);
232 if (pConv != NULL && (pConv->wStatus & ST_CLIENT))
234 BOOL ret;
236 /* to reestablish a connection, we have to make sure that:
237 * 1/ pConv is the conversation attached to the client window (it wouldn't be
238 * if a call to DdeReconnect would have already been done...)
239 * FIXME: is this really an error ???
240 * 2/ the pConv conversation had really been deconnected
242 if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) &&
243 (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED))
245 HWND hwndClient = pConv->hwndClient;
246 HWND hwndServer = pConv->hwndServer;
248 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0);
250 aSrv = WDML_MakeAtomFromHsz(pConv->hszService);
251 aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic);
252 if (!aSrv || !aTpc) goto theEnd;
254 /* note: sent messages shall not use packing */
255 ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient,
256 MAKELPARAM(aSrv, aTpc));
258 pConv = WDML_GetConv(hConv, FALSE);
259 if (pConv == NULL)
261 FIXME("Should fail reconnection\n");
262 goto theEnd;
265 if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL)
267 /* re-establish all links... */
268 WDML_LINK* pLink;
270 for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next)
272 if (pLink->hConv == hConv)
274 /* try to reestablish the links... */
275 DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt,
276 pLink->transactionType, 1000, NULL);
280 else
282 /* reset the conversation as it was */
283 SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
288 theEnd:
290 if (aSrv) GlobalDeleteAtom(aSrv);
291 if (aTpc) GlobalDeleteAtom(aTpc);
292 return (HCONV)pNewConv;
295 /******************************************************************
296 * WDML_ClientQueueAdvise
298 * Creates and queue an WM_DDE_ADVISE transaction
300 static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem)
302 DDEADVISE* pDdeAdvise;
303 WDML_XACT* pXAct;
304 ATOM atom;
306 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : "");
308 atom = WDML_MakeAtomFromHsz(hszItem);
309 if (!atom) return NULL;
311 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem);
312 if (!pXAct)
314 GlobalDeleteAtom(atom);
315 return NULL;
318 pXAct->wType = wType & ~0x0F;
319 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE));
320 /* FIXME: hMem is unfreed for now... should be deleted in server */
322 /* pack DdeAdvise */
323 pDdeAdvise = GlobalLock(pXAct->hMem);
324 pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) != 0;
325 pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) != 0;
326 pDdeAdvise->cfFormat = wFmt;
327 GlobalUnlock(pXAct->hMem);
329 pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom);
331 return pXAct;
334 /******************************************************************
335 * WDML_HandleAdviseReply
337 * handles the reply to an advise request
339 static WDML_QUEUE_STATE WDML_HandleAdviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
341 DDEACK ddeAck;
342 UINT_PTR uiLo, uiHi;
343 HSZ hsz;
345 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
347 return WDML_QS_PASS;
350 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
351 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
353 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
354 return WDML_QS_PASS;
356 GlobalDeleteAtom(uiHi);
357 FreeDDElParam(WM_DDE_ACK, msg->lParam);
359 if (ack) *ack = uiLo;
360 WDML_ExtractAck(uiLo, &ddeAck);
362 if (ddeAck.fAck)
364 WDML_LINK* pLink;
366 /* billx: first to see if the link is already created. */
367 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
368 pXAct->hszItem, TRUE, pXAct->wFmt);
369 if (pLink != NULL)
371 /* we found a link, and only need to modify it in case it changes */
372 pLink->transactionType = pXAct->wType;
374 else
376 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
377 pXAct->wType, pXAct->hszItem, pXAct->wFmt);
379 pXAct->hDdeData = (HDDEDATA)1;
381 else
383 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
384 GlobalFree(pXAct->hMem);
385 pXAct->hDdeData = NULL;
388 return WDML_QS_HANDLED;
391 /******************************************************************
392 * WDML_ClientQueueUnadvise
394 * queues an unadvise transaction
396 static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
398 WDML_XACT* pXAct;
399 ATOM atom;
401 TRACE("XTYP_ADVSTOP transaction\n");
403 atom = WDML_MakeAtomFromHsz(hszItem);
404 if (!atom) return NULL;
406 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem);
407 if (!pXAct)
409 GlobalDeleteAtom(atom);
410 return NULL;
413 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
414 * on the specified item.
416 pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom);
417 return pXAct;
420 /******************************************************************
421 * WDML_HandleUnadviseReply
425 static WDML_QUEUE_STATE WDML_HandleUnadviseReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
427 DDEACK ddeAck;
428 UINT_PTR uiLo, uiHi;
429 HSZ hsz;
431 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
433 return WDML_QS_PASS;
436 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
437 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
439 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
440 return WDML_QS_PASS;
442 FreeDDElParam(WM_DDE_ACK, msg->lParam);
443 GlobalDeleteAtom(uiHi);
445 if (ack) *ack = uiLo;
446 WDML_ExtractAck(uiLo, &ddeAck);
448 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
450 if (!ddeAck.fAck)
452 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
453 pXAct->hDdeData = NULL;
455 else
457 /* billx: remove the link */
458 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE,
459 pXAct->hszItem, pXAct->wFmt);
460 pXAct->hDdeData = (HDDEDATA)1;
462 return WDML_QS_HANDLED;
465 /******************************************************************
466 * WDML_ClientQueueRequest
470 static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem)
472 WDML_XACT* pXAct;
473 ATOM atom;
475 TRACE("XTYP_REQUEST transaction\n");
477 atom = WDML_MakeAtomFromHsz(hszItem);
478 if (!atom) return NULL;
480 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem);
481 if (!pXAct)
483 GlobalDeleteAtom(atom);
484 return NULL;
487 pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom);
489 return pXAct;
492 /******************************************************************
493 * WDML_HandleRequestReply
497 static WDML_QUEUE_STATE WDML_HandleRequestReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
499 DDEACK ddeAck;
500 WINE_DDEHEAD wdh;
501 UINT_PTR uiLo, uiHi;
502 HSZ hsz;
504 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
505 return WDML_QS_PASS;
507 switch (msg->message)
509 case WM_DDE_ACK:
510 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
511 FreeDDElParam(WM_DDE_ACK, msg->lParam);
512 GlobalDeleteAtom(uiHi);
513 if (ack) *ack = uiLo;
514 WDML_ExtractAck(uiLo, &ddeAck);
515 pXAct->hDdeData = 0;
516 if (ddeAck.fAck)
517 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
518 TRACE("Negative answer...\n");
519 break;
521 case WM_DDE_DATA:
522 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
523 TRACE("Got the result (%08lx)\n", uiLo);
525 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
527 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
528 return WDML_QS_PASS;
530 pXAct->hDdeData = WDML_Global2DataHandle(pConv, (HGLOBAL)uiLo, &wdh);
531 if (wdh.fRelease)
533 GlobalFree((HGLOBAL)uiLo);
535 if (wdh.fAckReq)
537 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
539 else
541 GlobalDeleteAtom(uiHi);
542 FreeDDElParam(WM_DDE_ACK, msg->lParam);
544 break;
546 default:
547 FreeDDElParam(msg->message, msg->lParam);
548 return WDML_QS_PASS;
551 return WDML_QS_HANDLED;
554 /******************************************************************
555 * WDML_BuildExecuteCommand
557 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
558 * It also takes care of string conversion between the two window procedures
560 static HGLOBAL WDML_BuildExecuteCommand(WDML_CONV* pConv, LPCVOID pData, DWORD cbData)
562 HGLOBAL hMem;
563 BOOL clientUnicode, serverUnicode;
564 DWORD memSize;
566 clientUnicode = pConv->instance->unicode;
567 TRACE("client %p uses unicode = %d\n", pConv->hwndClient, clientUnicode);
568 /* FIXME: how exactly Windows determines what to use for the server side? */
569 serverUnicode = IsWindowUnicode(pConv->hwndServer) && IsWindowUnicode(pConv->hwndClient);
570 TRACE("server %p uses unicode = %d\n", pConv->hwndServer, serverUnicode);
572 if (clientUnicode == serverUnicode)
574 memSize = cbData;
576 else
578 if (clientUnicode)
580 memSize = WideCharToMultiByte( CP_ACP, 0, pData, cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
582 else
584 memSize = MultiByteToWideChar( CP_ACP, 0, pData, cbData, NULL, 0) * sizeof(WCHAR);
588 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, memSize);
590 if (hMem)
592 LPSTR pDst;
594 pDst = GlobalLock(hMem);
595 if (pDst)
597 if (clientUnicode == serverUnicode)
599 memcpy(pDst, pData, cbData);
601 else
603 if (clientUnicode)
605 WideCharToMultiByte( CP_ACP, 0, pData, cbData / sizeof(WCHAR), pDst, memSize, NULL, NULL);
607 else
609 MultiByteToWideChar( CP_ACP, 0, pData, cbData, (LPWSTR)pDst, memSize/sizeof(WCHAR));
613 GlobalUnlock(hMem);
615 else
617 GlobalFree(hMem);
618 hMem = 0;
621 return hMem;
624 /******************************************************************
625 * WDML_ClientQueueExecute
629 static WDML_XACT* WDML_ClientQueueExecute(WDML_CONV* pConv, LPVOID pData, DWORD cbData)
631 WDML_XACT* pXAct;
633 TRACE("XTYP_EXECUTE transaction\n");
635 if (pData == NULL)
637 if (cbData == (DWORD)-1)
638 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
639 else
640 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
641 return NULL;
644 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
645 if (!pXAct)
646 return NULL;
648 if (cbData == (DWORD)-1)
650 HDDEDATA hDdeData = pData;
652 pData = DdeAccessData(hDdeData, &cbData);
653 if (pData)
655 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
656 DdeUnaccessData(hDdeData);
659 else
661 pXAct->hMem = WDML_BuildExecuteCommand(pConv, pData, cbData);
664 pXAct->lParam = (LPARAM)pXAct->hMem;
666 return pXAct;
669 /******************************************************************
670 * WDML_HandleExecuteReply
674 static WDML_QUEUE_STATE WDML_HandleExecuteReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
676 DDEACK ddeAck;
677 UINT_PTR uiLo, uiHi;
679 if (msg->message != WM_DDE_ACK || WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
681 return WDML_QS_PASS;
684 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
685 FreeDDElParam(WM_DDE_ACK, msg->lParam);
687 if ((HANDLE)uiHi != pXAct->hMem)
689 return WDML_QS_PASS;
692 if (ack) *ack = uiLo;
693 WDML_ExtractAck(uiLo, &ddeAck);
694 pXAct->hDdeData = (HDDEDATA)(UINT_PTR)ddeAck.fAck;
696 TRACE("hDdeData = %p\n", pXAct->hDdeData);
697 pConv->instance->lastError = (pXAct->hDdeData != 0) ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
699 return WDML_QS_HANDLED;
702 /******************************************************************
703 * WDML_ClientQueuePoke
707 static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPVOID pData, DWORD cbData,
708 UINT wFmt, HSZ hszItem)
710 DDE_DATAHANDLE_HEAD *dh;
711 WDML_XACT *pXAct;
712 DDEPOKE *ddePoke;
713 HGLOBAL hglobal;
714 ATOM atom;
716 TRACE("XTYP_POKE transaction\n");
718 atom = WDML_MakeAtomFromHsz(hszItem);
719 if (!atom) return NULL;
721 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem);
722 if (!pXAct)
724 GlobalDeleteAtom(atom);
725 return NULL;
728 if (cbData == (DWORD)-1)
730 hglobal = pData;
731 dh = GlobalLock(hglobal);
732 cbData = GlobalSize(hglobal) - sizeof(DDE_DATAHANDLE_HEAD);
733 pData = dh + 1;
734 GlobalUnlock(hglobal);
737 pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, FIELD_OFFSET(DDEPOKE, Value[cbData]));
738 ddePoke = GlobalLock(pXAct->hMem);
739 if (!ddePoke)
741 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
742 return NULL;
745 ddePoke->unused = 0;
746 ddePoke->fRelease = TRUE;
747 ddePoke->cfFormat = wFmt;
748 memcpy(ddePoke->Value, pData, cbData);
749 GlobalUnlock(pXAct->hMem);
751 pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom);
753 return pXAct;
756 /******************************************************************
757 * WDML_HandlePokeReply
761 static WDML_QUEUE_STATE WDML_HandlePokeReply(WDML_CONV* pConv, MSG* msg, WDML_XACT* pXAct, DWORD *ack)
763 UINT_PTR uiLo, uiHi;
764 HSZ hsz;
766 if (msg->message != WM_DDE_ACK && WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
768 return WDML_QS_PASS;
771 UnpackDDElParam(WM_DDE_ACK, msg->lParam, &uiLo, &uiHi);
772 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
773 if (DdeCmpStringHandles(hsz, pXAct->hszItem) != 0)
775 return WDML_QS_PASS;
777 FreeDDElParam(WM_DDE_ACK, msg->lParam);
778 GlobalDeleteAtom(uiHi);
780 if (ack) *ack = uiLo;
781 pXAct->hMem = GlobalFree(pXAct->hMem);
783 pXAct->hDdeData = (HDDEDATA)TRUE;
784 return TRUE;
787 /******************************************************************
788 * WDML_ClientQueueTerminate
790 * Creates and queue an WM_DDE_TERMINATE transaction
792 static WDML_XACT* WDML_ClientQueueTerminate(WDML_CONV* pConv)
794 WDML_XACT* pXAct;
796 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
797 if (!pXAct)
798 return NULL;
800 pXAct->lParam = 0;
801 pConv->wStatus &= ~ST_CONNECTED;
803 return pXAct;
806 /******************************************************************
807 * WDML_HandleTerminateReply
809 * handles the reply to a terminate request
811 static WDML_QUEUE_STATE WDML_HandleTerminateReply(WDML_CONV* pConv, MSG* msg)
813 if (msg->message != WM_DDE_TERMINATE)
815 /* FIXME: should delete data passed here */
816 return WDML_QS_SWALLOWED;
819 if (WIN_GetFullHandle((HWND)msg->wParam) != pConv->hwndServer)
821 FIXME("hmmm shouldn't happen\n");
822 return WDML_QS_PASS;
824 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
826 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv,
827 0, 0, 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
829 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
830 return WDML_QS_HANDLED;
833 /******************************************************************
834 * WDML_HandleIncomingData
838 static WDML_QUEUE_STATE WDML_HandleIncomingData(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
840 UINT_PTR uiLo, uiHi;
841 HDDEDATA hDdeDataIn, hDdeDataOut;
842 WDML_LINK* pLink;
843 WINE_DDEHEAD wdh;
844 HSZ hsz;
846 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
847 /* wParam -- sending window handle */
848 /* lParam -- hDdeData & item HSZ */
850 UnpackDDElParam(WM_DDE_DATA, msg->lParam, &uiLo, &uiHi);
851 hsz = WDML_MakeHszFromAtom(pConv->instance, uiHi);
853 hDdeDataIn = WDML_Global2DataHandle(pConv, (HGLOBAL)uiLo, &wdh);
855 /* billx:
856 * For hot link, data should be passed to its callback with
857 * XTYP_ADVDATA and callback should return the proper status.
859 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_CLIENT_SIDE, hsz,
860 uiLo != 0, wdh.cfFormat);
861 if (!pLink)
863 WDML_DecHSZ(pConv->instance, hsz);
864 DdeFreeDataHandle(hDdeDataIn);
865 return WDML_QS_PASS;
868 if (hDdeDataIn != 0 && wdh.fAckReq)
870 WDML_PostAck(pConv, WDML_CLIENT_SIDE, 0, FALSE, TRUE, uiHi, msg->lParam, WM_DDE_DATA);
871 if (msg->lParam)
872 msg->lParam = 0;
874 else
876 GlobalDeleteAtom(uiHi);
879 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_ADVDATA, pLink->uFmt, pLink->hConv,
880 pConv->hszTopic, pLink->hszItem, hDdeDataIn, 0, 0);
882 if (hDdeDataOut != (HDDEDATA)DDE_FACK || wdh.fRelease)
884 if (uiLo) GlobalFree((HANDLE)uiLo);
887 DdeFreeDataHandle(hDdeDataIn);
889 WDML_DecHSZ(pConv->instance, hsz);
890 if (msg->lParam)
891 FreeDDElParam(WM_DDE_DATA, msg->lParam);
893 return WDML_QS_HANDLED;
896 /******************************************************************
897 * WDML_HandleIncomingTerminate
901 static WDML_QUEUE_STATE WDML_HandleIncomingTerminate(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd)
903 if (pConv->hwndServer != WIN_GetFullHandle((HWND)msg->wParam))
904 return WDML_QS_PASS;
906 pConv->wStatus |= ST_TERMINATED;
907 if (pConv->wStatus & ST_CONNECTED)
909 /* don't care about result code (if server exists or not) */
910 PostMessageW(pConv->hwndServer, WM_DDE_TERMINATE, (WPARAM)pConv->hwndClient, 0L);
911 pConv->wStatus &= ~ST_CONNECTED;
913 /* have to keep connection around to allow reconnection */
914 return WDML_QS_HANDLED;
917 /******************************************************************
918 * WDML_HandleReply
920 * handles any incoming reply, and try to match to an already sent request
922 static WDML_QUEUE_STATE WDML_HandleReply(WDML_CONV* pConv, MSG* msg, HDDEDATA* hdd, DWORD *ack)
924 WDML_XACT* pXAct = pConv->transactions;
925 WDML_QUEUE_STATE qs;
927 if (pConv->transactions)
929 if (ack) *ack = DDE_FNOTPROCESSED;
930 /* first check message against a pending transaction, if any */
931 switch (pXAct->ddeMsg)
933 case WM_DDE_ADVISE:
934 qs = WDML_HandleAdviseReply(pConv, msg, pXAct, ack);
935 break;
936 case WM_DDE_UNADVISE:
937 qs = WDML_HandleUnadviseReply(pConv, msg, pXAct, ack);
938 break;
939 case WM_DDE_EXECUTE:
940 qs = WDML_HandleExecuteReply(pConv, msg, pXAct, ack);
941 break;
942 case WM_DDE_REQUEST:
943 qs = WDML_HandleRequestReply(pConv, msg, pXAct, ack);
944 break;
945 case WM_DDE_POKE:
946 qs = WDML_HandlePokeReply(pConv, msg, pXAct, ack);
947 break;
948 case WM_DDE_TERMINATE:
949 qs = WDML_HandleTerminateReply(pConv, msg);
950 break;
951 default:
952 qs = WDML_QS_ERROR;
953 FIXME("oooch\n");
956 else
958 qs = WDML_QS_PASS;
961 /* now check the results */
962 switch (qs)
964 case WDML_QS_ERROR:
965 case WDML_QS_SWALLOWED:
966 *hdd = 0;
967 break;
968 case WDML_QS_HANDLED:
969 /* ok, we have resolved a pending transaction
970 * notify callback if asynchronous.
972 if (pXAct->dwTimeout == TIMEOUT_ASYNC && pXAct->ddeMsg != WM_DDE_TERMINATE)
974 WDML_InvokeCallback(pConv->instance, XTYP_XACT_COMPLETE, pXAct->wFmt,
975 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
976 pXAct->hDdeData, MAKELONG(0, pXAct->xActID), 0 /* FIXME */);
977 qs = WDML_QS_PASS;
979 else
981 *hdd = pXAct->hDdeData;
983 break;
984 case WDML_QS_PASS:
985 /* no pending transaction found, try a warm/hot link or a termination request */
986 switch (msg->message)
988 case WM_DDE_DATA:
989 qs = WDML_HandleIncomingData(pConv, msg, hdd);
990 break;
991 case WM_DDE_TERMINATE:
992 qs = WDML_HandleIncomingTerminate(pConv, msg, hdd);
993 break;
994 case WM_DDE_ACK:
995 /* This happens at end of DdeClientTransaction XTYP_EXECUTE
996 * Without this assignment, DdeClientTransaction's return value is undefined
998 *hdd = (HDDEDATA)TRUE;
999 if (ack)
1000 *ack = DDE_FACK;
1001 break;
1003 break;
1004 case WDML_QS_BLOCK:
1005 FIXME("shouldn't be used on client side\n");
1006 break;
1009 return qs;
1012 /******************************************************************
1013 * WDML_SyncWaitTransactionReply
1015 * waits until an answer for a sent request is received
1016 * time out is also handled. only used for synchronous transactions
1018 static HDDEDATA WDML_SyncWaitTransactionReply(HCONV hConv, DWORD dwTimeout, const WDML_XACT* pXAct, DWORD *ack)
1020 DWORD start, elapsed;
1021 DWORD err;
1022 WDML_CONV* pConv;
1024 TRACE("Starting wait for a timeout of %d ms\n", dwTimeout);
1026 start = GetTickCount();
1027 while ((elapsed = GetTickCount() - start) < dwTimeout)
1029 /* we cannot be in the crit sect all the time because when client and server run in a
1030 * single process they need to share the access to the internal data
1032 if (MsgWaitForMultipleObjects(0, NULL, FALSE,
1033 dwTimeout - elapsed, QS_POSTMESSAGE) == WAIT_OBJECT_0)
1035 MSG msg;
1037 while (PeekMessageW(&msg, 0, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
1039 HDDEDATA hdd = NULL;
1041 pConv = WDML_GetConv(hConv, FALSE);
1042 if (pConv == NULL)
1044 /* conversation no longer available... return failure */
1045 return 0;
1047 if (msg.hwnd == pConv->hwndClient)
1049 /* check that either pXAct has been processed or no more xActions are pending */
1050 BOOL ret = (pConv->transactions == pXAct);
1051 if (WDML_HandleReply(pConv, &msg, &hdd, ack) == WDML_QS_HANDLED)
1053 TRACE("WDML_HandleReply returned WDML_QS_HANDLED\n");
1054 ret = TRUE;
1056 else
1057 ret = (pConv->transactions == NULL || ret);
1059 if (ret)
1061 pConv->instance->lastError = hdd ? DMLERR_NO_ERROR : DMLERR_NOTPROCESSED;
1062 return hdd;
1065 else
1067 DispatchMessageW(&msg);
1073 TRACE("Timeout !!\n");
1075 pConv = WDML_GetConv(hConv, FALSE);
1076 if (pConv != NULL)
1078 if (pConv->transactions)
1080 switch (pConv->transactions->ddeMsg)
1082 case WM_DDE_ADVISE: err = DMLERR_ADVACKTIMEOUT; break;
1083 case WM_DDE_REQUEST: err = DMLERR_DATAACKTIMEOUT; break;
1084 case WM_DDE_EXECUTE: err = DMLERR_EXECACKTIMEOUT; break;
1085 case WM_DDE_POKE: err = DMLERR_POKEACKTIMEOUT; break;
1086 case WM_DDE_UNADVISE: err = DMLERR_UNADVACKTIMEOUT; break;
1087 default: err = DMLERR_INVALIDPARAMETER; break;
1090 pConv->instance->lastError = err;
1094 return 0;
1098 /*****************************************************************
1099 * WDML_ClientHandle
1101 HDDEDATA WDML_ClientHandle(WDML_CONV *pConv, WDML_XACT *pXAct, DWORD dwTimeout, LPDWORD pdwResult)
1103 HDDEDATA hDdeData;
1105 if (!PostMessageW(pConv->hwndServer, pXAct->ddeMsg, (WPARAM)pConv->hwndClient, pXAct->lParam))
1107 WARN("Failed posting message %x to %p (error=0x%x)\n",
1108 pXAct->ddeMsg, pConv->hwndServer, GetLastError());
1109 pConv->wStatus &= ~ST_CONNECTED;
1110 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1111 return 0;
1113 pXAct->dwTimeout = dwTimeout;
1114 /* FIXME: should set the app bits on *pdwResult */
1116 if (dwTimeout == TIMEOUT_ASYNC)
1118 if (pdwResult)
1119 *pdwResult = MAKELONG(0, pXAct->xActID);
1121 hDdeData = (HDDEDATA)1;
1123 else
1124 hDdeData = WDML_SyncWaitTransactionReply((HCONV)pConv, dwTimeout, pXAct, pdwResult);
1126 return hDdeData;
1130 /*****************************************************************
1131 * DdeClientTransaction (USER32.@)
1133 HDDEDATA WINAPI DdeClientTransaction(LPBYTE pData, DWORD cbData, HCONV hConv, HSZ hszItem, UINT wFmt,
1134 UINT wType, DWORD dwTimeout, LPDWORD pdwResult)
1136 WDML_CONV* pConv;
1137 WDML_XACT* pXAct;
1138 HDDEDATA hDdeData;
1140 TRACE("(%p,%d,%p,%p,%x,%x,%d,%p)\n",
1141 pData, cbData, hConv, hszItem, wFmt, wType, dwTimeout, pdwResult);
1143 if (hConv == 0)
1145 WARN("Invalid conversation handle NULL\n");
1146 return 0;
1149 pConv = WDML_GetConv(hConv, TRUE);
1150 if (pConv == NULL)
1152 /* cannot set error... cannot get back to DDE instance */
1153 return 0;
1156 switch (wType)
1158 case XTYP_EXECUTE:
1159 /* Windows simply ignores hszItem and wFmt in this case */
1160 pXAct = WDML_ClientQueueExecute(pConv, pData, cbData);
1161 if (pXAct == NULL)
1162 return 0;
1163 break;
1164 case XTYP_POKE:
1165 if (!hszItem)
1167 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1168 return 0;
1170 pXAct = WDML_ClientQueuePoke(pConv, pData, cbData, wFmt, hszItem);
1171 break;
1172 case XTYP_ADVSTART|XTYPF_NODATA:
1173 case XTYP_ADVSTART|XTYPF_NODATA|XTYPF_ACKREQ:
1174 case XTYP_ADVSTART:
1175 case XTYP_ADVSTART|XTYPF_ACKREQ:
1176 if (pData)
1178 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1179 return 0;
1181 pXAct = WDML_ClientQueueAdvise(pConv, wType, wFmt, hszItem);
1182 break;
1183 case XTYP_ADVSTOP:
1184 if (pData)
1186 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1187 return 0;
1189 pXAct = WDML_ClientQueueUnadvise(pConv, wFmt, hszItem);
1190 break;
1191 case XTYP_REQUEST:
1192 if (pData || !hszItem)
1194 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1195 return 0;
1197 pXAct = WDML_ClientQueueRequest(pConv, wFmt, hszItem);
1198 break;
1199 default:
1200 FIXME("Unknown transaction type %04x\n", wType);
1201 /* unknown transaction type */
1202 pConv->instance->lastError = DMLERR_INVALIDPARAMETER;
1203 return 0;
1206 if (pXAct == NULL)
1208 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1209 return 0;
1212 WDML_QueueTransaction(pConv, pXAct);
1214 TRACE("pConv->wStatus %04x\n", pConv->wStatus);
1216 if (pConv->wStatus & ST_BLOCKED)
1218 TRACE("Transactions are blocked, add to the queue and exit\n");
1219 return (HDDEDATA)1;
1222 hDdeData = WDML_ClientHandle(pConv, pXAct, dwTimeout, pdwResult);
1223 if (dwTimeout != TIMEOUT_ASYNC)
1225 WDML_UnQueueTransaction(pConv, pXAct);
1226 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1229 return hDdeData;
1232 /*****************************************************************
1233 * DdeAbandonTransaction (USER32.@)
1235 BOOL WINAPI DdeAbandonTransaction(DWORD idInst, HCONV hConv, DWORD idTransaction)
1237 WDML_INSTANCE* pInstance;
1238 WDML_CONV* pConv;
1239 WDML_XACT* pXAct;
1241 if ((pInstance = WDML_GetInstance(idInst)))
1243 if (hConv)
1245 if ((pConv = WDML_GetConv(hConv, TRUE)) && pConv->instance == pInstance)
1248 pXAct = pConv->transactions;
1249 while (pXAct) {
1250 WDML_XACT *nextXAct = pXAct->next;
1252 if (pXAct->dwTimeout == TIMEOUT_ASYNC &&
1253 (idTransaction == 0 || pXAct->xActID == idTransaction))
1255 WDML_UnQueueTransaction(pConv, pXAct);
1256 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1258 pXAct = nextXAct;
1262 else
1264 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv; pConv = pConv->next)
1266 if (!(pConv->wStatus & ST_CONNECTED)) continue;
1267 pXAct = pConv->transactions;
1268 while (pXAct) {
1269 WDML_XACT *nextXAct = pXAct->next;
1271 if (pXAct->dwTimeout == TIMEOUT_ASYNC)
1273 WDML_UnQueueTransaction(pConv, pXAct);
1274 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1276 pXAct = nextXAct;
1282 return TRUE;
1285 /******************************************************************
1286 * WDML_ClientProc
1288 * Window Proc created on client side for each conversation
1290 static LRESULT CALLBACK WDML_ClientProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
1292 UINT uiLo, uiHi;
1293 WDML_CONV* pConv = NULL;
1294 HSZ hszSrv, hszTpc;
1296 TRACE("%p %04x %08lx %08lx\n", hwnd, iMsg, wParam , lParam);
1298 if (iMsg == WM_DDE_ACK &&
1299 /* in the initial WM_INITIATE sendmessage */
1300 ((pConv = WDML_GetConvFromWnd(hwnd)) == NULL || pConv->wStatus == XST_INIT1))
1302 /* In response to WM_DDE_INITIATE, save server window */
1303 char buf[256];
1304 WDML_INSTANCE* pInstance;
1306 /* note: sent messages do not need packing */
1307 uiLo = LOWORD(lParam);
1308 uiHi = HIWORD(lParam);
1310 /* FIXME: convlist should be handled here */
1311 if (pConv)
1313 /* we already have started the conv with a server, drop other replies */
1314 GlobalDeleteAtom(uiLo);
1315 GlobalDeleteAtom(uiHi);
1316 PostMessageW((HWND)wParam, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
1317 return 0;
1320 pInstance = WDML_GetInstanceFromWnd(hwnd);
1322 hszSrv = WDML_MakeHszFromAtom(pInstance, uiLo);
1323 hszTpc = WDML_MakeHszFromAtom(pInstance, uiHi);
1325 pConv = WDML_AddConv(pInstance, WDML_CLIENT_SIDE, hszSrv, hszTpc, hwnd, (HWND)wParam);
1327 SetWindowLongPtrW(hwnd, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
1328 pConv->wStatus |= ST_CONNECTED;
1329 pConv->wConvst = XST_INIT1;
1331 /* check if server is handled by DDEML */
1332 if ((GetClassNameA((HWND)wParam, buf, sizeof(buf)) &&
1333 lstrcmpiA(buf, WDML_szServerConvClassA) == 0) ||
1334 (GetClassNameW((HWND)wParam, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
1335 lstrcmpiW((LPWSTR)buf, WDML_szServerConvClassW) == 0))
1337 pConv->wStatus |= ST_ISLOCAL;
1340 GlobalDeleteAtom(uiLo);
1341 GlobalDeleteAtom(uiHi);
1343 /* accept conversation */
1344 return 1;
1347 if (iMsg >= WM_DDE_FIRST && iMsg <= WM_DDE_LAST)
1349 pConv = WDML_GetConvFromWnd(hwnd);
1351 if (pConv)
1353 MSG msg;
1354 HDDEDATA hdd;
1356 msg.hwnd = hwnd;
1357 msg.message = iMsg;
1358 msg.wParam = wParam;
1359 msg.lParam = lParam;
1361 WDML_HandleReply(pConv, &msg, &hdd, NULL);
1364 return 0;
1367 return IsWindowUnicode(hwnd) ? DefWindowProcW(hwnd, iMsg, wParam, lParam) :
1368 DefWindowProcA(hwnd, iMsg, wParam, lParam);
1371 /*****************************************************************
1372 * DdeDisconnect (USER32.@)
1374 BOOL WINAPI DdeDisconnect(HCONV hConv)
1376 WDML_CONV* pConv;
1377 WDML_XACT* pXAct;
1378 BOOL ret = FALSE;
1380 TRACE("(%p)\n", hConv);
1382 if (hConv == 0)
1384 WARN("DdeDisconnect(): hConv = 0\n");
1385 return FALSE;
1388 pConv = WDML_GetConv(hConv, TRUE);
1389 if (pConv != NULL)
1391 if (pConv->wStatus & ST_CLIENT)
1393 /* FIXME: should abandon all pending transactions */
1394 pXAct = WDML_ClientQueueTerminate(pConv);
1395 if (pXAct != NULL)
1397 if (PostMessageW(pConv->hwndServer, pXAct->ddeMsg,
1398 (WPARAM)pConv->hwndClient, pXAct->lParam))
1400 WDML_SyncWaitTransactionReply(hConv, 10000, pXAct, NULL);
1401 ret = TRUE;
1403 else
1404 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
1406 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
1407 /* still have to destroy data associated with conversation */
1408 WDML_RemoveConv(pConv, WDML_CLIENT_SIDE);
1410 else
1412 FIXME("Not implemented yet for a server side conversation\n");
1417 return ret;
1420 /*****************************************************************
1421 * DdeImpersonateClient (USER32.@)
1423 BOOL WINAPI DdeImpersonateClient(HCONV hConv)
1425 WDML_CONV* pConv;
1426 BOOL ret = FALSE;
1428 TRACE("(%p)\n", hConv);
1430 pConv = WDML_GetConv(hConv, TRUE);
1431 if (pConv)
1433 ret = ImpersonateDdeClientWindow(pConv->hwndClient, pConv->hwndServer);
1435 return ret;