1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1997 Alexandre Julliard
7 * Copyright 1997 Len White
8 * Copyright 1999 Keith Matthews
10 * Copyright 2001 Eric Pouech
11 * Copyright 2004, 2005 Dmitry Timoshkov
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include "wine/debug.h"
40 #include "dde/dde_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
44 static LRESULT CALLBACK
WDML_ClientProc(HWND
, UINT
, WPARAM
, LPARAM
); /* only for one client, not conv list */
45 const WCHAR WDML_szClientConvClass
[] = {'W','i','n','e','D','d','e','C','l','i','e','n','t',0};
47 /******************************************************************************
48 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
51 * idInst [I] Instance identifier
52 * hszService [I] Handle to service name string
53 * hszTopic [I] Handle to topic name string
54 * hConvList [I] Handle to conversation list
55 * pCC [I] Pointer to structure with context data
58 * Success: Handle to new conversation list
61 HCONVLIST WINAPI
DdeConnectList(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
62 HCONVLIST hConvList
, PCONVCONTEXT pCC
)
64 FIXME("(%ld,%p,%p,%p,%p): stub\n", idInst
, hszService
, hszTopic
, hConvList
, pCC
);
68 /*****************************************************************
69 * DdeQueryNextServer [USER32.@]
71 HCONV WINAPI
DdeQueryNextServer(HCONVLIST hConvList
, HCONV hConvPrev
)
73 FIXME("(%p,%p): stub\n", hConvList
, hConvPrev
);
77 /******************************************************************************
78 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
82 * hConvList [I] Handle to conversation list
88 BOOL WINAPI
DdeDisconnectList(HCONVLIST hConvList
)
90 FIXME("(%p): stub\n", hConvList
);
94 /*****************************************************************
95 * DdeConnect (USER32.@)
97 HCONV WINAPI
DdeConnect(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
101 WDML_INSTANCE
* pInstance
;
102 WDML_CONV
* pConv
= NULL
;
103 ATOM aSrv
= 0, aTpc
= 0;
104 WNDCLASSEXW wndclass
;
106 TRACE("(0x%lx,%p,%p,%p)\n", idInst
, hszService
, hszTopic
, pCC
);
108 EnterCriticalSection(&WDML_CritSect
);
110 pInstance
= WDML_GetInstance(idInst
);
116 /* make sure this conv is never created */
117 pConv
= WDML_FindConv(pInstance
, WDML_CLIENT_SIDE
, hszService
, hszTopic
);
120 ERR("This Conv already exists: (%p)\n", pConv
);
124 /* we need to establish a conversation with
125 server, so create a window for it */
127 wndclass
.cbSize
= sizeof(wndclass
);
129 wndclass
.lpfnWndProc
= WDML_ClientProc
;
130 wndclass
.cbClsExtra
= 0;
131 wndclass
.cbWndExtra
= 2 * sizeof(ULONG_PTR
);
132 wndclass
.hInstance
= 0;
134 wndclass
.hCursor
= 0;
135 wndclass
.hbrBackground
= 0;
136 wndclass
.lpszMenuName
= NULL
;
137 wndclass
.lpszClassName
= WDML_szClientConvClass
;
138 wndclass
.hIconSm
= 0;
140 RegisterClassExW(&wndclass
);
142 hwndClient
= CreateWindowW(WDML_szClientConvClass
, NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
144 SetWindowLongPtrW(hwndClient
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
148 aSrv
= WDML_MakeAtomFromHsz(hszService
);
149 if (!aSrv
) goto theEnd
;
153 aTpc
= WDML_MakeAtomFromHsz(hszTopic
);
154 if (!aTpc
) goto theEnd
;
157 LeaveCriticalSection(&WDML_CritSect
);
159 /* note: sent messages shall not use packing */
160 SendMessageTimeoutW( HWND_BROADCAST
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
, MAKELPARAM(aSrv
, aTpc
),
161 SMTO_ABORTIFHUNG
, 2000, NULL
);
163 EnterCriticalSection(&WDML_CritSect
);
165 pInstance
= WDML_GetInstance(idInst
);
171 /* At this point, Client WM_DDE_ACK should have saved hwndServer
172 for this instance id and hwndClient if server responds.
173 So get HCONV and return it. And add it to conv list */
174 pConv
= WDML_GetConvFromWnd(hwndClient
);
175 if (pConv
== NULL
|| pConv
->hwndServer
== 0)
177 ERR("Done with INITIATE, but no Server window available\n");
181 TRACE("Connected to Server window (%p)\n", pConv
->hwndServer
);
182 pConv
->wConvst
= XST_CONNECTED
;
184 /* finish init of pConv */
187 pConv
->convContext
= *pCC
;
191 memset(&pConv
->convContext
, 0, sizeof(pConv
->convContext
));
192 pConv
->convContext
.cb
= sizeof(pConv
->convContext
);
193 pConv
->convContext
.iCodePage
= CP_WINUNICODE
;
197 LeaveCriticalSection(&WDML_CritSect
);
199 if (aSrv
) GlobalDeleteAtom(aSrv
);
200 if (aTpc
) GlobalDeleteAtom(aTpc
);
204 /*****************************************************************
205 * DdeReconnect (DDEML.37)
206 * DdeReconnect (USER32.@)
208 HCONV WINAPI
DdeReconnect(HCONV hConv
)
211 WDML_CONV
* pNewConv
= NULL
;
212 ATOM aSrv
= 0, aTpc
= 0;
214 TRACE("(%p)\n", hConv
);
216 EnterCriticalSection(&WDML_CritSect
);
217 pConv
= WDML_GetConv(hConv
, FALSE
);
218 if (pConv
!= NULL
&& (pConv
->wStatus
& ST_CLIENT
))
222 /* to reestablist a connection, we have to make sure that:
223 * 1/ pConv is the converstation attached to the client window (it wouldn't be
224 * if a call to DdeReconnect would have already been done...)
225 * FIXME: is this really an error ???
226 * 2/ the pConv conversation had really been deconnected
228 if (pConv
== WDML_GetConvFromWnd(pConv
->hwndClient
) &&
229 (pConv
->wStatus
& ST_TERMINATED
) && !(pConv
->wStatus
& ST_CONNECTED
))
231 HWND hwndClient
= pConv
->hwndClient
;
232 HWND hwndServer
= pConv
->hwndServer
;
235 SetWindowLongPtrW(pConv
->hwndClient
, GWL_WDML_CONVERSATION
, 0);
237 aSrv
= WDML_MakeAtomFromHsz(pConv
->hszService
);
238 aTpc
= WDML_MakeAtomFromHsz(pConv
->hszTopic
);
239 if (!aSrv
|| !aTpc
) goto theEnd
;
241 LeaveCriticalSection(&WDML_CritSect
);
243 /* note: sent messages shall not use packing */
244 ret
= SendMessageW(hwndServer
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
,
245 MAKELPARAM(aSrv
, aTpc
));
247 EnterCriticalSection(&WDML_CritSect
);
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
);
280 LeaveCriticalSection(&WDML_CritSect
);
282 if (aSrv
) GlobalDeleteAtom(aSrv
);
283 if (aTpc
) GlobalDeleteAtom(aTpc
);
284 return (HCONV
)pNewConv
;
287 /******************************************************************
288 * WDML_ClientQueueAdvise
290 * Creates and queue an WM_DDE_ADVISE transaction
292 static WDML_XACT
* WDML_ClientQueueAdvise(WDML_CONV
* pConv
, UINT wType
, UINT wFmt
, HSZ hszItem
)
294 DDEADVISE
* pDdeAdvise
;
298 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType
& XTYPF_NODATA
) ? "out" : "");
300 atom
= WDML_MakeAtomFromHsz(hszItem
);
301 if (!atom
) return NULL
;
303 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_ADVISE
, wFmt
, hszItem
);
306 GlobalDeleteAtom(atom
);
310 pXAct
->wType
= wType
& ~0x0F;
311 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, sizeof(DDEADVISE
));
312 /* FIXME: hMem is unfreed for now... should be deleted in server */
315 pDdeAdvise
= (DDEADVISE
*)GlobalLock(pXAct
->hMem
);
316 pDdeAdvise
->fAckReq
= (wType
& XTYPF_ACKREQ
) ? TRUE
: FALSE
;
317 pDdeAdvise
->fDeferUpd
= (wType
& XTYPF_NODATA
) ? TRUE
: FALSE
;
318 pDdeAdvise
->cfFormat
= wFmt
;
319 GlobalUnlock(pXAct
->hMem
);
321 pXAct
->lParam
= PackDDElParam(WM_DDE_ADVISE
, (UINT_PTR
)pXAct
->hMem
, atom
);
326 /******************************************************************
327 * WDML_HandleAdviseReply
329 * handles the reply to an advise request
331 static WDML_QUEUE_STATE
WDML_HandleAdviseReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
337 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
342 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
343 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
345 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
348 GlobalDeleteAtom(uiHi
);
349 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
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
)
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 WDML_ExtractAck(uiLo
, &ddeAck
);
438 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
442 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
443 pXAct
->hDdeData
= NULL
;
447 /* billx: remove the link */
448 WDML_RemoveLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
449 pXAct
->hszItem
, pXAct
->wFmt
);
450 pXAct
->hDdeData
= (HDDEDATA
)1;
452 return WDML_QS_HANDLED
;
455 /******************************************************************
456 * WDML_ClientQueueRequest
460 static WDML_XACT
* WDML_ClientQueueRequest(WDML_CONV
* pConv
, UINT wFmt
, HSZ hszItem
)
465 TRACE("XTYP_REQUEST transaction\n");
467 atom
= WDML_MakeAtomFromHsz(hszItem
);
468 if (!atom
) return NULL
;
470 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_REQUEST
, wFmt
, hszItem
);
473 GlobalDeleteAtom(atom
);
477 pXAct
->lParam
= PackDDElParam(WM_DDE_REQUEST
, wFmt
, atom
);
482 /******************************************************************
483 * WDML_HandleRequestReply
487 static WDML_QUEUE_STATE
WDML_HandleRequestReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
494 if (WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
497 switch (msg
->message
)
500 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
501 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
502 GlobalDeleteAtom(uiHi
);
503 WDML_ExtractAck(uiLo
, &ddeAck
);
506 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
507 TRACE("Negative answer...\n");
511 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
512 TRACE("Got the result (%08x)\n", uiLo
);
514 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
516 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
519 pXAct
->hDdeData
= WDML_Global2DataHandle((HGLOBAL
)uiLo
, &wdh
);
522 GlobalFree((HGLOBAL
)uiLo
);
526 WDML_PostAck(pConv
, WDML_CLIENT_SIDE
, 0, FALSE
, TRUE
, uiHi
, msg
->lParam
, WM_DDE_DATA
);
530 GlobalDeleteAtom(uiHi
);
531 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
536 FreeDDElParam(msg
->message
, msg
->lParam
);
540 return WDML_QS_HANDLED
;
543 /******************************************************************
544 * WDML_BuildExecuteCommand
546 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
547 * It also takes care of string conversion between the two window procedures
549 static HGLOBAL
WDML_BuildExecuteCommand(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
)
552 BOOL clientUnicode
, serverUnicode
;
555 clientUnicode
= IsWindowUnicode(pConv
->hwndClient
);
556 serverUnicode
= IsWindowUnicode(pConv
->hwndServer
);
558 if (clientUnicode
== serverUnicode
)
566 memSize
= WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
, NULL
, 0, NULL
, NULL
);
570 memSize
= MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, NULL
, 0) * sizeof(WCHAR
);
574 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, memSize
);
580 pDst
= GlobalLock(hMem
);
583 if (clientUnicode
== serverUnicode
)
585 memcpy(pDst
, pData
, cbData
);
591 WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
, pDst
, memSize
, NULL
, NULL
);
595 MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, (LPWSTR
)pDst
, memSize
/sizeof(WCHAR
));
610 /******************************************************************
611 * WDML_ClientQueueExecute
615 static WDML_XACT
* WDML_ClientQueueExecute(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
)
619 TRACE("XTYP_EXECUTE transaction\n");
621 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_EXECUTE
, 0, 0);
625 if (cbData
== (DWORD
)-1)
627 HDDEDATA hDdeData
= (HDDEDATA
)pData
;
629 pData
= DdeAccessData(hDdeData
, &cbData
);
632 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
633 DdeUnaccessData(hDdeData
);
638 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
641 pXAct
->lParam
= (LPARAM
)pXAct
->hMem
;
646 /******************************************************************
647 * WDML_HandleExecuteReply
651 static WDML_QUEUE_STATE
WDML_HandleExecuteReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
656 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
661 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
662 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
664 if ((HANDLE
)uiHi
!= pXAct
->hMem
)
669 WDML_ExtractAck(uiLo
, &ddeAck
);
670 pXAct
->hDdeData
= (HDDEDATA
)(UINT_PTR
)ddeAck
.fAck
;
672 TRACE("hDdeData = %p\n", pXAct
->hDdeData
);
673 pConv
->instance
->lastError
= (pXAct
->hDdeData
!= 0) ? DMLERR_NO_ERROR
: DMLERR_NOTPROCESSED
;
675 return WDML_QS_HANDLED
;
678 /******************************************************************
679 * WDML_ClientQueuePoke
683 static WDML_XACT
* WDML_ClientQueuePoke(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
,
684 UINT wFmt
, HSZ hszItem
)
689 TRACE("XTYP_POKE transaction\n");
691 atom
= WDML_MakeAtomFromHsz(hszItem
);
692 if (!atom
) return NULL
;
694 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_POKE
, wFmt
, hszItem
);
697 GlobalDeleteAtom(atom
);
701 if (cbData
== (DWORD
)-1)
703 pXAct
->hMem
= (HDDEDATA
)pData
;
709 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, sizeof(DDEPOKE
) + cbData
);
710 ddePoke
= GlobalLock(pXAct
->hMem
);
713 memcpy(ddePoke
->Value
, pData
, cbData
);
714 ddePoke
->fRelease
= FALSE
; /* FIXME: app owned ? */
715 ddePoke
->cfFormat
= wFmt
;
716 GlobalUnlock(pXAct
->hMem
);
720 pXAct
->lParam
= PackDDElParam(WM_DDE_POKE
, (UINT_PTR
)pXAct
->hMem
, atom
);
725 /******************************************************************
726 * WDML_HandlePokeReply
730 static WDML_QUEUE_STATE
WDML_HandlePokeReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
736 if (msg
->message
!= WM_DDE_ACK
&& WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
741 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
742 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
743 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
747 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
748 GlobalDeleteAtom(uiHi
);
750 WDML_ExtractAck(uiLo
, &ddeAck
);
751 GlobalFree(pXAct
->hMem
);
753 pXAct
->hDdeData
= (HDDEDATA
)TRUE
;
757 /******************************************************************
758 * WDML_ClientQueueTerminate
760 * Creates and queue an WM_DDE_TERMINATE transaction
762 static WDML_XACT
* WDML_ClientQueueTerminate(WDML_CONV
* pConv
)
766 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_TERMINATE
, 0, 0);
771 pConv
->wStatus
&= ~ST_CONNECTED
;
776 /******************************************************************
777 * WDML_HandleTerminateReply
779 * handles the reply to a terminate request
781 static WDML_QUEUE_STATE
WDML_HandleTerminateReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
783 if (msg
->message
!= WM_DDE_TERMINATE
)
785 /* FIXME: should delete data passed here */
786 return WDML_QS_SWALLOWED
;
789 if (WIN_GetFullHandle((HWND
)msg
->wParam
) != pConv
->hwndServer
)
791 FIXME("hmmm shouldn't happen\n");
794 if (!pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
)
796 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
,
797 0, 0, 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
799 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
800 return WDML_QS_HANDLED
;
803 /******************************************************************
804 * WDML_HandleReplyData
808 static WDML_QUEUE_STATE
WDML_HandleIncomingData(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
811 HDDEDATA hDdeDataIn
, hDdeDataOut
;
816 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
817 /* wParam -- sending window handle */
818 /* lParam -- hDdeData & item HSZ */
820 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
821 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
823 hDdeDataIn
= WDML_Global2DataHandle((HGLOBAL
)uiLo
, &wdh
);
826 * For hot link, data should be passed to its callback with
827 * XTYP_ADVDATA and callback should return the proper status.
829 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
, hsz
,
830 uiLo
? TRUE
: FALSE
, wdh
.cfFormat
);
833 WDML_DecHSZ(pConv
->instance
, hsz
);
834 DdeFreeDataHandle(hDdeDataIn
);
838 if (hDdeDataIn
!= 0 && wdh
.fAckReq
)
840 WDML_PostAck(pConv
, WDML_CLIENT_SIDE
, 0, FALSE
, TRUE
, uiHi
, msg
->lParam
, WM_DDE_DATA
);
846 GlobalDeleteAtom(uiHi
);
849 hDdeDataOut
= WDML_InvokeCallback(pConv
->instance
, XTYP_ADVDATA
, pLink
->uFmt
, pLink
->hConv
,
850 pConv
->hszTopic
, pLink
->hszItem
, hDdeDataIn
, 0, 0);
852 if (hDdeDataOut
!= (HDDEDATA
)DDE_FACK
|| wdh
.fRelease
)
854 if (uiLo
) GlobalFree((HANDLE
)uiLo
);
857 DdeFreeDataHandle(hDdeDataIn
);
859 WDML_DecHSZ(pConv
->instance
, hsz
);
861 FreeDDElParam(WM_DDE_DATA
, msg
->lParam
);
863 return WDML_QS_HANDLED
;
866 /******************************************************************
867 * WDML_HandleIncomingTerminate
871 static WDML_QUEUE_STATE
WDML_HandleIncomingTerminate(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
873 if (pConv
->hwndServer
!= WIN_GetFullHandle((HWND
)msg
->wParam
))
876 pConv
->wStatus
|= ST_TERMINATED
;
877 if (!pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
)
879 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
,
880 0, 0, 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
882 if (pConv
->wStatus
& ST_CONNECTED
)
884 /* don't care about result code (if server exists or not) */
885 PostMessageW(pConv
->hwndServer
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndClient
, 0L);
886 pConv
->wStatus
&= ~ST_CONNECTED
;
888 /* have to keep connection around to allow reconnection */
889 return WDML_QS_HANDLED
;
892 /******************************************************************
895 * handles any incoming reply, and try to match to an already sent request
897 static WDML_QUEUE_STATE
WDML_HandleReply(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
899 WDML_XACT
* pXAct
= pConv
->transactions
;
902 if (pConv
->transactions
)
904 /* first check message against a pending transaction, if any */
905 switch (pXAct
->ddeMsg
)
908 qs
= WDML_HandleAdviseReply(pConv
, msg
, pXAct
);
910 case WM_DDE_UNADVISE
:
911 qs
= WDML_HandleUnadviseReply(pConv
, msg
, pXAct
);
914 qs
= WDML_HandleExecuteReply(pConv
, msg
, pXAct
);
917 qs
= WDML_HandleRequestReply(pConv
, msg
, pXAct
);
920 qs
= WDML_HandlePokeReply(pConv
, msg
, pXAct
);
922 case WM_DDE_TERMINATE
:
923 qs
= WDML_HandleTerminateReply(pConv
, msg
, pXAct
);
935 /* now check the results */
939 case WDML_QS_SWALLOWED
:
942 case WDML_QS_HANDLED
:
943 /* ok, we have resolved a pending transaction
944 * notify callback if asynchronous, and remove it in any case
946 WDML_UnQueueTransaction(pConv
, pXAct
);
947 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&& pXAct
->ddeMsg
!= WM_DDE_TERMINATE
)
949 WDML_InvokeCallback(pConv
->instance
, XTYP_XACT_COMPLETE
, pXAct
->wFmt
,
950 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
,
951 pXAct
->hDdeData
, MAKELONG(0, pXAct
->xActID
), 0 /* FIXME */);
956 *hdd
= pXAct
->hDdeData
;
958 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
961 /* no pending transaction found, try a warm/hot link or a termination request */
962 switch (msg
->message
)
965 qs
= WDML_HandleIncomingData(pConv
, msg
, hdd
);
967 case WM_DDE_TERMINATE
:
968 qs
= WDML_HandleIncomingTerminate(pConv
, msg
, hdd
);
973 FIXME("shouldn't be used on client side\n");
980 /******************************************************************
981 * WDML_SyncWaitTransactionReply
983 * waits until an answer for a sent request is received
984 * time out is also handled. only used for synchronous transactions
986 static HDDEDATA
WDML_SyncWaitTransactionReply(HCONV hConv
, DWORD dwTimeout
, WDML_XACT
* pXAct
)
992 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout
);
994 /* FIXME: time 32 bit wrap around */
995 dwTimeout
+= GetCurrentTime();
997 while ((dwTime
= GetCurrentTime()) < dwTimeout
)
999 /* we cannot be in the crit sect all the time because when client and server run in a
1000 * single process they need to share the access to the internal data
1002 if (MsgWaitForMultipleObjects(0, NULL
, FALSE
,
1003 dwTimeout
- dwTime
, QS_POSTMESSAGE
) == WAIT_OBJECT_0
)
1010 EnterCriticalSection(&WDML_CritSect
);
1012 pConv
= WDML_GetConv(hConv
, FALSE
);
1015 LeaveCriticalSection(&WDML_CritSect
);
1016 /* conversation no longer available... return failure */
1019 while (PeekMessageW(&msg
, pConv
->hwndClient
, WM_DDE_FIRST
, WM_DDE_LAST
, PM_REMOVE
))
1021 /* check that either pXAct has been processed or no more xActions are pending */
1022 ret
= (pConv
->transactions
== pXAct
);
1023 ret
= WDML_HandleReply(pConv
, &msg
, &hdd
) == WDML_QS_HANDLED
&&
1024 (pConv
->transactions
== NULL
|| ret
);
1027 LeaveCriticalSection(&WDML_CritSect
);
1035 TRACE("Timeout !!\n");
1037 EnterCriticalSection(&WDML_CritSect
);
1039 pConv
= WDML_GetConv(hConv
, FALSE
);
1042 if (pConv
->transactions
)
1044 switch (pConv
->transactions
->ddeMsg
)
1046 case WM_DDE_ADVISE
: err
= DMLERR_ADVACKTIMEOUT
; break;
1047 case WM_DDE_REQUEST
: err
= DMLERR_DATAACKTIMEOUT
; break;
1048 case WM_DDE_EXECUTE
: err
= DMLERR_EXECACKTIMEOUT
; break;
1049 case WM_DDE_POKE
: err
= DMLERR_POKEACKTIMEOUT
; break;
1050 case WM_DDE_UNADVISE
: err
= DMLERR_UNADVACKTIMEOUT
; break;
1051 default: err
= DMLERR_INVALIDPARAMETER
; break;
1054 pConv
->instance
->lastError
= err
;
1057 LeaveCriticalSection(&WDML_CritSect
);
1062 /*****************************************************************
1063 * DdeClientTransaction (USER32.@)
1065 HDDEDATA WINAPI
DdeClientTransaction(LPBYTE pData
, DWORD cbData
, HCONV hConv
, HSZ hszItem
, UINT wFmt
,
1066 UINT wType
, DWORD dwTimeout
, LPDWORD pdwResult
)
1070 HDDEDATA hDdeData
= 0;
1072 TRACE("(%p,%ld,%p,%p,%x,%x,%ld,%p)\n",
1073 pData
, cbData
, hConv
, hszItem
, wFmt
, wType
, dwTimeout
, pdwResult
);
1077 ERR("Invalid conversation handle\n");
1081 EnterCriticalSection(&WDML_CritSect
);
1083 pConv
= WDML_GetConv(hConv
, TRUE
);
1086 /* cannot set error... cannot get back to DDE instance */
1093 if (hszItem
!= 0 || wFmt
!= 0)
1095 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1098 pXAct
= WDML_ClientQueueExecute(pConv
, pData
, cbData
);
1101 pXAct
= WDML_ClientQueuePoke(pConv
, pData
, cbData
, wFmt
, hszItem
);
1103 case XTYP_ADVSTART
|XTYPF_NODATA
:
1104 case XTYP_ADVSTART
|XTYPF_NODATA
|XTYPF_ACKREQ
:
1106 case XTYP_ADVSTART
|XTYPF_ACKREQ
:
1109 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1112 pXAct
= WDML_ClientQueueAdvise(pConv
, wType
, wFmt
, hszItem
);
1117 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1120 pXAct
= WDML_ClientQueueUnadvise(pConv
, wFmt
, hszItem
);
1125 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1128 pXAct
= WDML_ClientQueueRequest(pConv
, wFmt
, hszItem
);
1131 FIXME("Unknown transation\n");
1132 /* unknown transaction type */
1133 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1139 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
1143 WDML_QueueTransaction(pConv
, pXAct
);
1145 if (!PostMessageW(pConv
->hwndServer
, pXAct
->ddeMsg
, (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1147 WARN("Failed posting message %x to %p (error=0x%lx)\n",
1148 pXAct
->ddeMsg
, pConv
->hwndServer
, GetLastError());
1149 pConv
->wStatus
&= ~ST_CONNECTED
;
1150 WDML_UnQueueTransaction(pConv
, pXAct
);
1151 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1154 pXAct
->dwTimeout
= dwTimeout
;
1155 /* FIXME: should set the app bits on *pdwResult */
1157 if (dwTimeout
== TIMEOUT_ASYNC
)
1161 *pdwResult
= MAKELONG(0, pXAct
->xActID
);
1163 hDdeData
= (HDDEDATA
)1;
1173 count
= WDML_CritSect
.RecursionCount
;
1174 for (i
= 0; i
< count
; i
++)
1175 LeaveCriticalSection(&WDML_CritSect
);
1176 hDdeData
= WDML_SyncWaitTransactionReply((HCONV
)pConv
, dwTimeout
, pXAct
);
1177 for (i
= 0; i
< count
; i
++)
1178 EnterCriticalSection(&WDML_CritSect
);
1180 LeaveCriticalSection(&WDML_CritSect
);
1184 LeaveCriticalSection(&WDML_CritSect
);
1188 /*****************************************************************
1189 * DdeAbandonTransaction (USER32.@)
1191 BOOL WINAPI
DdeAbandonTransaction(DWORD idInst
, HCONV hConv
, DWORD idTransaction
)
1193 WDML_INSTANCE
* pInstance
;
1197 TRACE("(%08lx,%p,%08ld);\n", idInst
, hConv
, idTransaction
);
1199 EnterCriticalSection(&WDML_CritSect
);
1200 if ((pInstance
= WDML_GetInstance(idInst
)))
1204 if ((pConv
= WDML_GetConv(hConv
, TRUE
)) && pConv
->instance
== pInstance
)
1206 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1208 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&&
1209 (idTransaction
== 0 || pXAct
->xActID
== idTransaction
))
1211 WDML_UnQueueTransaction(pConv
, pXAct
);
1212 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1219 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
; pConv
= pConv
->next
)
1221 if (!(pConv
->wStatus
& ST_CONNECTED
)) continue;
1222 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1224 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
)
1226 WDML_UnQueueTransaction(pConv
, pXAct
);
1227 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1233 LeaveCriticalSection(&WDML_CritSect
);
1238 /******************************************************************
1241 * Window Proc created on client side for each conversation
1243 static LRESULT CALLBACK
WDML_ClientProc(HWND hwnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
1246 WDML_CONV
* pConv
= NULL
;
1249 TRACE("%p %04x %08x %08lx\n", hwnd
, iMsg
, wParam
, lParam
);
1251 if (iMsg
== WM_DDE_ACK
&&
1252 /* in the initial WM_INITIATE sendmessage */
1253 ((pConv
= WDML_GetConvFromWnd(hwnd
)) == NULL
|| pConv
->wStatus
== XST_INIT1
))
1255 /* In response to WM_DDE_INITIATE, save server window */
1257 WDML_INSTANCE
* pInstance
;
1259 /* note: sent messages do not need packing */
1260 uiLo
= LOWORD(lParam
);
1261 uiHi
= HIWORD(lParam
);
1263 /* FIXME: convlist should be handled here */
1266 /* we already have started the conv with a server, drop other replies */
1267 GlobalDeleteAtom(uiLo
);
1268 GlobalDeleteAtom(uiHi
);
1269 PostMessageW((HWND
)wParam
, WM_DDE_TERMINATE
, (WPARAM
)hwnd
, 0);
1273 pInstance
= WDML_GetInstanceFromWnd(hwnd
);
1275 hszSrv
= WDML_MakeHszFromAtom(pInstance
, uiLo
);
1276 hszTpc
= WDML_MakeHszFromAtom(pInstance
, uiHi
);
1278 pConv
= WDML_AddConv(pInstance
, WDML_CLIENT_SIDE
, hszSrv
, hszTpc
, hwnd
, (HWND
)wParam
);
1280 SetWindowLongPtrW(hwnd
, GWL_WDML_CONVERSATION
, (ULONG_PTR
)pConv
);
1281 pConv
->wStatus
|= ST_CONNECTED
;
1282 pConv
->wConvst
= XST_INIT1
;
1284 /* check if server is handled by DDEML */
1285 if (GetClassNameW((HWND
)wParam
, buf
, sizeof(buf
)/sizeof(WCHAR
)) &&
1286 lstrcmpiW(buf
, WDML_szServerConvClass
) == 0)
1288 pConv
->wStatus
|= ST_ISLOCAL
;
1291 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_CONNECT_CONFIRM
, (WPARAM
)hwnd
, wParam
);
1293 GlobalDeleteAtom(uiLo
);
1294 GlobalDeleteAtom(uiHi
);
1296 /* accept conversation */
1300 if (iMsg
>= WM_DDE_FIRST
&& iMsg
<= WM_DDE_LAST
)
1302 EnterCriticalSection(&WDML_CritSect
);
1304 pConv
= WDML_GetConvFromWnd(hwnd
);
1313 msg
.wParam
= wParam
;
1314 msg
.lParam
= lParam
;
1316 WDML_HandleReply(pConv
, &msg
, &hdd
);
1319 LeaveCriticalSection(&WDML_CritSect
);
1323 return DefWindowProcW(hwnd
, iMsg
, wParam
, lParam
);
1326 /*****************************************************************
1327 * DdeDisconnect (USER32.@)
1329 BOOL WINAPI
DdeDisconnect(HCONV hConv
)
1331 WDML_CONV
* pConv
= NULL
;
1336 TRACE("(%p)\n", hConv
);
1340 ERR("DdeDisconnect(): hConv = 0\n");
1344 EnterCriticalSection(&WDML_CritSect
);
1345 pConv
= WDML_GetConv(hConv
, TRUE
);
1348 if (pConv
->wStatus
& ST_CLIENT
)
1350 /* FIXME: should abandon all pending transactions */
1351 pXAct
= WDML_ClientQueueTerminate(pConv
);
1354 count
= WDML_CritSect
.RecursionCount
;
1355 for (i
= 0; i
< count
; i
++)
1356 LeaveCriticalSection(&WDML_CritSect
);
1357 if (PostMessageW(pConv
->hwndServer
, pXAct
->ddeMsg
,
1358 (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1359 WDML_SyncWaitTransactionReply(hConv
, 10000, pXAct
);
1360 for (i
= 0; i
< count
; i
++)
1361 EnterCriticalSection(&WDML_CritSect
);
1363 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1364 /* still have to destroy data assosiated with conversation */
1365 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
1369 FIXME("Not implemented yet for a server side conversation\n");
1373 LeaveCriticalSection(&WDML_CritSect
);
1378 /*****************************************************************
1379 * DdeImpersonateClient (USER32.@)
1381 BOOL WINAPI
DdeImpersonateClient(HCONV hConv
)
1386 TRACE("(%p)\n", hConv
);
1388 EnterCriticalSection(&WDML_CritSect
);
1389 pConv
= WDML_GetConv(hConv
, TRUE
);
1392 ret
= ImpersonateDdeClientWindow(pConv
->hwndClient
, pConv
->hwndServer
);
1394 LeaveCriticalSection(&WDML_CritSect
);