4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
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
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38 #include "dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
42 static const WCHAR szServerNameClass[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','N','a','m','e',0};
43 const char WDML_szServerConvClassA[] = "WineDdeServerConvA";
44 const WCHAR WDML_szServerConvClassW[] = {'W','i','n','e','D','d','e','S','e','r','v','e','r','C','o','n','v','W',0};
46 static LRESULT CALLBACK WDML_ServerNameProc(HWND, UINT, WPARAM, LPARAM);
47 static LRESULT CALLBACK WDML_ServerConvProc(HWND, UINT, WPARAM, LPARAM);
49 /******************************************************************************
50 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
53 * idInst [I] Instance identifier
54 * hszTopic [I] Handle to topic name string
55 * hszItem [I] Handle to item name string
61 BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem)
63 WDML_INSTANCE* pInstance = NULL;
64 WDML_LINK* pLink = NULL;
65 HDDEDATA hDdeData = 0;
66 HGLOBAL hItemData = 0;
67 WDML_CONV* pConv = NULL;
71 TRACE("(%d,%p,%p)\n", idInst, hszTopic, hszItem);
73 pInstance = WDML_GetInstance(idInst);
75 if (pInstance == NULL)
78 atom = WDML_MakeAtomFromHsz(hszItem);
79 if (!atom) return FALSE;
81 /* first compute the number of links which will trigger a message */
83 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
85 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
90 if (count >= CADV_LATEACK)
92 FIXME("too high value for count\n");
96 for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next)
98 if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0)
100 hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv,
101 hszTopic, hszItem, 0, --count, 0);
103 if (hDdeData == CBR_BLOCK)
105 /* MS doc is not consistent here */
106 FIXME("CBR_BLOCK returned for ADVREQ\n");
111 if (pLink->transactionType & XTYPF_NODATA)
118 TRACE("with data\n");
120 hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE);
123 pConv = WDML_GetConv(pLink->hConv, TRUE);
127 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
131 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
132 PackDDElParam(WM_DDE_DATA, (UINT_PTR)hItemData, atom)))
134 ERR("post message failed\n");
135 pConv->wStatus &= ~ST_CONNECTED;
136 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
137 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
138 GlobalFree(hItemData);
141 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
148 GlobalDeleteAtom(atom);
153 /******************************************************************************
154 * DdeNameService [USER32.@] {Un}registers service name of DDE server
157 * idInst [I] Instance identifier
158 * hsz1 [I] Handle to service name string
160 * afCmd [I] Service name flags
166 HDDEDATA WINAPI DdeNameService(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT afCmd)
168 WDML_SERVER* pServer;
169 WDML_INSTANCE* pInstance;
171 WNDCLASSEXW wndclass;
173 TRACE("(%d,%p,%p,%x)\n", idInst, hsz1, hsz2, afCmd);
175 /* First check instance
177 pInstance = WDML_GetInstance(idInst);
178 if (pInstance == NULL)
180 TRACE("Instance not found as initialised\n");
181 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
187 /* Illegal, reserved parameter
189 pInstance->lastError = DMLERR_INVALIDPARAMETER;
190 WARN("Reserved parameter no-zero !!\n");
193 if (hsz1 == 0 && !(afCmd & DNS_UNREGISTER))
195 /* don't know if we should check this but it makes sense
196 * why supply REGISTER or filter flags if de-registering all
198 TRACE("General unregister unexpected flags\n");
199 pInstance->lastError = DMLERR_INVALIDPARAMETER;
203 switch (afCmd & (DNS_REGISTER | DNS_UNREGISTER))
206 pServer = WDML_FindServer(pInstance, hsz1, 0);
209 ERR("Trying to register already registered service!\n");
210 pInstance->lastError = DMLERR_DLL_USAGE;
214 TRACE("Adding service name\n");
216 WDML_IncHSZ(pInstance, hsz1);
218 pServer = WDML_AddServer(pInstance, hsz1, 0);
220 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_REGISTER,
221 pServer->atomService, pServer->atomServiceSpec);
223 wndclass.cbSize = sizeof(wndclass);
225 wndclass.lpfnWndProc = WDML_ServerNameProc;
226 wndclass.cbClsExtra = 0;
227 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
228 wndclass.hInstance = 0;
230 wndclass.hCursor = 0;
231 wndclass.hbrBackground = 0;
232 wndclass.lpszMenuName = NULL;
233 wndclass.lpszClassName = szServerNameClass;
234 wndclass.hIconSm = 0;
236 RegisterClassExW(&wndclass);
238 hwndServer = CreateWindowW(szServerNameClass, NULL,
239 WS_POPUP, 0, 0, 0, 0,
242 SetWindowLongPtrW(hwndServer, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
243 SetWindowLongPtrW(hwndServer, GWL_WDML_SERVER, (ULONG_PTR)pServer);
244 TRACE("Created nameServer=%p for instance=%08x\n", hwndServer, idInst);
246 pServer->hwndServer = hwndServer;
252 /* General unregister situation
253 * terminate all server side pending conversations
255 while (pInstance->servers)
256 WDML_RemoveServer(pInstance, pInstance->servers->hszService, 0);
257 pInstance->servers = NULL;
258 TRACE("General de-register - finished\n");
262 WDML_RemoveServer(pInstance, hsz1, 0L);
267 if (afCmd & (DNS_FILTERON | DNS_FILTEROFF))
269 /* Set filter flags on to hold notifications of connection
271 pServer = WDML_FindServer(pInstance, hsz1, 0);
274 /* trying to filter where no service names !!
276 pInstance->lastError = DMLERR_DLL_USAGE;
281 pServer->filterOn = (afCmd & DNS_FILTERON) != 0;
284 return (HDDEDATA)TRUE;
287 /******************************************************************
288 * WDML_CreateServerConv
292 static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient,
293 HWND hwndServerName, HSZ hszApp, HSZ hszTopic)
298 if (pInstance->unicode)
300 WNDCLASSEXW wndclass;
302 wndclass.cbSize = sizeof(wndclass);
304 wndclass.lpfnWndProc = WDML_ServerConvProc;
305 wndclass.cbClsExtra = 0;
306 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
307 wndclass.hInstance = 0;
309 wndclass.hCursor = 0;
310 wndclass.hbrBackground = 0;
311 wndclass.lpszMenuName = NULL;
312 wndclass.lpszClassName = WDML_szServerConvClassW;
313 wndclass.hIconSm = 0;
315 RegisterClassExW(&wndclass);
317 hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0,
318 WS_CHILD, 0, 0, 0, 0,
319 hwndServerName, 0, 0, 0);
323 WNDCLASSEXA wndclass;
325 wndclass.cbSize = sizeof(wndclass);
327 wndclass.lpfnWndProc = WDML_ServerConvProc;
328 wndclass.cbClsExtra = 0;
329 wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR);
330 wndclass.hInstance = 0;
332 wndclass.hCursor = 0;
333 wndclass.hbrBackground = 0;
334 wndclass.lpszMenuName = NULL;
335 wndclass.lpszClassName = WDML_szServerConvClassA;
336 wndclass.hIconSm = 0;
338 RegisterClassExA(&wndclass);
340 hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0,
341 WS_CHILD, 0, 0, 0, 0,
342 hwndServerName, 0, 0, 0);
345 TRACE("Created convServer=%p (nameServer=%p) for instance=%08x unicode=%d\n",
346 hwndServerConv, hwndServerName, pInstance->instanceID, pInstance->unicode);
348 pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic,
349 hwndClient, hwndServerConv);
352 SetWindowLongPtrW(hwndServerConv, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
353 SetWindowLongPtrW(hwndServerConv, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv);
355 /* this should be the only place using SendMessage for WM_DDE_ACK */
356 /* note: sent messages shall not use packing */
357 SendMessageW(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv,
358 MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), 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;
367 DestroyWindow(hwndServerConv);
372 /******************************************************************
373 * WDML_ServerNameProc
377 static LRESULT CALLBACK WDML_ServerNameProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
381 HDDEDATA hDdeData = 0;
382 WDML_INSTANCE* pInstance;
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 if (!pInstance) return 0;
398 TRACE("idInst=%d, threadID=0x%x\n", pInstance->instanceID, GetCurrentThreadId());
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))
410 CONVCONTEXT* pcc = NULL;
414 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
415 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
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 lstrcmpiA(buf, WDML_szClientConvClassA) == 0) ||
424 (GetClassNameW(hwndClient, (LPWSTR)buf, sizeof(buf)/sizeof(WCHAR)) &&
425 lstrcmpiW((LPWSTR)buf, WDML_szClientConvClassW) == 0))
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*)GetWindowLongPtrW(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, (ULONG_PTR)pcc, self);
446 if ((ULONG_PTR)hDdeData)
448 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
452 if (pcc) pConv->wStatus |= ST_ISLOCAL;
453 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
454 hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
459 else if (pInstance->servers)
461 /* pass on to the callback */
462 hDdeData = WDML_InvokeCallback(pInstance, XTYP_WILDCONNECT,
463 0, 0, hszTop, hszApp, 0, (ULONG_PTR)pcc, self);
465 if (hDdeData == CBR_BLOCK)
467 /* MS doc is not consistent here */
468 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
470 else if ((ULONG_PTR)hDdeData != 0)
474 hszp = (HSZPAIR*)DdeAccessData(hDdeData, NULL);
478 for (i = 0; hszp[i].hszSvc && hszp[i].hszTopic; i++)
480 pConv = WDML_CreateServerConv(pInstance, hwndClient, hwndServer,
481 hszp[i].hszSvc, hszp[i].hszTopic);
484 if (pcc) pConv->wStatus |= ST_ISLOCAL;
485 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
486 hszp[i].hszTopic, hszp[i].hszSvc, 0, (ULONG_PTR)pcc, self);
489 DdeUnaccessData(hDdeData);
491 if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData);
499 FIXME("WM_DDE_REQUEST message received!\n");
502 FIXME("WM_DDE_ADVISE message received!\n");
504 case WM_DDE_UNADVISE:
505 FIXME("WM_DDE_UNADVISE message received!\n");
508 FIXME("WM_DDE_EXECUTE message received!\n");
511 FIXME("WM_DDE_POKE message received!\n");
513 case WM_DDE_TERMINATE:
514 FIXME("WM_DDE_TERMINATE message received!\n");
520 return DefWindowProcW(hwndServer, iMsg, wParam, lParam);
523 /******************************************************************
524 * WDML_ServerQueueRequest
528 static WDML_XACT* WDML_ServerQueueRequest(WDML_CONV* pConv, LPARAM lParam)
533 UnpackDDElParam(WM_DDE_REQUEST, lParam, &uiLo, &uiHi);
535 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST,
536 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
537 if (pXAct) pXAct->atom = uiHi;
541 /******************************************************************
542 * WDML_ServerHandleRequest
546 static WDML_QUEUE_STATE WDML_ServerHandleRequest(WDML_CONV* pConv, WDML_XACT* pXAct)
548 HDDEDATA hDdeData = 0;
551 if (!(pConv->instance->CBFflags & CBF_FAIL_REQUESTS))
554 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_REQUEST, pXAct->wFmt, (HCONV)pConv,
555 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
558 switch ((ULONG_PTR)hDdeData)
561 TRACE("No data returned from the Callback\n");
565 case (ULONG_PTR)CBR_BLOCK:
566 return WDML_QS_BLOCK;
570 HGLOBAL hMem = WDML_DataHandle2Global(hDdeData, TRUE, FALSE, FALSE, FALSE);
571 if (!PostMessageW(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer,
572 ReuseDDElParam(pXAct->lParam, WM_DDE_REQUEST, WM_DDE_DATA,
573 (UINT_PTR)hMem, (UINT_PTR)pXAct->atom)))
575 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
576 DdeFreeDataHandle(hDdeData);
584 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_REQUEST);
586 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
588 return WDML_QS_HANDLED;
591 /******************************************************************
592 * WDML_ServerQueueAdvise
596 static WDML_XACT* WDML_ServerQueueAdvise(WDML_CONV* pConv, LPARAM lParam)
601 /* XTYP_ADVSTART transaction:
602 establish link and save link info to InstanceInfoTable */
604 if (!UnpackDDElParam(WM_DDE_ADVISE, lParam, &uiLo, &uiHi))
607 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE,
608 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
611 pXAct->hMem = (HGLOBAL)uiLo;
617 /******************************************************************
618 * WDML_ServerHandleAdvise
622 static WDML_QUEUE_STATE WDML_ServerHandleAdvise(WDML_CONV* pConv, WDML_XACT* pXAct)
626 DDEADVISE* pDdeAdvise;
627 HDDEDATA hDdeData = 0;
630 pDdeAdvise = GlobalLock(pXAct->hMem);
631 uType = XTYP_ADVSTART |
632 (pDdeAdvise->fDeferUpd ? XTYPF_NODATA : 0) |
633 (pDdeAdvise->fAckReq ? XTYPF_ACKREQ : 0);
635 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
637 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_ADVSTART, pDdeAdvise->cfFormat,
638 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
641 switch ((ULONG_PTR)hDdeData)
644 TRACE("No data returned from the Callback\n");
648 case (ULONG_PTR)CBR_BLOCK:
649 return WDML_QS_BLOCK;
652 /* billx: first to see if the link is already created. */
653 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
654 pXAct->hszItem, TRUE, pDdeAdvise->cfFormat);
658 /* we found a link, and only need to modify it in case it changes */
659 pLink->transactionType = uType;
663 TRACE("Adding Link with hConv %p\n", pConv);
664 WDML_AddLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
665 uType, pXAct->hszItem, pDdeAdvise->cfFormat);
670 GlobalUnlock(pXAct->hMem);
673 GlobalFree(pXAct->hMem);
677 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, fAck, pXAct->atom, pXAct->lParam, WM_DDE_ADVISE);
679 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
681 return WDML_QS_HANDLED;
684 /******************************************************************
685 * WDML_ServerQueueUnadvise
689 static WDML_XACT* WDML_ServerQueueUnadvise(WDML_CONV* pConv, LPARAM lParam)
694 UnpackDDElParam(WM_DDE_UNADVISE, lParam, &uiLo, &uiHi);
696 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE,
697 uiLo, WDML_MakeHszFromAtom(pConv->instance, uiHi));
698 if (pXAct) pXAct->atom = uiHi;
702 /******************************************************************
703 * WDML_ServerHandleUnadvise
707 static WDML_QUEUE_STATE WDML_ServerHandleUnadvise(WDML_CONV* pConv, WDML_XACT* pXAct)
711 if (pXAct->hszItem == NULL || pXAct->wFmt == 0)
713 ERR("Unsupported yet options (null item or clipboard format)\n");
714 return WDML_QS_ERROR;
717 pLink = WDML_FindLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
718 pXAct->hszItem, TRUE, pXAct->wFmt);
721 ERR("Couln'd find link for %p, dropping request\n", pXAct->hszItem);
722 FreeDDElParam(WM_DDE_UNADVISE, pXAct->lParam);
723 return WDML_QS_ERROR;
726 if (!(pConv->instance->CBFflags & CBF_FAIL_ADVISES))
728 WDML_InvokeCallback(pConv->instance, XTYP_ADVSTOP, pXAct->wFmt, (HCONV)pConv,
729 pConv->hszTopic, pXAct->hszItem, 0, 0, 0);
732 WDML_RemoveLink(pConv->instance, (HCONV)pConv, WDML_SERVER_SIDE,
733 pXAct->hszItem, pXAct->wFmt);
736 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, FALSE, TRUE, pXAct->atom,
737 pXAct->lParam, WM_DDE_UNADVISE);
739 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
741 return WDML_QS_HANDLED;
744 /******************************************************************
749 static WDML_XACT* WDML_ServerQueueExecute(WDML_CONV* pConv, LPARAM lParam)
753 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_EXECUTE, 0, 0);
756 pXAct->hMem = (HGLOBAL)lParam;
761 static BOOL data_looks_unicode( const WCHAR *data, DWORD size )
765 if (size % sizeof(WCHAR)) return FALSE;
766 for (i = 0; i < size / sizeof(WCHAR); i++) if (data[i] > 255) return FALSE;
770 /* convert data to Unicode, unless it looks like it's already Unicode */
771 static HDDEDATA map_A_to_W( DWORD instance, void *ptr, DWORD size )
777 if (!data_looks_unicode( ptr, size ))
779 if ((end = memchr( ptr, 0, size ))) size = end + 1 - (const char *)ptr;
780 len = MultiByteToWideChar( CP_ACP, 0, ptr, size, NULL, 0 );
781 ret = DdeCreateDataHandle( instance, NULL, len * sizeof(WCHAR), 0, 0, CF_TEXT, 0);
782 MultiByteToWideChar( CP_ACP, 0, ptr, size, (WCHAR *)DdeAccessData(ret, NULL), len );
784 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
789 /* convert data to ASCII, unless it looks like it's not in Unicode format */
790 static HDDEDATA map_W_to_A( DWORD instance, void *ptr, DWORD size )
796 if (data_looks_unicode( ptr, size ))
798 size /= sizeof(WCHAR);
799 if ((end = memchrW( ptr, 0, size ))) size = end + 1 - (const WCHAR *)ptr;
800 len = WideCharToMultiByte( CP_ACP, 0, ptr, size, NULL, 0, NULL, NULL );
801 ret = DdeCreateDataHandle( instance, NULL, len, 0, 0, CF_TEXT, 0);
802 WideCharToMultiByte( CP_ACP, 0, ptr, size, (char *)DdeAccessData(ret, NULL), len, NULL, NULL );
804 else ret = DdeCreateDataHandle( instance, ptr, size, 0, 0, CF_TEXT, 0 );
809 /******************************************************************
810 * WDML_ServerHandleExecute
814 static WDML_QUEUE_STATE WDML_ServerHandleExecute(WDML_CONV* pConv, WDML_XACT* pXAct)
816 HDDEDATA hDdeData = DDE_FNOTPROCESSED;
817 BOOL fAck = FALSE, fBusy = FALSE;
819 if (!(pConv->instance->CBFflags & CBF_FAIL_EXECUTES))
821 LPVOID ptr = GlobalLock(pXAct->hMem);
822 DWORD size = GlobalSize(pXAct->hMem);
826 if (pConv->instance->unicode) /* Unicode server, try to map A->W */
827 hDdeData = map_A_to_W( pConv->instance->instanceID, ptr, size );
828 else if (!IsWindowUnicode( pConv->hwndClient )) /* ASCII server and client, try to map W->A */
829 hDdeData = map_W_to_A( pConv->instance->instanceID, ptr, size );
831 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, ptr, size, 0, 0, CF_TEXT, 0);
832 GlobalUnlock(pXAct->hMem);
834 hDdeData = WDML_InvokeCallback(pConv->instance, XTYP_EXECUTE, 0, (HCONV)pConv,
835 pConv->hszTopic, 0, hDdeData, 0L, 0L);
838 switch ((ULONG_PTR)hDdeData)
840 case (ULONG_PTR)CBR_BLOCK:
841 return WDML_QS_BLOCK;
850 FIXME("Unsupported returned value %p\n", hDdeData);
852 case DDE_FNOTPROCESSED:
855 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, (UINT_PTR)pXAct->hMem, 0, 0);
857 return WDML_QS_HANDLED;
860 /******************************************************************
861 * WDML_ServerQueuePoke
865 static WDML_XACT* WDML_ServerQueuePoke(WDML_CONV* pConv, LPARAM lParam)
870 UnpackDDElParam(WM_DDE_POKE, lParam, &uiLo, &uiHi);
872 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE,
873 0, WDML_MakeHszFromAtom(pConv->instance, uiHi));
877 pXAct->hMem = (HGLOBAL)uiLo;
882 /******************************************************************
883 * WDML_ServerHandlePoke
887 static WDML_QUEUE_STATE WDML_ServerHandlePoke(WDML_CONV* pConv, WDML_XACT* pXAct)
891 BOOL fBusy = FALSE, fAck = FALSE;
893 pDdePoke = GlobalLock(pXAct->hMem);
896 return WDML_QS_ERROR;
899 if (!(pConv->instance->CBFflags & CBF_FAIL_POKES))
901 hDdeData = DdeCreateDataHandle(pConv->instance->instanceID, pDdePoke->Value,
902 GlobalSize(pXAct->hMem) - FIELD_OFFSET(DDEPOKE, Value),
903 0, 0, pDdePoke->cfFormat, 0);
906 HDDEDATA hDdeDataOut;
908 hDdeDataOut = WDML_InvokeCallback(pConv->instance, XTYP_POKE, pDdePoke->cfFormat,
909 (HCONV)pConv, pConv->hszTopic, pXAct->hszItem,
911 switch ((ULONG_PTR)hDdeDataOut)
920 FIXME("Unsupported returned value %p\n", hDdeDataOut);
922 case DDE_FNOTPROCESSED:
925 DdeFreeDataHandle(hDdeData);
928 GlobalUnlock(pXAct->hMem);
932 GlobalFree(pXAct->hMem);
934 WDML_PostAck(pConv, WDML_SERVER_SIDE, 0, fBusy, fAck, pXAct->atom, pXAct->lParam, WM_DDE_POKE);
936 WDML_DecHSZ(pConv->instance, pXAct->hszItem);
938 return WDML_QS_HANDLED;
941 /******************************************************************
942 * WDML_ServerQueueTerminate
946 static WDML_XACT* WDML_ServerQueueTerminate(WDML_CONV* pConv, LPARAM lParam)
950 pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_TERMINATE, 0, 0);
954 /******************************************************************
955 * WDML_ServerHandleTerminate
959 static WDML_QUEUE_STATE WDML_ServerHandleTerminate(WDML_CONV* pConv, WDML_XACT* pXAct)
961 /* billx: two things to remove: the conv, and associated links.
962 * Respond with another WM_DDE_TERMINATE iMsg.
964 if (!(pConv->instance->CBFflags & CBF_SKIP_DISCONNECTS))
966 WDML_InvokeCallback(pConv->instance, XTYP_DISCONNECT, 0, (HCONV)pConv, 0, 0,
967 0, 0, (pConv->wStatus & ST_ISSELF) ? 1 : 0);
969 PostMessageW(pConv->hwndClient, WM_DDE_TERMINATE, (WPARAM)pConv->hwndServer, 0);
970 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
972 return WDML_QS_HANDLED;
975 /******************************************************************
980 WDML_QUEUE_STATE WDML_ServerHandle(WDML_CONV* pConv, WDML_XACT* pXAct)
982 WDML_QUEUE_STATE qs = WDML_QS_ERROR;
984 switch (pXAct->ddeMsg)
986 case WM_DDE_INITIATE:
987 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
990 qs = WDML_ServerHandleRequest(pConv, pXAct);
994 qs = WDML_ServerHandleAdvise(pConv, pXAct);
997 case WM_DDE_UNADVISE:
998 qs = WDML_ServerHandleUnadvise(pConv, pXAct);
1001 case WM_DDE_EXECUTE:
1002 qs = WDML_ServerHandleExecute(pConv, pXAct);
1006 qs = WDML_ServerHandlePoke(pConv, pXAct);
1009 case WM_DDE_TERMINATE:
1010 qs = WDML_ServerHandleTerminate(pConv, pXAct);
1014 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1018 FIXME("Unsupported message %d\n", pXAct->ddeMsg);
1023 /******************************************************************
1024 * WDML_ServerConvProc
1028 static LRESULT CALLBACK WDML_ServerConvProc(HWND hwndServer, UINT iMsg, WPARAM wParam, LPARAM lParam)
1030 WDML_INSTANCE* pInstance;
1032 WDML_XACT* pXAct = NULL;
1034 TRACE("%p %04x %08lx %08lx\n", hwndServer, iMsg, wParam, lParam);
1036 if (iMsg == WM_DESTROY)
1038 pConv = WDML_GetConvFromWnd(hwndServer);
1039 if (pConv && !(pConv->wStatus & ST_TERMINATED))
1041 WDML_ServerHandleTerminate(pConv, NULL);
1044 if (iMsg < WM_DDE_FIRST || iMsg > WM_DDE_LAST)
1046 return IsWindowUnicode(hwndServer) ? DefWindowProcW(hwndServer, iMsg, wParam, lParam) :
1047 DefWindowProcA(hwndServer, iMsg, wParam, lParam);
1050 pInstance = WDML_GetInstanceFromWnd(hwndServer);
1051 pConv = WDML_GetConvFromWnd(hwndServer);
1055 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg);
1058 if (pConv->hwndClient != WIN_GetFullHandle( (HWND)wParam ) || pConv->hwndServer != hwndServer)
1060 ERR("mismatch between C/S windows and conversation\n");
1063 if (pConv->instance != pInstance || pConv->instance == NULL)
1065 ERR("mismatch in instances\n");
1071 case WM_DDE_INITIATE:
1072 FIXME("WM_DDE_INITIATE message received!\n");
1075 case WM_DDE_REQUEST:
1076 pXAct = WDML_ServerQueueRequest(pConv, lParam);
1080 pXAct = WDML_ServerQueueAdvise(pConv, lParam);
1083 case WM_DDE_UNADVISE:
1084 pXAct = WDML_ServerQueueUnadvise(pConv, lParam);
1087 case WM_DDE_EXECUTE:
1088 pXAct = WDML_ServerQueueExecute(pConv, lParam);
1092 pXAct = WDML_ServerQueuePoke(pConv, lParam);
1095 case WM_DDE_TERMINATE:
1096 pXAct = WDML_ServerQueueTerminate(pConv, lParam);
1100 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1104 FIXME("Unsupported message %x\n", iMsg);
1110 pXAct->lParam = lParam;
1112 if ((pConv->wStatus & ST_BLOCKED) || WDML_ServerHandle(pConv, pXAct) == WDML_QS_BLOCK)
1114 TRACE("Transactions are blocked, add to the queue and exit\n");
1115 WDML_QueueTransaction(pConv, pXAct);
1119 WDML_FreeTransaction(pInstance, pXAct, TRUE);
1123 pConv->instance->lastError = DMLERR_MEMORY_ERROR;