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
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "wine/debug.h"
38 #include "dde/dde_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
42 static LRESULT CALLBACK
WDML_ClientProc(HWND
, UINT
, WPARAM
, LPARAM
); /* only for one client, not conv list */
43 const char WDML_szClientConvClassA
[] = "DdeClientAnsi";
44 const WCHAR WDML_szClientConvClassW
[] = {'D','d','e','C','l','i','e','n','t','U','n','i','c','o','d','e',0};
46 /******************************************************************************
47 * DdeConnectList [USER32.@] Establishes conversation with DDE servers
50 * idInst [I] Instance identifier
51 * hszService [I] Handle to service name string
52 * hszTopic [I] Handle to topic name string
53 * hConvList [I] Handle to conversation list
54 * pCC [I] Pointer to structure with context data
57 * Success: Handle to new conversation list
60 HCONVLIST WINAPI
DdeConnectList(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
61 HCONVLIST hConvList
, LPCONVCONTEXT pCC
)
63 FIXME("(%ld,%d,%d,%d,%p): stub\n", idInst
, hszService
, hszTopic
, hConvList
, pCC
);
67 /*****************************************************************
68 * DdeQueryNextServer [USER32.@]
70 HCONV WINAPI
DdeQueryNextServer(HCONVLIST hConvList
, HCONV hConvPrev
)
72 FIXME("(%d,%d): stub\n", hConvList
, hConvPrev
);
76 /******************************************************************************
77 * DdeDisconnectList [USER32.@] Destroys list and terminates conversations
81 * hConvList [I] Handle to conversation list
87 BOOL WINAPI
DdeDisconnectList(HCONVLIST hConvList
)
89 FIXME("(%d): stub\n", hConvList
);
93 /*****************************************************************
94 * DdeConnect (USER32.@)
96 HCONV WINAPI
DdeConnect(DWORD idInst
, HSZ hszService
, HSZ hszTopic
,
100 WDML_INSTANCE
* pInstance
;
101 WDML_CONV
* pConv
= NULL
;
102 ATOM aSrv
= 0, aTpc
= 0;
104 TRACE("(0x%lx,0x%x,0x%x,%p)\n", idInst
, hszService
, hszTopic
, pCC
);
106 EnterCriticalSection(&WDML_CritSect
);
108 pInstance
= WDML_GetInstance(idInst
);
114 /* make sure this conv is never created */
115 pConv
= WDML_FindConv(pInstance
, WDML_CLIENT_SIDE
, hszService
, hszTopic
);
118 ERR("This Conv already exists: (0x%lx)\n", (DWORD
)pConv
);
122 /* we need to establish a conversation with
123 server, so create a window for it */
125 if (pInstance
->unicode
)
127 WNDCLASSEXW wndclass
;
129 wndclass
.cbSize
= sizeof(wndclass
);
131 wndclass
.lpfnWndProc
= WDML_ClientProc
;
132 wndclass
.cbClsExtra
= 0;
133 wndclass
.cbWndExtra
= 2 * sizeof(DWORD
);
134 wndclass
.hInstance
= 0;
136 wndclass
.hCursor
= 0;
137 wndclass
.hbrBackground
= 0;
138 wndclass
.lpszMenuName
= NULL
;
139 wndclass
.lpszClassName
= WDML_szClientConvClassW
;
140 wndclass
.hIconSm
= 0;
142 RegisterClassExW(&wndclass
);
144 hwndClient
= CreateWindowW(WDML_szClientConvClassW
, NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
148 WNDCLASSEXA wndclass
;
150 wndclass
.cbSize
= sizeof(wndclass
);
152 wndclass
.lpfnWndProc
= WDML_ClientProc
;
153 wndclass
.cbClsExtra
= 0;
154 wndclass
.cbWndExtra
= 2 * sizeof(DWORD
);
155 wndclass
.hInstance
= 0;
157 wndclass
.hCursor
= 0;
158 wndclass
.hbrBackground
= 0;
159 wndclass
.lpszMenuName
= NULL
;
160 wndclass
.lpszClassName
= WDML_szClientConvClassA
;
161 wndclass
.hIconSm
= 0;
163 RegisterClassExA(&wndclass
);
165 hwndClient
= CreateWindowA(WDML_szClientConvClassA
, NULL
, WS_POPUP
, 0, 0, 0, 0, 0, 0, 0, 0);
168 SetWindowLongA(hwndClient
, GWL_WDML_INSTANCE
, (DWORD
)pInstance
);
172 aSrv
= WDML_MakeAtomFromHsz(hszService
);
173 if (!aSrv
) goto theEnd
;
177 aTpc
= WDML_MakeAtomFromHsz(hszTopic
);
178 if (!aTpc
) goto theEnd
;
181 LeaveCriticalSection(&WDML_CritSect
);
183 /* note: sent messages shall not use packing */
184 SendMessageA(HWND_BROADCAST
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
, MAKELPARAM(aSrv
, aTpc
));
186 EnterCriticalSection(&WDML_CritSect
);
188 pInstance
= WDML_GetInstance(idInst
);
194 /* At this point, Client WM_DDE_ACK should have saved hwndServer
195 for this instance id and hwndClient if server responds.
196 So get HCONV and return it. And add it to conv list */
197 pConv
= WDML_GetConvFromWnd(hwndClient
);
198 if (pConv
== NULL
|| pConv
->hwndServer
== 0)
200 ERR("Done with INITIATE, but no Server window available\n");
204 TRACE("Connected to Server window (%x)\n", pConv
->hwndServer
);
205 pConv
->wConvst
= XST_CONNECTED
;
207 /* finish init of pConv */
210 pConv
->convContext
= *pCC
;
214 memset(&pConv
->convContext
, 0, sizeof(pConv
->convContext
));
215 pConv
->convContext
.cb
= sizeof(pConv
->convContext
);
216 pConv
->convContext
.iCodePage
= (pInstance
->unicode
) ? CP_WINUNICODE
: CP_WINANSI
;
220 LeaveCriticalSection(&WDML_CritSect
);
222 if (aSrv
) GlobalDeleteAtom(aSrv
);
223 if (aTpc
) GlobalDeleteAtom(aTpc
);
227 /*****************************************************************
228 * DdeReconnect (DDEML.37)
229 * DdeReconnect (USER32.@)
231 HCONV WINAPI
DdeReconnect(HCONV hConv
)
234 WDML_CONV
* pNewConv
= NULL
;
235 ATOM aSrv
= 0, aTpc
= 0;
237 EnterCriticalSection(&WDML_CritSect
);
238 pConv
= WDML_GetConv(hConv
, FALSE
);
239 if (pConv
!= NULL
&& (pConv
->wStatus
& ST_CLIENT
))
243 /* to reestablist a connection, we have to make sure that:
244 * 1/ pConv is the converstation attached to the client window (it wouldn't be
245 * if a call to DdeReconnect would have already been done...)
246 * FIXME: is this really an error ???
247 * 2/ the pConv conversation had really been deconnected
249 if (pConv
== WDML_GetConvFromWnd(pConv
->hwndClient
) &&
250 (pConv
->wStatus
& ST_TERMINATED
) && !(pConv
->wStatus
& ST_CONNECTED
))
252 HWND hwndClient
= pConv
->hwndClient
;
253 HWND hwndServer
= pConv
->hwndServer
;
256 SetWindowLongA(pConv
->hwndClient
, GWL_WDML_CONVERSATION
, 0);
258 aSrv
= WDML_MakeAtomFromHsz(pConv
->hszService
);
259 aTpc
= WDML_MakeAtomFromHsz(pConv
->hszTopic
);
260 if (!aSrv
|| !aTpc
) goto theEnd
;
262 LeaveCriticalSection(&WDML_CritSect
);
264 /* note: sent messages shall not use packing */
265 ret
= SendMessageA(hwndServer
, WM_DDE_INITIATE
, (WPARAM
)hwndClient
,
266 MAKELPARAM(aSrv
, aTpc
));
268 EnterCriticalSection(&WDML_CritSect
);
270 pConv
= WDML_GetConv(hConv
, FALSE
);
273 FIXME("Should fail reconnection\n");
277 if (ret
&& (pNewConv
= WDML_GetConvFromWnd(pConv
->hwndClient
)) != NULL
)
279 /* re-establish all links... */
282 for (pLink
= pConv
->instance
->links
[WDML_CLIENT_SIDE
]; pLink
; pLink
= pLink
->next
)
284 if (pLink
->hConv
== hConv
)
286 /* try to reestablish the links... */
287 DdeClientTransaction(NULL
, 0, (HCONV
)pNewConv
, pLink
->hszItem
, pLink
->uFmt
,
288 pLink
->transactionType
, 1000, NULL
);
294 /* reset the conversation as it was */
295 SetWindowLongA(pConv
->hwndClient
, GWL_WDML_CONVERSATION
, (DWORD
)pConv
);
301 LeaveCriticalSection(&WDML_CritSect
);
303 if (aSrv
) GlobalDeleteAtom(aSrv
);
304 if (aTpc
) GlobalDeleteAtom(aTpc
);
305 return (HCONV
)pNewConv
;
308 /******************************************************************
309 * WDML_ClientQueueAdvise
311 * Creates and queue an WM_DDE_ADVISE transaction
313 static WDML_XACT
* WDML_ClientQueueAdvise(WDML_CONV
* pConv
, UINT wType
, UINT wFmt
, HSZ hszItem
)
315 DDEADVISE
* pDdeAdvise
;
319 TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType
& XTYPF_NODATA
) ? "out" : "");
321 atom
= WDML_MakeAtomFromHsz(hszItem
);
322 if (!atom
) return NULL
;
324 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_ADVISE
, wFmt
, hszItem
);
327 GlobalDeleteAtom(atom
);
331 pXAct
->wType
= wType
& ~0x0F;
332 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, sizeof(DDEADVISE
));
333 /* FIXME: hMem is unfreed for now... should be deleted in server */
336 pDdeAdvise
= (DDEADVISE
*)GlobalLock(pXAct
->hMem
);
337 pDdeAdvise
->fAckReq
= (wType
& XTYPF_ACKREQ
) ? TRUE
: FALSE
;
338 pDdeAdvise
->fDeferUpd
= (wType
& XTYPF_NODATA
) ? TRUE
: FALSE
;
339 pDdeAdvise
->cfFormat
= wFmt
;
340 GlobalUnlock(pXAct
->hMem
);
342 pXAct
->lParam
= PackDDElParam(WM_DDE_ADVISE
, (UINT
)pXAct
->hMem
, atom
);
347 /******************************************************************
348 * WDML_HandleAdviseReply
350 * handles the reply to an advise request
352 static WDML_QUEUE_STATE
WDML_HandleAdviseReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
358 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
363 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
364 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
366 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
369 GlobalDeleteAtom(uiHi
);
370 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
372 WDML_ExtractAck(uiLo
, &ddeAck
);
378 /* billx: first to see if the link is already created. */
379 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
380 pXAct
->hszItem
, TRUE
, pXAct
->wFmt
);
383 /* we found a link, and only need to modify it in case it changes */
384 pLink
->transactionType
= pXAct
->wType
;
388 WDML_AddLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
389 pXAct
->wType
, pXAct
->hszItem
, pXAct
->wFmt
);
391 pXAct
->hDdeData
= (HDDEDATA
)1;
395 TRACE("Returning FALSE on XTYP_ADVSTART - fAck was FALSE\n");
396 GlobalFree(pXAct
->hMem
);
397 pXAct
->hDdeData
= (HDDEDATA
)0;
400 return WDML_QS_HANDLED
;
403 /******************************************************************
404 * WDML_ClientQueueUnadvise
406 * queues an unadvise transaction
408 static WDML_XACT
* WDML_ClientQueueUnadvise(WDML_CONV
* pConv
, UINT wFmt
, HSZ hszItem
)
413 TRACE("XTYP_ADVSTOP transaction\n");
415 atom
= WDML_MakeAtomFromHsz(hszItem
);
416 if (!atom
) return NULL
;
418 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_UNADVISE
, wFmt
, hszItem
);
421 GlobalDeleteAtom(atom
);
425 /* end advise loop: post WM_DDE_UNADVISE to server to terminate link
426 * on the specified item.
428 pXAct
->lParam
= PackDDElParam(WM_DDE_UNADVISE
, wFmt
, atom
);
432 /******************************************************************
433 * WDML_HandleUnadviseReply
437 static WDML_QUEUE_STATE
WDML_HandleUnadviseReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
443 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
448 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
449 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
451 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
454 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
455 GlobalDeleteAtom(uiHi
);
457 WDML_ExtractAck(uiLo
, &ddeAck
);
459 TRACE("WM_DDE_ACK received while waiting for a timeout\n");
463 TRACE("Returning FALSE on XTYP_ADVSTOP - fAck was FALSE\n");
464 pXAct
->hDdeData
= (HDDEDATA
)0;
468 /* billx: remove the link */
469 WDML_RemoveLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
,
470 pXAct
->hszItem
, pXAct
->wFmt
);
471 pXAct
->hDdeData
= (HDDEDATA
)1;
473 return WDML_QS_HANDLED
;
476 /******************************************************************
477 * WDML_ClientQueueRequest
481 static WDML_XACT
* WDML_ClientQueueRequest(WDML_CONV
* pConv
, UINT wFmt
, HSZ hszItem
)
486 TRACE("XTYP_REQUEST transaction\n");
488 atom
= WDML_MakeAtomFromHsz(hszItem
);
489 if (!atom
) return NULL
;
491 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_REQUEST
, wFmt
, hszItem
);
494 GlobalDeleteAtom(atom
);
498 pXAct
->lParam
= PackDDElParam(WM_DDE_REQUEST
, wFmt
, atom
);
503 /******************************************************************
504 * WDML_HandleRequestReply
508 static WDML_QUEUE_STATE
WDML_HandleRequestReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
515 if (WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
518 switch (msg
->message
)
521 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
522 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
523 GlobalDeleteAtom(uiHi
);
524 WDML_ExtractAck(uiLo
, &ddeAck
);
527 ERR("Positive answer should appear in NACK for a request, assuming negative\n");
528 TRACE("Negative answer...\n");
532 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
533 TRACE("Got the result (%08lx)\n", (DWORD
)uiLo
);
535 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
537 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
540 pXAct
->hDdeData
= WDML_Global2DataHandle((HGLOBAL
)uiLo
, &wdh
);
543 GlobalFree((HGLOBAL
)uiLo
);
547 WDML_PostAck(pConv
, WDML_CLIENT_SIDE
, 0, FALSE
, TRUE
, uiHi
, msg
->lParam
, WM_DDE_DATA
);
551 GlobalDeleteAtom(uiHi
);
552 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
557 FreeDDElParam(msg
->message
, msg
->lParam
);
561 return WDML_QS_HANDLED
;
564 /******************************************************************
565 * WDML_BuildExecuteCommand
567 * Creates a DDE block suitable for sending in WM_DDE_COMMAND
568 * It also takes care of string conversion between the two window procedures
570 static HGLOBAL
WDML_BuildExecuteCommand(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
)
573 BOOL clientUnicode
, serverUnicode
;
576 clientUnicode
= IsWindowUnicode(pConv
->hwndClient
);
577 serverUnicode
= IsWindowUnicode(pConv
->hwndServer
);
579 if (clientUnicode
== serverUnicode
)
587 memSize
= WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
, NULL
, 0, NULL
, NULL
);
591 memSize
= MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, NULL
, 0);
595 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, memSize
);
601 pDst
= GlobalLock(hMem
);
604 if (clientUnicode
== serverUnicode
)
606 memcpy(pDst
, pData
, cbData
);
612 WideCharToMultiByte( CP_ACP
, 0, pData
, cbData
, pDst
, memSize
, NULL
, NULL
);
616 MultiByteToWideChar( CP_ACP
, 0, pData
, cbData
, (LPWSTR
)pDst
, memSize
);
631 /******************************************************************
632 * WDML_ClientQueueExecute
636 static WDML_XACT
* WDML_ClientQueueExecute(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
)
640 TRACE("XTYP_EXECUTE transaction\n");
642 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_EXECUTE
, 0, 0);
646 if (cbData
== (DWORD
)-1)
648 HDDEDATA hDdeData
= (HDDEDATA
)pData
;
650 pData
= DdeAccessData(hDdeData
, &cbData
);
653 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
654 DdeUnaccessData(hDdeData
);
659 pXAct
->hMem
= WDML_BuildExecuteCommand(pConv
, pData
, cbData
);
662 pXAct
->lParam
= pXAct
->hMem
;
667 /******************************************************************
668 * WDML_HandleExecuteReply
672 static WDML_QUEUE_STATE
WDML_HandleExecuteReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
677 if (msg
->message
!= WM_DDE_ACK
|| WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
682 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
683 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
685 if (uiHi
!= pXAct
->hMem
)
690 WDML_ExtractAck(uiLo
, &ddeAck
);
691 pXAct
->hDdeData
= (HDDEDATA
)ddeAck
.fAck
;
693 return WDML_QS_HANDLED
;
696 /******************************************************************
697 * WDML_ClientQueuePoke
701 static WDML_XACT
* WDML_ClientQueuePoke(WDML_CONV
* pConv
, LPCVOID pData
, DWORD cbData
,
702 UINT wFmt
, HSZ hszItem
)
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)
721 pXAct
->hMem
= (HDDEDATA
)pData
;
727 pXAct
->hMem
= GlobalAlloc(GHND
| GMEM_DDESHARE
, sizeof(DDEPOKE
) + cbData
);
728 ddePoke
= GlobalLock(pXAct
->hMem
);
731 memcpy(ddePoke
->Value
, pData
, cbData
);
732 ddePoke
->fRelease
= FALSE
; /* FIXME: app owned ? */
733 ddePoke
->cfFormat
= wFmt
;
734 GlobalUnlock(pXAct
->hMem
);
738 pXAct
->lParam
= PackDDElParam(WM_DDE_POKE
, pXAct
->hMem
, atom
);
743 /******************************************************************
744 * WDML_HandlePokeReply
748 static WDML_QUEUE_STATE
WDML_HandlePokeReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
754 if (msg
->message
!= WM_DDE_ACK
&& WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
759 UnpackDDElParam(WM_DDE_ACK
, msg
->lParam
, &uiLo
, &uiHi
);
760 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
761 if (DdeCmpStringHandles(hsz
, pXAct
->hszItem
) != 0)
765 FreeDDElParam(WM_DDE_ACK
, msg
->lParam
);
766 GlobalDeleteAtom(uiHi
);
768 WDML_ExtractAck(uiLo
, &ddeAck
);
769 GlobalFree(pXAct
->hMem
);
771 pXAct
->hDdeData
= (HDDEDATA
)TRUE
;
775 /******************************************************************
776 * WDML_ClientQueueTerminate
778 * Creates and queue an WM_DDE_TERMINATE transaction
780 static WDML_XACT
* WDML_ClientQueueTerminate(WDML_CONV
* pConv
)
784 pXAct
= WDML_AllocTransaction(pConv
->instance
, WM_DDE_TERMINATE
, 0, 0);
789 pConv
->wStatus
&= ~ST_CONNECTED
;
794 /******************************************************************
795 * WDML_HandleTerminateReply
797 * handles the reply to a terminate request
799 static WDML_QUEUE_STATE
WDML_HandleTerminateReply(WDML_CONV
* pConv
, MSG
* msg
, WDML_XACT
* pXAct
)
801 if (msg
->message
!= WM_DDE_TERMINATE
)
803 /* FIXME: should delete data passed here */
804 return WDML_QS_SWALLOWED
;
807 if (WIN_GetFullHandle(msg
->wParam
) != pConv
->hwndServer
)
809 FIXME("hmmm shouldn't happen\n");
812 if (!pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
)
814 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
,
815 0, 0, 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
817 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
818 return WDML_QS_HANDLED
;
821 /******************************************************************
822 * WDML_HandleReplyData
826 static WDML_QUEUE_STATE
WDML_HandleIncomingData(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
829 HDDEDATA hDdeDataIn
, hDdeDataOut
;
834 TRACE("WM_DDE_DATA message received in the Client Proc!\n");
835 /* wParam -- sending window handle */
836 /* lParam -- hDdeData & item HSZ */
838 UnpackDDElParam(WM_DDE_DATA
, msg
->lParam
, &uiLo
, &uiHi
);
839 hsz
= WDML_MakeHszFromAtom(pConv
->instance
, uiHi
);
841 hDdeDataIn
= WDML_Global2DataHandle((HGLOBAL
)uiLo
, &wdh
);
844 * For hot link, data should be passed to its callback with
845 * XTYP_ADVDATA and callback should return the proper status.
847 pLink
= WDML_FindLink(pConv
->instance
, (HCONV
)pConv
, WDML_CLIENT_SIDE
, hsz
,
848 uiLo
? TRUE
: FALSE
, wdh
.cfFormat
);
851 WDML_DecHSZ(pConv
->instance
, hsz
);
852 DdeFreeDataHandle(hDdeDataIn
);
856 if (hDdeDataIn
!= 0 && wdh
.fAckReq
)
858 WDML_PostAck(pConv
, WDML_CLIENT_SIDE
, 0, FALSE
, TRUE
, uiHi
, msg
->lParam
, WM_DDE_DATA
);
864 GlobalDeleteAtom(uiHi
);
867 hDdeDataOut
= WDML_InvokeCallback(pConv
->instance
, XTYP_ADVDATA
, pLink
->uFmt
, pLink
->hConv
,
868 pConv
->hszTopic
, pLink
->hszItem
, hDdeDataIn
, 0, 0);
870 if (hDdeDataOut
!= (HDDEDATA
)DDE_FACK
|| wdh
.fRelease
)
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(msg
->wParam
))
897 pConv
->wStatus
|= ST_TERMINATED
;
898 if (!pConv
->instance
->CBFflags
& CBF_SKIP_DISCONNECTS
)
900 WDML_InvokeCallback(pConv
->instance
, XTYP_DISCONNECT
, 0, (HCONV
)pConv
,
901 0, 0, 0, 0, (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
903 if (pConv
->wStatus
& ST_CONNECTED
)
905 /* don't care about result code (if server exists or not) */
906 PostMessageA(pConv
->hwndServer
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndClient
, 0L);
907 pConv
->wStatus
&= ~ST_CONNECTED
;
909 /* have to keep connection around to allow reconnection */
910 return WDML_QS_HANDLED
;
913 /******************************************************************
916 * handles any incoming reply, and try to match to an already sent request
918 static WDML_QUEUE_STATE
WDML_HandleReply(WDML_CONV
* pConv
, MSG
* msg
, HDDEDATA
* hdd
)
920 WDML_XACT
* pXAct
= pConv
->transactions
;
923 if (pConv
->transactions
)
925 /* first check message against a pending transaction, if any */
926 switch (pXAct
->ddeMsg
)
929 qs
= WDML_HandleAdviseReply(pConv
, msg
, pXAct
);
931 case WM_DDE_UNADVISE
:
932 qs
= WDML_HandleUnadviseReply(pConv
, msg
, pXAct
);
935 qs
= WDML_HandleExecuteReply(pConv
, msg
, pXAct
);
938 qs
= WDML_HandleRequestReply(pConv
, msg
, pXAct
);
941 qs
= WDML_HandlePokeReply(pConv
, msg
, pXAct
);
943 case WM_DDE_TERMINATE
:
944 qs
= WDML_HandleTerminateReply(pConv
, msg
, pXAct
);
956 /* now check the results */
960 case WDML_QS_SWALLOWED
:
963 case WDML_QS_HANDLED
:
964 /* ok, we have resolved a pending transaction
965 * notify callback if asynchronous, and remove it in any case
967 WDML_UnQueueTransaction(pConv
, pXAct
);
968 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&& pXAct
->ddeMsg
!= WM_DDE_TERMINATE
)
970 WDML_InvokeCallback(pConv
->instance
, XTYP_XACT_COMPLETE
, pXAct
->wFmt
,
971 (HCONV
)pConv
, pConv
->hszTopic
, pXAct
->hszItem
,
972 pXAct
->hDdeData
, MAKELONG(0, pXAct
->xActID
), 0 /* FIXME */);
977 *hdd
= pXAct
->hDdeData
;
979 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
982 /* no pending transaction found, try a warm/hot link or a termination request */
983 switch (msg
->message
)
986 qs
= WDML_HandleIncomingData(pConv
, msg
, hdd
);
988 case WM_DDE_TERMINATE
:
989 qs
= WDML_HandleIncomingTerminate(pConv
, msg
, hdd
);
994 FIXME("shouldn't be used on client side\n");
1001 /******************************************************************
1002 * WDML_SyncWaitTransactionReply
1004 * waits until an answer for a sent request is received
1005 * time out is also handled. only used for synchronous transactions
1007 static HDDEDATA
WDML_SyncWaitTransactionReply(HCONV hConv
, DWORD dwTimeout
, WDML_XACT
* pXAct
)
1013 TRACE("Starting wait for a timeout of %ld ms\n", dwTimeout
);
1015 /* FIXME: time 32 bit wrap around */
1016 dwTimeout
+= GetCurrentTime();
1018 while ((dwTime
= GetCurrentTime()) < 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
- dwTime
, QS_POSTMESSAGE
) == WAIT_OBJECT_0
)
1031 EnterCriticalSection(&WDML_CritSect
);
1033 pConv
= WDML_GetConv(hConv
, FALSE
);
1036 LeaveCriticalSection(&WDML_CritSect
);
1037 /* conversation no longer available... return failure */
1040 while (PeekMessageA(&msg
, pConv
->hwndClient
, WM_DDE_FIRST
, WM_DDE_LAST
, PM_REMOVE
))
1042 /* check that either pXAct has been processed or no more xActions are pending */
1043 ret
= (pConv
->transactions
== pXAct
);
1044 ret
= WDML_HandleReply(pConv
, &msg
, &hdd
) == WDML_QS_HANDLED
&&
1045 (pConv
->transactions
== NULL
|| ret
);
1048 LeaveCriticalSection(&WDML_CritSect
);
1056 TRACE("Timeout !!\n");
1058 EnterCriticalSection(&WDML_CritSect
);
1060 pConv
= WDML_GetConv(hConv
, FALSE
);
1063 if (pConv
->transactions
)
1065 switch (pConv
->transactions
->ddeMsg
)
1067 case WM_DDE_ADVISE
: err
= DMLERR_ADVACKTIMEOUT
; break;
1068 case WM_DDE_REQUEST
: err
= DMLERR_DATAACKTIMEOUT
; break;
1069 case WM_DDE_EXECUTE
: err
= DMLERR_EXECACKTIMEOUT
; break;
1070 case WM_DDE_POKE
: err
= DMLERR_POKEACKTIMEOUT
; break;
1071 case WM_DDE_UNADVISE
: err
= DMLERR_UNADVACKTIMEOUT
; break;
1072 default: err
= DMLERR_INVALIDPARAMETER
; break;
1075 pConv
->instance
->lastError
= err
;
1078 LeaveCriticalSection(&WDML_CritSect
);
1083 /*****************************************************************
1084 * DdeClientTransaction (USER32.@)
1086 HDDEDATA WINAPI
DdeClientTransaction(LPBYTE pData
, DWORD cbData
, HCONV hConv
, HSZ hszItem
, UINT wFmt
,
1087 UINT wType
, DWORD dwTimeout
, LPDWORD pdwResult
)
1091 HDDEDATA hDdeData
= 0;
1093 TRACE("(%p,%ld,0x%lx,0x%x,%d,%d,%ld,%p)\n",
1094 pData
, cbData
, (DWORD
)hConv
, hszItem
, wFmt
, wType
, dwTimeout
, pdwResult
);
1098 ERR("Invalid conversation handle\n");
1102 EnterCriticalSection(&WDML_CritSect
);
1104 pConv
= WDML_GetConv(hConv
, TRUE
);
1107 /* cannot set error... cannot get back to DDE instance */
1114 if (hszItem
!= 0 || wFmt
!= 0)
1116 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1119 pXAct
= WDML_ClientQueueExecute(pConv
, pData
, cbData
);
1122 pXAct
= WDML_ClientQueuePoke(pConv
, pData
, cbData
, wFmt
, hszItem
);
1124 case XTYP_ADVSTART
|XTYPF_NODATA
:
1125 case XTYP_ADVSTART
|XTYPF_NODATA
|XTYPF_ACKREQ
:
1127 case XTYP_ADVSTART
|XTYPF_ACKREQ
:
1130 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1133 pXAct
= WDML_ClientQueueAdvise(pConv
, wType
, wFmt
, hszItem
);
1138 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1141 pXAct
= WDML_ClientQueueUnadvise(pConv
, wFmt
, hszItem
);
1146 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1149 pXAct
= WDML_ClientQueueRequest(pConv
, wFmt
, hszItem
);
1152 FIXME("Unknown transation\n");
1153 /* unknown transaction type */
1154 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
;
1160 pConv
->instance
->lastError
= DMLERR_MEMORY_ERROR
;
1164 WDML_QueueTransaction(pConv
, pXAct
);
1166 if (!PostMessageA(pConv
->hwndServer
, pXAct
->ddeMsg
, (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1168 TRACE("Failed posting message %d to 0x%04x (error=0x%lx)\n",
1169 pXAct
->ddeMsg
, pConv
->hwndServer
, GetLastError());
1170 pConv
->wStatus
&= ~ST_CONNECTED
;
1171 WDML_UnQueueTransaction(pConv
, pXAct
);
1172 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1175 pXAct
->dwTimeout
= dwTimeout
;
1176 /* FIXME: should set the app bits on *pdwResult */
1178 if (dwTimeout
== TIMEOUT_ASYNC
)
1182 *pdwResult
= MAKELONG(0, pXAct
->xActID
);
1184 hDdeData
= (HDDEDATA
)1;
1194 count
= WDML_CritSect
.RecursionCount
;
1195 for (i
= 0; i
< count
; i
++)
1196 LeaveCriticalSection(&WDML_CritSect
);
1197 hDdeData
= WDML_SyncWaitTransactionReply((HCONV
)pConv
, dwTimeout
, pXAct
);
1198 for (i
= 0; i
< count
; i
++)
1199 EnterCriticalSection(&WDML_CritSect
);
1201 LeaveCriticalSection(&WDML_CritSect
);
1205 LeaveCriticalSection(&WDML_CritSect
);
1209 /*****************************************************************
1210 * DdeAbandonTransaction (USER32.@)
1212 BOOL WINAPI
DdeAbandonTransaction(DWORD idInst
, HCONV hConv
, DWORD idTransaction
)
1214 WDML_INSTANCE
* pInstance
;
1218 TRACE("(%08lx,%08lx,%08ld);\n", idInst
, (DWORD
)hConv
, idTransaction
);
1220 EnterCriticalSection(&WDML_CritSect
);
1221 if ((pInstance
= WDML_GetInstance(idInst
)))
1225 if ((pConv
= WDML_GetConv(hConv
, TRUE
)) && pConv
->instance
== pInstance
)
1227 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1229 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
&&
1230 (idTransaction
== 0 || pXAct
->xActID
== idTransaction
))
1232 WDML_UnQueueTransaction(pConv
, pXAct
);
1233 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1240 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
; pConv
= pConv
->next
)
1242 if (!(pConv
->wStatus
& ST_CONNECTED
)) continue;
1243 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1245 if (pXAct
->dwTimeout
== TIMEOUT_ASYNC
)
1247 WDML_UnQueueTransaction(pConv
, pXAct
);
1248 WDML_FreeTransaction(pInstance
, pXAct
, TRUE
);
1254 LeaveCriticalSection(&WDML_CritSect
);
1259 /******************************************************************
1262 * Window Proc created on client side for each conversation
1264 static LRESULT CALLBACK
WDML_ClientProc(HWND hwnd
, UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
1267 WDML_CONV
* pConv
= NULL
;
1270 if (iMsg
== WM_DDE_ACK
&&
1271 /* in the initial WM_INITIATE sendmessage */
1272 ((pConv
= WDML_GetConvFromWnd(hwnd
)) == NULL
|| pConv
->wStatus
== XST_INIT1
))
1274 /* In response to WM_DDE_INITIATE, save server window */
1276 WDML_INSTANCE
* pInstance
;
1278 /* note: sent messages do not need packing */
1279 uiLo
= LOWORD(lParam
);
1280 uiHi
= HIWORD(lParam
);
1282 /* FIXME: convlist should be handled here */
1285 /* we already have started the conv with a server, drop other replies */
1286 GlobalDeleteAtom(uiLo
);
1287 GlobalDeleteAtom(uiHi
);
1288 PostMessageA((HWND
)wParam
, WM_DDE_TERMINATE
, (WPARAM
)hwnd
, 0);
1292 pInstance
= WDML_GetInstanceFromWnd(hwnd
);
1294 hszSrv
= WDML_MakeHszFromAtom(pInstance
, uiLo
);
1295 hszTpc
= WDML_MakeHszFromAtom(pInstance
, uiHi
);
1297 pConv
= WDML_AddConv(pInstance
, WDML_CLIENT_SIDE
, hszSrv
, hszTpc
, hwnd
, (HWND
)wParam
);
1299 SetWindowLongA(hwnd
, GWL_WDML_CONVERSATION
, (DWORD
)pConv
);
1300 pConv
->wStatus
|= ST_CONNECTED
;
1301 pConv
->wConvst
= XST_INIT1
;
1303 /* check if server is handled by DDEML */
1304 if ((GetClassNameA((HWND
)wParam
, buf
, sizeof(buf
)) &&
1305 strcmp(buf
, WDML_szServerConvClassA
) == 0) ||
1306 (GetClassNameW((HWND
)wParam
, (LPWSTR
)buf
, sizeof(buf
)/sizeof(WCHAR
)) &&
1307 lstrcmpW((LPWSTR
)buf
, WDML_szServerConvClassW
) == 0))
1309 pConv
->wStatus
|= ST_ISLOCAL
;
1312 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_CONNECT_CONFIRM
, (WPARAM
)hwnd
, wParam
);
1314 GlobalDeleteAtom(uiLo
);
1315 GlobalDeleteAtom(uiHi
);
1317 /* accept conversation */
1321 if (iMsg
>= WM_DDE_FIRST
&& iMsg
<= WM_DDE_LAST
)
1323 EnterCriticalSection(&WDML_CritSect
);
1325 pConv
= WDML_GetConvFromWnd(hwnd
);
1334 msg
.wParam
= wParam
;
1335 msg
.lParam
= lParam
;
1337 WDML_HandleReply(pConv
, &msg
, &hdd
);
1340 LeaveCriticalSection(&WDML_CritSect
);
1344 return (IsWindowUnicode(hwnd
)) ?
1345 DefWindowProcA(hwnd
, iMsg
, wParam
, lParam
) : DefWindowProcW(hwnd
, iMsg
, wParam
, lParam
);
1348 /*****************************************************************
1349 * DdeDisconnect (USER32.@)
1351 BOOL WINAPI
DdeDisconnect(HCONV hConv
)
1353 WDML_CONV
* pConv
= NULL
;
1358 TRACE("(%ld)\n", (DWORD
)hConv
);
1362 ERR("DdeDisconnect(): hConv = 0\n");
1366 EnterCriticalSection(&WDML_CritSect
);
1367 pConv
= WDML_GetConv(hConv
, TRUE
);
1370 if (pConv
->wStatus
& ST_CLIENT
)
1372 /* FIXME: should abandon all pending transactions */
1373 pXAct
= WDML_ClientQueueTerminate(pConv
);
1376 count
= WDML_CritSect
.RecursionCount
;
1377 for (i
= 0; i
< count
; i
++)
1378 LeaveCriticalSection(&WDML_CritSect
);
1379 if (PostMessageA(pConv
->hwndServer
, pXAct
->ddeMsg
,
1380 (WPARAM
)pConv
->hwndClient
, pXAct
->lParam
))
1381 WDML_SyncWaitTransactionReply(hConv
, 10000, pXAct
);
1382 for (i
= 0; i
< count
; i
++)
1383 EnterCriticalSection(&WDML_CritSect
);
1385 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1386 /* still have to destroy data assosiated with conversation */
1387 WDML_RemoveConv(pConv
, WDML_CLIENT_SIDE
);
1391 FIXME("Not implemented yet for a server side conversation\n");
1395 LeaveCriticalSection(&WDML_CritSect
);
1400 /*****************************************************************
1401 * DdeImpersonateClient (USER32.@)
1403 BOOL WINAPI
DdeImpersonateClient(HCONV hConv
)
1408 EnterCriticalSection(&WDML_CritSect
);
1409 pConv
= WDML_GetConv(hConv
, TRUE
);
1412 ret
= ImpersonateDdeClientWindow(pConv
->hwndClient
, pConv
->hwndServer
);
1414 LeaveCriticalSection(&WDML_CritSect
);