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
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.
43 * idInst [I] Instance identifier
44 * hszTopic [I] Handle to topic name string
45 * hszItem [I] Handle to item name string
51 BOOL WINAPI
DdePostAdvise(DWORD idInst
, HSZ hszTopic
, HSZ hszItem
)
53 WDML_INSTANCE
* pInstance
;
61 TRACE("(%ld,%p,%p)\n", idInst
, hszTopic
, hszItem
);
63 pInstance
= WDML_GetInstance(idInst
);
65 if (pInstance
== NULL
)
68 atom
= WDML_MakeAtomFromHsz(hszItem
);
69 if (!atom
) return FALSE
;
71 /* first compute the number of links which will trigger a message */
73 for (pLink
= pInstance
->links
[WDML_SERVER_SIDE
]; pLink
!= NULL
; pLink
= pLink
->next
)
75 if (DdeCmpStringHandles(hszItem
, pLink
->hszItem
) == 0)
80 if (count
>= CADV_LATEACK
)
82 FIXME("too high value for count\n");
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");
101 if (pLink
->transactionType
& XTYPF_NODATA
)
108 TRACE("with data\n");
110 hItemData
= WDML_DataHandle2Global(hDdeData
, FALSE
, FALSE
, FALSE
, FALSE
);
113 pConv
= WDML_GetConv(pLink
->hConv
, TRUE
);
117 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
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
);
131 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
138 GlobalDeleteAtom(atom
);
143 /******************************************************************************
144 * DdeNameService [USER32.@] {Un}registers service name of DDE server
147 * idInst [I] Instance identifier
148 * hsz1 [I] Handle to service name string
150 * afCmd [I] Service name flags
156 HDDEDATA WINAPI
DdeNameService(DWORD idInst
, HSZ hsz1
, HSZ hsz2
, UINT afCmd
)
158 WDML_SERVER
* pServer
;
159 WDML_INSTANCE
* pInstance
;
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 */
177 /* Illegal, reserved parameter
179 pInstance
->lastError
= DMLERR_INVALIDPARAMETER
;
180 WARN("Reserved parameter no-zero !!\n");
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
;
193 switch (afCmd
& (DNS_REGISTER
| DNS_UNREGISTER
))
196 pServer
= WDML_FindServer(pInstance
, hsz1
, 0);
199 ERR("Trying to register already registered service!\n");
200 pInstance
->lastError
= DMLERR_DLL_USAGE
;
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
);
215 wndclass
.lpfnWndProc
= WDML_ServerNameProc
;
216 wndclass
.cbClsExtra
= 0;
217 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
218 wndclass
.hInstance
= 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
;
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");
249 WDML_RemoveServer(pInstance
, hsz1
, 0L);
254 if (afCmd
& (DNS_FILTERON
| DNS_FILTEROFF
))
256 /* Set filter flags on to hold notifications of connection
258 pServer
= WDML_FindServer(pInstance
, hsz1
, 0);
261 /* trying to filter where no service names !!
263 pInstance
->lastError
= DMLERR_DLL_USAGE
;
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
)
285 if (pInstance
->unicode
)
287 WNDCLASSEXW wndclass
;
289 wndclass
.cbSize
= sizeof(wndclass
);
291 wndclass
.lpfnWndProc
= WDML_ServerConvProc
;
292 wndclass
.cbClsExtra
= 0;
293 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
294 wndclass
.hInstance
= 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);
310 WNDCLASSEXA wndclass
;
312 wndclass
.cbSize
= sizeof(wndclass
);
314 wndclass
.lpfnWndProc
= WDML_ServerConvProc
;
315 wndclass
.cbClsExtra
= 0;
316 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
317 wndclass
.hInstance
= 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
);
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
;
354 NtUserDestroyWindow(hwndServerConv
);
359 /******************************************************************
360 * WDML_ServerNameProc
364 static LRESULT CALLBACK
WDML_ServerNameProc(HWND hwndServer
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
369 WDML_INSTANCE
* pInstance
;
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
))
397 CONVCONTEXT
* pcc
= NULL
;
401 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
402 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
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))
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
,
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)
461 hszp
= (HSZPAIR
*)DdeAccessData(hDdeData
, NULL
);
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
);
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
);
486 FIXME("WM_DDE_REQUEST message received!\n");
489 FIXME("WM_DDE_ADVISE message received!\n");
491 case WM_DDE_UNADVISE
:
492 FIXME("WM_DDE_UNADVISE message received!\n");
495 FIXME("WM_DDE_EXECUTE message received!\n");
498 FIXME("WM_DDE_POKE message received!\n");
500 case WM_DDE_TERMINATE
:
501 FIXME("WM_DDE_TERMINATE message received!\n");
507 return DefWindowProcW(hwndServer
, iMsg
, wParam
, lParam
);
510 /******************************************************************
511 * WDML_ServerQueueRequest
515 static WDML_XACT
* WDML_ServerQueueRequest(WDML_CONV
* pConv
, LPARAM lParam
)
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
;
528 /******************************************************************
529 * WDML_ServerHandleRequest
533 static WDML_QUEUE_STATE
WDML_ServerHandleRequest(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
535 HDDEDATA hDdeData
= 0;
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
)
548 TRACE("No data returned from the Callback\n");
552 case (ULONG_PTR
)CBR_BLOCK
:
553 return WDML_QS_BLOCK
;
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
);
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
)
588 /* XTYP_ADVSTART transaction:
589 establish link and save link info to InstanceInfoTable */
591 if (!UnpackDDElParam(WM_DDE_ADVISE
, lParam
, &uiLo
, &uiHi
))
594 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_ADVISE
,
595 0, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
598 pXAct
->hMem
= (HGLOBAL
)uiLo
;
604 /******************************************************************
605 * WDML_ServerHandleAdvise
609 static WDML_QUEUE_STATE
WDML_ServerHandleAdvise(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
613 DDEADVISE
* pDdeAdvise
;
614 HDDEDATA hDdeData
= 0;
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
)
631 TRACE("No data returned from the Callback\n");
635 case (ULONG_PTR
)CBR_BLOCK
:
636 return WDML_QS_BLOCK
;
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
);
645 /* we found a link, and only need to modify it in case it changes */
646 pLink
->transactionType
= uType
;
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
);
657 GlobalUnlock(pXAct
->hMem
);
660 GlobalFree(pXAct
->hMem
);
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
)
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
;
689 /******************************************************************
690 * WDML_ServerHandleUnadvise
694 static WDML_QUEUE_STATE
WDML_ServerHandleUnadvise(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
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
);
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
);
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 /******************************************************************
736 static WDML_XACT
* WDML_ServerQueueExecute(WDML_CONV
* pConv
, LPARAM lParam
)
740 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_EXECUTE
, 0, 0);
743 pXAct
->hMem
= (HGLOBAL
)lParam
;
748 static BOOL
data_looks_unicode( const WCHAR
*data
, DWORD size
)
752 if (size
% sizeof(WCHAR
)) return FALSE
;
753 for (i
= 0; i
< size
/ sizeof(WCHAR
); i
++) if (data
[i
] > 255) return FALSE
;
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
)
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 );
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
)
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 );
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
);
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
);
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
;
837 FIXME("Unsupported returned value %p\n", hDdeData
);
839 case DDE_FNOTPROCESSED
:
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
)
857 UnpackDDElParam(WM_DDE_POKE
, lParam
, &uiLo
, &uiHi
);
859 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_POKE
,
860 0, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
864 pXAct
->hMem
= (HGLOBAL
)uiLo
;
869 /******************************************************************
870 * WDML_ServerHandlePoke
874 static WDML_QUEUE_STATE
WDML_ServerHandlePoke(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
878 BOOL fBusy
= FALSE
, fAck
= FALSE
;
880 pDdePoke
= GlobalLock(pXAct
->hMem
);
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);
893 HDDEDATA hDdeDataOut
;
895 hDdeDataOut
= WDML_InvokeCallback(pConv
->instance
, XTYP_POKE
, pDdePoke
->cfFormat
,
896 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
,
898 switch ((ULONG_PTR
)hDdeDataOut
)
907 FIXME("Unsupported returned value %p\n", hDdeDataOut
);
909 case DDE_FNOTPROCESSED
:
912 DdeFreeDataHandle(hDdeData
);
915 GlobalUnlock(pXAct
->hMem
);
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
)
937 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_TERMINATE
, 0, 0);
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 /******************************************************************
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");
977 qs
= WDML_ServerHandleRequest(pConv
, pXAct
);
981 qs
= WDML_ServerHandleAdvise(pConv
, pXAct
);
984 case WM_DDE_UNADVISE
:
985 qs
= WDML_ServerHandleUnadvise(pConv
, pXAct
);
989 qs
= WDML_ServerHandleExecute(pConv
, pXAct
);
993 qs
= WDML_ServerHandlePoke(pConv
, pXAct
);
996 case WM_DDE_TERMINATE
:
997 qs
= WDML_ServerHandleTerminate(pConv
, pXAct
);
1001 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1005 FIXME("Unsupported message %d\n", pXAct
->ddeMsg
);
1010 /******************************************************************
1011 * WDML_ServerConvProc
1015 static LRESULT CALLBACK
WDML_ServerConvProc(HWND hwndServer
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
1017 WDML_INSTANCE
* pInstance
;
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
);
1042 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg
);
1045 if (pConv
->hwndClient
!= WIN_GetFullHandle( (HWND
)wParam
) || pConv
->hwndServer
!= hwndServer
)
1047 ERR("mismatch between C/S windows and conversation\n");
1050 if (pConv
->instance
!= pInstance
|| pConv
->instance
== NULL
)
1052 ERR("mismatch in instances\n");
1058 case WM_DDE_INITIATE
:
1059 FIXME("WM_DDE_INITIATE message received!\n");
1062 case WM_DDE_REQUEST
:
1063 pXAct
= WDML_ServerQueueRequest(pConv
, lParam
);
1067 pXAct
= WDML_ServerQueueAdvise(pConv
, lParam
);
1070 case WM_DDE_UNADVISE
:
1071 pXAct
= WDML_ServerQueueUnadvise(pConv
, lParam
);
1074 case WM_DDE_EXECUTE
:
1075 pXAct
= WDML_ServerQueueExecute(pConv
, lParam
);
1079 pXAct
= WDML_ServerQueuePoke(pConv
, lParam
);
1082 case WM_DDE_TERMINATE
:
1083 pXAct
= WDML_ServerQueueTerminate(pConv
, lParam
);
1087 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1091 FIXME("Unsupported message %x\n", iMsg
);
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
);
1106 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1110 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;