4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
8 * Copyright 2001 Eric Pouech
9 * Copyright 2004, 2005 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "user_private.h"
27 #include "dde_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
32 static LRESULT CALLBACK
WDML_ClientProc(HWND
, UINT
, WPARAM
, LPARAM
); /* only for one client, not conv list */
33 const char WDML_szClientConvClassA
[] = "WineDdeClientA";
34 const WCHAR WDML_szClientConvClassW
[] = L
"WineDdeClientW";
36 /******************************************************************************
37 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
40 * idInst [I] Instance identifier
41 * hszService [I] Handle to service name string
42 * hszTopic [I] Handle to topic name string
43 * hConvList [I] Handle to conversation list
44 * pCC [I] Pointer to structure with context data
47 * Success: Handle to new conversation list
50 HCONVLIST WINAPI
DdeConnectList(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
51 HCONVLIST hConvList
, PCONVCONTEXT pCC
)
53 FIXME("(%ld,%p,%p,%p,%p): stub\n", idInst
, hszService
, hszTopic
, hConvList
, pCC
);
57 /*****************************************************************
58 * DdeQueryNextServer [USER32.@]
60 HCONV WINAPI
DdeQueryNextServer(HCONVLIST hConvList
, HCONV hConvPrev
)
62 FIXME("(%p,%p): stub\n", hConvList
, hConvPrev
);
66 /******************************************************************************
67 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
71 * hConvList [I] Handle to conversation list
77 BOOL WINAPI
DdeDisconnectList(HCONVLIST hConvList
)
79 FIXME("(%p): stub\n", hConvList
);
83 /*****************************************************************
84 * DdeConnect (USER32.@)
86 HCONV WINAPI
DdeConnect(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
90 WDML_INSTANCE
* pInstance
;
92 ATOM aSrv
= 0, aTpc
= 0;
94 TRACE("(0x%lx,%p,%p,%p)\n", idInst
, hszService
, hszTopic
, pCC
);
96 pInstance
= WDML_GetInstance(idInst
);
100 /* make sure this conv is never created */
101 pConv
= WDML_FindConv(pInstance
, WDML_CLIENT_SIDE
, hszService
, hszTopic
);
104 ERR("This Conv already exists: (%p)\n", pConv
);
108 /* we need to establish a conversation with
109 server, so create a window for it */
111 if (pInstance
->unicode
)
113 WNDCLASSEXW wndclass
;
115 wndclass
.cbSize
= sizeof(wndclass
);
117 wndclass
.lpfnWndProc
= WDML_ClientProc
;
118 wndclass
.cbClsExtra
= 0;
119 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
120 wndclass
.hInstance
= 0;
122 wndclass
.hCursor
= 0;
123 wndclass
.hbrBackground
= 0;
124 wndclass
.lpszMenuName
= NULL
;
125 wndclass
.lpszClassName
= WDML_szClientConvClassW
;
126 wndclass
.hIconSm
= 0;
128 RegisterClassExW(&wndclass
);
130 hwndClient
= CreateWindowW(WDML_szClientConvClassW
, NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
134 WNDCLASSEXA wndclass
;
136 wndclass
.cbSize
= sizeof(wndclass
);
138 wndclass
.lpfnWndProc
= WDML_ClientProc
;
139 wndclass
.cbClsExtra
= 0;
140 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
141 wndclass
.hInstance
= 0;
143 wndclass
.hCursor
= 0;
144 wndclass
.hbrBackground
= 0;
145 wndclass
.lpszMenuName
= NULL
;
146 wndclass
.lpszClassName
= WDML_szClientConvClassA
;
147 wndclass
.hIconSm
= 0;
149 RegisterClassExA(&wndclass
);
151 hwndClient
= CreateWindowA(WDML_szClientConvClassA
, NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
154 SetWindowLongPtrW(hwndClient
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
158 aSrv
= WDML_MakeAtomFromHsz(hszService
);
159 if (!aSrv
) goto theEnd
;
163 aTpc
= WDML_MakeAtomFromHsz(hszTopic
);
164 if (!aTpc
) goto theEnd
;
167 /* note: sent messages shall not use packing */
168 SendMessageTimeoutW( HWND_BROADCAST
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
, MAKELPARAM(aSrv
, aTpc
),
169 SMTO_ABORTIFHUNG
, 0, NULL
);
171 pInstance
= WDML_GetInstance(idInst
);
177 /* At this point, Client WM_DDE_ACK should have saved hwndServer
178 for this instance id and hwndClient if server responds.
179 So get HCONV and return it. And add it to conv list */
180 pConv
= WDML_GetConvFromWnd(hwndClient
);
181 if (pConv
== NULL
|| pConv
->hwndServer
== 0)
183 WARN("Done with INITIATE, but no Server window available\n");
185 pInstance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
188 TRACE("Connected to Server window (%p)\n", pConv
->hwndServer
);
189 pConv
->wConvst
= XST_CONNECTED
;
191 /* finish init of pConv */
194 pConv
->convContext
= *pCC
;
198 memset(&pConv
->convContext
, 0, sizeof(pConv
->convContext
));
199 pConv
->convContext
.cb
= sizeof(pConv
->convContext
);
200 pConv
->convContext
.iCodePage
= (pInstance
->unicode
) ? CP_WINUNICODE
: CP_WINANSI
;
205 if (aSrv
) GlobalDeleteAtom(aSrv
);
206 if (aTpc
) GlobalDeleteAtom(aTpc
);
210 /*****************************************************************
211 * DdeReconnect (DDEML.37)
212 * DdeReconnect (USER32.@)
214 HCONV WINAPI
DdeReconnect(HCONV hConv
)
217 WDML_CONV
* pNewConv
= NULL
;
218 ATOM aSrv
= 0, aTpc
= 0;
220 TRACE("(%p)\n", hConv
);
222 pConv
= WDML_GetConv(hConv
, FALSE
);
223 if (pConv
!= NULL
&& (pConv
->wStatus
& ST_CLIENT
))
227 /* to reestablish a connection, we have to make sure that:
228 * 1/ pConv is the conversation attached to the client window (it wouldn't be
229 * if a call to DdeReconnect would have already been done...)
230 * FIXME: is this really an error ???
231 * 2/ the pConv conversation had really been deconnected
233 if (pConv
== WDML_GetConvFromWnd(pConv
->hwndClient
) &&
234 (pConv
->wStatus
& ST_TERMINATED
) && !(pConv
->wStatus
& ST_CONNECTED
))
236 HWND hwndClient
= pConv
->hwndClient
;
237 HWND hwndServer
= pConv
->hwndServer
;
239 SetWindowLongPtrW(pConv
->hwndClient
, GWL_WDML_CONVERSATION
, 0);
241 aSrv
= WDML_MakeAtomFromHsz(pConv
->hszService
);
242 aTpc
= WDML_MakeAtomFromHsz(pConv
->hszTopic
);
243 if (!aSrv
|| !aTpc
) goto theEnd
;
245 /* note: sent messages shall not use packing */
246 ret
= SendMessageW(hwndServer
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
,
247 MAKELPARAM(aSrv
, aTpc
));
249 pConv
= WDML_GetConv(hConv
, FALSE
);
252 FIXME("Should fail reconnection\n");
256 if (ret
&& (pNewConv
= WDML_GetConvFromWnd(pConv
->hwndClient
)) != NULL
)
258 /* re-establish all links... */
261 for (pLink
= pConv
->instance
->links
[WDML_CLIENT_SIDE
]; pLink
; pLink
= pLink
->next
)
263 if (pLink
->hConv
== hConv
)
265 /* try to reestablish the links... */
266 DdeClientTransaction(NULL
, 0, (HCONV
)pNewConv
, pLink
->hszItem
, pLink
->uFmt
,
267 pLink
->transactionType
, 1000, NULL
);
273 /* reset the conversation as it was */
274 SetWindowLongPtrW(pConv
->hwndClient
, GWL_WDML_CONVERSATION
, (ULONG_PTR
)pConv
);
281 if (aSrv
) GlobalDeleteAtom(aSrv
);
282 if (aTpc
) GlobalDeleteAtom(aTpc
);
283 return (HCONV
)pNewConv
;
286 /******************************************************************
287 * WDML_ClientQueueAdvise
289 * Creates and queue an WM_DDE_ADVISE transaction
291 static WDML_XACT
* WDML_ClientQueueAdvise(WDML_CONV
* pConv
, UINT wType
, UINT wFmt
, HSZ hszItem
)
293 DDEADVISE
* pDdeAdvise
;
297 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType
& XTYPF_NODATA
) ? "out" : "");
299 atom
= WDML_MakeAtomFromHsz(hszItem
);
300 if (!atom
) return NULL
;
302 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_ADVISE
, wFmt
, hszItem
);
305 GlobalDeleteAtom(atom
);
309 pXAct
->wType
= wType
& ~0x0F;
310 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, sizeof(DDEADVISE
));
311 /* FIXME: hMem is unfreed for now... should be deleted in server */
314 pDdeAdvise
= GlobalLock(pXAct
->hMem
);
315 pDdeAdvise
->fAckReq
= (wType
& XTYPF_ACKREQ
) != 0;
316 pDdeAdvise
->fDeferUpd
= (wType
& XTYPF_NODATA
) != 0;
317 pDdeAdvise
->cfFormat
= wFmt
;
318 GlobalUnlock(pXAct
->hMem
);
320 pXAct
->lParam
= PackDDElParam(WM_DDE_ADVISE
, (UINT_PTR
)pXAct
->hMem
, atom
);
325 /******************************************************************
326 * WDML_HandleAdviseReply
328 * handles the reply to an advise request
330 static WDML_QUEUE_STATE
WDML_HandleAdviseReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
, DWORD
*ack
)
336 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
341 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
342 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
344 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
347 GlobalDeleteAtom(uiHi
);
348 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
350 if (ack
) *ack
= uiLo
;
351 WDML_ExtractAck(uiLo
, &ddeAck
);
357 /* billx: first to see if the link is already created. */
358 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
359 pXAct
->hszItem
, TRUE
, pXAct
->wFmt
);
362 /* we found a link, and only need to modify it in case it changes */
363 pLink
->transactionType
= pXAct
->wType
;
367 WDML_AddLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
368 pXAct
->wType
, pXAct
->hszItem
, pXAct
->wFmt
);
370 pXAct
->hDdeData
= (HDDEDATA
)1;
374 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
375 GlobalFree(pXAct
->hMem
);
376 pXAct
->hDdeData
= NULL
;
379 return WDML_QS_HANDLED
;
382 /******************************************************************
383 * WDML_ClientQueueUnadvise
385 * queues an unadvise transaction
387 static WDML_XACT
* WDML_ClientQueueUnadvise(WDML_CONV
* pConv
, UINT wFmt
, HSZ hszItem
)
392 TRACE("XTYP_ADVSTOP transaction\n");
394 atom
= WDML_MakeAtomFromHsz(hszItem
);
395 if (!atom
) return NULL
;
397 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_UNADVISE
, wFmt
, hszItem
);
400 GlobalDeleteAtom(atom
);
404 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
405 * on the specified item.
407 pXAct
->lParam
= PackDDElParam(WM_DDE_UNADVISE
, wFmt
, atom
);
411 /******************************************************************
412 * WDML_HandleUnadviseReply
416 static WDML_QUEUE_STATE
WDML_HandleUnadviseReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
, DWORD
*ack
)
422 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
427 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
428 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
430 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
433 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
434 GlobalDeleteAtom(uiHi
);
436 if (ack
) *ack
= uiLo
;
437 WDML_ExtractAck(uiLo
, &ddeAck
);
439 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
443 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
444 pXAct
->hDdeData
= NULL
;
448 /* billx: remove the link */
449 WDML_RemoveLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
450 pXAct
->hszItem
, pXAct
->wFmt
);
451 pXAct
->hDdeData
= (HDDEDATA
)1;
453 return WDML_QS_HANDLED
;
456 /******************************************************************
457 * WDML_ClientQueueRequest
461 static WDML_XACT
* WDML_ClientQueueRequest(WDML_CONV
* pConv
, UINT wFmt
, HSZ hszItem
)
466 TRACE("XTYP_REQUEST transaction\n");
468 atom
= WDML_MakeAtomFromHsz(hszItem
);
469 if (!atom
) return NULL
;
471 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_REQUEST
, wFmt
, hszItem
);
474 GlobalDeleteAtom(atom
);
478 pXAct
->lParam
= PackDDElParam(WM_DDE_REQUEST
, wFmt
, atom
);
483 /******************************************************************
484 * WDML_HandleRequestReply
488 static WDML_QUEUE_STATE
WDML_HandleRequestReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
, DWORD
*ack
)
495 if (WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
498 switch (msg
->message
)
501 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
502 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
503 GlobalDeleteAtom(uiHi
);
504 if (ack
) *ack
= uiLo
;
505 WDML_ExtractAck(uiLo
, &ddeAck
);
508 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
509 TRACE("Negative answer...\n");
513 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
514 TRACE("Got the result (%08Ix)\n", uiLo
);
516 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
518 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
521 pXAct
->hDdeData
= WDML_Global2DataHandle(pConv
, (HGLOBAL
)uiLo
, &wdh
);
524 GlobalFree((HGLOBAL
)uiLo
);
528 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
532 GlobalDeleteAtom(uiHi
);
533 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
538 FreeDDElParam(msg
->message
, msg
->lParam
);
542 return WDML_QS_HANDLED
;
545 /******************************************************************
546 * WDML_BuildExecuteCommand
548 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
549 * It also takes care of string conversion between the two window procedures
551 static HGLOBAL
WDML_BuildExecuteCommand(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
)
554 BOOL clientUnicode
, serverUnicode
;
557 clientUnicode
= pConv
->instance
->unicode
;
558 TRACE("client %p uses unicode = %d\n", pConv
->hwndClient
, clientUnicode
);
559 /* FIXME: how exactly Windows determines what to use for the server side? */
560 serverUnicode
= IsWindowUnicode(pConv
->hwndServer
) && IsWindowUnicode(pConv
->hwndClient
);
561 TRACE("server %p uses unicode = %d\n", pConv
->hwndServer
, serverUnicode
);
563 if (clientUnicode
== serverUnicode
)
571 memSize
= WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
575 memSize
= MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, NULL
, 0) * sizeof(WCHAR
);
579 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, memSize
);
585 pDst
= GlobalLock(hMem
);
588 if (clientUnicode
== serverUnicode
)
590 memcpy(pDst
, pData
, cbData
);
596 WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
/ sizeof(WCHAR
), pDst
, memSize
, NULL
, NULL
);
600 MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, (LPWSTR
)pDst
, memSize
/sizeof(WCHAR
));
615 /******************************************************************
616 * WDML_ClientQueueExecute
620 static WDML_XACT
* WDML_ClientQueueExecute(WDML_CONV
* pConv
, LPVOID pData
, DWORD cbData
)
624 TRACE("XTYP_EXECUTE transaction\n");
628 if (cbData
== (DWORD
)-1)
629 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
631 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
635 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_EXECUTE
, 0, 0);
639 if (cbData
== (DWORD
)-1)
641 HDDEDATA hDdeData
= pData
;
643 pData
= DdeAccessData(hDdeData
, &cbData
);
646 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
647 DdeUnaccessData(hDdeData
);
652 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
655 pXAct
->lParam
= (LPARAM
)pXAct
->hMem
;
660 /******************************************************************
661 * WDML_HandleExecuteReply
665 static WDML_QUEUE_STATE
WDML_HandleExecuteReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
, DWORD
*ack
)
670 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
675 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
676 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
678 if ((HANDLE
)uiHi
!= pXAct
->hMem
)
683 if (ack
) *ack
= uiLo
;
684 WDML_ExtractAck(uiLo
, &ddeAck
);
685 pXAct
->hDdeData
= (HDDEDATA
)(UINT_PTR
)ddeAck
.fAck
;
687 TRACE("hDdeData = %p\n", pXAct
->hDdeData
);
688 pConv
->instance
->lastError
= (pXAct
->hDdeData
!= 0) ? DMLERR_NO_ERROR
: DMLERR_NOTPROCESSED
;
690 return WDML_QS_HANDLED
;
693 /******************************************************************
694 * WDML_ClientQueuePoke
698 static WDML_XACT
* WDML_ClientQueuePoke(WDML_CONV
* pConv
, LPVOID pData
, DWORD cbData
,
699 UINT wFmt
, HSZ hszItem
)
701 DDE_DATAHANDLE_HEAD
*dh
;
707 TRACE("XTYP_POKE transaction\n");
709 atom
= WDML_MakeAtomFromHsz(hszItem
);
710 if (!atom
) return NULL
;
712 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_POKE
, wFmt
, hszItem
);
715 GlobalDeleteAtom(atom
);
719 if (cbData
== (DWORD
)-1)
722 dh
= GlobalLock(hglobal
);
723 cbData
= GlobalSize(hglobal
) - sizeof(DDE_DATAHANDLE_HEAD
);
725 GlobalUnlock(hglobal
);
728 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, FIELD_OFFSET(DDEPOKE
, Value
[cbData
]));
729 ddePoke
= GlobalLock(pXAct
->hMem
);
732 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
737 ddePoke
->fRelease
= TRUE
;
738 ddePoke
->cfFormat
= wFmt
;
739 memcpy(ddePoke
->Value
, pData
, cbData
);
740 GlobalUnlock(pXAct
->hMem
);
742 pXAct
->lParam
= PackDDElParam(WM_DDE_POKE
, (UINT_PTR
)pXAct
->hMem
, atom
);
747 /******************************************************************
748 * WDML_HandlePokeReply
752 static WDML_QUEUE_STATE
WDML_HandlePokeReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
, DWORD
*ack
)
757 if (msg
->message
!= WM_DDE_ACK
&& WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
762 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
763 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
764 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
768 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
769 GlobalDeleteAtom(uiHi
);
771 if (ack
) *ack
= uiLo
;
772 pXAct
->hMem
= GlobalFree(pXAct
->hMem
);
774 pXAct
->hDdeData
= (HDDEDATA
)TRUE
;
778 /******************************************************************
779 * WDML_ClientQueueTerminate
781 * Creates and queue an WM_DDE_TERMINATE transaction
783 static WDML_XACT
* WDML_ClientQueueTerminate(WDML_CONV
* pConv
)
787 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_TERMINATE
, 0, 0);
792 pConv
->wStatus
&= ~ST_CONNECTED
;
797 /******************************************************************
798 * WDML_HandleTerminateReply
800 * handles the reply to a terminate request
802 static WDML_QUEUE_STATE
WDML_HandleTerminateReply(WDML_CONV
* pConv
, MSG
* msg
)
804 if (msg
->message
!= WM_DDE_TERMINATE
)
806 /* FIXME: should delete data passed here */
807 return WDML_QS_SWALLOWED
;
810 if (WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
812 FIXME("hmmm shouldn't happen\n");
815 if (!(pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
))
817 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
,
818 0, 0, 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
820 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
821 return WDML_QS_HANDLED
;
824 /******************************************************************
825 * WDML_HandleIncomingData
829 static WDML_QUEUE_STATE
WDML_HandleIncomingData(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
832 HDDEDATA hDdeDataIn
, hDdeDataOut
;
837 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
838 /* wParam -- sending window handle */
839 /* lParam -- hDdeData & item HSZ */
841 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
842 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
844 hDdeDataIn
= WDML_Global2DataHandle(pConv
, (HGLOBAL
)uiLo
, &wdh
);
847 * For hot link, data should be passed to its callback with
848 * XTYP_ADVDATA and callback should return the proper status.
850 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
, hsz
,
851 uiLo
!= 0, wdh
.cfFormat
);
854 WDML_DecHSZ(pConv
->instance
, hsz
);
855 DdeFreeDataHandle(hDdeDataIn
);
859 if (hDdeDataIn
!= 0 && wdh
.fAckReq
)
861 WDML_PostAck(pConv
, WDML_CLIENT_SIDE
, 0, FALSE
, TRUE
, uiHi
, msg
->lParam
, WM_DDE_DATA
);
867 GlobalDeleteAtom(uiHi
);
870 hDdeDataOut
= WDML_InvokeCallback(pConv
->instance
, XTYP_ADVDATA
, pLink
->uFmt
, pLink
->hConv
,
871 pConv
->hszTopic
, pLink
->hszItem
, hDdeDataIn
, 0, 0);
873 if (hDdeDataOut
!= (HDDEDATA
)DDE_FACK
|| wdh
.fRelease
)
875 if (uiLo
) GlobalFree((HANDLE
)uiLo
);
878 DdeFreeDataHandle(hDdeDataIn
);
880 WDML_DecHSZ(pConv
->instance
, hsz
);
882 FreeDDElParam(WM_DDE_DATA
, msg
->lParam
);
884 return WDML_QS_HANDLED
;
887 /******************************************************************
888 * WDML_HandleIncomingTerminate
892 static WDML_QUEUE_STATE
WDML_HandleIncomingTerminate(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
894 if (pConv
->hwndServer
!= WIN_GetFullHandle((HWND
)msg
->wParam
))
897 pConv
->wStatus
|= ST_TERMINATED
;
898 if (pConv
->wStatus
& ST_CONNECTED
)
900 /* don't care about result code (if server exists or not) */
901 PostMessageW(pConv
->hwndServer
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndClient
, 0L);
902 pConv
->wStatus
&= ~ST_CONNECTED
;
904 /* have to keep connection around to allow reconnection */
905 return WDML_QS_HANDLED
;
908 /******************************************************************
911 * handles any incoming reply, and try to match to an already sent request
913 static WDML_QUEUE_STATE
WDML_HandleReply(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
, DWORD
*ack
)
915 WDML_XACT
* pXAct
= pConv
->transactions
;
918 if (pConv
->transactions
)
920 if (ack
) *ack
= DDE_FNOTPROCESSED
;
921 /* first check message against a pending transaction, if any */
922 switch (pXAct
->ddeMsg
)
925 qs
= WDML_HandleAdviseReply(pConv
, msg
, pXAct
, ack
);
927 case WM_DDE_UNADVISE
:
928 qs
= WDML_HandleUnadviseReply(pConv
, msg
, pXAct
, ack
);
931 qs
= WDML_HandleExecuteReply(pConv
, msg
, pXAct
, ack
);
934 qs
= WDML_HandleRequestReply(pConv
, msg
, pXAct
, ack
);
937 qs
= WDML_HandlePokeReply(pConv
, msg
, pXAct
, ack
);
939 case WM_DDE_TERMINATE
:
940 qs
= WDML_HandleTerminateReply(pConv
, msg
);
952 /* now check the results */
956 case WDML_QS_SWALLOWED
:
959 case WDML_QS_HANDLED
:
960 /* ok, we have resolved a pending transaction
961 * notify callback if asynchronous.
963 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&& pXAct
->ddeMsg
!= WM_DDE_TERMINATE
)
965 WDML_InvokeCallback(pConv
->instance
, XTYP_XACT_COMPLETE
, pXAct
->wFmt
,
966 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
,
967 pXAct
->hDdeData
, MAKELONG(0, pXAct
->xActID
), 0 /* FIXME */);
972 *hdd
= pXAct
->hDdeData
;
976 /* no pending transaction found, try a warm/hot link or a termination request */
977 switch (msg
->message
)
980 qs
= WDML_HandleIncomingData(pConv
, msg
, hdd
);
982 case WM_DDE_TERMINATE
:
983 qs
= WDML_HandleIncomingTerminate(pConv
, msg
, hdd
);
986 /* This happens at end of DdeClientTransaction XTYP_EXECUTE
987 * Without this assignment, DdeClientTransaction's return value is undefined
989 *hdd
= (HDDEDATA
)TRUE
;
996 FIXME("shouldn't be used on client side\n");
1003 /******************************************************************
1004 * WDML_SyncWaitTransactionReply
1006 * waits until an answer for a sent request is received
1007 * time out is also handled. only used for synchronous transactions
1009 static HDDEDATA
WDML_SyncWaitTransactionReply(HCONV hConv
, DWORD dwTimeout
, const WDML_XACT
* pXAct
, DWORD
*ack
)
1011 DWORD start
, elapsed
;
1015 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout
);
1017 start
= GetTickCount();
1018 while ((elapsed
= GetTickCount() - start
) < dwTimeout
)
1020 /* we cannot be in the crit sect all the time because when client and server run in a
1021 * single process they need to share the access to the internal data
1023 if (MsgWaitForMultipleObjects(0, NULL
, FALSE
,
1024 dwTimeout
- elapsed
, QS_POSTMESSAGE
) == WAIT_OBJECT_0
)
1028 while (PeekMessageW(&msg
, 0, WM_DDE_FIRST
, WM_DDE_LAST
, PM_REMOVE
))
1030 HDDEDATA hdd
= NULL
;
1032 pConv
= WDML_GetConv(hConv
, FALSE
);
1035 /* conversation no longer available... return failure */
1038 if (msg
.hwnd
== pConv
->hwndClient
)
1040 /* check that either pXAct has been processed or no more xActions are pending */
1041 BOOL ret
= (pConv
->transactions
== pXAct
);
1042 if (WDML_HandleReply(pConv
, &msg
, &hdd
, ack
) == WDML_QS_HANDLED
)
1044 TRACE("WDML_HandleReply returned WDML_QS_HANDLED\n");
1048 ret
= (pConv
->transactions
== NULL
|| ret
);
1052 pConv
->instance
->lastError
= hdd
? DMLERR_NO_ERROR
: DMLERR_NOTPROCESSED
;
1058 DispatchMessageW(&msg
);
1064 TRACE("Timeout !!\n");
1066 pConv
= WDML_GetConv(hConv
, FALSE
);
1069 if (pConv
->transactions
)
1071 switch (pConv
->transactions
->ddeMsg
)
1073 case WM_DDE_ADVISE
: err
= DMLERR_ADVACKTIMEOUT
; break;
1074 case WM_DDE_REQUEST
: err
= DMLERR_DATAACKTIMEOUT
; break;
1075 case WM_DDE_EXECUTE
: err
= DMLERR_EXECACKTIMEOUT
; break;
1076 case WM_DDE_POKE
: err
= DMLERR_POKEACKTIMEOUT
; break;
1077 case WM_DDE_UNADVISE
: err
= DMLERR_UNADVACKTIMEOUT
; break;
1078 default: err
= DMLERR_INVALIDPARAMETER
; break;
1081 pConv
->instance
->lastError
= err
;
1089 /*****************************************************************
1092 HDDEDATA
WDML_ClientHandle(WDML_CONV
*pConv
, WDML_XACT
*pXAct
, DWORD dwTimeout
, LPDWORD pdwResult
)
1096 if (!PostMessageW(pConv
->hwndServer
, pXAct
->ddeMsg
, (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1098 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1099 pXAct
->ddeMsg
, pConv
->hwndServer
, GetLastError());
1100 pConv
->wStatus
&= ~ST_CONNECTED
;
1101 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
1104 pXAct
->dwTimeout
= dwTimeout
;
1105 /* FIXME: should set the app bits on *pdwResult */
1107 if (dwTimeout
== TIMEOUT_ASYNC
)
1110 *pdwResult
= MAKELONG(0, pXAct
->xActID
);
1112 hDdeData
= (HDDEDATA
)1;
1115 hDdeData
= WDML_SyncWaitTransactionReply((HCONV
)pConv
, dwTimeout
, pXAct
, pdwResult
);
1121 /*****************************************************************
1122 * DdeClientTransaction (USER32.@)
1124 HDDEDATA WINAPI
DdeClientTransaction(LPBYTE pData
, DWORD cbData
, HCONV hConv
, HSZ hszItem
, UINT wFmt
,
1125 UINT wType
, DWORD dwTimeout
, LPDWORD pdwResult
)
1131 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1132 pData
, cbData
, hConv
, hszItem
, wFmt
, wType
, dwTimeout
, pdwResult
);
1136 WARN("Invalid conversation handle NULL\n");
1140 pConv
= WDML_GetConv(hConv
, TRUE
);
1143 /* cannot set error... cannot get back to DDE instance */
1150 /* Windows simply ignores hszItem and wFmt in this case */
1151 pXAct
= WDML_ClientQueueExecute(pConv
, pData
, cbData
);
1158 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1161 pXAct
= WDML_ClientQueuePoke(pConv
, pData
, cbData
, wFmt
, hszItem
);
1163 case XTYP_ADVSTART
|XTYPF_NODATA
:
1164 case XTYP_ADVSTART
|XTYPF_NODATA
|XTYPF_ACKREQ
:
1166 case XTYP_ADVSTART
|XTYPF_ACKREQ
:
1169 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1172 pXAct
= WDML_ClientQueueAdvise(pConv
, wType
, wFmt
, hszItem
);
1177 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1180 pXAct
= WDML_ClientQueueUnadvise(pConv
, wFmt
, hszItem
);
1183 if (pData
|| !hszItem
)
1185 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1188 pXAct
= WDML_ClientQueueRequest(pConv
, wFmt
, hszItem
);
1191 FIXME("Unknown transaction type %04x\n", wType
);
1192 /* unknown transaction type */
1193 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1199 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
1203 WDML_QueueTransaction(pConv
, pXAct
);
1205 TRACE("pConv->wStatus %04lx\n", pConv
->wStatus
);
1207 if (pConv
->wStatus
& ST_BLOCKED
)
1209 TRACE("Transactions are blocked, add to the queue and exit\n");
1213 hDdeData
= WDML_ClientHandle(pConv
, pXAct
, dwTimeout
, pdwResult
);
1214 if (dwTimeout
!= TIMEOUT_ASYNC
)
1216 WDML_UnQueueTransaction(pConv
, pXAct
);
1217 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1223 /*****************************************************************
1224 * DdeAbandonTransaction (USER32.@)
1226 BOOL WINAPI
DdeAbandonTransaction(DWORD idInst
, HCONV hConv
, DWORD idTransaction
)
1228 WDML_INSTANCE
* pInstance
;
1232 if ((pInstance
= WDML_GetInstance(idInst
)))
1236 if ((pConv
= WDML_GetConv(hConv
, TRUE
)) && pConv
->instance
== pInstance
)
1239 pXAct
= pConv
->transactions
;
1241 WDML_XACT
*nextXAct
= pXAct
->next
;
1243 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&&
1244 (idTransaction
== 0 || pXAct
->xActID
== idTransaction
))
1246 WDML_UnQueueTransaction(pConv
, pXAct
);
1247 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1255 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
; pConv
= pConv
->next
)
1257 if (!(pConv
->wStatus
& ST_CONNECTED
)) continue;
1258 pXAct
= pConv
->transactions
;
1260 WDML_XACT
*nextXAct
= pXAct
->next
;
1262 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
)
1264 WDML_UnQueueTransaction(pConv
, pXAct
);
1265 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1276 /******************************************************************
1279 * Window Proc created on client side for each conversation
1281 static LRESULT CALLBACK
WDML_ClientProc(HWND hwnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
1284 WDML_CONV
* pConv
= NULL
;
1287 TRACE("%p %04x %08Ix %08Ix\n", hwnd
, iMsg
, wParam
, lParam
);
1289 if (iMsg
== WM_DDE_ACK
&&
1290 /* in the initial WM_INITIATE sendmessage */
1291 ((pConv
= WDML_GetConvFromWnd(hwnd
)) == NULL
|| pConv
->wStatus
== XST_INIT1
))
1293 /* In response to WM_DDE_INITIATE, save server window */
1295 WDML_INSTANCE
* pInstance
;
1297 /* note: sent messages do not need packing */
1298 uiLo
= LOWORD(lParam
);
1299 uiHi
= HIWORD(lParam
);
1301 /* FIXME: convlist should be handled here */
1304 /* we already have started the conv with a server, drop other replies */
1305 GlobalDeleteAtom(uiLo
);
1306 GlobalDeleteAtom(uiHi
);
1307 PostMessageW((HWND
)wParam
, WM_DDE_TERMINATE
, (WPARAM
)hwnd
, 0);
1311 pInstance
= WDML_GetInstanceFromWnd(hwnd
);
1313 hszSrv
= WDML_MakeHszFromAtom(pInstance
, uiLo
);
1314 hszTpc
= WDML_MakeHszFromAtom(pInstance
, uiHi
);
1316 pConv
= WDML_AddConv(pInstance
, WDML_CLIENT_SIDE
, hszSrv
, hszTpc
, hwnd
, (HWND
)wParam
);
1318 SetWindowLongPtrW(hwnd
, GWL_WDML_CONVERSATION
, (ULONG_PTR
)pConv
);
1319 pConv
->wStatus
|= ST_CONNECTED
;
1320 pConv
->wConvst
= XST_INIT1
;
1322 /* check if server is handled by DDEML */
1323 if ((GetClassNameA((HWND
)wParam
, buf
, sizeof(buf
)) &&
1324 lstrcmpiA(buf
, WDML_szServerConvClassA
) == 0) ||
1325 (GetClassNameW((HWND
)wParam
, (LPWSTR
)buf
, sizeof(buf
)/sizeof(WCHAR
)) &&
1326 lstrcmpiW((LPWSTR
)buf
, WDML_szServerConvClassW
) == 0))
1328 pConv
->wStatus
|= ST_ISLOCAL
;
1331 GlobalDeleteAtom(uiLo
);
1332 GlobalDeleteAtom(uiHi
);
1334 /* accept conversation */
1338 if (iMsg
>= WM_DDE_FIRST
&& iMsg
<= WM_DDE_LAST
)
1340 pConv
= WDML_GetConvFromWnd(hwnd
);
1349 msg
.wParam
= wParam
;
1350 msg
.lParam
= lParam
;
1352 WDML_HandleReply(pConv
, &msg
, &hdd
, NULL
);
1358 return IsWindowUnicode(hwnd
) ? DefWindowProcW(hwnd
, iMsg
, wParam
, lParam
) :
1359 DefWindowProcA(hwnd
, iMsg
, wParam
, lParam
);
1362 /*****************************************************************
1363 * DdeDisconnect (USER32.@)
1365 BOOL WINAPI
DdeDisconnect(HCONV hConv
)
1371 TRACE("(%p)\n", hConv
);
1375 WARN("DdeDisconnect(): hConv = 0\n");
1379 pConv
= WDML_GetConv(hConv
, TRUE
);
1382 if (pConv
->wStatus
& ST_CLIENT
)
1384 /* FIXME: should abandon all pending transactions */
1385 pXAct
= WDML_ClientQueueTerminate(pConv
);
1388 if (PostMessageW(pConv
->hwndServer
, pXAct
->ddeMsg
,
1389 (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1391 WDML_SyncWaitTransactionReply(hConv
, 10000, pXAct
, NULL
);
1395 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
1397 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1398 /* still have to destroy data associated with conversation */
1399 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
1403 FIXME("Not implemented yet for a server side conversation\n");
1411 /*****************************************************************
1412 * DdeImpersonateClient (USER32.@)
1414 BOOL WINAPI
DdeImpersonateClient(HCONV hConv
)
1419 TRACE("(%p)\n", hConv
);
1421 pConv
= WDML_GetConv(hConv
, TRUE
);
1424 ret
= ImpersonateDdeClientWindow(pConv
->hwndClient
, pConv
->hwndServer
);