4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
8 * Copyright 2001 Eric Pouech
9 * Copyright 2003, 2004, 2005 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "dde_private.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
35 /* convert between ATOM and HSZ avoiding compiler warnings */
36 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
37 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
39 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
40 static LONG WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
41 const WCHAR WDML_szEventClass
[] = {'D','d','e','E','v','e','n','t','C','l','a','s','s',0};
43 /* protection for instance list */
44 CRITICAL_SECTION WDML_CritSect
;
45 CRITICAL_SECTION_DEBUG critsect_debug
=
48 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": WDML_CritSect") }
51 CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
53 /* ================================================================
55 * Pure DDE (non DDEML) management
57 * ================================================================ */
60 /*****************************************************************
61 * PackDDElParam (USER32.@)
66 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
77 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
79 ERR("GlobalAlloc failed\n");
82 if (!(params
= GlobalLock(hMem
)))
84 ERR("GlobalLock failed (%p)\n", hMem
);
96 return MAKELPARAM(uiLo
, uiHi
);
101 /*****************************************************************
102 * UnpackDDElParam (USER32.@)
108 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
109 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
119 if (!lParam
|| !(params
= GlobalLock((HGLOBAL
)lParam
)))
125 if (uiLo
) *uiLo
= params
[0];
126 if (uiHi
) *uiHi
= params
[1];
127 GlobalUnlock( (HGLOBAL
)lParam
);
132 if (uiHi
) *uiHi
= lParam
;
136 if (uiLo
) *uiLo
= LOWORD(lParam
);
137 if (uiHi
) *uiHi
= HIWORD(lParam
);
143 /*****************************************************************
144 * FreeDDElParam (USER32.@)
150 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
158 /* first check if it's a global handle */
159 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
160 return !GlobalFree( (HGLOBAL
)lParam
);
168 /*****************************************************************
169 * ReuseDDElParam (USER32.@)
174 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
175 UINT_PTR uiLo
, UINT_PTR uiHi
)
191 if (!lParam
) return 0;
192 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
194 ERR("GlobalLock failed\n");
199 TRACE("Reusing pack %08lx %08lx\n", uiLo
, uiHi
);
200 GlobalUnlock( (HGLOBAL
)lParam
);
204 FreeDDElParam( msgIn
, lParam
);
208 FreeDDElParam( msgIn
, lParam
);
209 return MAKELPARAM(uiLo
, uiHi
);
213 return PackDDElParam( msgOut
, uiLo
, uiHi
);
217 /*****************************************************************
218 * ImpersonateDdeClientWindow (USER32.@)
221 * hWndClient [I] handle to DDE client window
222 * hWndServer [I] handle to DDE server window
224 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
226 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
230 /*****************************************************************
231 * DdeSetQualityOfService (USER32.@)
234 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, CONST SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
235 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
237 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
241 /* ================================================================
243 * Instance management
245 * ================================================================ */
247 /******************************************************************************
248 * IncrementInstanceId
250 * generic routine to increment the max instance Id and allocate a new application instance
252 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
254 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
256 pInstance
->instanceID
= id
;
257 TRACE("New instance id %d allocated\n", id
);
260 /******************************************************************
265 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
267 WDML_INSTANCE
* pInstance
;
272 case WM_WDML_REGISTER
:
273 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
274 /* try calling the Callback */
275 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
277 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
278 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
279 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
280 WDML_DecHSZ(pInstance
, hsz1
);
281 WDML_DecHSZ(pInstance
, hsz2
);
285 case WM_WDML_UNREGISTER
:
286 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
287 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
289 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
290 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
291 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
292 WDML_DecHSZ(pInstance
, hsz1
);
293 WDML_DecHSZ(pInstance
, hsz2
);
297 case WM_WDML_CONNECT_CONFIRM
:
298 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
299 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
302 /* confirm connection...
303 * lookup for this conv handle
305 HWND client
= (HWND
)wParam
;
306 HWND server
= (HWND
)lParam
;
307 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
309 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
314 pConv
->wStatus
|= ST_ISLOCAL
;
316 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
317 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
318 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
323 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
328 /******************************************************************
333 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
334 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
, BOOL b16
)
336 WDML_INSTANCE
* pInstance
;
337 WDML_INSTANCE
* reference_inst
;
339 WNDCLASSEXW wndclass
;
341 TRACE("(%p,%p,0x%x,%d)\n",
342 pidInst
, pfnCallback
, afCmd
, ulRes
);
346 ERR("Reserved value not zero? What does this mean?\n");
347 /* trap this and no more until we know more */
348 return DMLERR_NO_ERROR
;
351 /* grab enough heap for one control struct - not really necessary for re-initialise
352 * but allows us to use same validation routines */
353 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
354 if (pInstance
== NULL
)
356 /* catastrophe !! warn user & abort */
357 ERR("Instance create failed - out of memory\n");
358 return DMLERR_SYS_ERROR
;
360 pInstance
->next
= NULL
;
361 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
363 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
365 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
366 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
367 pInstance
->threadID
= GetCurrentThreadId();
368 pInstance
->callback
= *pfnCallback
;
369 pInstance
->unicode
= bUnicode
;
370 pInstance
->win16
= b16
;
371 pInstance
->nodeList
= NULL
; /* node will be added later */
372 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
373 pInstance
->wStatus
= 0;
374 pInstance
->servers
= NULL
;
375 pInstance
->convs
[0] = NULL
;
376 pInstance
->convs
[1] = NULL
;
377 pInstance
->links
[0] = NULL
;
378 pInstance
->links
[1] = NULL
;
380 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
382 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
384 if (!pInstance
->clientOnly
)
386 /* Check for other way of setting Client-only !! */
387 pInstance
->clientOnly
=
388 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
391 TRACE("instance created - checking validity\n");
395 /* Initialisation of new Instance Identifier */
396 TRACE("new instance, callback %p flags %X\n",pfnCallback
,afCmd
);
398 EnterCriticalSection(&WDML_CritSect
);
400 if (WDML_InstanceList
== NULL
)
402 /* can't be another instance in this case, assign to the base pointer */
403 WDML_InstanceList
= pInstance
;
405 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
407 * ------------------------------- NOTE NOTE NOTE --------------------------
409 * the manual is not clear if this condition
410 * applies to the first call to DdeInitialize from an application, or the
411 * first call for a given callback !!!
414 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
415 TRACE("First application instance detected OK\n");
416 /* allocate new instance ID */
417 WDML_IncrementInstanceId(pInstance
);
421 /* really need to chain the new one in to the latest here, but after checking conditions
422 * such as trying to start a conversation from an application trying to monitor */
423 reference_inst
= WDML_InstanceList
;
424 TRACE("Subsequent application instance - starting checks\n");
425 while (reference_inst
->next
!= NULL
)
428 * This set of tests will work if application uses same instance Id
429 * at application level once allocated - which is what manual implies
430 * should happen. If someone tries to be
431 * clever (lazy ?) it will fail to pick up that later calls are for
432 * the same application - should we trust them ?
434 if (pInstance
->instanceID
== reference_inst
->instanceID
)
436 /* Check 1 - must be same Client-only state */
438 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
440 ret
= DMLERR_DLL_USAGE
;
444 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
446 if (pInstance
->monitor
!= reference_inst
->monitor
)
448 ret
= DMLERR_INVALIDPARAMETER
;
452 /* Check 3 - must supply different callback address */
454 if (pInstance
->callback
== reference_inst
->callback
)
456 ret
= DMLERR_DLL_USAGE
;
460 reference_inst
= reference_inst
->next
;
462 /* All cleared, add to chain */
464 TRACE("Application Instance checks finished\n");
465 WDML_IncrementInstanceId(pInstance
);
466 reference_inst
->next
= pInstance
;
468 LeaveCriticalSection(&WDML_CritSect
);
470 *pidInst
= pInstance
->instanceID
;
472 /* for deadlock issues, windows must always be created when outside the critical section */
473 wndclass
.cbSize
= sizeof(wndclass
);
475 wndclass
.lpfnWndProc
= WDML_EventProc
;
476 wndclass
.cbClsExtra
= 0;
477 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
478 wndclass
.hInstance
= 0;
480 wndclass
.hCursor
= 0;
481 wndclass
.hbrBackground
= 0;
482 wndclass
.lpszMenuName
= NULL
;
483 wndclass
.lpszClassName
= WDML_szEventClass
;
484 wndclass
.hIconSm
= 0;
486 RegisterClassExW(&wndclass
);
488 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
489 WS_POPUP
, 0, 0, 0, 0,
492 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
494 TRACE("New application instance processing finished OK\n");
498 /* Reinitialisation situation --- FIX */
499 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
501 EnterCriticalSection(&WDML_CritSect
);
503 if (WDML_InstanceList
== NULL
)
505 ret
= DMLERR_INVALIDPARAMETER
;
508 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
509 /* can't reinitialise if we have initialised nothing !! */
510 reference_inst
= WDML_InstanceList
;
511 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
513 * MS allows initialisation without specifying a callback, should we allow addition of the
514 * callback by a later call to initialise ? - if so this lot will have to change
516 while (reference_inst
->next
!= NULL
)
518 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
520 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
522 if (reference_inst
->clientOnly
)
524 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
526 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
528 if (!(afCmd
& APPCMD_CLIENTONLY
))
530 ret
= DMLERR_INVALIDPARAMETER
;
535 /* Check 2 - cannot change monitor modes */
537 if (pInstance
->monitor
!= reference_inst
->monitor
)
539 ret
= DMLERR_INVALIDPARAMETER
;
543 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
545 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
547 ret
= DMLERR_INVALIDPARAMETER
;
552 reference_inst
= reference_inst
->next
;
554 if (reference_inst
->next
== NULL
)
556 ret
= DMLERR_INVALIDPARAMETER
;
559 /* All checked - change relevant flags */
561 reference_inst
->CBFflags
= pInstance
->CBFflags
;
562 reference_inst
->clientOnly
= pInstance
->clientOnly
;
563 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
564 LeaveCriticalSection(&WDML_CritSect
);
567 return DMLERR_NO_ERROR
;
569 HeapFree(GetProcessHeap(), 0, pInstance
);
570 LeaveCriticalSection(&WDML_CritSect
);
574 /******************************************************************************
575 * DdeInitializeA (USER32.@)
577 * See DdeInitializeW.
579 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
580 DWORD afCmd
, DWORD ulRes
)
582 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
, FALSE
);
585 /******************************************************************************
586 * DdeInitializeW [USER32.@]
587 * Registers an application with the DDEML
590 * pidInst [I] Pointer to instance identifier
591 * pfnCallback [I] Pointer to callback function
592 * afCmd [I] Set of command and filter flags
596 * Success: DMLERR_NO_ERROR
597 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
599 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
600 DWORD afCmd
, DWORD ulRes
)
602 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
, FALSE
);
605 /*****************************************************************
606 * DdeUninitialize [USER32.@] Frees DDEML resources
609 * idInst [I] Instance identifier
616 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
618 /* Stage one - check if we have a handle for this instance
620 WDML_INSTANCE
* pInstance
;
622 WDML_CONV
* pConvNext
;
624 TRACE("(%d)\n", idInst
);
626 /* First check instance
628 pInstance
= WDML_GetInstance(idInst
);
629 if (pInstance
== NULL
)
632 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
637 /* first terminate all conversations client side
638 * this shall close existing links...
640 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
642 pConvNext
= pConv
->next
;
643 DdeDisconnect((HCONV
)pConv
);
645 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
646 FIXME("still pending conversations\n");
648 /* then unregister all known service names */
649 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
651 /* Free the nodes that were not freed by this instance
652 * and remove the nodes from the list of HSZ nodes.
654 WDML_FreeAllHSZ(pInstance
);
656 DestroyWindow(pInstance
->hwndEvent
);
658 /* OK now delete the instance handle itself */
660 if (WDML_InstanceList
== pInstance
)
662 /* special case - the first/only entry */
663 WDML_InstanceList
= pInstance
->next
;
667 /* general case, remove entry */
670 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
671 inst
->next
= pInstance
->next
;
673 /* release the heap entry
675 HeapFree(GetProcessHeap(), 0, pInstance
);
680 /******************************************************************
681 * WDML_NotifyThreadExit
685 void WDML_NotifyThreadDetach(void)
687 WDML_INSTANCE
* pInstance
;
689 DWORD tid
= GetCurrentThreadId();
691 EnterCriticalSection(&WDML_CritSect
);
692 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
694 next
= pInstance
->next
;
695 if (pInstance
->threadID
== tid
)
697 LeaveCriticalSection(&WDML_CritSect
);
698 DdeUninitialize(pInstance
->instanceID
);
699 EnterCriticalSection(&WDML_CritSect
);
702 LeaveCriticalSection(&WDML_CritSect
);
705 /******************************************************************
706 * WDML_InvokeCallback
710 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
711 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
712 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
716 if (pInstance
== NULL
)
719 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
720 pInstance
->win16
? 16 : 32, pInstance
->callback
, uType
, uFmt
,
721 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
723 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
725 TRACE("done => %p\n", ret
);
729 /*****************************************************************************
732 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
733 * for an instance Id, or NULL if the entry does not exist
736 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
738 WDML_INSTANCE
* pInstance
;
740 EnterCriticalSection(&WDML_CritSect
);
742 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
744 if (pInstance
->instanceID
== instId
)
746 if (GetCurrentThreadId() != pInstance
->threadID
)
748 FIXME("Tried to get instance from wrong thread\n");
755 LeaveCriticalSection(&WDML_CritSect
);
758 WARN("Instance entry missing for id %04x\n", instId
);
762 /******************************************************************
763 * WDML_GetInstanceFromWnd
767 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
769 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
772 /******************************************************************************
773 * DdeGetLastError [USER32.@] Gets most recent error code
776 * idInst [I] Instance identifier
781 UINT WINAPI
DdeGetLastError(DWORD idInst
)
784 WDML_INSTANCE
* pInstance
;
786 /* First check instance
788 pInstance
= WDML_GetInstance(idInst
);
789 if (pInstance
== NULL
)
791 error_code
= DMLERR_INVALIDPARAMETER
;
795 error_code
= pInstance
->lastError
;
796 pInstance
->lastError
= 0;
802 /* ================================================================
806 * ================================================================ */
809 /******************************************************************
814 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
818 if (pInstance
== NULL
) return NULL
;
820 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
822 if (pNode
->hsz
== hsz
) break;
824 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
828 /******************************************************************
829 * WDML_MakeAtomFromHsz
831 * Creates a global atom from an existing HSZ
832 * Generally used before sending an HSZ as an atom to a remote app
834 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
836 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
838 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
839 return GlobalAddAtomW(nameBuffer
);
840 WARN("HSZ %p not found\n", hsz
);
844 /******************************************************************
845 * WDML_MakeHszFromAtom
847 * Creates a HSZ from an existing global atom
848 * Generally used while receiving a global atom and transforming it
851 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
853 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
855 if (!atom
) return NULL
;
857 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
859 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
860 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
862 WARN("ATOM 0x%x not found\n", atom
);
866 /******************************************************************
871 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
875 pNode
= WDML_FindNode(pInstance
, hsz
);
876 if (!pNode
) return FALSE
;
882 /******************************************************************************
883 * WDML_DecHSZ (INTERNAL)
885 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
887 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
889 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
891 HSZNode
* pPrev
= NULL
;
894 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
896 /* If we found the node we were looking for and its ref count is one,
899 if (pCurrent
->hsz
== hsz
)
901 if (--pCurrent
->refCount
== 0)
903 if (pCurrent
== pInstance
->nodeList
)
905 pInstance
->nodeList
= pCurrent
->next
;
909 pPrev
->next
= pCurrent
->next
;
911 HeapFree(GetProcessHeap(), 0, pCurrent
);
912 DeleteAtom(HSZ2ATOM(hsz
));
917 WARN("HSZ %p not found\n", hsz
);
922 /******************************************************************************
923 * WDML_FreeAllHSZ (INTERNAL)
925 * Frees up all the strings still allocated in the list and
926 * remove all the nodes from the list of HSZ nodes.
928 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
930 /* Free any strings created in this instance.
932 while (pInstance
->nodeList
!= NULL
)
934 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
938 /******************************************************************************
939 * InsertHSZNode (INTERNAL)
941 * Insert a node to the head of the list.
943 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
947 HSZNode
* pNew
= NULL
;
948 /* Create a new node for this HSZ.
950 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
954 pNew
->next
= pInstance
->nodeList
;
956 pInstance
->nodeList
= pNew
;
960 ERR("Primary HSZ Node allocation failed - out of memory\n");
965 /******************************************************************
970 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
973 WCHAR pString
[MAX_BUFFER_LEN
];
975 /* If psz is null, we have to return only the length
981 cchMax
= MAX_BUFFER_LEN
;
987 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
990 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
993 ERR("Unknown code page %d\n", codepage
);
999 /*****************************************************************
1000 * DdeQueryStringA [USER32.@]
1002 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
1005 WDML_INSTANCE
* pInstance
;
1007 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1009 /* First check instance
1011 pInstance
= WDML_GetInstance(idInst
);
1012 if (pInstance
!= NULL
)
1014 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
1015 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1018 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
1022 /*****************************************************************
1023 * DdeQueryStringW [USER32.@]
1026 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
1029 WDML_INSTANCE
* pInstance
;
1031 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1033 /* First check instance
1035 pInstance
= WDML_GetInstance(idInst
);
1036 if (pInstance
!= NULL
)
1038 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
1039 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1042 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
1046 /******************************************************************
1051 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
1058 hsz
= ATOM2HSZ(AddAtomA(ptr
));
1059 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
1062 hsz
= ATOM2HSZ(AddAtomW(ptr
));
1063 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
1066 ERR("Unknown code page %d\n", codepage
);
1069 WDML_InsertHSZNode(pInstance
, hsz
);
1073 /*****************************************************************
1074 * DdeCreateStringHandleA [USER32.@]
1076 * See DdeCreateStringHandleW.
1078 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
1081 WDML_INSTANCE
* pInstance
;
1083 TRACE("(%d,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
1085 pInstance
= WDML_GetInstance(idInst
);
1088 if (codepage
== 0) codepage
= CP_WINANSI
;
1089 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1096 /******************************************************************************
1097 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1100 * idInst [I] Instance identifier
1101 * psz [I] Pointer to string
1102 * codepage [I] Code page identifier
1104 * Success: String handle
1107 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
1109 WDML_INSTANCE
* pInstance
;
1112 pInstance
= WDML_GetInstance(idInst
);
1115 if (codepage
== 0) codepage
= CP_WINUNICODE
;
1116 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1122 /*****************************************************************
1123 * DdeFreeStringHandle (USER32.@)
1128 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
1130 WDML_INSTANCE
* pInstance
;
1133 TRACE("(%d,%p):\n", idInst
, hsz
);
1135 /* First check instance
1137 pInstance
= WDML_GetInstance(idInst
);
1139 ret
= WDML_DecHSZ(pInstance
, hsz
);
1144 /*****************************************************************
1145 * DdeKeepStringHandle (USER32.@)
1151 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
1153 WDML_INSTANCE
* pInstance
;
1156 TRACE("(%d,%p):\n", idInst
, hsz
);
1158 /* First check instance
1160 pInstance
= WDML_GetInstance(idInst
);
1162 ret
= WDML_IncHSZ(pInstance
, hsz
);
1167 /*****************************************************************
1168 * DdeCmpStringHandles (USER32.@)
1170 * Compares the value of two string handles. This comparison is
1171 * not case sensitive.
1174 * hsz1 [I] Handle to the first string
1175 * hsz2 [I] Handle to the second string
1178 * -1 The value of hsz1 is zero or less than hsz2
1179 * 0 The values of hsz 1 and 2 are the same or both zero.
1180 * 1 The value of hsz2 is zero of less than hsz1
1182 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
1184 WCHAR psz1
[MAX_BUFFER_LEN
];
1185 WCHAR psz2
[MAX_BUFFER_LEN
];
1189 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
1190 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
1192 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
1194 /* Make sure we found both strings. */
1195 if (ret1
== 0 && ret2
== 0)
1197 /* If both are not found, return both "zero strings". */
1202 /* If hsz1 is a not found, return hsz1 is "zero string". */
1207 /* If hsz2 is a not found, return hsz2 is "zero string". */
1212 /* Compare the two strings we got (case insensitive). */
1213 ret
= lstrcmpiW(psz1
, psz2
);
1214 /* Since strcmp returns any number smaller than
1215 * 0 when the first string is found to be less than
1216 * the second one we must make sure we are returning
1217 * the proper values.
1232 /* ================================================================
1234 * Data handle management
1236 * ================================================================ */
1238 /*****************************************************************
1239 * DdeCreateDataHandle (USER32.@)
1241 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1242 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1244 /* For now, we ignore idInst, hszItem.
1245 * The purpose of these arguments still need to be investigated.
1250 DDE_DATAHANDLE_HEAD
* pDdh
;
1251 WCHAR psz
[MAX_BUFFER_LEN
];
1253 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1255 psz
[0] = HSZ2ATOM(hszItem
);
1259 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1260 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1262 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1265 /* we use the first 4 bytes to store the size */
1266 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1268 ERR("GlobalAlloc failed\n");
1272 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1279 pDdh
->cfFormat
= wFmt
;
1280 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1282 pByte
= (LPBYTE
)(pDdh
+ 1);
1285 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1289 TRACE("=> %p\n", hMem
);
1290 return (HDDEDATA
)hMem
;
1293 /*****************************************************************
1295 * DdeAddData (USER32.@)
1297 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1299 DWORD old_sz
, new_sz
;
1302 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData
, pSrc
, cb
, cbOff
);
1304 pDst
= DdeAccessData(hData
, &old_sz
);
1305 if (!pDst
) return 0;
1307 new_sz
= cb
+ cbOff
;
1308 if (new_sz
> old_sz
)
1310 DdeUnaccessData(hData
);
1311 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1312 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1313 pDst
= DdeAccessData(hData
, &old_sz
);
1316 if (!pDst
) return 0;
1318 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1319 DdeUnaccessData(hData
);
1323 /******************************************************************************
1324 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1328 * hData [I] Handle to DDE object
1329 * pDst [I] Pointer to destination buffer
1330 * cbMax [I] Amount of data to copy
1331 * cbOff [I] Offset to beginning of data
1334 * Size of memory object associated with handle
1336 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1338 DWORD dwSize
, dwRet
;
1341 TRACE("(%p,%p,%d,%d)\n", hData
, pDst
, cbMax
, cbOff
);
1343 pByte
= DdeAccessData(hData
, &dwSize
);
1351 else if (cbOff
+ cbMax
< dwSize
)
1355 else if (cbOff
< dwSize
)
1357 dwRet
= dwSize
- cbOff
;
1363 if (pDst
&& dwRet
!= 0)
1365 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1367 DdeUnaccessData(hData
);
1376 /*****************************************************************
1377 * DdeAccessData (USER32.@)
1379 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1381 HGLOBAL hMem
= hData
;
1382 DDE_DATAHANDLE_HEAD
* pDdh
;
1384 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1386 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1389 ERR("Failed on GlobalLock(%p)\n", hMem
);
1393 if (pcbDataSize
!= NULL
)
1395 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1397 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1398 return (LPBYTE
)(pDdh
+ 1);
1401 /*****************************************************************
1402 * DdeUnaccessData (USER32.@)
1404 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1406 HGLOBAL hMem
= hData
;
1408 TRACE("(%p)\n", hData
);
1415 /*****************************************************************
1416 * DdeFreeDataHandle (USER32.@)
1418 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1420 TRACE("(%p)\n", hData
);
1421 return GlobalFree(hData
) == 0;
1424 /******************************************************************
1429 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1431 DDE_DATAHANDLE_HEAD
* pDdh
;
1434 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hData
);
1437 ret
= pDdh
->bAppOwned
;
1438 GlobalUnlock(hData
);
1443 /* ================================================================
1445 * Global <=> Data handle management
1447 * ================================================================ */
1449 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1451 * (bytes) (bits) comment
1452 * 0 16 bit fields for options (release, ackreq, response...)
1453 * 2 16 clipboard format
1454 * 4 ? data to be used
1456 HDDEDATA
WDML_Global2DataHandle(HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1464 pDd
= GlobalLock(hMem
);
1465 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1468 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1469 switch (pDd
->cfFormat
)
1472 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1473 pDd
->cfFormat
, hMem
);
1477 ret
= DdeCreateDataHandle(0, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1480 if (size
>= sizeof(BITMAP
))
1482 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1483 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1484 if (size
>= sizeof(BITMAP
) + count
)
1488 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1489 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1490 pDd
->Value
+ sizeof(BITMAP
))))
1492 ret
= DdeCreateDataHandle(0, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1493 0, 0, CF_BITMAP
, 0);
1495 else ERR("Can't create bmp\n");
1499 ERR("Wrong count: %u / %d\n", size
, count
);
1501 } else ERR("No bitmap header\n");
1510 /******************************************************************
1511 * WDML_DataHandle2Global
1515 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1516 BOOL fDeferUpd
, BOOL fAckReq
)
1518 DDE_DATAHANDLE_HEAD
* pDdh
;
1522 dwSize
= GlobalSize((HGLOBAL
)hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1523 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hDdeData
);
1526 WINE_DDEHEAD
* wdh
= NULL
;
1528 switch (pDdh
->cfFormat
)
1531 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1532 pDdh
->cfFormat
, hDdeData
);
1536 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1537 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1539 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1543 if (dwSize
>= sizeof(HBITMAP
))
1547 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1549 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1551 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1552 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1553 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1554 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1556 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1557 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1566 wdh
->fResponse
= fResponse
;
1567 wdh
->fRelease
= fRelease
;
1568 wdh
->fDeferUpd
= fDeferUpd
;
1569 wdh
->fAckReq
= fAckReq
;
1570 wdh
->cfFormat
= pDdh
->cfFormat
;
1573 GlobalUnlock((HGLOBAL
)hDdeData
);
1579 /* ================================================================
1583 * ================================================================ */
1585 /******************************************************************
1590 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1592 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1593 WDML_SERVER
* pServer
;
1597 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1598 if (pServer
== NULL
) return NULL
;
1600 pServer
->hszService
= hszService
;
1601 WDML_IncHSZ(pInstance
, hszService
);
1603 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1604 snprintfW(buf2
, 256, fmtW
, buf1
, GetCurrentProcessId());
1605 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1607 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1608 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1610 pServer
->filterOn
= TRUE
;
1612 pServer
->next
= pInstance
->servers
;
1613 pInstance
->servers
= pServer
;
1617 /******************************************************************
1622 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1624 WDML_SERVER
* pPrev
= NULL
;
1625 WDML_SERVER
* pServer
= NULL
;
1627 WDML_CONV
* pConvNext
;
1629 pServer
= pInstance
->servers
;
1631 while (pServer
!= NULL
)
1633 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1635 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1636 pServer
->atomService
, pServer
->atomServiceSpec
);
1637 /* terminate all conversations for given topic */
1638 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1640 pConvNext
= pConv
->next
;
1641 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1643 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1644 /* don't care about return code (whether client window is present or not) */
1645 PostMessageW(pConv
->hwndClient
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndServer
, 0);
1648 if (pServer
== pInstance
->servers
)
1650 pInstance
->servers
= pServer
->next
;
1654 pPrev
->next
= pServer
->next
;
1657 DestroyWindow(pServer
->hwndServer
);
1658 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1659 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1661 GlobalDeleteAtom(pServer
->atomService
);
1662 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1664 HeapFree(GetProcessHeap(), 0, pServer
);
1669 pServer
= pServer
->next
;
1673 /*****************************************************************************
1676 * generic routine to return a pointer to the relevant ServiceNode
1677 * for a given service name, or NULL if the entry does not exist
1680 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1682 WDML_SERVER
* pServer
;
1684 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1686 if (hszService
== pServer
->hszService
)
1691 TRACE("Service name missing\n");
1695 /* ================================================================
1697 * Conversation management
1699 * ================================================================ */
1701 /******************************************************************
1706 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1707 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
1711 /* no conversation yet, add it */
1712 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
1713 if (!pConv
) return NULL
;
1715 pConv
->instance
= pInstance
;
1716 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
1717 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
1718 pConv
->magic
= WDML_CONV_MAGIC
;
1719 pConv
->hwndServer
= hwndServer
;
1720 pConv
->hwndClient
= hwndClient
;
1721 pConv
->transactions
= NULL
;
1723 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
1724 pConv
->wStatus
|= pInstance
->wStatus
;
1725 /* check if both side of the conversation are of the same instance */
1726 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
1727 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
1729 pConv
->wStatus
|= ST_ISSELF
;
1731 pConv
->wConvst
= XST_NULL
;
1733 pConv
->next
= pInstance
->convs
[side
];
1734 pInstance
->convs
[side
] = pConv
;
1736 TRACE("pConv->wStatus %04x\n", pConv
->wStatus
);
1741 /******************************************************************
1746 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1747 HSZ hszService
, HSZ hszTopic
)
1749 WDML_CONV
* pCurrent
= NULL
;
1751 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1753 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
1754 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
1763 /******************************************************************
1768 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
1770 WDML_CONV
* pPrev
= NULL
;
1771 WDML_CONV
* pCurrent
;
1773 WDML_XACT
* pXActNext
;
1779 /* remove any pending transaction */
1780 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
1782 pXActNext
= pXAct
->next
;
1783 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
1786 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
1788 /* FIXME: should we keep the window around ? it seems so (at least on client side
1789 * to let QueryConvInfo work after conv termination, but also to implement
1792 /* destroy conversation window, but first remove pConv from hWnd.
1793 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1795 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
1796 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
1798 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
1800 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
1801 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
1803 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
1805 if (pCurrent
== pRef
)
1807 if (pCurrent
== pRef
->instance
->convs
[side
])
1809 pRef
->instance
->convs
[side
] = pCurrent
->next
;
1813 pPrev
->next
= pCurrent
->next
;
1815 pCurrent
->magic
= 0;
1816 HeapFree(GetProcessHeap(), 0, pCurrent
);
1822 /******************************************************************
1823 * WDML_EnableCallback
1825 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
1827 if (wCmd
== EC_DISABLE
)
1829 pConv
->wStatus
|= ST_BLOCKED
;
1830 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1834 if (wCmd
== EC_QUERYWAITING
)
1835 return pConv
->transactions
? TRUE
: FALSE
;
1837 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
1839 FIXME("Unknown command code %04x\n", wCmd
);
1843 if (wCmd
== EC_ENABLEALL
)
1845 pConv
->wStatus
&= ~ST_BLOCKED
;
1846 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1849 while (pConv
->transactions
)
1851 WDML_XACT
*pXAct
= pConv
->transactions
;
1853 if (pConv
->wStatus
& ST_CLIENT
)
1855 /* transaction should be in the queue until handled */
1856 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
1857 WDML_UnQueueTransaction(pConv
, pXAct
);
1861 /* transaction should be removed from the queue before handling */
1862 WDML_UnQueueTransaction(pConv
, pXAct
);
1863 WDML_ServerHandle(pConv
, pXAct
);
1866 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1868 if (wCmd
== EC_ENABLEONE
) break;
1873 /*****************************************************************
1874 * DdeEnableCallback (USER32.@)
1876 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
1881 TRACE("(%d, %p, %04x)\n", idInst
, hConv
, wCmd
);
1885 pConv
= WDML_GetConv(hConv
, TRUE
);
1887 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
1888 ret
= WDML_EnableCallback(pConv
, wCmd
);
1892 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
1897 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
1898 pInstance
->wStatus
|= wCmd
;
1900 if (wCmd
== EC_DISABLE
)
1902 pInstance
->wStatus
|= ST_BLOCKED
;
1903 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1905 else if (wCmd
== EC_ENABLEALL
)
1907 pInstance
->wStatus
&= ~ST_BLOCKED
;
1908 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1913 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
1915 ret
= WDML_EnableCallback(pConv
, wCmd
);
1916 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
1923 /******************************************************************
1928 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
1930 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
1932 /* FIXME: should do better checking */
1933 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
1935 if (!pConv
->instance
|| pConv
->instance
->threadID
!= GetCurrentThreadId())
1937 WARN("wrong thread ID\n");
1938 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
1942 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
1944 WARN("found conv but ain't connected\n");
1945 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
1952 /******************************************************************
1953 * WDML_GetConvFromWnd
1957 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
1959 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
1962 /******************************************************************
1967 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
1968 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
1973 if (side
== WDML_SERVER_SIDE
)
1975 from
= pConv
->hwndServer
;
1976 to
= pConv
->hwndClient
;
1980 to
= pConv
->hwndServer
;
1981 from
= pConv
->hwndClient
;
1984 ddeAck
.bAppReturnCode
= appRetCode
;
1985 ddeAck
.reserved
= 0;
1986 ddeAck
.fBusy
= fBusy
;
1989 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
1991 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
1992 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
1993 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
1995 pConv
->wStatus
&= ~ST_CONNECTED
;
1996 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
1997 FreeDDElParam(WM_DDE_ACK
, lParam
);
2003 /*****************************************************************
2004 * DdeSetUserHandle (USER32.@)
2006 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2010 pConv
= WDML_GetConv(hConv
, FALSE
);
2016 pConv
->hUser
= hUser
;
2022 pXAct
= WDML_FindTransaction(pConv
, id
);
2025 pXAct
->hUser
= hUser
;
2029 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2036 /******************************************************************
2037 * WDML_GetLocalConvInfo
2041 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2047 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2048 ci
->hszSvcPartner
= pConv
->hszService
;
2049 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2050 ci
->hszTopic
= pConv
->hszTopic
;
2051 ci
->wStatus
= pConv
->wStatus
;
2053 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2055 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2057 if (pLink
->hConv
== (HCONV
)pConv
)
2059 ci
->wStatus
|= ST_ADVISE
;
2064 /* FIXME: non handled status flags:
2070 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2072 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2074 ci
->ConvCtxt
= pConv
->convContext
;
2075 if (ci
->wStatus
& ST_CLIENT
)
2077 ci
->hwnd
= pConv
->hwndClient
;
2078 ci
->hwndPartner
= pConv
->hwndServer
;
2082 ci
->hwnd
= pConv
->hwndServer
;
2083 ci
->hwndPartner
= pConv
->hwndClient
;
2087 ci
->hUser
= pConv
->hUser
;
2096 pXAct
= WDML_FindTransaction(pConv
, id
);
2099 ci
->hUser
= pXAct
->hUser
;
2100 ci
->hszItem
= pXAct
->hszItem
;
2101 ci
->wFmt
= pXAct
->wFmt
;
2102 ci
->wType
= pXAct
->wType
;
2107 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2113 /******************************************************************
2114 * DdeQueryConvInfo (USER32.@)
2116 * FIXME: Set last DDE error on failure.
2118 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2120 UINT ret
= lpConvInfo
->cb
;
2124 TRACE("(%p,%x,%p)\n", hConv
, id
, lpConvInfo
);
2128 FIXME("hConv is NULL\n");
2132 pConv
= WDML_GetConv(hConv
, FALSE
);
2135 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2140 if ((ULONG_PTR
)hConv
& 1)
2142 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2144 FIXME("Request on remote conversation information is not implemented yet\n");
2150 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2154 /* ================================================================
2156 * Link (hot & warm) management
2158 * ================================================================ */
2160 /******************************************************************
2165 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2166 UINT wType
, HSZ hszItem
, UINT wFmt
)
2170 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
2177 pLink
->hConv
= hConv
;
2178 pLink
->transactionType
= wType
;
2179 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
2181 pLink
->next
= pInstance
->links
[side
];
2182 pInstance
->links
[side
] = pLink
;
2185 /******************************************************************
2190 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2191 HSZ hszItem
, UINT uFmt
)
2193 WDML_LINK
* pPrev
= NULL
;
2194 WDML_LINK
* pCurrent
= NULL
;
2196 pCurrent
= pInstance
->links
[side
];
2198 while (pCurrent
!= NULL
)
2200 if (pCurrent
->hConv
== hConv
&&
2201 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2202 pCurrent
->uFmt
== uFmt
)
2204 if (pCurrent
== pInstance
->links
[side
])
2206 pInstance
->links
[side
] = pCurrent
->next
;
2210 pPrev
->next
= pCurrent
->next
;
2213 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2214 HeapFree(GetProcessHeap(), 0, pCurrent
);
2219 pCurrent
= pCurrent
->next
;
2223 /* this function is called to remove all links related to the conv.
2224 It should be called from both client and server when terminating
2227 /******************************************************************
2228 * WDML_RemoveAllLinks
2232 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
2234 WDML_LINK
* pPrev
= NULL
;
2235 WDML_LINK
* pCurrent
= NULL
;
2236 WDML_LINK
* pNext
= NULL
;
2238 pCurrent
= pInstance
->links
[side
];
2240 while (pCurrent
!= NULL
)
2242 if (pCurrent
->hConv
== (HCONV
)pConv
)
2244 if (pCurrent
== pInstance
->links
[side
])
2246 pInstance
->links
[side
] = pCurrent
->next
;
2247 pNext
= pCurrent
->next
;
2251 pPrev
->next
= pCurrent
->next
;
2252 pNext
= pCurrent
->next
;
2255 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2257 HeapFree(GetProcessHeap(), 0, pCurrent
);
2264 pCurrent
= pCurrent
->next
;
2273 /******************************************************************
2278 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2279 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
2281 WDML_LINK
* pCurrent
= NULL
;
2283 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2285 /* we don't need to check for transaction type as it can be altered */
2287 if (pCurrent
->hConv
== hConv
&&
2288 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2289 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
2299 /* ================================================================
2301 * Transaction management
2303 * ================================================================ */
2305 /******************************************************************
2306 * WDML_AllocTransaction
2308 * Alloc a transaction structure for handling the message ddeMsg
2310 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
2311 UINT wFmt
, HSZ hszItem
)
2314 static WORD tid
= 1; /* FIXME: wrap around */
2316 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
2319 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
2323 pXAct
->xActID
= tid
++;
2324 pXAct
->ddeMsg
= ddeMsg
;
2325 pXAct
->hDdeData
= 0;
2330 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
2338 /******************************************************************
2339 * WDML_QueueTransaction
2341 * Adds a transaction to the list of transaction
2343 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2347 /* advance to last in queue */
2348 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
2352 /******************************************************************
2353 * WDML_UnQueueTransaction
2357 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2361 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
2372 /******************************************************************
2373 * WDML_FreeTransaction
2377 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
2379 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2380 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
2382 GlobalFree(pXAct
->hMem
);
2384 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
2386 HeapFree(GetProcessHeap(), 0, pXAct
);
2389 /******************************************************************
2390 * WDML_FindTransaction
2394 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2399 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2401 if (pXAct
->xActID
== tid
)
2407 /* ================================================================
2409 * Information broadcast across DDEML implementations
2411 * ================================================================ */
2413 struct tagWDML_BroadcastPmt
2421 /******************************************************************
2422 * WDML_BroadcastEnumProc
2426 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2428 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2431 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2432 lstrcmpiW(buffer
, s
->clsName
) == 0)
2434 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2439 /******************************************************************
2440 * WDML_BroadcastDDEWindows
2444 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2446 struct tagWDML_BroadcastPmt s
;
2448 s
.clsName
= clsName
;
2452 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);