Make sure that HWND comparisons are always done with full 32-bit
[wine/multimedia.git] / dlls / user / dde / server.c
blob301c2448b529a62ed785c9a3f63cfc6627825086
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
13 #include <string.h>
14 #include "winbase.h"
15 #include "windef.h"
16 #include "wingdi.h"
17 #include "winuser.h"
18 #include "winerror.h"
19 #include "dde.h"
20 #include "ddeml.h"
21 #include "win.h"
22 #include "debugtools.h"
23 #include "dde/dde_private.h"
25 DEFAULT_DEBUG_CHANNEL(ddeml);
27 static const char szServerNameClassA[] = "DdeServerNameAnsi";
28 const char WDML_szServerConvClassA[] = "DdeServerConvAnsi";
29 const WCHAR WDML_szServerConvClassW[] = {'D','d','e','S','e','r','v','e','r','C','o','n','v','U','n','i','c','o','d','e',0};
31 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
32 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
34 /******************************************************************************
35 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
37 * PARAMS
38 * idInst [I] Instance identifier
39 * hszTopic [I] Handle to topic name string
40 * hszItem [I] Handle to item name string
42 * RETURNS
43 * Success: TRUE
44 * Failure: FALSE
46 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
48 WDML_INSTANCE* pInstance = NULL;
49 WDML_LINK* pLink = NULL;
50 HDDEDATA hDdeData = 0, hItemData = 0;
51 WDML_CONV* pConv = NULL;
52 ATOM atom = 0;
53 UINT count;
55 TRACE("(%ld,0x%x,0x%x)\n", idInst, hszTopic, hszItem);
57 EnterCriticalSection(&WDML_CritSect);
59 pInstance = WDML_GetInstance(idInst);
61 if (pInstance == NULL || pInstance->links == NULL)
63 goto theError;
66 atom = WDML_MakeAtomFromHsz(hszItem);
67 if (!atom) goto theError;
69 /* first compute the number of links which will trigger a message */
70 count = 0;
71 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
73 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
75 count++;
78 if (count >= CADV_LATEACK)
80 FIXME("too high value for count\n");
81 count &= 0xFFFF;
84 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
86 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
88 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
89 hszTopic, hszItem, 0, count--, 0);
91 if (hDdeData == CBR_BLOCK)
93 /* MS doc is not consistent here */
94 FIXME("CBR_BLOCK returned for ADVREQ\n");
95 continue;
97 if (hDdeData)
99 if (pLink->transactionType & XTYPF_NODATA)
101 TRACE("no data\n");
102 hItemData = 0;
104 else
106 TRACE("with data\n");
108 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
111 pConv = WDML_GetConv(pLink->hConv, TRUE);
113 if (pConv == NULL)
115 /* FIXME: wrong if app owned... */
116 DdeFreeDataHandle(hDdeData);
117 goto theError;
120 if (!PostMessageA(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
121 PackDDElParam(WM_DDE_DATA, (UINT)hItemData, atom)))
123 ERR("post message failed\n");
124 /* FIXME: wrong if app owned... */
125 DdeFreeDataHandle(hDdeData);
126 GlobalFree(hItemData);
127 goto theError;
132 LeaveCriticalSection(&WDML_CritSect);
133 return TRUE;
134 theError:
135 LeaveCriticalSection(&WDML_CritSect);
136 if (atom) GlobalDeleteAtom(atom);
137 return FALSE;
141 /******************************************************************************
142 * DdeNameService [USER32.@] {Un}registers service name of DDE server
144 * PARAMS
145 * idInst [I] Instance identifier
146 * hsz1 [I] Handle to service name string
147 * hsz2 [I] Reserved
148 * afCmd [I] Service name flags
150 * RETURNS
151 * Success: Non-zero
152 * Failure: 0
154 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
156 WDML_SERVER* pServer;
157 WDML_INSTANCE* pInstance;
158 HDDEDATA hDdeData;
159 HWND hwndServer;
160 WNDCLASSEXA wndclass;
162 hDdeData = (HDDEDATA)NULL;
164 TRACE("(%ld,0x%x,0x%x,%d)\n", idInst, hsz1, hsz2, afCmd);
166 EnterCriticalSection(&WDML_CritSect);
168 /* First check instance
170 pInstance = WDML_GetInstance(idInst);
171 if (pInstance == NULL)
173 TRACE("Instance not found as initialised\n");
174 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
175 goto theError;
178 if (hsz2 != 0L)
180 /* Illegal, reserved parameter
182 pInstance->lastError = DMLERR_INVALIDPARAMETER;
183 WARN("Reserved parameter no-zero !!\n");
184 goto theError;
186 if (hsz1 == 0 && afCmd != DNS_UNREGISTER)
188 /* don't know if we should check this but it makes sense
189 * why supply REGISTER or filter flags if de-registering all
191 TRACE("General unregister unexpected flags\n");
192 pInstance->lastError = DMLERR_INVALIDPARAMETER;
193 goto theError;
196 switch (afCmd)
198 case DNS_REGISTER:
199 pServer = WDML_FindServer(pInstance, hsz1, 0);
200 if (pServer)
202 ERR("Trying to register already registered service!\n");
203 pInstance->lastError = DMLERR_DLL_USAGE;
204 goto theError;
207 TRACE("Adding service name\n");
209 WDML_IncHSZ(pInstance, hsz1);
211 pServer = WDML_AddServer(pInstance, hsz1, 0);
213 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
214 pServer->atomService, pServer->atomServiceSpec);
216 wndclass.cbSize = sizeof(wndclass);
217 wndclass.style = 0;
218 wndclass.lpfnWndProc = WDML_ServerNameProc;
219 wndclass.cbClsExtra = 0;
220 wndclass.cbWndExtra = 2 * sizeof(DWORD);
221 wndclass.hInstance = 0;
222 wndclass.hIcon = 0;
223 wndclass.hCursor = 0;
224 wndclass.hbrBackground = 0;
225 wndclass.lpszMenuName = NULL;
226 wndclass.lpszClassName = szServerNameClassA;
227 wndclass.hIconSm = 0;
229 RegisterClassExA(&wndclass);
231 LeaveCriticalSection(&WDML_CritSect);
232 hwndServer = CreateWindowA(szServerNameClassA, NULL,
233 WS_POPUP, 0, 0, 0, 0,
234 0, 0, 0, 0);
235 EnterCriticalSection(&WDML_CritSect);
237 SetWindowLongA(hwndServer, GWL_WDML_INSTANCE, (DWORD)pInstance);
238 SetWindowLongA(hwndServer, GWL_WDML_SERVER, (DWORD)pServer);
239 TRACE("Created nameServer=%04x for instance=%08lx\n", hwndServer, idInst);
241 pServer->hwndServer = hwndServer;
242 break;
244 case DNS_UNREGISTER:
245 if (hsz1 == 0L)
247 /* General unregister situation
248 * terminate all server side pending conversations
250 while (pInstance->servers)
251 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
252 pInstance->servers = NULL;
253 TRACE("General de-register - finished\n");
255 else
257 WDML_RemoveServer(pInstance, hsz1, 0L);
259 break;
260 case DNS_FILTERON:
261 case DNS_FILTEROFF:
262 /* Set filter flags on to hold notifications of connection
264 pServer = WDML_FindServer(pInstance, hsz1, 0);
265 if (!pServer)
267 /* trying to filter where no service names !!
269 pInstance->lastError = DMLERR_DLL_USAGE;
270 goto theError;
272 else
274 pServer->filterOn = (afCmd == DNS_FILTERON);
276 break;
278 LeaveCriticalSection(&WDML_CritSect);
279 return (HDDEDATA)TRUE;
281 theError:
282 LeaveCriticalSection(&WDML_CritSect);
283 return FALSE;
286 /******************************************************************
287 * WDML_CreateServerConv
291 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
292 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
294 HWND hwndServerConv;
295 WDML_CONV* pConv;
297 if (pInstance->unicode)
299 WNDCLASSEXW wndclass;
301 wndclass.cbSize = sizeof(wndclass);
302 wndclass.style = 0;
303 wndclass.lpfnWndProc = WDML_ServerConvProc;
304 wndclass.cbClsExtra = 0;
305 wndclass.cbWndExtra = 2 * sizeof(DWORD);
306 wndclass.hInstance = 0;
307 wndclass.hIcon = 0;
308 wndclass.hCursor = 0;
309 wndclass.hbrBackground = 0;
310 wndclass.lpszMenuName = NULL;
311 wndclass.lpszClassName = WDML_szServerConvClassW;
312 wndclass.hIconSm = 0;
314 RegisterClassExW(&wndclass);
316 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
317 WS_CHILD, 0, 0, 0, 0,
318 hwndServerName, 0, 0, 0);
320 else
322 WNDCLASSEXA wndclass;
324 wndclass.cbSize = sizeof(wndclass);
325 wndclass.style = 0;
326 wndclass.lpfnWndProc = WDML_ServerConvProc;
327 wndclass.cbClsExtra = 0;
328 wndclass.cbWndExtra = 2 * sizeof(DWORD);
329 wndclass.hInstance = 0;
330 wndclass.hIcon = 0;
331 wndclass.hCursor = 0;
332 wndclass.hbrBackground = 0;
333 wndclass.lpszMenuName = NULL;
334 wndclass.lpszClassName = WDML_szServerConvClassA;
335 wndclass.hIconSm = 0;
337 RegisterClassExA(&wndclass);
339 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
340 WS_CHILD, 0, 0, 0, 0,
341 hwndServerName, 0, 0, 0);
344 TRACE("Created convServer=%04x (nameServer=%04x) for instance=%08lx\n",
345 hwndServerConv, hwndServerName, pInstance->instanceID);
347 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
348 hwndClient, hwndServerConv);
349 if (pConv)
351 SetWindowLongA(hwndServerConv, GWL_WDML_INSTANCE, (DWORD)pInstance);
352 SetWindowLongA(hwndServerConv, GWL_WDML_CONVERSATION, (DWORD)pConv);
354 /* this should be the only place using SendMessage for WM_DDE_ACK */
355 SendMessageA(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
356 PackDDElParam(WM_DDE_ACK,
357 WDML_MakeAtomFromHsz(hszApp),
358 WDML_MakeAtomFromHsz(hszTopic)));
359 /* we assume we're connected since we've sent an answer...
360 * I'm not sure what we can do... it doesn't look like the return value
361 * of SendMessage is used... sigh...
363 pConv->wStatus |= ST_CONNECTED;
365 else
367 DestroyWindow(hwndServerConv);
369 return pConv;
372 /******************************************************************
373 * WDML_ServerNameProc
377 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
379 HWND hwndClient;
380 HSZ hszApp, hszTop;
381 HDDEDATA hDdeData = 0;
382 WDML_INSTANCE* pInstance;
383 UINT uiLo, uiHi;
385 switch (iMsg)
387 case WM_DDE_INITIATE:
389 /* wParam -- sending window handle
390 LOWORD(lParam) -- application atom
391 HIWORD(lParam) -- topic atom */
393 TRACE("WM_DDE_INITIATE message received!\n");
394 hwndClient = (HWND)wParam;
396 pInstance = WDML_GetInstanceFromWnd(hwndServer);
397 TRACE("idInst=%ld, threadID=0x%lx\n", pInstance->instanceID, GetCurrentThreadId());
398 if (!pInstance) return 0;
400 /* don't free DDEParams, since this is a broadcast */
401 UnpackDDElParam(WM_DDE_INITIATE, lParam, &uiLo, &uiHi);
403 hszApp = WDML_MakeHszFromAtom(pInstance, uiLo);
404 hszTop = WDML_MakeHszFromAtom(pInstance, uiHi);
406 if (!(pInstance->CBFflags & CBF_FAIL_CONNECTIONS))
408 BOOL self = FALSE;
409 CONVCONTEXT cc;
410 CONVCONTEXT* pcc = NULL;
411 WDML_CONV* pConv;
412 char buf[256];
414 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
415 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
417 self = TRUE;
419 /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
420 * handled under DDEML, and if so build a default context
422 if ((GetClassNameA(hwndClient, buf, sizeof(buf)) &&
423 strcmp(buf, WDML_szClientConvClassA) == 0) ||
424 (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
425 lstrcmpW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
427 pcc = &cc;
428 memset(pcc, 0, sizeof(*pcc));
429 pcc->cb = sizeof(*pcc);
430 pcc->iCodePage = IsWindowUnicode(hwndClient) ? CP_WINUNICODE : CP_WINANSI;
432 if ((pInstance->CBFflags & CBF_FAIL_SELFCONNECTIONS) && self)
434 TRACE("Don't do self connection as requested\n");
436 else if (hszApp && hszTop)
438 WDML_SERVER* pServer = (WDML_SERVER*)GetWindowLongA(hwndServer, GWL_WDML_SERVER);
440 /* check filters for name service */
441 if (!pServer->filterOn || DdeCmpStringHandles(pServer->hszService, hszApp) == 0)
443 /* pass on to the callback */
444 hDdeData = WDML_InvokeCallback(pInstance, XTYP_CONNECT,
445 0, 0, hszTop, hszApp, 0, (DWORD)pcc, self);
446 if ((UINT)hDdeData)
448 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
449 hszApp, hszTop);
450 if (pConv && pcc) pConv->wStatus |= ST_ISLOCAL;
454 else if (pInstance->servers)
456 /* pass on to the callback */
457 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
458 0, 0, hszTop, hszApp, 0, (DWORD)pcc, self);
460 if (hDdeData == CBR_BLOCK)
462 /* MS doc is not consistent here */
463 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
465 else if ((UINT)hDdeData != 0)
467 HSZPAIR* hszp;
469 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
470 if (hszp)
472 int i;
473 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
475 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
476 hszp[i].hszSvc, hszp[i].hszTopic);
477 if (pConv && pcc) pConv->wStatus |= ST_ISLOCAL;
479 DdeUnaccessData(hDdeData);
485 billx: make a conv and add it to the server list -
486 this can be delayed when link is created for the conv. NO NEED !!!
489 return 0;
492 case WM_DDE_REQUEST:
493 FIXME("WM_DDE_REQUEST message received!\n");
494 return 0;
495 case WM_DDE_ADVISE:
496 FIXME("WM_DDE_ADVISE message received!\n");
497 return 0;
498 case WM_DDE_UNADVISE:
499 FIXME("WM_DDE_UNADVISE message received!\n");
500 return 0;
501 case WM_DDE_EXECUTE:
502 FIXME("WM_DDE_EXECUTE message received!\n");
503 return 0;
504 case WM_DDE_POKE:
505 FIXME("WM_DDE_POKE message received!\n");
506 return 0;
507 case WM_DDE_TERMINATE:
508 FIXME("WM_DDE_TERMINATE message received!\n");
509 return 0;
513 return DefWindowProcA(hwndServer, iMsg, wParam, lParam);
516 /******************************************************************
517 * WDML_ServerQueueRequest
521 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
523 UINT uiLo, uiHi;
524 WDML_XACT* pXAct;
526 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
528 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
529 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
530 if (pXAct) pXAct->atom = uiHi;
531 return pXAct;
534 /******************************************************************
535 * WDML_ServerHandleRequest
539 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
541 HDDEDATA hDdeData = 0;
542 WDML_QUEUE_STATE ret = WDML_QS_HANDLED;
544 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
547 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
548 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
551 switch (hDdeData)
553 case 0:
554 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, FALSE, pXAct->hszItem,
555 pXAct->lParam, WM_DDE_REQUEST);
556 break;
557 case CBR_BLOCK:
558 ret = WDML_QS_BLOCK;
559 break;
560 default:
562 HGLOBAL hMem = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
563 if (!PostMessageA(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
564 ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
565 (UINT)hMem, (UINT)pXAct->atom)))
567 DdeFreeDataHandle(hDdeData);
568 GlobalFree(hMem);
571 break;
573 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
574 return ret;
577 /******************************************************************
578 * WDML_ServerQueueAdvise
582 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
584 UINT uiLo, uiHi;
585 WDML_XACT* pXAct;
587 /* XTYP_ADVSTART transaction:
588 establish link and save link info to InstanceInfoTable */
590 UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi);
592 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
593 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
594 if (pXAct)
596 pXAct->hMem = (HGLOBAL)uiLo;
597 pXAct->atom = uiHi;
599 return pXAct;
602 /******************************************************************
603 * WDML_ServerHandleAdvise
607 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
609 UINT uType;
610 WDML_LINK* pLink;
611 DDEADVISE* pDdeAdvise;
612 HDDEDATA hDdeData;
613 BOOL fAck;
615 pDdeAdvise = (DDEADVISE*)GlobalLock(pXAct->hMem);
616 uType = XTYP_ADVSTART |
617 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
618 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
620 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
622 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
623 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
625 else
627 hDdeData = 0;
630 if ((UINT)hDdeData)
632 fAck = TRUE;
634 /* billx: first to see if the link is already created. */
635 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
636 pXAct->hszItem, pDdeAdvise->cfFormat);
638 if (pLink != NULL)
640 /* we found a link, and only need to modify it in case it changes */
641 pLink->transactionType = uType;
643 else
645 TRACE("Adding Link with hConv=0x%lx\n", (DWORD)pConv);
647 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
648 uType, pXAct->hszItem, pDdeAdvise->cfFormat);
651 else
653 TRACE("No data returned from the Callback\n");
654 fAck = FALSE;
657 GlobalUnlock(pXAct->hMem);
658 if (fAck)
659 GlobalFree(pXAct->hMem);
661 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
663 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
665 return WDML_QS_HANDLED;
668 /******************************************************************
669 * WDML_ServerQueueUnadvise
673 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
675 UINT uiLo, uiHi;
676 WDML_XACT* pXAct;
678 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
680 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
681 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
682 if (pXAct) pXAct->atom = uiHi;
683 return pXAct;
686 /******************************************************************
687 * WDML_ServerHandleUnadvise
691 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
693 WDML_LINK* pLink;
695 if (pXAct->hszItem == (HSZ)0 || pXAct->wFmt == 0)
697 ERR("Unsupported yet options (null item or clipboard format)\n");
698 return WDML_QS_ERROR;
701 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
702 pXAct->hszItem, pXAct->wFmt);
703 if (pLink == NULL)
705 ERR("Couln'd find link for %08lx, dropping request\n", (DWORD)pXAct->hszItem);
706 FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
707 return WDML_QS_ERROR;
710 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
712 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
713 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
716 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
717 pXAct->hszItem, pXAct->wFmt);
719 /* send back ack */
720 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
721 pXAct->lParam, WM_DDE_UNADVISE);
723 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
725 return WDML_QS_HANDLED;
728 /******************************************************************
729 * WDML_QueueExecute
733 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
735 WDML_XACT* pXAct;
737 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
738 if (pXAct)
740 pXAct->hMem = (HGLOBAL)lParam;
742 return pXAct;
745 /******************************************************************
746 * WDML_ServerHandleExecute
750 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
752 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
753 BOOL fAck = FALSE, fBusy = FALSE;
755 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
757 LPVOID ptr = GlobalLock(pXAct->hMem);
759 if (ptr)
761 hDdeData = DdeCreateDataHandle(0, ptr, GlobalSize(pXAct->hMem),
762 0, 0, CF_TEXT, 0);
763 GlobalUnlock(pXAct->hMem);
765 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
766 pConv->hszTopic, 0, hDdeData, 0L, 0L);
769 switch ((UINT)hDdeData)
771 case DDE_FACK:
772 fAck = TRUE;
773 break;
774 case DDE_FBUSY:
775 fBusy = TRUE;
776 break;
777 default:
778 WARN("Bad result code\n");
779 /* fall through */
780 case DDE_FNOTPROCESSED:
781 break;
783 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->hMem, 0, 0);
785 return WDML_QS_HANDLED;
788 /******************************************************************
789 * WDML_ServerQueuePoke
793 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
795 UINT uiLo, uiHi;
796 WDML_XACT* pXAct;
798 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
800 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
801 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
802 if (pXAct)
804 pXAct->atom = uiHi;
805 pXAct->hMem = (HGLOBAL)uiLo;
807 return pXAct;
810 /******************************************************************
811 * WDML_ServerHandlePoke
815 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
817 DDEPOKE* pDdePoke;
818 HDDEDATA hDdeData;
819 BOOL fBusy = FALSE, fAck = FALSE;
821 pDdePoke = (DDEPOKE*)GlobalLock(pXAct->hMem);
822 if (!pDdePoke)
824 return WDML_QS_ERROR;
827 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
829 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
830 GlobalSize(pXAct->hMem) - sizeof(DDEPOKE) + 1,
831 0, 0, pDdePoke->cfFormat, 0);
832 if (hDdeData)
834 HDDEDATA hDdeDataOut;
836 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
837 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
838 hDdeData, 0, 0);
839 switch ((UINT)hDdeDataOut)
841 case DDE_FACK:
842 fAck = TRUE;
843 break;
844 case DDE_FBUSY:
845 fBusy = TRUE;
846 break;
847 default:
848 FIXME("Unsupported returned value %08lx\n", (DWORD)hDdeDataOut);
849 /* fal through */
850 case DDE_FNOTPROCESSED:
851 break;
853 DdeFreeDataHandle(hDdeData);
856 GlobalUnlock(pXAct->hMem);
858 if (!fAck)
859 GlobalFree(pXAct->hMem);
861 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
863 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
865 return WDML_QS_HANDLED;
868 /******************************************************************
869 * WDML_ServerQueueTerminate
873 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
875 WDML_XACT* pXAct;
877 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
878 return pXAct;
881 /******************************************************************
882 * WDML_ServerHandleTerminate
886 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
888 /* billx: two things to remove: the conv, and associated links.
889 * Respond with another WM_DDE_TERMINATE iMsg.
891 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
893 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
894 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
897 PostMessageA(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
898 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
900 return WDML_QS_HANDLED;
903 /******************************************************************
904 * WDML_ServerHandle
908 static WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
910 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
912 switch (pXAct->ddeMsg)
914 case WM_DDE_INITIATE:
915 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
916 break;
917 case WM_DDE_REQUEST:
918 qs = WDML_ServerHandleRequest(pConv, pXAct);
919 break;
921 case WM_DDE_ADVISE:
922 qs = WDML_ServerHandleAdvise(pConv, pXAct);
923 break;
925 case WM_DDE_UNADVISE:
926 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
927 break;
929 case WM_DDE_EXECUTE:
930 qs = WDML_ServerHandleExecute(pConv, pXAct);
931 break;
933 case WM_DDE_POKE:
934 qs = WDML_ServerHandlePoke(pConv, pXAct);
935 break;
937 case WM_DDE_TERMINATE:
938 qs = WDML_ServerHandleTerminate(pConv, pXAct);
939 break;
941 case WM_DDE_ACK:
942 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
943 break;
945 default:
946 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
948 return qs;
951 /******************************************************************
952 * WDML_ServerConvProc
956 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
958 WDML_INSTANCE* pInstance;
959 WDML_CONV* pConv;
960 WDML_XACT* pXAct = NULL;
962 if (iMsg == WM_DESTROY)
964 EnterCriticalSection(&WDML_CritSect);
965 pConv = WDML_GetConvFromWnd(hwndServer);
966 if (pConv && !(pConv->wStatus & ST_TERMINATED))
968 WDML_ServerHandleTerminate(pConv, NULL);
970 LeaveCriticalSection(&WDML_CritSect);
972 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
974 return DefWindowProcA(hwndServer, iMsg, wParam, lParam);
977 EnterCriticalSection(&WDML_CritSect);
979 pInstance = WDML_GetInstanceFromWnd(hwndServer);
980 pConv = WDML_GetConvFromWnd(hwndServer);
982 if (!pConv)
984 ERR("Got a message (%u) on a not known conversation, dropping request\n", iMsg);
985 goto theError;
987 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
989 ERR("mismatch between C/S windows and converstation\n");
990 goto theError;
992 if (pConv->instance != pInstance || pConv->instance == NULL)
994 ERR("mismatch in instances\n");
995 goto theError;
998 switch (iMsg)
1000 case WM_DDE_INITIATE:
1001 FIXME("WM_DDE_INITIATE message received!\n");
1002 break;
1004 case WM_DDE_REQUEST:
1005 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1006 break;
1008 case WM_DDE_ADVISE:
1009 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1010 break;
1012 case WM_DDE_UNADVISE:
1013 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1014 break;
1016 case WM_DDE_EXECUTE:
1017 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1018 break;
1020 case WM_DDE_POKE:
1021 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1022 break;
1024 case WM_DDE_TERMINATE:
1025 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1026 break;
1028 case WM_DDE_ACK:
1029 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1030 break;
1032 default:
1033 FIXME("Unsupported message %d\n", iMsg);
1036 if (pXAct)
1038 pXAct->lParam = lParam;
1039 if (WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1041 WDML_QueueTransaction(pConv, pXAct);
1043 else
1045 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1048 theError:
1049 LeaveCriticalSection(&WDML_CritSect);
1050 return 0;