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
37 #include "wine/debug.h"
38 #include "dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
42 const char WDML_szServerConvClassA
[] = "WineDdeServerConvA";
43 const WCHAR WDML_szServerConvClassW
[] = L
"WineDdeServerConvW";
45 static LRESULT CALLBACK
WDML_ServerNameProc(HWND
, UINT
, WPARAM
, LPARAM
);
46 static LRESULT CALLBACK
WDML_ServerConvProc(HWND
, UINT
, WPARAM
, LPARAM
);
48 /******************************************************************************
49 * DdePostAdvise [USER32.@] Send transaction to DDE callback function.
52 * idInst [I] Instance identifier
53 * hszTopic [I] Handle to topic name string
54 * hszItem [I] Handle to item name string
60 BOOL WINAPI
DdePostAdvise(DWORD idInst
, HSZ hszTopic
, HSZ hszItem
)
62 WDML_INSTANCE
* pInstance
;
70 TRACE("(%d,%p,%p)\n", idInst
, hszTopic
, hszItem
);
72 pInstance
= WDML_GetInstance(idInst
);
74 if (pInstance
== NULL
)
77 atom
= WDML_MakeAtomFromHsz(hszItem
);
78 if (!atom
) return FALSE
;
80 /* first compute the number of links which will trigger a message */
82 for (pLink
= pInstance
->links
[WDML_SERVER_SIDE
]; pLink
!= NULL
; pLink
= pLink
->next
)
84 if (DdeCmpStringHandles(hszItem
, pLink
->hszItem
) == 0)
89 if (count
>= CADV_LATEACK
)
91 FIXME("too high value for count\n");
95 for (pLink
= pInstance
->links
[WDML_SERVER_SIDE
]; pLink
!= NULL
; pLink
= pLink
->next
)
97 if (DdeCmpStringHandles(hszItem
, pLink
->hszItem
) == 0)
99 hDdeData
= WDML_InvokeCallback(pInstance
, XTYP_ADVREQ
, pLink
->uFmt
, pLink
->hConv
,
100 hszTopic
, hszItem
, 0, --count
, 0);
102 if (hDdeData
== CBR_BLOCK
)
104 /* MS doc is not consistent here */
105 FIXME("CBR_BLOCK returned for ADVREQ\n");
110 if (pLink
->transactionType
& XTYPF_NODATA
)
117 TRACE("with data\n");
119 hItemData
= WDML_DataHandle2Global(hDdeData
, FALSE
, FALSE
, FALSE
, FALSE
);
122 pConv
= WDML_GetConv(pLink
->hConv
, TRUE
);
126 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
130 if (!PostMessageW(pConv
->hwndClient
, WM_DDE_DATA
, (WPARAM
)pConv
->hwndServer
,
131 PackDDElParam(WM_DDE_DATA
, (UINT_PTR
)hItemData
, atom
)))
133 ERR("post message failed\n");
134 pConv
->wStatus
&= ~ST_CONNECTED
;
135 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
136 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
137 GlobalFree(hItemData
);
140 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
147 GlobalDeleteAtom(atom
);
152 /******************************************************************************
153 * DdeNameService [USER32.@] {Un}registers service name of DDE server
156 * idInst [I] Instance identifier
157 * hsz1 [I] Handle to service name string
159 * afCmd [I] Service name flags
165 HDDEDATA WINAPI
DdeNameService(DWORD idInst
, HSZ hsz1
, HSZ hsz2
, UINT afCmd
)
167 WDML_SERVER
* pServer
;
168 WDML_INSTANCE
* pInstance
;
170 WNDCLASSEXW wndclass
;
172 TRACE("(%d,%p,%p,%x)\n", idInst
, hsz1
, hsz2
, afCmd
);
174 /* First check instance
176 pInstance
= WDML_GetInstance(idInst
);
177 if (pInstance
== NULL
)
179 TRACE("Instance not found as initialised\n");
180 /* Nothing has been initialised - exit now ! can return TRUE since effect is the same */
186 /* Illegal, reserved parameter
188 pInstance
->lastError
= DMLERR_INVALIDPARAMETER
;
189 WARN("Reserved parameter no-zero !!\n");
192 if (hsz1
== 0 && !(afCmd
& DNS_UNREGISTER
))
194 /* don't know if we should check this but it makes sense
195 * why supply REGISTER or filter flags if de-registering all
197 TRACE("General unregister unexpected flags\n");
198 pInstance
->lastError
= DMLERR_INVALIDPARAMETER
;
202 switch (afCmd
& (DNS_REGISTER
| DNS_UNREGISTER
))
205 pServer
= WDML_FindServer(pInstance
, hsz1
, 0);
208 ERR("Trying to register already registered service!\n");
209 pInstance
->lastError
= DMLERR_DLL_USAGE
;
213 TRACE("Adding service name\n");
215 WDML_IncHSZ(pInstance
, hsz1
);
217 pServer
= WDML_AddServer(pInstance
, hsz1
, 0);
219 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_REGISTER
,
220 pServer
->atomService
, pServer
->atomServiceSpec
);
222 wndclass
.cbSize
= sizeof(wndclass
);
224 wndclass
.lpfnWndProc
= WDML_ServerNameProc
;
225 wndclass
.cbClsExtra
= 0;
226 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
227 wndclass
.hInstance
= 0;
229 wndclass
.hCursor
= 0;
230 wndclass
.hbrBackground
= 0;
231 wndclass
.lpszMenuName
= NULL
;
232 wndclass
.lpszClassName
= L
"WineDdeServerName";
233 wndclass
.hIconSm
= 0;
235 RegisterClassExW(&wndclass
);
237 hwndServer
= CreateWindowW(L
"WineDdeServerName", NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
238 SetWindowLongPtrW(hwndServer
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
239 SetWindowLongPtrW(hwndServer
, GWL_WDML_SERVER
, (ULONG_PTR
)pServer
);
240 TRACE("Created nameServer=%p for instance=%08x\n", hwndServer
, idInst
);
242 pServer
->hwndServer
= hwndServer
;
248 /* General unregister situation
249 * terminate all server side pending conversations
251 while (pInstance
->servers
)
252 WDML_RemoveServer(pInstance
, pInstance
->servers
->hszService
, 0);
253 pInstance
->servers
= NULL
;
254 TRACE("General de-register - finished\n");
258 WDML_RemoveServer(pInstance
, hsz1
, 0L);
263 if (afCmd
& (DNS_FILTERON
| DNS_FILTEROFF
))
265 /* Set filter flags on to hold notifications of connection
267 pServer
= WDML_FindServer(pInstance
, hsz1
, 0);
270 /* trying to filter where no service names !!
272 pInstance
->lastError
= DMLERR_DLL_USAGE
;
277 pServer
->filterOn
= (afCmd
& DNS_FILTERON
) != 0;
280 return (HDDEDATA
)TRUE
;
283 /******************************************************************
284 * WDML_CreateServerConv
288 static WDML_CONV
* WDML_CreateServerConv(WDML_INSTANCE
* pInstance
, HWND hwndClient
,
289 HWND hwndServerName
, HSZ hszApp
, HSZ hszTopic
)
294 if (pInstance
->unicode
)
296 WNDCLASSEXW wndclass
;
298 wndclass
.cbSize
= sizeof(wndclass
);
300 wndclass
.lpfnWndProc
= WDML_ServerConvProc
;
301 wndclass
.cbClsExtra
= 0;
302 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
303 wndclass
.hInstance
= 0;
305 wndclass
.hCursor
= 0;
306 wndclass
.hbrBackground
= 0;
307 wndclass
.lpszMenuName
= NULL
;
308 wndclass
.lpszClassName
= WDML_szServerConvClassW
;
309 wndclass
.hIconSm
= 0;
311 RegisterClassExW(&wndclass
);
313 hwndServerConv
= CreateWindowW(WDML_szServerConvClassW
, 0,
314 WS_CHILD
, 0, 0, 0, 0,
315 hwndServerName
, 0, 0, 0);
319 WNDCLASSEXA wndclass
;
321 wndclass
.cbSize
= sizeof(wndclass
);
323 wndclass
.lpfnWndProc
= WDML_ServerConvProc
;
324 wndclass
.cbClsExtra
= 0;
325 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
326 wndclass
.hInstance
= 0;
328 wndclass
.hCursor
= 0;
329 wndclass
.hbrBackground
= 0;
330 wndclass
.lpszMenuName
= NULL
;
331 wndclass
.lpszClassName
= WDML_szServerConvClassA
;
332 wndclass
.hIconSm
= 0;
334 RegisterClassExA(&wndclass
);
336 hwndServerConv
= CreateWindowA(WDML_szServerConvClassA
, 0,
337 WS_CHILD
, 0, 0, 0, 0,
338 hwndServerName
, 0, 0, 0);
341 TRACE("Created convServer=%p (nameServer=%p) for instance=%08x unicode=%d\n",
342 hwndServerConv
, hwndServerName
, pInstance
->instanceID
, pInstance
->unicode
);
344 pConv
= WDML_AddConv(pInstance
, WDML_SERVER_SIDE
, hszApp
, hszTopic
,
345 hwndClient
, hwndServerConv
);
348 SetWindowLongPtrW(hwndServerConv
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
349 SetWindowLongPtrW(hwndServerConv
, GWL_WDML_CONVERSATION
, (ULONG_PTR
)pConv
);
351 /* this should be the only place using SendMessage for WM_DDE_ACK */
352 /* note: sent messages shall not use packing */
353 SendMessageW(hwndClient
, WM_DDE_ACK
, (WPARAM
)hwndServerConv
,
354 MAKELPARAM(WDML_MakeAtomFromHsz(hszApp
), WDML_MakeAtomFromHsz(hszTopic
)));
355 /* we assume we're connected since we've sent an answer...
356 * I'm not sure what we can do... it doesn't look like the return value
357 * of SendMessage is used... sigh...
359 pConv
->wStatus
|= ST_CONNECTED
;
363 DestroyWindow(hwndServerConv
);
368 /******************************************************************
369 * WDML_ServerNameProc
373 static LRESULT CALLBACK
WDML_ServerNameProc(HWND hwndServer
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
378 WDML_INSTANCE
* pInstance
;
383 case WM_DDE_INITIATE
:
385 /* wParam -- sending window handle
386 LOWORD(lParam) -- application atom
387 HIWORD(lParam) -- topic atom */
389 TRACE("WM_DDE_INITIATE message received!\n");
390 hwndClient
= (HWND
)wParam
;
392 pInstance
= WDML_GetInstanceFromWnd(hwndServer
);
393 if (!pInstance
) return 0;
394 TRACE("idInst=%d, threadID=0x%x\n", pInstance
->instanceID
, GetCurrentThreadId());
396 /* don't free DDEParams, since this is a broadcast */
397 UnpackDDElParam(WM_DDE_INITIATE
, lParam
, &uiLo
, &uiHi
);
399 hszApp
= WDML_MakeHszFromAtom(pInstance
, uiLo
);
400 hszTop
= WDML_MakeHszFromAtom(pInstance
, uiHi
);
402 if (!(pInstance
->CBFflags
& CBF_FAIL_CONNECTIONS
))
406 CONVCONTEXT
* pcc
= NULL
;
410 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
411 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
415 /* FIXME: so far, we don't grab distant convcontext, so only check if remote is
416 * handled under DDEML, and if so build a default context
418 if ((GetClassNameA(hwndClient
, buf
, sizeof(buf
)) &&
419 lstrcmpiA(buf
, WDML_szClientConvClassA
) == 0) ||
420 (GetClassNameW(hwndClient
, (LPWSTR
)buf
, sizeof(buf
)/sizeof(WCHAR
)) &&
421 lstrcmpiW((LPWSTR
)buf
, WDML_szClientConvClassW
) == 0))
424 memset(pcc
, 0, sizeof(*pcc
));
425 pcc
->cb
= sizeof(*pcc
);
426 pcc
->iCodePage
= IsWindowUnicode(hwndClient
) ? CP_WINUNICODE
: CP_WINANSI
;
428 if ((pInstance
->CBFflags
& CBF_FAIL_SELFCONNECTIONS
) && self
)
430 TRACE("Don't do self connection as requested\n");
432 else if (hszApp
&& hszTop
)
434 WDML_SERVER
* pServer
= (WDML_SERVER
*)GetWindowLongPtrW(hwndServer
, GWL_WDML_SERVER
);
436 /* check filters for name service */
437 if (!pServer
->filterOn
|| DdeCmpStringHandles(pServer
->hszService
, hszApp
) == 0)
439 /* pass on to the callback */
440 hDdeData
= WDML_InvokeCallback(pInstance
, XTYP_CONNECT
,
441 0, 0, hszTop
, hszApp
, 0, (ULONG_PTR
)pcc
, self
);
442 if ((ULONG_PTR
)hDdeData
)
444 pConv
= WDML_CreateServerConv(pInstance
, hwndClient
, hwndServer
,
448 if (pcc
) pConv
->wStatus
|= ST_ISLOCAL
;
449 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
450 hszTop
, hszApp
, 0, (ULONG_PTR
)pcc
, self
);
455 else if (pInstance
->servers
)
457 /* pass on to the callback */
458 hDdeData
= WDML_InvokeCallback(pInstance
, XTYP_WILDCONNECT
,
459 0, 0, hszTop
, hszApp
, 0, (ULONG_PTR
)pcc
, self
);
461 if (hDdeData
== CBR_BLOCK
)
463 /* MS doc is not consistent here */
464 FIXME("CBR_BLOCK returned for WILDCONNECT\n");
466 else if ((ULONG_PTR
)hDdeData
!= 0)
470 hszp
= (HSZPAIR
*)DdeAccessData(hDdeData
, NULL
);
474 for (i
= 0; hszp
[i
].hszSvc
&& hszp
[i
].hszTopic
; i
++)
476 pConv
= WDML_CreateServerConv(pInstance
, hwndClient
, hwndServer
,
477 hszp
[i
].hszSvc
, hszp
[i
].hszTopic
);
480 if (pcc
) pConv
->wStatus
|= ST_ISLOCAL
;
481 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
482 hszp
[i
].hszTopic
, hszp
[i
].hszSvc
, 0, (ULONG_PTR
)pcc
, self
);
485 DdeUnaccessData(hDdeData
);
487 if (!WDML_IsAppOwned(hDdeData
)) DdeFreeDataHandle(hDdeData
);
495 FIXME("WM_DDE_REQUEST message received!\n");
498 FIXME("WM_DDE_ADVISE message received!\n");
500 case WM_DDE_UNADVISE
:
501 FIXME("WM_DDE_UNADVISE message received!\n");
504 FIXME("WM_DDE_EXECUTE message received!\n");
507 FIXME("WM_DDE_POKE message received!\n");
509 case WM_DDE_TERMINATE
:
510 FIXME("WM_DDE_TERMINATE message received!\n");
516 return DefWindowProcW(hwndServer
, iMsg
, wParam
, lParam
);
519 /******************************************************************
520 * WDML_ServerQueueRequest
524 static WDML_XACT
* WDML_ServerQueueRequest(WDML_CONV
* pConv
, LPARAM lParam
)
529 UnpackDDElParam(WM_DDE_REQUEST
, lParam
, &uiLo
, &uiHi
);
531 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_REQUEST
,
532 uiLo
, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
533 if (pXAct
) pXAct
->atom
= uiHi
;
537 /******************************************************************
538 * WDML_ServerHandleRequest
542 static WDML_QUEUE_STATE
WDML_ServerHandleRequest(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
544 HDDEDATA hDdeData
= 0;
547 if (!(pConv
->instance
->CBFflags
& CBF_FAIL_REQUESTS
))
550 hDdeData
= WDML_InvokeCallback(pConv
->instance
, XTYP_REQUEST
, pXAct
->wFmt
, (HCONV
)pConv
,
551 pConv
->hszTopic
, pXAct
->hszItem
, 0, 0, 0);
554 switch ((ULONG_PTR
)hDdeData
)
557 TRACE("No data returned from the Callback\n");
561 case (ULONG_PTR
)CBR_BLOCK
:
562 return WDML_QS_BLOCK
;
566 HGLOBAL hMem
= WDML_DataHandle2Global(hDdeData
, TRUE
, FALSE
, FALSE
, FALSE
);
567 if (!PostMessageW(pConv
->hwndClient
, WM_DDE_DATA
, (WPARAM
)pConv
->hwndServer
,
568 ReuseDDElParam(pXAct
->lParam
, WM_DDE_REQUEST
, WM_DDE_DATA
,
569 (UINT_PTR
)hMem
, (UINT_PTR
)pXAct
->atom
)))
571 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
572 DdeFreeDataHandle(hDdeData
);
580 WDML_PostAck(pConv
, WDML_SERVER_SIDE
, 0, FALSE
, fAck
, pXAct
->atom
, pXAct
->lParam
, WM_DDE_REQUEST
);
582 WDML_DecHSZ(pConv
->instance
, pXAct
->hszItem
);
584 return WDML_QS_HANDLED
;
587 /******************************************************************
588 * WDML_ServerQueueAdvise
592 static WDML_XACT
* WDML_ServerQueueAdvise(WDML_CONV
* pConv
, LPARAM lParam
)
597 /* XTYP_ADVSTART transaction:
598 establish link and save link info to InstanceInfoTable */
600 if (!UnpackDDElParam(WM_DDE_ADVISE
, lParam
, &uiLo
, &uiHi
))
603 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_ADVISE
,
604 0, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
607 pXAct
->hMem
= (HGLOBAL
)uiLo
;
613 /******************************************************************
614 * WDML_ServerHandleAdvise
618 static WDML_QUEUE_STATE
WDML_ServerHandleAdvise(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
622 DDEADVISE
* pDdeAdvise
;
623 HDDEDATA hDdeData
= 0;
626 pDdeAdvise
= GlobalLock(pXAct
->hMem
);
627 uType
= XTYP_ADVSTART
|
628 (pDdeAdvise
->fDeferUpd
? XTYPF_NODATA
: 0) |
629 (pDdeAdvise
->fAckReq
? XTYPF_ACKREQ
: 0);
631 if (!(pConv
->instance
->CBFflags
& CBF_FAIL_ADVISES
))
633 hDdeData
= WDML_InvokeCallback(pConv
->instance
, XTYP_ADVSTART
, pDdeAdvise
->cfFormat
,
634 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
, 0, 0, 0);
637 switch ((ULONG_PTR
)hDdeData
)
640 TRACE("No data returned from the Callback\n");
644 case (ULONG_PTR
)CBR_BLOCK
:
645 return WDML_QS_BLOCK
;
648 /* billx: first to see if the link is already created. */
649 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_SERVER_SIDE
,
650 pXAct
->hszItem
, TRUE
, pDdeAdvise
->cfFormat
);
654 /* we found a link, and only need to modify it in case it changes */
655 pLink
->transactionType
= uType
;
659 TRACE("Adding Link with hConv %p\n", pConv
);
660 WDML_AddLink(pConv
->instance
, (HCONV
)pConv
, WDML_SERVER_SIDE
,
661 uType
, pXAct
->hszItem
, pDdeAdvise
->cfFormat
);
666 GlobalUnlock(pXAct
->hMem
);
669 GlobalFree(pXAct
->hMem
);
673 WDML_PostAck(pConv
, WDML_SERVER_SIDE
, 0, FALSE
, fAck
, pXAct
->atom
, pXAct
->lParam
, WM_DDE_ADVISE
);
675 WDML_DecHSZ(pConv
->instance
, pXAct
->hszItem
);
677 return WDML_QS_HANDLED
;
680 /******************************************************************
681 * WDML_ServerQueueUnadvise
685 static WDML_XACT
* WDML_ServerQueueUnadvise(WDML_CONV
* pConv
, LPARAM lParam
)
690 UnpackDDElParam(WM_DDE_UNADVISE
, lParam
, &uiLo
, &uiHi
);
692 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_UNADVISE
,
693 uiLo
, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
694 if (pXAct
) pXAct
->atom
= uiHi
;
698 /******************************************************************
699 * WDML_ServerHandleUnadvise
703 static WDML_QUEUE_STATE
WDML_ServerHandleUnadvise(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
707 if (pXAct
->hszItem
== NULL
|| pXAct
->wFmt
== 0)
709 ERR("Unsupported yet options (null item or clipboard format)\n");
710 return WDML_QS_ERROR
;
713 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_SERVER_SIDE
,
714 pXAct
->hszItem
, TRUE
, pXAct
->wFmt
);
717 ERR("Couldn't find link for %p, dropping request\n", pXAct
->hszItem
);
718 FreeDDElParam(WM_DDE_UNADVISE
, pXAct
->lParam
);
719 return WDML_QS_ERROR
;
722 if (!(pConv
->instance
->CBFflags
& CBF_FAIL_ADVISES
))
724 WDML_InvokeCallback(pConv
->instance
, XTYP_ADVSTOP
, pXAct
->wFmt
, (HCONV
)pConv
,
725 pConv
->hszTopic
, pXAct
->hszItem
, 0, 0, 0);
728 WDML_RemoveLink(pConv
->instance
, (HCONV
)pConv
, WDML_SERVER_SIDE
,
729 pXAct
->hszItem
, pXAct
->wFmt
);
732 WDML_PostAck(pConv
, WDML_SERVER_SIDE
, 0, FALSE
, TRUE
, pXAct
->atom
,
733 pXAct
->lParam
, WM_DDE_UNADVISE
);
735 WDML_DecHSZ(pConv
->instance
, pXAct
->hszItem
);
737 return WDML_QS_HANDLED
;
740 /******************************************************************
745 static WDML_XACT
* WDML_ServerQueueExecute(WDML_CONV
* pConv
, LPARAM lParam
)
749 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_EXECUTE
, 0, 0);
752 pXAct
->hMem
= (HGLOBAL
)lParam
;
757 static BOOL
data_looks_unicode( const WCHAR
*data
, DWORD size
)
761 if (size
% sizeof(WCHAR
)) return FALSE
;
762 for (i
= 0; i
< size
/ sizeof(WCHAR
); i
++) if (data
[i
] > 255) return FALSE
;
766 /* convert data to Unicode, unless it looks like it's already Unicode */
767 static HDDEDATA
map_A_to_W( DWORD instance
, void *ptr
, DWORD size
)
773 if (!data_looks_unicode( ptr
, size
))
775 if ((end
= memchr( ptr
, 0, size
))) size
= end
+ 1 - (const char *)ptr
;
776 len
= MultiByteToWideChar( CP_ACP
, 0, ptr
, size
, NULL
, 0 );
777 ret
= DdeCreateDataHandle( instance
, NULL
, len
* sizeof(WCHAR
), 0, 0, CF_TEXT
, 0);
778 MultiByteToWideChar( CP_ACP
, 0, ptr
, size
, (WCHAR
*)DdeAccessData(ret
, NULL
), len
);
780 else ret
= DdeCreateDataHandle( instance
, ptr
, size
, 0, 0, CF_TEXT
, 0 );
785 /* convert data to ASCII, unless it looks like it's not in Unicode format */
786 static HDDEDATA
map_W_to_A( DWORD instance
, void *ptr
, DWORD size
)
792 if (data_looks_unicode( ptr
, size
))
794 size
/= sizeof(WCHAR
);
795 if ((end
= wmemchr( ptr
, 0, size
))) size
= end
+ 1 - (const WCHAR
*)ptr
;
796 len
= WideCharToMultiByte( CP_ACP
, 0, ptr
, size
, NULL
, 0, NULL
, NULL
);
797 ret
= DdeCreateDataHandle( instance
, NULL
, len
, 0, 0, CF_TEXT
, 0);
798 WideCharToMultiByte( CP_ACP
, 0, ptr
, size
, (char *)DdeAccessData(ret
, NULL
), len
, NULL
, NULL
);
800 else ret
= DdeCreateDataHandle( instance
, ptr
, size
, 0, 0, CF_TEXT
, 0 );
805 /******************************************************************
806 * WDML_ServerHandleExecute
810 static WDML_QUEUE_STATE
WDML_ServerHandleExecute(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
812 HDDEDATA hDdeData
= DDE_FNOTPROCESSED
;
813 BOOL fAck
= FALSE
, fBusy
= FALSE
;
815 if (!(pConv
->instance
->CBFflags
& CBF_FAIL_EXECUTES
))
817 LPVOID ptr
= GlobalLock(pXAct
->hMem
);
818 DWORD size
= GlobalSize(pXAct
->hMem
);
822 if (pConv
->instance
->unicode
) /* Unicode server, try to map A->W */
823 hDdeData
= map_A_to_W( pConv
->instance
->instanceID
, ptr
, size
);
824 else if (!IsWindowUnicode( pConv
->hwndClient
)) /* ASCII server and client, try to map W->A */
825 hDdeData
= map_W_to_A( pConv
->instance
->instanceID
, ptr
, size
);
827 hDdeData
= DdeCreateDataHandle(pConv
->instance
->instanceID
, ptr
, size
, 0, 0, CF_TEXT
, 0);
828 GlobalUnlock(pXAct
->hMem
);
830 hDdeData
= WDML_InvokeCallback(pConv
->instance
, XTYP_EXECUTE
, 0, (HCONV
)pConv
,
831 pConv
->hszTopic
, 0, hDdeData
, 0L, 0L);
834 switch ((ULONG_PTR
)hDdeData
)
836 case (ULONG_PTR
)CBR_BLOCK
:
837 return WDML_QS_BLOCK
;
846 FIXME("Unsupported returned value %p\n", hDdeData
);
848 case DDE_FNOTPROCESSED
:
851 WDML_PostAck(pConv
, WDML_SERVER_SIDE
, 0, fBusy
, fAck
, (UINT_PTR
)pXAct
->hMem
, 0, 0);
853 return WDML_QS_HANDLED
;
856 /******************************************************************
857 * WDML_ServerQueuePoke
861 static WDML_XACT
* WDML_ServerQueuePoke(WDML_CONV
* pConv
, LPARAM lParam
)
866 UnpackDDElParam(WM_DDE_POKE
, lParam
, &uiLo
, &uiHi
);
868 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_POKE
,
869 0, WDML_MakeHszFromAtom(pConv
->instance
, uiHi
));
873 pXAct
->hMem
= (HGLOBAL
)uiLo
;
878 /******************************************************************
879 * WDML_ServerHandlePoke
883 static WDML_QUEUE_STATE
WDML_ServerHandlePoke(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
887 BOOL fBusy
= FALSE
, fAck
= FALSE
;
889 pDdePoke
= GlobalLock(pXAct
->hMem
);
892 return WDML_QS_ERROR
;
895 if (!(pConv
->instance
->CBFflags
& CBF_FAIL_POKES
))
897 hDdeData
= DdeCreateDataHandle(pConv
->instance
->instanceID
, pDdePoke
->Value
,
898 GlobalSize(pXAct
->hMem
) - FIELD_OFFSET(DDEPOKE
, Value
),
899 0, 0, pDdePoke
->cfFormat
, 0);
902 HDDEDATA hDdeDataOut
;
904 hDdeDataOut
= WDML_InvokeCallback(pConv
->instance
, XTYP_POKE
, pDdePoke
->cfFormat
,
905 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
,
907 switch ((ULONG_PTR
)hDdeDataOut
)
916 FIXME("Unsupported returned value %p\n", hDdeDataOut
);
918 case DDE_FNOTPROCESSED
:
921 DdeFreeDataHandle(hDdeData
);
924 GlobalUnlock(pXAct
->hMem
);
928 GlobalFree(pXAct
->hMem
);
930 WDML_PostAck(pConv
, WDML_SERVER_SIDE
, 0, fBusy
, fAck
, pXAct
->atom
, pXAct
->lParam
, WM_DDE_POKE
);
932 WDML_DecHSZ(pConv
->instance
, pXAct
->hszItem
);
934 return WDML_QS_HANDLED
;
937 /******************************************************************
938 * WDML_ServerQueueTerminate
942 static WDML_XACT
* WDML_ServerQueueTerminate(WDML_CONV
* pConv
, LPARAM lParam
)
946 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_TERMINATE
, 0, 0);
950 /******************************************************************
951 * WDML_ServerHandleTerminate
955 static WDML_QUEUE_STATE
WDML_ServerHandleTerminate(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
957 /* billx: two things to remove: the conv, and associated links.
958 * Respond with another WM_DDE_TERMINATE iMsg.
960 if (!(pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
))
962 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
, 0, 0,
963 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
965 PostMessageW(pConv
->hwndClient
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndServer
, 0);
966 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
968 return WDML_QS_HANDLED
;
971 /******************************************************************
976 WDML_QUEUE_STATE
WDML_ServerHandle(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
978 WDML_QUEUE_STATE qs
= WDML_QS_ERROR
;
980 switch (pXAct
->ddeMsg
)
982 case WM_DDE_INITIATE
:
983 FIXME("WM_DDE_INITIATE shouldn't be there!\n");
986 qs
= WDML_ServerHandleRequest(pConv
, pXAct
);
990 qs
= WDML_ServerHandleAdvise(pConv
, pXAct
);
993 case WM_DDE_UNADVISE
:
994 qs
= WDML_ServerHandleUnadvise(pConv
, pXAct
);
998 qs
= WDML_ServerHandleExecute(pConv
, pXAct
);
1002 qs
= WDML_ServerHandlePoke(pConv
, pXAct
);
1005 case WM_DDE_TERMINATE
:
1006 qs
= WDML_ServerHandleTerminate(pConv
, pXAct
);
1010 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1014 FIXME("Unsupported message %d\n", pXAct
->ddeMsg
);
1019 /******************************************************************
1020 * WDML_ServerConvProc
1024 static LRESULT CALLBACK
WDML_ServerConvProc(HWND hwndServer
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
1026 WDML_INSTANCE
* pInstance
;
1028 WDML_XACT
* pXAct
= NULL
;
1030 TRACE("%p %04x %08lx %08lx\n", hwndServer
, iMsg
, wParam
, lParam
);
1032 if (iMsg
== WM_DESTROY
)
1034 pConv
= WDML_GetConvFromWnd(hwndServer
);
1035 if (pConv
&& !(pConv
->wStatus
& ST_TERMINATED
))
1037 WDML_ServerHandleTerminate(pConv
, NULL
);
1040 if (iMsg
< WM_DDE_FIRST
|| iMsg
> WM_DDE_LAST
)
1042 return IsWindowUnicode(hwndServer
) ? DefWindowProcW(hwndServer
, iMsg
, wParam
, lParam
) :
1043 DefWindowProcA(hwndServer
, iMsg
, wParam
, lParam
);
1046 pInstance
= WDML_GetInstanceFromWnd(hwndServer
);
1047 pConv
= WDML_GetConvFromWnd(hwndServer
);
1051 ERR("Got a message (%x) on a not known conversation, dropping request\n", iMsg
);
1054 if (pConv
->hwndClient
!= WIN_GetFullHandle( (HWND
)wParam
) || pConv
->hwndServer
!= hwndServer
)
1056 ERR("mismatch between C/S windows and conversation\n");
1059 if (pConv
->instance
!= pInstance
|| pConv
->instance
== NULL
)
1061 ERR("mismatch in instances\n");
1067 case WM_DDE_INITIATE
:
1068 FIXME("WM_DDE_INITIATE message received!\n");
1071 case WM_DDE_REQUEST
:
1072 pXAct
= WDML_ServerQueueRequest(pConv
, lParam
);
1076 pXAct
= WDML_ServerQueueAdvise(pConv
, lParam
);
1079 case WM_DDE_UNADVISE
:
1080 pXAct
= WDML_ServerQueueUnadvise(pConv
, lParam
);
1083 case WM_DDE_EXECUTE
:
1084 pXAct
= WDML_ServerQueueExecute(pConv
, lParam
);
1088 pXAct
= WDML_ServerQueuePoke(pConv
, lParam
);
1091 case WM_DDE_TERMINATE
:
1092 pXAct
= WDML_ServerQueueTerminate(pConv
, lParam
);
1096 WARN("Shouldn't receive a ACK message (never requests them). Ignoring it\n");
1100 FIXME("Unsupported message %x\n", iMsg
);
1106 pXAct
->lParam
= lParam
;
1108 if ((pConv
->wStatus
& ST_BLOCKED
) || WDML_ServerHandle(pConv
, pXAct
) == WDML_QS_BLOCK
)
1110 TRACE("Transactions are blocked, add to the queue and exit\n");
1111 WDML_QueueTransaction(pConv
, pXAct
);
1115 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1119 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;