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 2003, 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
29 #include "wine/port.h"
42 #include "dde/dde_private.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
48 /* convert between ATOM and HSZ avoiding compiler warnings */
49 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
50 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
52 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
53 static DWORD WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
54 const WCHAR WDML_szEventClass
[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
56 static CRITICAL_SECTION_DEBUG critsect_debug
=
59 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
60 0, 0, { 0, (DWORD
)(__FILE__
": WDML_CritSect") }
62 CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
64 /* ================================================================
66 * Pure DDE (non DDEML) management
68 * ================================================================ */
71 /*****************************************************************
72 * PackDDElParam (USER32.@)
77 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
88 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
90 ERR("GlobalAlloc failed\n");
93 if (!(params
= GlobalLock(hMem
)))
95 ERR("GlobalLock failed (%p)\n", hMem
);
107 return MAKELPARAM(uiLo
, uiHi
);
112 /*****************************************************************
113 * UnpackDDElParam (USER32.@)
119 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
120 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
130 if (!lParam
) return FALSE
;
131 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
133 ERR("GlobalLock failed (%lx)\n", lParam
);
136 TRACE("unpacked: low %08x, high %08x\n", params
[0], params
[1]);
137 if (uiLo
) *uiLo
= params
[0];
138 if (uiHi
) *uiHi
= params
[1];
139 GlobalUnlock( (HGLOBAL
)lParam
);
144 if (uiHi
) *uiHi
= lParam
;
148 if (uiLo
) *uiLo
= LOWORD(lParam
);
149 if (uiHi
) *uiHi
= HIWORD(lParam
);
155 /*****************************************************************
156 * FreeDDElParam (USER32.@)
162 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
170 /* first check if it's a global handle */
171 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
172 return !GlobalFree( (HGLOBAL
)lParam
);
180 /*****************************************************************
181 * ReuseDDElParam (USER32.@)
186 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
187 UINT_PTR uiLo
, UINT_PTR uiHi
)
203 if (!lParam
) return 0;
204 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
206 ERR("GlobalLock failed\n");
211 TRACE("Reusing pack %08x %08x\n", uiLo
, uiHi
);
212 GlobalLock( (HGLOBAL
)lParam
);
216 FreeDDElParam( msgIn
, lParam
);
220 FreeDDElParam( msgIn
, lParam
);
221 return MAKELPARAM(uiLo
, uiHi
);
225 return PackDDElParam( msgOut
, uiLo
, uiHi
);
229 /*****************************************************************
230 * ImpersonateDdeClientWindow (USER32.@)
233 * hWndClient [I] handle to DDE client window
234 * hWndServer [I] handle to DDE server window
236 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
238 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
242 /*****************************************************************
243 * DdeSetQualityOfService (USER32.@)
246 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, CONST SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
247 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
249 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
253 /* ================================================================
255 * Instance management
257 * ================================================================ */
259 /******************************************************************************
260 * IncrementInstanceId
262 * generic routine to increment the max instance Id and allocate a new application instance
264 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
266 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
268 pInstance
->instanceID
= id
;
269 TRACE("New instance id %ld allocated\n", id
);
272 /******************************************************************
277 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
279 WDML_INSTANCE
* pInstance
;
284 case WM_WDML_REGISTER
:
285 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
286 /* try calling the Callback */
287 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
289 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
290 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
291 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
292 WDML_DecHSZ(pInstance
, hsz1
);
293 WDML_DecHSZ(pInstance
, hsz2
);
297 case WM_WDML_UNREGISTER
:
298 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
299 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
301 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
302 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
303 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
304 WDML_DecHSZ(pInstance
, hsz1
);
305 WDML_DecHSZ(pInstance
, hsz2
);
309 case WM_WDML_CONNECT_CONFIRM
:
310 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
311 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
314 /* confirm connection...
315 * lookup for this conv handle
317 HWND client
= WIN_GetFullHandle( (HWND
)wParam
);
318 HWND server
= WIN_GetFullHandle( (HWND
)lParam
);
319 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
321 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
326 pConv
->wStatus
|= ST_ISLOCAL
;
328 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
329 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
330 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
335 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
340 /******************************************************************
345 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
346 DWORD afCmd
, DWORD ulRes
, BOOL b16
)
348 WDML_INSTANCE
* pInstance
;
349 WDML_INSTANCE
* reference_inst
;
351 WNDCLASSEXW wndclass
;
353 TRACE("(%p,%p,0x%lx,%ld)\n",
354 pidInst
, pfnCallback
, afCmd
, ulRes
);
358 ERR("Reserved value not zero? What does this mean?\n");
359 /* trap this and no more until we know more */
360 return DMLERR_NO_ERROR
;
363 /* grab enough heap for one control struct - not really necessary for re-initialise
364 * but allows us to use same validation routines */
365 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
366 if (pInstance
== NULL
)
368 /* catastrophe !! warn user & abort */
369 ERR("Instance create failed - out of memory\n");
370 return DMLERR_SYS_ERROR
;
372 pInstance
->next
= NULL
;
373 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
375 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
377 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
378 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
379 pInstance
->threadID
= GetCurrentThreadId();
380 pInstance
->callback
= *pfnCallback
;
381 pInstance
->win16
= b16
;
382 pInstance
->nodeList
= NULL
; /* node will be added later */
383 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
384 pInstance
->servers
= NULL
;
385 pInstance
->convs
[0] = NULL
;
386 pInstance
->convs
[1] = NULL
;
387 pInstance
->links
[0] = NULL
;
388 pInstance
->links
[1] = NULL
;
390 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
392 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
394 if (!pInstance
->clientOnly
)
396 /* Check for other way of setting Client-only !! */
397 pInstance
->clientOnly
=
398 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
401 TRACE("instance created - checking validity \n");
405 /* Initialisation of new Instance Identifier */
406 TRACE("new instance, callback %p flags %lX\n",pfnCallback
,afCmd
);
408 EnterCriticalSection(&WDML_CritSect
);
410 if (WDML_InstanceList
== NULL
)
412 /* can't be another instance in this case, assign to the base pointer */
413 WDML_InstanceList
= pInstance
;
415 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
417 * ------------------------------- NOTE NOTE NOTE --------------------------
419 * the manual is not clear if this condition
420 * applies to the first call to DdeInitialize from an application, or the
421 * first call for a given callback !!!
424 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
425 TRACE("First application instance detected OK\n");
426 /* allocate new instance ID */
427 WDML_IncrementInstanceId(pInstance
);
431 /* really need to chain the new one in to the latest here, but after checking conditions
432 * such as trying to start a conversation from an application trying to monitor */
433 reference_inst
= WDML_InstanceList
;
434 TRACE("Subsequent application instance - starting checks\n");
435 while (reference_inst
->next
!= NULL
)
438 * This set of tests will work if application uses same instance Id
439 * at application level once allocated - which is what manual implies
440 * should happen. If someone tries to be
441 * clever (lazy ?) it will fail to pick up that later calls are for
442 * the same application - should we trust them ?
444 if (pInstance
->instanceID
== reference_inst
->instanceID
)
446 /* Check 1 - must be same Client-only state */
448 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
450 ret
= DMLERR_DLL_USAGE
;
454 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
456 if (pInstance
->monitor
!= reference_inst
->monitor
)
458 ret
= DMLERR_INVALIDPARAMETER
;
462 /* Check 3 - must supply different callback address */
464 if (pInstance
->callback
== reference_inst
->callback
)
466 ret
= DMLERR_DLL_USAGE
;
470 reference_inst
= reference_inst
->next
;
472 /* All cleared, add to chain */
474 TRACE("Application Instance checks finished\n");
475 WDML_IncrementInstanceId(pInstance
);
476 reference_inst
->next
= pInstance
;
478 LeaveCriticalSection(&WDML_CritSect
);
480 *pidInst
= pInstance
->instanceID
;
482 /* for deadlock issues, windows must always be created when outside the critical section */
483 wndclass
.cbSize
= sizeof(wndclass
);
485 wndclass
.lpfnWndProc
= WDML_EventProc
;
486 wndclass
.cbClsExtra
= 0;
487 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
488 wndclass
.hInstance
= 0;
490 wndclass
.hCursor
= 0;
491 wndclass
.hbrBackground
= 0;
492 wndclass
.lpszMenuName
= NULL
;
493 wndclass
.lpszClassName
= WDML_szEventClass
;
494 wndclass
.hIconSm
= 0;
496 RegisterClassExW(&wndclass
);
498 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
499 WS_POPUP
, 0, 0, 0, 0,
502 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
504 TRACE("New application instance processing finished OK\n");
508 /* Reinitialisation situation --- FIX */
509 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
511 EnterCriticalSection(&WDML_CritSect
);
513 if (WDML_InstanceList
== NULL
)
515 ret
= DMLERR_INVALIDPARAMETER
;
518 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
519 /* can't reinitialise if we have initialised nothing !! */
520 reference_inst
= WDML_InstanceList
;
521 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
523 * MS allows initialisation without specifying a callback, should we allow addition of the
524 * callback by a later call to initialise ? - if so this lot will have to change
526 while (reference_inst
->next
!= NULL
)
528 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
530 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
532 if (reference_inst
->clientOnly
)
534 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
536 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
538 if (!(afCmd
& APPCMD_CLIENTONLY
))
540 ret
= DMLERR_INVALIDPARAMETER
;
545 /* Check 2 - cannot change monitor modes */
547 if (pInstance
->monitor
!= reference_inst
->monitor
)
549 ret
= DMLERR_INVALIDPARAMETER
;
553 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
555 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
557 ret
= DMLERR_INVALIDPARAMETER
;
562 reference_inst
= reference_inst
->next
;
564 if (reference_inst
->next
== NULL
)
566 ret
= DMLERR_INVALIDPARAMETER
;
569 /* All checked - change relevant flags */
571 reference_inst
->CBFflags
= pInstance
->CBFflags
;
572 reference_inst
->clientOnly
= pInstance
->clientOnly
;
573 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
574 LeaveCriticalSection(&WDML_CritSect
);
577 return DMLERR_NO_ERROR
;
579 HeapFree(GetProcessHeap(), 0, pInstance
);
580 LeaveCriticalSection(&WDML_CritSect
);
584 /******************************************************************************
585 * DdeInitializeA (USER32.@)
587 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
588 DWORD afCmd
, DWORD ulRes
)
590 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
);
593 /******************************************************************************
594 * DdeInitializeW [USER32.@]
595 * Registers an application with the DDEML
598 * pidInst [I] Pointer to instance identifier
599 * pfnCallback [I] Pointer to callback function
600 * afCmd [I] Set of command and filter flags
604 * Success: DMLERR_NO_ERROR
605 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
607 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
608 DWORD afCmd
, DWORD ulRes
)
610 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
);
613 /*****************************************************************
614 * DdeUninitialize [USER32.@] Frees DDEML resources
617 * idInst [I] Instance identifier
624 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
626 /* Stage one - check if we have a handle for this instance
628 WDML_INSTANCE
* pInstance
;
630 WDML_CONV
* pConvNext
;
632 TRACE("(%ld)\n", idInst
);
634 EnterCriticalSection(&WDML_CritSect
);
636 /* First check instance
638 pInstance
= WDML_GetInstance(idInst
);
639 if (pInstance
== NULL
)
641 LeaveCriticalSection(&WDML_CritSect
);
643 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
648 /* first terminate all conversations client side
649 * this shall close existing links...
651 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
653 pConvNext
= pConv
->next
;
654 DdeDisconnect((HCONV
)pConv
);
656 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
657 FIXME("still pending conversations\n");
659 /* then unregister all known service names */
660 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
662 /* Free the nodes that were not freed by this instance
663 * and remove the nodes from the list of HSZ nodes.
665 WDML_FreeAllHSZ(pInstance
);
667 DestroyWindow(pInstance
->hwndEvent
);
669 /* OK now delete the instance handle itself */
671 if (WDML_InstanceList
== pInstance
)
673 /* special case - the first/only entry */
674 WDML_InstanceList
= pInstance
->next
;
678 /* general case, remove entry */
681 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
682 inst
->next
= pInstance
->next
;
684 /* leave crit sect and release the heap entry
686 HeapFree(GetProcessHeap(), 0, pInstance
);
687 LeaveCriticalSection(&WDML_CritSect
);
691 /******************************************************************
692 * WDML_NotifyThreadExit
696 void WDML_NotifyThreadDetach(void)
698 WDML_INSTANCE
* pInstance
;
700 DWORD tid
= GetCurrentThreadId();
702 EnterCriticalSection(&WDML_CritSect
);
703 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
705 next
= pInstance
->next
;
706 if (pInstance
->threadID
== tid
)
708 DdeUninitialize(pInstance
->instanceID
);
711 LeaveCriticalSection(&WDML_CritSect
);
714 /******************************************************************
715 * WDML_InvokeCallback
719 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
720 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
721 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
725 if (pInstance
== NULL
)
727 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
728 pInstance
->win16
? 16 : 32, pInstance
->callback
, uType
, uFmt
,
729 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
730 if (pInstance
->win16
)
732 ret
= WDML_InvokeCallback16(pInstance
->callback
, uType
, uFmt
, hConv
,
733 hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
737 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
739 TRACE("done => %p\n", ret
);
743 /*****************************************************************************
746 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
747 * for an instance Id, or NULL if the entry does not exist
750 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
752 WDML_INSTANCE
* pInstance
;
754 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
756 if (pInstance
->instanceID
== instId
)
758 if (GetCurrentThreadId() != pInstance
->threadID
)
760 FIXME("Tried to get instance from wrong thread\n");
766 TRACE("Instance entry missing\n");
770 /******************************************************************
771 * WDML_GetInstanceFromWnd
775 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
777 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
780 /******************************************************************************
781 * DdeGetLastError [USER32.@] Gets most recent error code
784 * idInst [I] Instance identifier
789 UINT WINAPI
DdeGetLastError(DWORD idInst
)
792 WDML_INSTANCE
* pInstance
;
794 EnterCriticalSection(&WDML_CritSect
);
796 /* First check instance
798 pInstance
= WDML_GetInstance(idInst
);
799 if (pInstance
== NULL
)
801 error_code
= DMLERR_INVALIDPARAMETER
;
805 error_code
= pInstance
->lastError
;
806 pInstance
->lastError
= 0;
809 LeaveCriticalSection(&WDML_CritSect
);
813 /* ================================================================
817 * ================================================================ */
820 /******************************************************************
825 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
829 if (pInstance
== NULL
) return NULL
;
831 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
833 if (pNode
->hsz
== hsz
) break;
835 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
839 /******************************************************************
840 * WDML_MakeAtomFromHsz
842 * Creates a global atom from an existing HSZ
843 * Generally used before sending an HSZ as an atom to a remote app
845 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
847 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
849 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
850 return GlobalAddAtomW(nameBuffer
);
851 WARN("HSZ %p not found\n", hsz
);
855 /******************************************************************
856 * WDML_MakeHszFromAtom
858 * Creates a HSZ from an existing global atom
859 * Generally used while receiving a global atom and transforming it
862 HSZ
WDML_MakeHszFromAtom(WDML_INSTANCE
* pInstance
, ATOM atom
)
864 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
866 if (!atom
) return NULL
;
868 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
870 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
871 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
873 WARN("ATOM 0x%x not found\n", atom
);
877 /******************************************************************
882 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
886 pNode
= WDML_FindNode(pInstance
, hsz
);
887 if (!pNode
) return FALSE
;
893 /******************************************************************************
894 * WDML_DecHSZ (INTERNAL)
896 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
898 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
900 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
902 HSZNode
* pPrev
= NULL
;
905 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
907 /* If we found the node we were looking for and its ref count is one,
910 if (pCurrent
->hsz
== hsz
)
912 if (--pCurrent
->refCount
== 0)
914 if (pCurrent
== pInstance
->nodeList
)
916 pInstance
->nodeList
= pCurrent
->next
;
920 pPrev
->next
= pCurrent
->next
;
922 HeapFree(GetProcessHeap(), 0, pCurrent
);
923 DeleteAtom(HSZ2ATOM(hsz
));
928 WARN("HSZ %p not found\n", hsz
);
933 /******************************************************************************
934 * WDML_FreeAllHSZ (INTERNAL)
936 * Frees up all the strings still allocated in the list and
937 * remove all the nodes from the list of HSZ nodes.
939 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
941 /* Free any strings created in this instance.
943 while (pInstance
->nodeList
!= NULL
)
945 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
949 /******************************************************************************
950 * InsertHSZNode (INTERNAL)
952 * Insert a node to the head of the list.
954 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
958 HSZNode
* pNew
= NULL
;
959 /* Create a new node for this HSZ.
961 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
965 pNew
->next
= pInstance
->nodeList
;
967 pInstance
->nodeList
= pNew
;
971 ERR("Primary HSZ Node allocation failed - out of memory\n");
976 /******************************************************************
981 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
984 WCHAR pString
[MAX_BUFFER_LEN
];
986 /* If psz is null, we have to return only the length
992 cchMax
= MAX_BUFFER_LEN
;
998 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
1001 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
1004 ERR("Unknown code page %d\n", codepage
);
1010 /*****************************************************************
1011 * DdeQueryStringA [USER32.@]
1013 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
1016 WDML_INSTANCE
* pInstance
;
1018 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1020 EnterCriticalSection(&WDML_CritSect
);
1022 /* First check instance
1024 pInstance
= WDML_GetInstance(idInst
);
1025 if (pInstance
!= NULL
)
1027 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
1028 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1030 LeaveCriticalSection(&WDML_CritSect
);
1032 TRACE("returning %ld (%s)\n", ret
, debugstr_a(psz
));
1036 /*****************************************************************
1037 * DdeQueryStringW [USER32.@]
1040 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
1043 WDML_INSTANCE
* pInstance
;
1045 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1047 EnterCriticalSection(&WDML_CritSect
);
1049 /* First check instance
1051 pInstance
= WDML_GetInstance(idInst
);
1052 if (pInstance
!= NULL
)
1054 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
1055 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1057 LeaveCriticalSection(&WDML_CritSect
);
1059 TRACE("returning %ld (%s)\n", ret
, debugstr_w(psz
));
1063 /******************************************************************
1068 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
1075 hsz
= ATOM2HSZ(AddAtomA(ptr
));
1076 TRACE("added atom %s with HSZ %p, \n", debugstr_a(ptr
), hsz
);
1079 hsz
= ATOM2HSZ(AddAtomW(ptr
));
1080 TRACE("added atom %s with HSZ %p, \n", debugstr_w(ptr
), hsz
);
1083 ERR("Unknown code page %d\n", codepage
);
1086 WDML_InsertHSZNode(pInstance
, hsz
);
1090 /*****************************************************************
1091 * DdeCreateStringHandleA [USER32.@]
1094 * Success: String handle
1097 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
1100 WDML_INSTANCE
* pInstance
;
1102 TRACE("(%ld,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
1104 EnterCriticalSection(&WDML_CritSect
);
1106 pInstance
= WDML_GetInstance(idInst
);
1109 if (codepage
== 0) codepage
= CP_WINANSI
;
1110 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1113 LeaveCriticalSection(&WDML_CritSect
);
1118 /******************************************************************************
1119 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1122 * idInst [I] Instance identifier
1123 * psz [I] Pointer to string
1124 * codepage [I] Code page identifier
1126 * Success: String handle
1129 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
1131 WDML_INSTANCE
* pInstance
;
1134 TRACE("(%ld,%s,%d)\n", idInst
, debugstr_w(psz
), codepage
);
1136 EnterCriticalSection(&WDML_CritSect
);
1138 pInstance
= WDML_GetInstance(idInst
);
1141 if (codepage
== 0) codepage
= CP_WINUNICODE
;
1142 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1144 LeaveCriticalSection(&WDML_CritSect
);
1149 /*****************************************************************
1150 * DdeFreeStringHandle (USER32.@)
1151 * RETURNS: success: nonzero
1154 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
1156 WDML_INSTANCE
* pInstance
;
1159 TRACE("(%ld,%p): \n", idInst
, hsz
);
1161 EnterCriticalSection(&WDML_CritSect
);
1163 /* First check instance
1165 pInstance
= WDML_GetInstance(idInst
);
1167 ret
= WDML_DecHSZ(pInstance
, hsz
);
1169 LeaveCriticalSection(&WDML_CritSect
);
1174 /*****************************************************************
1175 * DdeKeepStringHandle (USER32.@)
1177 * RETURNS: success: nonzero
1180 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
1182 WDML_INSTANCE
* pInstance
;
1185 TRACE("(%ld,%p): \n", idInst
, hsz
);
1187 EnterCriticalSection(&WDML_CritSect
);
1189 /* First check instance
1191 pInstance
= WDML_GetInstance(idInst
);
1193 ret
= WDML_IncHSZ(pInstance
, hsz
);
1195 LeaveCriticalSection(&WDML_CritSect
);
1199 /*****************************************************************
1200 * DdeCmpStringHandles (USER32.@)
1202 * Compares the value of two string handles. This comparison is
1203 * not case sensitive.
1206 * -1 The value of hsz1 is zero or less than hsz2
1207 * 0 The values of hsz 1 and 2 are the same or both zero.
1208 * 1 The value of hsz2 is zero of less than hsz1
1210 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
1212 WCHAR psz1
[MAX_BUFFER_LEN
];
1213 WCHAR psz2
[MAX_BUFFER_LEN
];
1217 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
1218 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
1220 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
1222 /* Make sure we found both strings. */
1223 if (ret1
== 0 && ret2
== 0)
1225 /* If both are not found, return both "zero strings". */
1230 /* If hsz1 is a not found, return hsz1 is "zero string". */
1235 /* If hsz2 is a not found, return hsz2 is "zero string". */
1240 /* Compare the two strings we got (case insensitive). */
1241 ret
= lstrcmpiW(psz1
, psz2
);
1242 /* Since strcmp returns any number smaller than
1243 * 0 when the first string is found to be less than
1244 * the second one we must make sure we are returning
1245 * the proper values.
1260 /* ================================================================
1262 * Data handle management
1264 * ================================================================ */
1266 /*****************************************************************
1267 * DdeCreateDataHandle (USER32.@)
1269 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1270 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1272 /* For now, we ignore idInst, hszItem.
1273 * The purpose of these arguments still need to be investigated.
1278 DDE_DATAHANDLE_HEAD
* pDdh
;
1279 WCHAR psz
[MAX_BUFFER_LEN
];
1281 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1283 psz
[0] = HSZ2ATOM(hszItem
);
1287 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,fmt %04x,%x)\n",
1288 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1290 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1293 /* we use the first 4 bytes to store the size */
1294 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1296 ERR("GlobalAlloc failed\n");
1300 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1307 pDdh
->cfFormat
= wFmt
;
1308 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1310 pByte
= (LPBYTE
)(pDdh
+ 1);
1313 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1317 TRACE("=> %p\n", hMem
);
1318 return (HDDEDATA
)hMem
;
1321 /*****************************************************************
1323 * DdeAddData (USER32.@)
1325 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1327 DWORD old_sz
, new_sz
;
1330 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData
, pSrc
, cb
, cbOff
);
1332 pDst
= DdeAccessData(hData
, &old_sz
);
1333 if (!pDst
) return 0;
1335 new_sz
= cb
+ cbOff
;
1336 if (new_sz
> old_sz
)
1338 DdeUnaccessData(hData
);
1339 hData
= GlobalReAlloc((HGLOBAL
)hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1340 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1341 pDst
= DdeAccessData(hData
, &old_sz
);
1344 if (!pDst
) return 0;
1346 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1347 DdeUnaccessData(hData
);
1351 /******************************************************************************
1352 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1356 * hData [I] Handle to DDE object
1357 * pDst [I] Pointer to destination buffer
1358 * cbMax [I] Amount of data to copy
1359 * cbOff [I] Offset to beginning of data
1362 * Size of memory object associated with handle
1364 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1366 DWORD dwSize
, dwRet
;
1369 TRACE("(%p,%p,%ld,%ld)\n", hData
, pDst
, cbMax
, cbOff
);
1371 pByte
= DdeAccessData(hData
, &dwSize
);
1379 else if (cbOff
+ cbMax
< dwSize
)
1383 else if (cbOff
< dwSize
)
1385 dwRet
= dwSize
- cbOff
;
1391 if (pDst
&& dwRet
!= 0)
1393 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1395 DdeUnaccessData(hData
);
1404 /*****************************************************************
1405 * DdeAccessData (USER32.@)
1407 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1409 HGLOBAL hMem
= (HGLOBAL
)hData
;
1410 DDE_DATAHANDLE_HEAD
* pDdh
;
1412 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1414 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1417 ERR("Failed on GlobalLock(%p)\n", hMem
);
1421 if (pcbDataSize
!= NULL
)
1423 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1425 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1426 return (LPBYTE
)(pDdh
+ 1);
1429 /*****************************************************************
1430 * DdeUnaccessData (USER32.@)
1432 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1434 HGLOBAL hMem
= (HGLOBAL
)hData
;
1436 TRACE("(%p)\n", hData
);
1443 /*****************************************************************
1444 * DdeFreeDataHandle (USER32.@)
1446 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1448 TRACE("(%p)\n", hData
);
1449 return GlobalFree((HGLOBAL
)hData
) == 0;
1452 /******************************************************************
1457 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1459 DDE_DATAHANDLE_HEAD
* pDdh
;
1462 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hData
);
1465 ret
= pDdh
->bAppOwned
;
1466 GlobalUnlock((HGLOBAL
)hData
);
1471 /* ================================================================
1473 * Global <=> Data handle management
1475 * ================================================================ */
1477 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1479 * (bytes) (bits) comment
1480 * 0 16 bit fields for options (release, ackreq, response...)
1481 * 2 16 clipboard format
1482 * 4 ? data to be used
1484 HDDEDATA
WDML_Global2DataHandle(HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1492 pDd
= GlobalLock(hMem
);
1493 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1496 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1497 switch (pDd
->cfFormat
)
1500 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1501 pDd
->cfFormat
, hMem
);
1505 ret
= DdeCreateDataHandle(0, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1508 if (size
>= sizeof(BITMAP
))
1510 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1511 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1512 if (size
>= sizeof(BITMAP
) + count
)
1516 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1517 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1518 pDd
->Value
+ sizeof(BITMAP
))))
1520 ret
= DdeCreateDataHandle(0, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1521 0, 0, CF_BITMAP
, 0);
1523 else ERR("Can't create bmp\n");
1527 ERR("Wrong count: %lu / %d\n", size
, sizeof(BITMAP
) + count
);
1529 } else ERR("No bitmap header\n");
1538 /******************************************************************
1539 * WDML_DataHandle2Global
1543 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1544 BOOL fDeferUpd
, BOOL fAckReq
)
1546 DDE_DATAHANDLE_HEAD
* pDdh
;
1550 dwSize
= GlobalSize((HGLOBAL
)hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1551 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hDdeData
);
1554 WINE_DDEHEAD
* wdh
= NULL
;
1556 switch (pDdh
->cfFormat
)
1559 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1560 pDdh
->cfFormat
, hDdeData
);
1564 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1565 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1567 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1571 if (dwSize
>= sizeof(HBITMAP
))
1575 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1577 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1579 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1580 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1581 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1582 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1584 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1585 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1594 wdh
->fResponse
= fResponse
;
1595 wdh
->fRelease
= fRelease
;
1596 wdh
->fDeferUpd
= fDeferUpd
;
1597 wdh
->fAckReq
= fAckReq
;
1598 wdh
->cfFormat
= pDdh
->cfFormat
;
1601 GlobalUnlock((HGLOBAL
)hDdeData
);
1607 /* ================================================================
1611 * ================================================================ */
1613 /******************************************************************
1618 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1620 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1621 WDML_SERVER
* pServer
;
1625 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1626 if (pServer
== NULL
) return NULL
;
1628 pServer
->hszService
= hszService
;
1629 WDML_IncHSZ(pInstance
, hszService
);
1631 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1632 snprintfW(buf2
, 256, fmtW
, buf1
, GetCurrentProcessId());
1633 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1635 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1636 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1638 pServer
->filterOn
= TRUE
;
1640 pServer
->next
= pInstance
->servers
;
1641 pInstance
->servers
= pServer
;
1645 /******************************************************************
1650 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1652 WDML_SERVER
* pPrev
= NULL
;
1653 WDML_SERVER
* pServer
= NULL
;
1655 WDML_CONV
* pConvNext
;
1657 pServer
= pInstance
->servers
;
1659 while (pServer
!= NULL
)
1661 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1663 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1664 pServer
->atomService
, pServer
->atomServiceSpec
);
1665 /* terminate all conversations for given topic */
1666 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1668 pConvNext
= pConv
->next
;
1669 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1671 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1672 /* don't care about return code (whether client window is present or not) */
1673 PostMessageW(pConv
->hwndClient
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndServer
, 0);
1676 if (pServer
== pInstance
->servers
)
1678 pInstance
->servers
= pServer
->next
;
1682 pPrev
->next
= pServer
->next
;
1685 DestroyWindow(pServer
->hwndServer
);
1686 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1687 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1689 GlobalDeleteAtom(pServer
->atomService
);
1690 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1692 HeapFree(GetProcessHeap(), 0, pServer
);
1697 pServer
= pServer
->next
;
1701 /*****************************************************************************
1704 * generic routine to return a pointer to the relevant ServiceNode
1705 * for a given service name, or NULL if the entry does not exist
1708 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1710 WDML_SERVER
* pServer
;
1712 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1714 if (hszService
== pServer
->hszService
)
1719 TRACE("Service name missing\n");
1723 /* ================================================================
1725 * Conversation management
1727 * ================================================================ */
1729 /******************************************************************
1734 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1735 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
1739 /* no converstation yet, add it */
1740 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
1741 if (!pConv
) return NULL
;
1743 pConv
->instance
= pInstance
;
1744 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
1745 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
1746 pConv
->hwndServer
= hwndServer
;
1747 pConv
->hwndClient
= hwndClient
;
1748 pConv
->transactions
= NULL
;
1750 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
1751 /* check if both side of the conversation are of the same instance */
1752 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
1753 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
1755 pConv
->wStatus
|= ST_ISSELF
;
1757 pConv
->wConvst
= XST_NULL
;
1759 pConv
->next
= pInstance
->convs
[side
];
1760 pInstance
->convs
[side
] = pConv
;
1765 /******************************************************************
1770 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1771 HSZ hszService
, HSZ hszTopic
)
1773 WDML_CONV
* pCurrent
= NULL
;
1775 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1777 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
1778 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
1787 /******************************************************************
1792 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
1794 WDML_CONV
* pPrev
= NULL
;
1795 WDML_CONV
* pCurrent
;
1797 WDML_XACT
* pXActNext
;
1803 /* remove any pending transaction */
1804 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
1806 pXActNext
= pXAct
->next
;
1807 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
1810 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
1812 /* FIXME: should we keep the window around ? it seems so (at least on client side
1813 * to let QueryConvInfo work after conv termination, but also to implement
1816 /* destroy conversation window, but first remove pConv from hWnd.
1817 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1819 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
1820 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
1822 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
1824 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
1825 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
1827 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
1829 if (pCurrent
== pRef
)
1831 if (pCurrent
== pRef
->instance
->convs
[side
])
1833 pRef
->instance
->convs
[side
] = pCurrent
->next
;
1837 pPrev
->next
= pCurrent
->next
;
1840 HeapFree(GetProcessHeap(), 0, pCurrent
);
1846 /******************************************************************
1847 * WDML_EnableCallback
1849 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
1851 if (wCmd
== EC_DISABLE
)
1853 FIXME("EC_DISABLE is not implemented\n");
1857 if (wCmd
== EC_QUERYWAITING
)
1858 return pConv
->transactions
? TRUE
: FALSE
;
1860 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
1862 FIXME("Unknown command code %04x\n", wCmd
);
1866 while (pConv
->transactions
)
1868 WDML_XACT
*pXAct
= pConv
->transactions
;
1869 WDML_UnQueueTransaction(pConv
, pXAct
);
1871 if (pConv
->wStatus
& ST_CLIENT
)
1873 /*WDML_ClientHandle(pConv, pXAct);*/
1874 FIXME("Client delayed transaction queue handling is not supported\n");
1877 WDML_ServerHandle(pConv
, pXAct
);
1879 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1881 if (wCmd
== EC_ENABLEONE
) break;
1886 /*****************************************************************
1887 * DdeEnableCallback (USER32.@)
1889 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
1894 TRACE("(%ld, %p, %04x)\n", idInst
, hConv
, wCmd
);
1896 EnterCriticalSection(&WDML_CritSect
);
1898 pConv
= WDML_GetConv(hConv
, TRUE
);
1900 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
1901 ret
= WDML_EnableCallback(pConv
, wCmd
);
1903 LeaveCriticalSection(&WDML_CritSect
);
1907 /******************************************************************
1912 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
1914 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
1916 /* FIXME: should do better checking */
1917 if (pConv
== NULL
) return NULL
;
1919 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
1921 FIXME("found conv but ain't connected\n");
1924 if (GetCurrentThreadId() != pConv
->instance
->threadID
)
1926 FIXME("wrong thread ID\n");
1933 /******************************************************************
1934 * WDML_GetConvFromWnd
1938 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
1940 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
1943 /******************************************************************
1948 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
1949 BOOL fBusy
, BOOL fAck
, UINT pmt
, LPARAM lParam
, UINT oldMsg
)
1954 if (side
== WDML_SERVER_SIDE
)
1956 from
= pConv
->hwndServer
;
1957 to
= pConv
->hwndClient
;
1961 to
= pConv
->hwndServer
;
1962 from
= pConv
->hwndClient
;
1965 ddeAck
.bAppReturnCode
= appRetCode
;
1966 ddeAck
.reserved
= 0;
1967 ddeAck
.fBusy
= fBusy
;
1970 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
1972 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
1973 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
1974 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
1976 pConv
->wStatus
&= ~ST_CONNECTED
;
1977 FreeDDElParam(WM_DDE_ACK
, lParam
);
1983 /*****************************************************************
1984 * DdeSetUserHandle (USER32.@)
1986 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
1991 TRACE("(%p,%lx,%lx)\n", hConv
, id
, hUser
);
1993 EnterCriticalSection(&WDML_CritSect
);
1995 pConv
= WDML_GetConv(hConv
, FALSE
);
2003 pConv
->hUser
= hUser
;
2009 pXAct
= WDML_FindTransaction(pConv
, id
);
2012 pXAct
->hUser
= hUser
;
2016 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2021 LeaveCriticalSection(&WDML_CritSect
);
2025 /******************************************************************
2026 * WDML_GetLocalConvInfo
2030 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2036 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2037 ci
->hszSvcPartner
= pConv
->hszService
;
2038 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2039 ci
->hszTopic
= pConv
->hszTopic
;
2040 ci
->wStatus
= pConv
->wStatus
;
2042 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2044 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2046 if (pLink
->hConv
== (HCONV
)pConv
)
2048 ci
->wStatus
|= ST_ADVISE
;
2053 /* FIXME: non handled status flags:
2059 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2061 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2063 ci
->ConvCtxt
= pConv
->convContext
;
2064 if (ci
->wStatus
& ST_CLIENT
)
2066 ci
->hwnd
= pConv
->hwndClient
;
2067 ci
->hwndPartner
= pConv
->hwndServer
;
2071 ci
->hwnd
= pConv
->hwndServer
;
2072 ci
->hwndPartner
= pConv
->hwndClient
;
2076 ci
->hUser
= pConv
->hUser
;
2085 pXAct
= WDML_FindTransaction(pConv
, id
);
2088 ci
->hUser
= pXAct
->hUser
;
2089 ci
->hszItem
= pXAct
->hszItem
;
2090 ci
->wFmt
= pXAct
->wFmt
;
2091 ci
->wType
= pXAct
->wType
;
2096 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2102 /******************************************************************
2103 * DdeQueryConvInfo (USER32.@)
2106 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2108 UINT ret
= lpConvInfo
->cb
;
2112 TRACE("(%p,%lx,%p)\n", hConv
, id
, lpConvInfo
);
2116 FIXME("hConv is NULL\n");
2120 EnterCriticalSection(&WDML_CritSect
);
2122 pConv
= WDML_GetConv(hConv
, FALSE
);
2123 if (pConv
!= NULL
&& !WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2127 else if ((ULONG_PTR
)hConv
& 1)
2129 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2132 FIXME("Request on remote conversation information is not implemented yet\n");
2136 LeaveCriticalSection(&WDML_CritSect
);
2138 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2142 /* ================================================================
2144 * Link (hot & warm) management
2146 * ================================================================ */
2148 /******************************************************************
2153 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2154 UINT wType
, HSZ hszItem
, UINT wFmt
)
2158 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
2165 pLink
->hConv
= hConv
;
2166 pLink
->transactionType
= wType
;
2167 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
2169 pLink
->next
= pInstance
->links
[side
];
2170 pInstance
->links
[side
] = pLink
;
2173 /******************************************************************
2178 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2179 HSZ hszItem
, UINT uFmt
)
2181 WDML_LINK
* pPrev
= NULL
;
2182 WDML_LINK
* pCurrent
= NULL
;
2184 pCurrent
= pInstance
->links
[side
];
2186 while (pCurrent
!= NULL
)
2188 if (pCurrent
->hConv
== hConv
&&
2189 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2190 pCurrent
->uFmt
== uFmt
)
2192 if (pCurrent
== pInstance
->links
[side
])
2194 pInstance
->links
[side
] = pCurrent
->next
;
2198 pPrev
->next
= pCurrent
->next
;
2201 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2202 HeapFree(GetProcessHeap(), 0, pCurrent
);
2207 pCurrent
= pCurrent
->next
;
2211 /* this function is called to remove all links related to the conv.
2212 It should be called from both client and server when terminating
2215 /******************************************************************
2216 * WDML_RemoveAllLinks
2220 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
2222 WDML_LINK
* pPrev
= NULL
;
2223 WDML_LINK
* pCurrent
= NULL
;
2224 WDML_LINK
* pNext
= NULL
;
2226 pCurrent
= pInstance
->links
[side
];
2228 while (pCurrent
!= NULL
)
2230 if (pCurrent
->hConv
== (HCONV
)pConv
)
2232 if (pCurrent
== pInstance
->links
[side
])
2234 pInstance
->links
[side
] = pCurrent
->next
;
2235 pNext
= pCurrent
->next
;
2239 pPrev
->next
= pCurrent
->next
;
2240 pNext
= pCurrent
->next
;
2243 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2245 HeapFree(GetProcessHeap(), 0, pCurrent
);
2252 pCurrent
= pCurrent
->next
;
2261 /******************************************************************
2266 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2267 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
2269 WDML_LINK
* pCurrent
= NULL
;
2271 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2273 /* we don't need to check for transaction type as it can be altered */
2275 if (pCurrent
->hConv
== hConv
&&
2276 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2277 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
2287 /* ================================================================
2289 * Transaction management
2291 * ================================================================ */
2293 /******************************************************************
2294 * WDML_AllocTransaction
2296 * Alloc a transaction structure for handling the message ddeMsg
2298 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
2299 UINT wFmt
, HSZ hszItem
)
2302 static WORD tid
= 1; /* FIXME: wrap around */
2304 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
2307 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
2311 pXAct
->xActID
= tid
++;
2312 pXAct
->ddeMsg
= ddeMsg
;
2313 pXAct
->hDdeData
= 0;
2318 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
2326 /******************************************************************
2327 * WDML_QueueTransaction
2329 * Adds a transaction to the list of transaction
2331 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2335 /* advance to last in queue */
2336 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
2340 /******************************************************************
2341 * WDML_UnQueueTransaction
2345 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2349 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
2360 /******************************************************************
2361 * WDML_FreeTransaction
2365 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
2367 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2368 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
2370 GlobalFree(pXAct
->hMem
);
2372 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
2374 HeapFree(GetProcessHeap(), 0, pXAct
);
2377 /******************************************************************
2378 * WDML_FindTransaction
2382 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2387 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2389 if (pXAct
->xActID
== tid
)
2395 /* ================================================================
2397 * Information broadcast across DDEML implementations
2399 * ================================================================ */
2401 struct tagWDML_BroadcastPmt
2409 /******************************************************************
2410 * WDML_BroadcastEnumProc
2414 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2416 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2419 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2420 lstrcmpiW(buffer
, s
->clsName
) == 0)
2422 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2427 /******************************************************************
2428 * WDML_BroadcastDDEWindows
2432 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2434 struct tagWDML_BroadcastPmt s
;
2436 s
.clsName
= clsName
;
2440 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);