gdi32/tests: Mark tests failing randomly on Windows as flaky.
[wine.git] / dlls / user32 / dde_server.c
blobecc7fbaee347261db21212d60f0e90acfdc0dc19
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 2003, 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 <wchar.h>
27 #include "user_private.h"
28 #include "dde_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
33 const char WDML_szServerConvClassA[] = "WineDdeServerConvA";
34 const WCHAR WDML_szServerConvClassW[] = L"WineDdeServerConvW";
36 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
37 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
39 /******************************************************************************
40 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
42 * PARAMS
43 * idInst [I] Instance identifier
44 * hszTopic [I] Handle to topic name string
45 * hszItem [I] Handle to item name string
47 * RETURNS
48 * Success: TRUE
49 * Failure: FALSE
51 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
53 WDML_INSTANCE* pInstance;
54 WDML_LINK* pLink;
55 HDDEDATA hDdeData;
56 HGLOBAL hItemData;
57 WDML_CONV* pConv;
58 ATOM atom;
59 UINT count;
61 TRACE("(%ld,%p,%p)\n", idInst, hszTopic, hszItem);
63 pInstance = WDML_GetInstance(idInst);
65 if (pInstance == NULL)
66 return FALSE;
68 atom = WDML_MakeAtomFromHsz(hszItem);
69 if (!atom) return FALSE;
71 /* first compute the number of links which will trigger a message */
72 count = 0;
73 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
75 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
77 count++;
80 if (count >= CADV_LATEACK)
82 FIXME("too high value for count\n");
83 count &= 0xFFFF;
86 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
88 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
90 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
91 hszTopic, hszItem, 0, --count, 0);
93 if (hDdeData == CBR_BLOCK)
95 /* MS doc is not consistent here */
96 FIXME("CBR_BLOCK returned for ADVREQ\n");
97 continue;
99 if (hDdeData)
101 if (pLink->transactionType & XTYPF_NODATA)
103 TRACE("no data\n");
104 hItemData = 0;
106 else
108 TRACE("with data\n");
110 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
113 pConv = WDML_GetConv(pLink->hConv, TRUE);
115 if (pConv == NULL)
117 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
118 goto theError;
121 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
122 PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
124 ERR("post message failed\n");
125 pConv->wStatus &= ~ST_CONNECTED;
126 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
127 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
128 GlobalFree(hItemData);
129 goto theError;
131 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
135 return TRUE;
137 theError:
138 GlobalDeleteAtom(atom);
139 return FALSE;
143 /******************************************************************************
144 * DdeNameService [USER32.@] {Un}registers service name of DDE server
146 * PARAMS
147 * idInst [I] Instance identifier
148 * hsz1 [I] Handle to service name string
149 * hsz2 [I] Reserved
150 * afCmd [I] Service name flags
152 * RETURNS
153 * Success: Non-zero
154 * Failure: 0
156 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
158 WDML_SERVER* pServer;
159 WDML_INSTANCE* pInstance;
160 HWND hwndServer;
161 WNDCLASSEXW wndclass;
163 TRACE("(%ld,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
165 /* First check instance
167 pInstance = WDML_GetInstance(idInst);
168 if (pInstance == NULL)
170 TRACE("Instance not found as initialised\n");
171 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
172 return NULL;
175 if (hsz2 != 0L)
177 /* Illegal, reserved parameter
179 pInstance->lastError = DMLERR_INVALIDPARAMETER;
180 WARN("Reserved parameter no-zero !!\n");
181 return NULL;
183 if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
185 /* don't know if we should check this but it makes sense
186 * why supply REGISTER or filter flags if de-registering all
188 TRACE("General unregister unexpected flags\n");
189 pInstance->lastError = DMLERR_INVALIDPARAMETER;
190 return NULL;
193 switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
195 case DNS_REGISTER:
196 pServer = WDML_FindServer(pInstance, hsz1, 0);
197 if (pServer)
199 ERR("Trying to register already registered service!\n");
200 pInstance->lastError = DMLERR_DLL_USAGE;
201 return NULL;
204 TRACE("Adding service name\n");
206 WDML_IncHSZ(pInstance, hsz1);
208 pServer = WDML_AddServer(pInstance, hsz1, 0);
210 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
211 pServer->atomService, pServer->atomServiceSpec);
213 wndclass.cbSize = sizeof(wndclass);
214 wndclass.style = 0;
215 wndclass.lpfnWndProc = WDML_ServerNameProc;
216 wndclass.cbClsExtra = 0;
217 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
218 wndclass.hInstance = 0;
219 wndclass.hIcon = 0;
220 wndclass.hCursor = 0;
221 wndclass.hbrBackground = 0;
222 wndclass.lpszMenuName = NULL;
223 wndclass.lpszClassName = L"WineDdeServerName";
224 wndclass.hIconSm = 0;
226 RegisterClassExW(&wndclass);
228 hwndServer = CreateWindowW(L"WineDdeServerName", NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
229 SetWindowLongPtrW(hwndServer, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
230 SetWindowLongPtrW(hwndServer, GWL_WDML_SERVER, (ULONG_PTR)pServer);
231 TRACE("Created nameServer=%p for instance=%08lx\n", hwndServer, idInst);
233 pServer->hwndServer = hwndServer;
234 break;
236 case DNS_UNREGISTER:
237 if (hsz1 == 0L)
239 /* General unregister situation
240 * terminate all server side pending conversations
242 while (pInstance->servers)
243 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
244 pInstance->servers = NULL;
245 TRACE("General de-register - finished\n");
247 else
249 WDML_RemoveServer(pInstance, hsz1, 0L);
251 break;
254 if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
256 /* Set filter flags on to hold notifications of connection
258 pServer = WDML_FindServer(pInstance, hsz1, 0);
259 if (!pServer)
261 /* trying to filter where no service names !!
263 pInstance->lastError = DMLERR_DLL_USAGE;
264 return NULL;
266 else
268 pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
271 return (HDDEDATA)TRUE;
274 /******************************************************************
275 * WDML_CreateServerConv
279 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
280 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
282 HWND hwndServerConv;
283 WDML_CONV* pConv;
285 if (pInstance->unicode)
287 WNDCLASSEXW wndclass;
289 wndclass.cbSize = sizeof(wndclass);
290 wndclass.style = 0;
291 wndclass.lpfnWndProc = WDML_ServerConvProc;
292 wndclass.cbClsExtra = 0;
293 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
294 wndclass.hInstance = 0;
295 wndclass.hIcon = 0;
296 wndclass.hCursor = 0;
297 wndclass.hbrBackground = 0;
298 wndclass.lpszMenuName = NULL;
299 wndclass.lpszClassName = WDML_szServerConvClassW;
300 wndclass.hIconSm = 0;
302 RegisterClassExW(&wndclass);
304 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
305 WS_CHILD, 0, 0, 0, 0,
306 hwndServerName, 0, 0, 0);
308 else
310 WNDCLASSEXA wndclass;
312 wndclass.cbSize = sizeof(wndclass);
313 wndclass.style = 0;
314 wndclass.lpfnWndProc = WDML_ServerConvProc;
315 wndclass.cbClsExtra = 0;
316 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
317 wndclass.hInstance = 0;
318 wndclass.hIcon = 0;
319 wndclass.hCursor = 0;
320 wndclass.hbrBackground = 0;
321 wndclass.lpszMenuName = NULL;
322 wndclass.lpszClassName = WDML_szServerConvClassA;
323 wndclass.hIconSm = 0;
325 RegisterClassExA(&wndclass);
327 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
328 WS_CHILD, 0, 0, 0, 0,
329 hwndServerName, 0, 0, 0);
332 TRACE("Created convServer=%p (nameServer=%p) for instance=%08lx unicode=%d\n",
333 hwndServerConv, hwndServerName, pInstance->instanceID, pInstance->unicode);
335 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
336 hwndClient, hwndServerConv);
337 if (pConv)
339 SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
340 SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
342 /* this should be the only place using SendMessage for WM_DDE_ACK */
343 /* note: sent messages shall not use packing */
344 SendMessageW(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
345 MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), WDML_MakeAtomFromHsz(hszTopic)));
346 /* we assume we're connected since we've sent an answer...
347 * I'm not sure what we can do... it doesn't look like the return value
348 * of SendMessage is used... sigh...
350 pConv->wStatus |= ST_CONNECTED;
352 else
354 NtUserDestroyWindow(hwndServerConv);
356 return pConv;
359 /******************************************************************
360 * WDML_ServerNameProc
364 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
366 HWND hwndClient;
367 HSZ hszApp, hszTop;
368 HDDEDATA hDdeData;
369 WDML_INSTANCE* pInstance;
370 UINT_PTR uiLo, uiHi;
372 switch (iMsg)
374 case WM_DDE_INITIATE:
376 /* wParam -- sending window handle
377 LOWORD(lParam) -- application atom
378 HIWORD(lParam) -- topic atom */
380 TRACE("WM_DDE_INITIATE message received!\n");
381 hwndClient = (HWND)wParam;
383 pInstance = WDML_GetInstanceFromWnd(hwndServer);
384 if (!pInstance) return 0;
385 TRACE("idInst=%ld, threadID=0x%lx\n", pInstance->instanceID, GetCurrentThreadId());
387 /* don't free DDEParams, since this is a broadcast */
388 UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
390 hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
391 hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
393 if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
395 BOOL self = FALSE;
396 CONVCONTEXT cc;
397 CONVCONTEXT* pcc = NULL;
398 WDML_CONV* pConv;
399 char buf[256];
401 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
402 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
404 self = TRUE;
406 /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
407 * handled under DDEML, and if so build a default context
409 if ((GetClassNameA(hwndClient, buf, sizeof(buf)) &&
410 lstrcmpiA(buf, WDML_szClientConvClassA) == 0) ||
411 (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
412 lstrcmpiW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
414 pcc = &cc;
415 memset(pcc, 0, sizeof(*pcc));
416 pcc->cb = sizeof(*pcc);
417 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
419 if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
421 TRACE("Don't do self connection as requested\n");
423 else if (hszApp && hszTop)
425 WDML_SERVER* pServer = (WDML_SERVER*)GetWindowLongPtrW(hwndServer, GWL_WDML_SERVER);
427 /* check filters for name service */
428 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
430 /* pass on to the callback */
431 hDdeData = WDML_InvokeCallback(pInstance, XTYP_CONNECT,
432 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
433 if ((ULONG_PTR)hDdeData)
435 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
436 hszApp, hszTop);
437 if (pConv)
439 if (pcc) pConv->wStatus |= ST_ISLOCAL;
440 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
441 hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
446 else if (pInstance->servers)
448 /* pass on to the callback */
449 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
450 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
452 if (hDdeData == CBR_BLOCK)
454 /* MS doc is not consistent here */
455 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
457 else if ((ULONG_PTR)hDdeData != 0)
459 HSZPAIR* hszp;
461 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
462 if (hszp)
464 int i;
465 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
467 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
468 hszp[i].hszSvc, hszp[i].hszTopic);
469 if (pConv)
471 if (pcc) pConv->wStatus |= ST_ISLOCAL;
472 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
473 hszp[i].hszTopic, hszp[i].hszSvc, 0, (ULONG_PTR)pcc, self);
476 DdeUnaccessData(hDdeData);
478 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
483 return 0;
485 case WM_DDE_REQUEST:
486 FIXME("WM_DDE_REQUEST message received!\n");
487 return 0;
488 case WM_DDE_ADVISE:
489 FIXME("WM_DDE_ADVISE message received!\n");
490 return 0;
491 case WM_DDE_UNADVISE:
492 FIXME("WM_DDE_UNADVISE message received!\n");
493 return 0;
494 case WM_DDE_EXECUTE:
495 FIXME("WM_DDE_EXECUTE message received!\n");
496 return 0;
497 case WM_DDE_POKE:
498 FIXME("WM_DDE_POKE message received!\n");
499 return 0;
500 case WM_DDE_TERMINATE:
501 FIXME("WM_DDE_TERMINATE message received!\n");
502 return 0;
503 default:
504 break;
507 return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
510 /******************************************************************
511 * WDML_ServerQueueRequest
515 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
517 UINT_PTR uiLo, uiHi;
518 WDML_XACT* pXAct;
520 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
522 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
523 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
524 if (pXAct) pXAct->atom = uiHi;
525 return pXAct;
528 /******************************************************************
529 * WDML_ServerHandleRequest
533 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
535 HDDEDATA hDdeData = 0;
536 BOOL fAck = TRUE;
538 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
541 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
542 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
545 switch ((ULONG_PTR)hDdeData)
547 case 0:
548 TRACE("No data returned from the Callback\n");
549 fAck = FALSE;
550 break;
552 case (ULONG_PTR)CBR_BLOCK:
553 return WDML_QS_BLOCK;
555 default:
557 HGLOBAL hMem = WDML_DataHandle2Global(hDdeData, TRUE, FALSE, FALSE, FALSE);
558 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
559 ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
560 (UINT_PTR)hMem, (UINT_PTR)pXAct->atom)))
562 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
563 DdeFreeDataHandle(hDdeData);
564 GlobalFree(hMem);
565 fAck = FALSE;
568 break;
571 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
573 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
575 return WDML_QS_HANDLED;
578 /******************************************************************
579 * WDML_ServerQueueAdvise
583 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
585 UINT_PTR uiLo, uiHi;
586 WDML_XACT* pXAct;
588 /* XTYP_ADVSTART transaction:
589 establish link and save link info to InstanceInfoTable */
591 if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
592 return NULL;
594 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
595 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
596 if (pXAct)
598 pXAct->hMem = (HGLOBAL)uiLo;
599 pXAct->atom = uiHi;
601 return pXAct;
604 /******************************************************************
605 * WDML_ServerHandleAdvise
609 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
611 UINT uType;
612 WDML_LINK* pLink;
613 DDEADVISE* pDdeAdvise;
614 HDDEDATA hDdeData = 0;
615 BOOL fAck = TRUE;
617 pDdeAdvise = GlobalLock(pXAct->hMem);
618 uType = XTYP_ADVSTART |
619 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
620 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
622 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
624 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
625 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
628 switch ((ULONG_PTR)hDdeData)
630 case 0:
631 TRACE("No data returned from the Callback\n");
632 fAck = FALSE;
633 break;
635 case (ULONG_PTR)CBR_BLOCK:
636 return WDML_QS_BLOCK;
638 default:
639 /* billx: first to see if the link is already created. */
640 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
641 pXAct->hszItem, TRUE, pDdeAdvise->cfFormat);
643 if (pLink != NULL)
645 /* we found a link, and only need to modify it in case it changes */
646 pLink->transactionType = uType;
648 else
650 TRACE("Adding Link with hConv %p\n", pConv);
651 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
652 uType, pXAct->hszItem, pDdeAdvise->cfFormat);
654 break;
657 GlobalUnlock(pXAct->hMem);
658 if (fAck)
660 GlobalFree(pXAct->hMem);
662 pXAct->hMem = 0;
664 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
666 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
668 return WDML_QS_HANDLED;
671 /******************************************************************
672 * WDML_ServerQueueUnadvise
676 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
678 UINT_PTR uiLo, uiHi;
679 WDML_XACT* pXAct;
681 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
683 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
684 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
685 if (pXAct) pXAct->atom = uiHi;
686 return pXAct;
689 /******************************************************************
690 * WDML_ServerHandleUnadvise
694 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
696 WDML_LINK* pLink;
698 if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
700 ERR("Unsupported yet options (null item or clipboard format)\n");
701 return WDML_QS_ERROR;
704 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
705 pXAct->hszItem, TRUE, pXAct->wFmt);
706 if (pLink == NULL)
708 ERR("Couldn't find link for %p, dropping request\n", pXAct->hszItem);
709 FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
710 return WDML_QS_ERROR;
713 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
715 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
716 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
719 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
720 pXAct->hszItem, pXAct->wFmt);
722 /* send back ack */
723 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
724 pXAct->lParam, WM_DDE_UNADVISE);
726 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
728 return WDML_QS_HANDLED;
731 /******************************************************************
732 * WDML_QueueExecute
736 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
738 WDML_XACT* pXAct;
740 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
741 if (pXAct)
743 pXAct->hMem = (HGLOBAL)lParam;
745 return pXAct;
748 static BOOL data_looks_unicode( const WCHAR *data, DWORD size )
750 DWORD i;
752 if (size % sizeof(WCHAR)) return FALSE;
753 for (i = 0; i < size / sizeof(WCHAR); i++) if (data[i] > 255) return FALSE;
754 return TRUE;
757 /* convert data to Unicode, unless it looks like it's already Unicode */
758 static HDDEDATA map_A_to_W( DWORD instance, void *ptr, DWORD size )
760 HDDEDATA ret;
761 DWORD len;
762 const char *end;
764 if (!data_looks_unicode( ptr, size ))
766 if ((end = memchr( ptr, 0, size ))) size = end + 1 - (const char *)ptr;
767 len = MultiByteToWideChar( CP_ACP, 0, ptr, size, NULL, 0 );
768 ret = DdeCreateDataHandle( instance, NULL, len * sizeof(WCHAR), 0, 0, CF_TEXT, 0);
769 MultiByteToWideChar( CP_ACP, 0, ptr, size, (WCHAR *)DdeAccessData(ret, NULL), len );
771 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
773 return ret;
776 /* convert data to ANSI, unless it looks like it's not in Unicode format */
777 static HDDEDATA map_W_to_A( DWORD instance, void *ptr, DWORD size )
779 HDDEDATA ret;
780 DWORD len;
781 const WCHAR *end;
783 if (data_looks_unicode( ptr, size ))
785 size /= sizeof(WCHAR);
786 if ((end = wmemchr( ptr, 0, size ))) size = end + 1 - (const WCHAR *)ptr;
787 len = WideCharToMultiByte( CP_ACP, 0, ptr, size, NULL, 0, NULL, NULL );
788 ret = DdeCreateDataHandle( instance, NULL, len, 0, 0, CF_TEXT, 0);
789 WideCharToMultiByte( CP_ACP, 0, ptr, size, (char *)DdeAccessData(ret, NULL), len, NULL, NULL );
791 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
793 return ret;
796 /******************************************************************
797 * WDML_ServerHandleExecute
801 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
803 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
804 BOOL fAck = FALSE, fBusy = FALSE;
806 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
808 LPVOID ptr = GlobalLock(pXAct->hMem);
809 DWORD size = GlobalSize(pXAct->hMem);
811 if (ptr)
813 if (pConv->instance->unicode) /* Unicode server, try to map A->W */
814 hDdeData = map_A_to_W( pConv->instance->instanceID, ptr, size );
815 else if (!IsWindowUnicode( pConv->hwndClient )) /* ANSI server and client, try to map W->A */
816 hDdeData = map_W_to_A( pConv->instance->instanceID, ptr, size );
817 else
818 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, ptr, size, 0, 0, CF_TEXT, 0);
819 GlobalUnlock(pXAct->hMem);
821 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
822 pConv->hszTopic, 0, hDdeData, 0L, 0L);
825 switch ((ULONG_PTR)hDdeData)
827 case (ULONG_PTR)CBR_BLOCK:
828 return WDML_QS_BLOCK;
830 case DDE_FACK:
831 fAck = TRUE;
832 break;
833 case DDE_FBUSY:
834 fBusy = TRUE;
835 break;
836 default:
837 FIXME("Unsupported returned value %p\n", hDdeData);
838 /* fall through */
839 case DDE_FNOTPROCESSED:
840 break;
842 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT_PTR)pXAct->hMem, 0, 0);
844 return WDML_QS_HANDLED;
847 /******************************************************************
848 * WDML_ServerQueuePoke
852 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
854 UINT_PTR uiLo, uiHi;
855 WDML_XACT* pXAct;
857 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
859 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
860 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
861 if (pXAct)
863 pXAct->atom = uiHi;
864 pXAct->hMem = (HGLOBAL)uiLo;
866 return pXAct;
869 /******************************************************************
870 * WDML_ServerHandlePoke
874 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
876 DDEPOKE* pDdePoke;
877 HDDEDATA hDdeData;
878 BOOL fBusy = FALSE, fAck = FALSE;
880 pDdePoke = GlobalLock(pXAct->hMem);
881 if (!pDdePoke)
883 return WDML_QS_ERROR;
886 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
888 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
889 GlobalSize(pXAct->hMem) - FIELD_OFFSET(DDEPOKE, Value),
890 0, 0, pDdePoke->cfFormat, 0);
891 if (hDdeData)
893 HDDEDATA hDdeDataOut;
895 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
896 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
897 hDdeData, 0, 0);
898 switch ((ULONG_PTR)hDdeDataOut)
900 case DDE_FACK:
901 fAck = TRUE;
902 break;
903 case DDE_FBUSY:
904 fBusy = TRUE;
905 break;
906 default:
907 FIXME("Unsupported returned value %p\n", hDdeDataOut);
908 /* fal through */
909 case DDE_FNOTPROCESSED:
910 break;
912 DdeFreeDataHandle(hDdeData);
915 GlobalUnlock(pXAct->hMem);
917 if (!fAck)
919 GlobalFree(pXAct->hMem);
921 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
923 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
925 return WDML_QS_HANDLED;
928 /******************************************************************
929 * WDML_ServerQueueTerminate
933 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
935 WDML_XACT* pXAct;
937 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
938 return pXAct;
941 /******************************************************************
942 * WDML_ServerHandleTerminate
946 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
948 /* billx: two things to remove: the conv, and associated links.
949 * Respond with another WM_DDE_TERMINATE iMsg.
951 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
953 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
954 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
956 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
957 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
959 return WDML_QS_HANDLED;
962 /******************************************************************
963 * WDML_ServerHandle
967 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
969 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
971 switch (pXAct->ddeMsg)
973 case WM_DDE_INITIATE:
974 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
975 break;
976 case WM_DDE_REQUEST:
977 qs = WDML_ServerHandleRequest(pConv, pXAct);
978 break;
980 case WM_DDE_ADVISE:
981 qs = WDML_ServerHandleAdvise(pConv, pXAct);
982 break;
984 case WM_DDE_UNADVISE:
985 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
986 break;
988 case WM_DDE_EXECUTE:
989 qs = WDML_ServerHandleExecute(pConv, pXAct);
990 break;
992 case WM_DDE_POKE:
993 qs = WDML_ServerHandlePoke(pConv, pXAct);
994 break;
996 case WM_DDE_TERMINATE:
997 qs = WDML_ServerHandleTerminate(pConv, pXAct);
998 break;
1000 case WM_DDE_ACK:
1001 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1002 break;
1004 default:
1005 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
1007 return qs;
1010 /******************************************************************
1011 * WDML_ServerConvProc
1015 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
1017 WDML_INSTANCE* pInstance;
1018 WDML_CONV* pConv;
1019 WDML_XACT* pXAct = NULL;
1021 TRACE("%p %04x %08Ix %08Ix\n", hwndServer, iMsg, wParam, lParam);
1023 if (iMsg == WM_DESTROY)
1025 pConv = WDML_GetConvFromWnd(hwndServer);
1026 if (pConv && !(pConv->wStatus & ST_TERMINATED))
1028 WDML_ServerHandleTerminate(pConv, NULL);
1031 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
1033 return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
1034 DefWindowProcA(hwndServer, iMsg, wParam, lParam);
1037 pInstance = WDML_GetInstanceFromWnd(hwndServer);
1038 pConv = WDML_GetConvFromWnd(hwndServer);
1040 if (!pConv)
1042 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1043 return 0;
1045 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1047 ERR("mismatch between C/S windows and conversation\n");
1048 return 0;
1050 if (pConv->instance != pInstance || pConv->instance == NULL)
1052 ERR("mismatch in instances\n");
1053 return 0;
1056 switch (iMsg)
1058 case WM_DDE_INITIATE:
1059 FIXME("WM_DDE_INITIATE message received!\n");
1060 break;
1062 case WM_DDE_REQUEST:
1063 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1064 break;
1066 case WM_DDE_ADVISE:
1067 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1068 break;
1070 case WM_DDE_UNADVISE:
1071 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1072 break;
1074 case WM_DDE_EXECUTE:
1075 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1076 break;
1078 case WM_DDE_POKE:
1079 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1080 break;
1082 case WM_DDE_TERMINATE:
1083 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1084 break;
1086 case WM_DDE_ACK:
1087 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1088 break;
1090 default:
1091 FIXME("Unsupported message %x\n", iMsg);
1092 break;
1095 if (pXAct)
1097 pXAct->lParam = lParam;
1099 if ((pConv->wStatus & ST_BLOCKED) || WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1101 TRACE("Transactions are blocked, add to the queue and exit\n");
1102 WDML_QueueTransaction(pConv, pXAct);
1104 else
1106 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1109 else
1110 pConv->instance->lastError = DMLERR_MEMORY_ERROR;
1112 return 0;