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
27 #include "dde_private.h"
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
33 /* convert between ATOM and HSZ avoiding compiler warnings */
34 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
35 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
37 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
38 static LONG WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
39 const WCHAR WDML_szEventClass
[] = {'D','d','e','E','v','e','n','t','C','l','a','s','s',0};
41 /* protection for instance list */
42 CRITICAL_SECTION WDML_CritSect
;
43 CRITICAL_SECTION_DEBUG critsect_debug
=
46 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
47 0, 0, { (DWORD_PTR
)(__FILE__
": WDML_CritSect") }
49 CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
51 /* ================================================================
53 * Pure DDE (non DDEML) management
55 * ================================================================ */
58 /*****************************************************************
59 * PackDDElParam (USER32.@)
64 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
75 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
77 ERR("GlobalAlloc failed\n");
80 if (!(params
= GlobalLock(hMem
)))
82 ERR("GlobalLock failed (%p)\n", hMem
);
94 return MAKELPARAM(uiLo
, uiHi
);
99 /*****************************************************************
100 * UnpackDDElParam (USER32.@)
106 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
107 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
117 if (!lParam
|| !(params
= GlobalLock((HGLOBAL
)lParam
)))
123 if (uiLo
) *uiLo
= params
[0];
124 if (uiHi
) *uiHi
= params
[1];
125 GlobalUnlock( (HGLOBAL
)lParam
);
130 if (uiHi
) *uiHi
= lParam
;
134 if (uiLo
) *uiLo
= LOWORD(lParam
);
135 if (uiHi
) *uiHi
= HIWORD(lParam
);
141 /*****************************************************************
142 * FreeDDElParam (USER32.@)
148 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
156 /* first check if it's a global handle */
157 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
158 return !GlobalFree( (HGLOBAL
)lParam
);
166 /*****************************************************************
167 * ReuseDDElParam (USER32.@)
172 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
173 UINT_PTR uiLo
, UINT_PTR uiHi
)
189 if (!lParam
) return 0;
190 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
192 ERR("GlobalLock failed\n");
197 TRACE("Reusing pack %08lx %08lx\n", uiLo
, uiHi
);
198 GlobalUnlock( (HGLOBAL
)lParam
);
202 FreeDDElParam( msgIn
, lParam
);
206 FreeDDElParam( msgIn
, lParam
);
207 return MAKELPARAM(uiLo
, uiHi
);
211 return PackDDElParam( msgOut
, uiLo
, uiHi
);
215 /*****************************************************************
216 * ImpersonateDdeClientWindow (USER32.@)
219 * hWndClient [I] handle to DDE client window
220 * hWndServer [I] handle to DDE server window
222 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
224 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
228 /*****************************************************************
229 * DdeSetQualityOfService (USER32.@)
232 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, CONST SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
233 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
235 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
239 /* ================================================================
241 * Instance management
243 * ================================================================ */
245 /******************************************************************************
246 * IncrementInstanceId
248 * generic routine to increment the max instance Id and allocate a new application instance
250 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
252 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
254 pInstance
->instanceID
= id
;
255 TRACE("New instance id %d allocated\n", id
);
258 /******************************************************************
263 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
265 WDML_INSTANCE
* pInstance
;
270 case WM_WDML_REGISTER
:
271 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
272 /* try calling the Callback */
273 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
275 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
276 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
277 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
278 WDML_DecHSZ(pInstance
, hsz1
);
279 WDML_DecHSZ(pInstance
, hsz2
);
283 case WM_WDML_UNREGISTER
:
284 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
285 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
287 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
288 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
289 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
290 WDML_DecHSZ(pInstance
, hsz1
);
291 WDML_DecHSZ(pInstance
, hsz2
);
295 case WM_WDML_CONNECT_CONFIRM
:
296 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
297 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
300 /* confirm connection...
301 * lookup for this conv handle
303 HWND client
= (HWND
)wParam
;
304 HWND server
= (HWND
)lParam
;
305 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
307 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
312 pConv
->wStatus
|= ST_ISLOCAL
;
314 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
315 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
316 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
321 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
326 /******************************************************************
331 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
332 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
)
334 WDML_INSTANCE
* pInstance
;
335 WDML_INSTANCE
* reference_inst
;
337 WNDCLASSEXW wndclass
;
339 TRACE("(%p,%p,0x%x,%d,0x%x)\n",
340 pidInst
, pfnCallback
, afCmd
, ulRes
, bUnicode
);
344 ERR("Reserved value not zero? What does this mean?\n");
345 /* trap this and no more until we know more */
346 return DMLERR_NO_ERROR
;
349 /* grab enough heap for one control struct - not really necessary for re-initialise
350 * but allows us to use same validation routines */
351 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
352 if (pInstance
== NULL
)
354 /* catastrophe !! warn user & abort */
355 ERR("Instance create failed - out of memory\n");
356 return DMLERR_SYS_ERROR
;
358 pInstance
->next
= NULL
;
359 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
361 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
363 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
364 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
365 pInstance
->threadID
= GetCurrentThreadId();
366 pInstance
->callback
= *pfnCallback
;
367 pInstance
->unicode
= bUnicode
;
368 pInstance
->nodeList
= NULL
; /* node will be added later */
369 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
370 pInstance
->wStatus
= 0;
371 pInstance
->lastError
= DMLERR_NO_ERROR
;
372 pInstance
->servers
= NULL
;
373 pInstance
->convs
[0] = NULL
;
374 pInstance
->convs
[1] = NULL
;
375 pInstance
->links
[0] = NULL
;
376 pInstance
->links
[1] = NULL
;
378 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
380 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
382 if (!pInstance
->clientOnly
)
384 /* Check for other way of setting Client-only !! */
385 pInstance
->clientOnly
=
386 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
389 TRACE("instance created - checking validity\n");
393 /* Initialisation of new Instance Identifier */
394 TRACE("new instance, callback %p flags %X\n",pfnCallback
,afCmd
);
396 EnterCriticalSection(&WDML_CritSect
);
398 if (WDML_InstanceList
== NULL
)
400 /* can't be another instance in this case, assign to the base pointer */
401 WDML_InstanceList
= pInstance
;
403 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
405 * ------------------------------- NOTE NOTE NOTE --------------------------
407 * the manual is not clear if this condition
408 * applies to the first call to DdeInitialize from an application, or the
409 * first call for a given callback !!!
412 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
413 TRACE("First application instance detected OK\n");
414 /* allocate new instance ID */
415 WDML_IncrementInstanceId(pInstance
);
419 /* really need to chain the new one in to the latest here, but after checking conditions
420 * such as trying to start a conversation from an application trying to monitor */
421 reference_inst
= WDML_InstanceList
;
422 TRACE("Subsequent application instance - starting checks\n");
423 while (reference_inst
->next
!= NULL
)
426 * This set of tests will work if application uses same instance Id
427 * at application level once allocated - which is what manual implies
428 * should happen. If someone tries to be
429 * clever (lazy ?) it will fail to pick up that later calls are for
430 * the same application - should we trust them ?
432 if (pInstance
->instanceID
== reference_inst
->instanceID
)
434 /* Check 1 - must be same Client-only state */
436 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
438 ret
= DMLERR_DLL_USAGE
;
442 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
444 if (pInstance
->monitor
!= reference_inst
->monitor
)
446 ret
= DMLERR_INVALIDPARAMETER
;
450 /* Check 3 - must supply different callback address */
452 if (pInstance
->callback
== reference_inst
->callback
)
454 ret
= DMLERR_DLL_USAGE
;
458 reference_inst
= reference_inst
->next
;
460 /* All cleared, add to chain */
462 TRACE("Application Instance checks finished\n");
463 WDML_IncrementInstanceId(pInstance
);
464 reference_inst
->next
= pInstance
;
466 LeaveCriticalSection(&WDML_CritSect
);
468 *pidInst
= pInstance
->instanceID
;
470 /* for deadlock issues, windows must always be created when outside the critical section */
471 wndclass
.cbSize
= sizeof(wndclass
);
473 wndclass
.lpfnWndProc
= WDML_EventProc
;
474 wndclass
.cbClsExtra
= 0;
475 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
476 wndclass
.hInstance
= 0;
478 wndclass
.hCursor
= 0;
479 wndclass
.hbrBackground
= 0;
480 wndclass
.lpszMenuName
= NULL
;
481 wndclass
.lpszClassName
= WDML_szEventClass
;
482 wndclass
.hIconSm
= 0;
484 RegisterClassExW(&wndclass
);
486 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
487 WS_POPUP
, 0, 0, 0, 0,
490 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
492 TRACE("New application instance processing finished OK\n");
496 /* Reinitialisation situation --- FIX */
497 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
499 EnterCriticalSection(&WDML_CritSect
);
501 if (WDML_InstanceList
== NULL
)
503 ret
= DMLERR_INVALIDPARAMETER
;
506 /* can't reinitialise if we have initialised nothing !! */
507 reference_inst
= WDML_InstanceList
;
508 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
510 * MS allows initialisation without specifying a callback, should we allow addition of the
511 * callback by a later call to initialise ? - if so this lot will have to change
513 while (reference_inst
->next
!= NULL
)
515 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
517 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
519 if (reference_inst
->clientOnly
)
521 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
523 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
525 if (!(afCmd
& APPCMD_CLIENTONLY
))
527 ret
= DMLERR_INVALIDPARAMETER
;
532 /* Check 2 - cannot change monitor modes */
534 if (pInstance
->monitor
!= reference_inst
->monitor
)
536 ret
= DMLERR_INVALIDPARAMETER
;
540 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
542 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
544 ret
= DMLERR_INVALIDPARAMETER
;
549 reference_inst
= reference_inst
->next
;
551 if (reference_inst
->next
== NULL
)
553 ret
= DMLERR_INVALIDPARAMETER
;
556 /* All checked - change relevant flags */
558 reference_inst
->CBFflags
= pInstance
->CBFflags
;
559 reference_inst
->clientOnly
= pInstance
->clientOnly
;
560 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
562 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
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
);
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
);
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[%p] (%x %x %p %p %p %p %lx %lx)\n",
720 pInstance
->callback
, uType
, uFmt
,
721 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
722 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
723 TRACE("done => %p\n", ret
);
727 /*****************************************************************************
730 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
731 * for an instance Id, or NULL if the entry does not exist
734 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
736 WDML_INSTANCE
* pInstance
;
738 EnterCriticalSection(&WDML_CritSect
);
740 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
742 if (pInstance
->instanceID
== instId
)
744 if (GetCurrentThreadId() != pInstance
->threadID
)
746 FIXME("Tried to get instance from wrong thread\n");
753 LeaveCriticalSection(&WDML_CritSect
);
756 WARN("Instance entry missing for id %04x\n", instId
);
760 /******************************************************************
761 * WDML_GetInstanceFromWnd
765 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
767 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
770 /******************************************************************************
771 * DdeGetLastError [USER32.@] Gets most recent error code
774 * idInst [I] Instance identifier
779 UINT WINAPI
DdeGetLastError(DWORD idInst
)
782 WDML_INSTANCE
* pInstance
;
784 /* First check instance
786 pInstance
= WDML_GetInstance(idInst
);
787 if (pInstance
== NULL
)
789 error_code
= DMLERR_INVALIDPARAMETER
;
793 error_code
= pInstance
->lastError
;
794 pInstance
->lastError
= 0;
800 /******************************************************************
801 * WDML_SetAllLastError
805 static void WDML_SetAllLastError(DWORD lastError
)
808 WDML_INSTANCE
* pInstance
;
809 threadID
= GetCurrentThreadId();
810 pInstance
= WDML_InstanceList
;
813 if (pInstance
->threadID
== threadID
)
814 pInstance
->lastError
= lastError
;
815 pInstance
= pInstance
->next
;
819 /* ================================================================
823 * ================================================================ */
826 /******************************************************************
831 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
835 if (pInstance
== NULL
) return NULL
;
837 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
839 if (pNode
->hsz
== hsz
) break;
841 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
845 /******************************************************************
846 * WDML_MakeAtomFromHsz
848 * Creates a global atom from an existing HSZ
849 * Generally used before sending an HSZ as an atom to a remote app
851 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
853 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
855 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
856 return GlobalAddAtomW(nameBuffer
);
857 WARN("HSZ %p not found\n", hsz
);
861 /******************************************************************
862 * WDML_MakeHszFromAtom
864 * Creates a HSZ from an existing global atom
865 * Generally used while receiving a global atom and transforming it
868 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
870 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
872 if (!atom
) return NULL
;
874 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
876 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
877 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
879 WARN("ATOM 0x%x not found\n", atom
);
883 /******************************************************************
888 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
892 pNode
= WDML_FindNode(pInstance
, hsz
);
893 if (!pNode
) return FALSE
;
899 /******************************************************************************
900 * WDML_DecHSZ (INTERNAL)
902 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
904 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
906 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
908 HSZNode
* pPrev
= NULL
;
911 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
913 /* If we found the node we were looking for and its ref count is one,
916 if (pCurrent
->hsz
== hsz
)
918 if (--pCurrent
->refCount
== 0)
920 if (pCurrent
== pInstance
->nodeList
)
922 pInstance
->nodeList
= pCurrent
->next
;
926 pPrev
->next
= pCurrent
->next
;
928 HeapFree(GetProcessHeap(), 0, pCurrent
);
929 DeleteAtom(HSZ2ATOM(hsz
));
934 WARN("HSZ %p not found\n", hsz
);
939 /******************************************************************************
940 * WDML_FreeAllHSZ (INTERNAL)
942 * Frees up all the strings still allocated in the list and
943 * remove all the nodes from the list of HSZ nodes.
945 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
947 /* Free any strings created in this instance.
949 while (pInstance
->nodeList
!= NULL
)
951 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
955 /******************************************************************************
956 * InsertHSZNode (INTERNAL)
958 * Insert a node to the head of the list.
960 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
964 HSZNode
* pNew
= NULL
;
965 /* Create a new node for this HSZ.
967 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
971 pNew
->next
= pInstance
->nodeList
;
973 pInstance
->nodeList
= pNew
;
977 ERR("Primary HSZ Node allocation failed - out of memory\n");
982 /******************************************************************
987 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
990 WCHAR pString
[MAX_BUFFER_LEN
];
992 /* If psz is null, we have to return only the length
998 cchMax
= MAX_BUFFER_LEN
;
1001 /* if there is no input windows returns a NULL string */
1012 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
1015 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
1018 ERR("Unknown code page %d\n", codepage
);
1024 /*****************************************************************
1025 * DdeQueryStringA [USER32.@]
1027 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
1030 WDML_INSTANCE
* pInstance
;
1032 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1034 /* First check instance
1036 pInstance
= WDML_GetInstance(idInst
);
1037 if (pInstance
!= NULL
)
1039 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
1040 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1043 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
1047 /*****************************************************************
1048 * DdeQueryStringW [USER32.@]
1051 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
1054 WDML_INSTANCE
* pInstance
;
1056 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1058 /* First check instance
1060 pInstance
= WDML_GetInstance(idInst
);
1061 if (pInstance
!= NULL
)
1063 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
1064 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1067 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
1071 /******************************************************************
1076 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
1083 hsz
= ATOM2HSZ(AddAtomA(ptr
));
1084 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
1087 hsz
= ATOM2HSZ(AddAtomW(ptr
));
1088 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
1091 ERR("Unknown code page %d\n", codepage
);
1094 WDML_InsertHSZNode(pInstance
, hsz
);
1098 /*****************************************************************
1099 * DdeCreateStringHandleA [USER32.@]
1101 * See DdeCreateStringHandleW.
1103 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
1106 WDML_INSTANCE
* pInstance
;
1108 TRACE("(%d,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
1110 pInstance
= WDML_GetInstance(idInst
);
1111 if (pInstance
== NULL
)
1112 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1115 if (codepage
== 0) codepage
= CP_WINANSI
;
1116 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1123 /******************************************************************************
1124 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1127 * idInst [I] Instance identifier
1128 * psz [I] Pointer to string
1129 * codepage [I] Code page identifier
1131 * Success: String handle
1134 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
1136 WDML_INSTANCE
* pInstance
;
1139 pInstance
= WDML_GetInstance(idInst
);
1140 if (pInstance
== NULL
)
1141 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1144 if (codepage
== 0) codepage
= CP_WINUNICODE
;
1145 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1151 /*****************************************************************
1152 * DdeFreeStringHandle (USER32.@)
1157 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
1159 WDML_INSTANCE
* pInstance
;
1162 TRACE("(%d,%p):\n", idInst
, hsz
);
1164 /* First check instance
1166 pInstance
= WDML_GetInstance(idInst
);
1168 ret
= WDML_DecHSZ(pInstance
, hsz
);
1173 /*****************************************************************
1174 * DdeKeepStringHandle (USER32.@)
1180 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
1182 WDML_INSTANCE
* pInstance
;
1185 TRACE("(%d,%p):\n", idInst
, hsz
);
1187 /* First check instance
1189 pInstance
= WDML_GetInstance(idInst
);
1191 ret
= WDML_IncHSZ(pInstance
, hsz
);
1196 /*****************************************************************
1197 * DdeCmpStringHandles (USER32.@)
1199 * Compares the value of two string handles. This comparison is
1200 * not case sensitive.
1203 * hsz1 [I] Handle to the first string
1204 * hsz2 [I] Handle to the second string
1207 * -1 The value of hsz1 is zero or less than hsz2
1208 * 0 The values of hsz 1 and 2 are the same or both zero.
1209 * 1 The value of hsz2 is zero of less than hsz1
1211 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
1213 WCHAR psz1
[MAX_BUFFER_LEN
];
1214 WCHAR psz2
[MAX_BUFFER_LEN
];
1218 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
1219 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
1221 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
1223 /* Make sure we found both strings. */
1224 if (ret1
== 0 && ret2
== 0)
1226 /* If both are not found, return both "zero strings". */
1231 /* If hsz1 is a not found, return hsz1 is "zero string". */
1236 /* If hsz2 is a not found, return hsz2 is "zero string". */
1241 /* Compare the two strings we got (case insensitive). */
1242 ret
= lstrcmpiW(psz1
, psz2
);
1243 /* Since strcmp returns any number smaller than
1244 * 0 when the first string is found to be less than
1245 * the second one we must make sure we are returning
1246 * the proper values.
1261 /* ================================================================
1263 * Data handle management
1265 * ================================================================ */
1267 /*****************************************************************
1268 * DdeCreateDataHandle (USER32.@)
1270 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1271 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1274 /* Other than check for validity we will ignore for now idInst, hszItem.
1275 * The purpose of these arguments still need to be investigated.
1278 WDML_INSTANCE
* pInstance
;
1281 DDE_DATAHANDLE_HEAD
* pDdh
;
1282 WCHAR psz
[MAX_BUFFER_LEN
];
1284 pInstance
= WDML_GetInstance(idInst
);
1285 if (pInstance
== NULL
)
1287 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1291 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1293 psz
[0] = HSZ2ATOM(hszItem
);
1297 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1298 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1300 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1303 /* we use the first 4 bytes to store the size */
1304 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1306 ERR("GlobalAlloc failed\n");
1310 pDdh
= GlobalLock(hMem
);
1317 pDdh
->cfFormat
= wFmt
;
1318 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1320 pByte
= (LPBYTE
)(pDdh
+ 1);
1323 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1327 TRACE("=> %p\n", hMem
);
1331 /*****************************************************************
1333 * DdeAddData (USER32.@)
1335 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1337 DWORD old_sz
, new_sz
;
1340 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData
, pSrc
, cb
, cbOff
);
1342 pDst
= DdeAccessData(hData
, &old_sz
);
1343 if (!pDst
) return 0;
1345 new_sz
= cb
+ cbOff
;
1346 if (new_sz
> old_sz
)
1348 DdeUnaccessData(hData
);
1349 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1350 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1351 pDst
= DdeAccessData(hData
, &old_sz
);
1354 if (!pDst
) return 0;
1356 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1357 DdeUnaccessData(hData
);
1361 /******************************************************************************
1362 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1366 * hData [I] Handle to DDE object
1367 * pDst [I] Pointer to destination buffer
1368 * cbMax [I] Amount of data to copy
1369 * cbOff [I] Offset to beginning of data
1372 * Size of memory object associated with handle
1374 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1376 DWORD dwSize
, dwRet
;
1379 TRACE("(%p,%p,%d,%d)\n", hData
, pDst
, cbMax
, cbOff
);
1381 pByte
= DdeAccessData(hData
, &dwSize
);
1389 else if (cbOff
+ cbMax
< dwSize
)
1393 else if (cbOff
< dwSize
)
1395 dwRet
= dwSize
- cbOff
;
1401 if (pDst
&& dwRet
!= 0)
1403 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1405 DdeUnaccessData(hData
);
1414 /*****************************************************************
1415 * DdeAccessData (USER32.@)
1417 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1419 HGLOBAL hMem
= hData
;
1420 DDE_DATAHANDLE_HEAD
* pDdh
;
1422 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1424 pDdh
= GlobalLock(hMem
);
1427 ERR("Failed on GlobalLock(%p)\n", hMem
);
1431 if (pcbDataSize
!= NULL
)
1433 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1435 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1436 return (LPBYTE
)(pDdh
+ 1);
1439 /*****************************************************************
1440 * DdeUnaccessData (USER32.@)
1442 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1444 HGLOBAL hMem
= hData
;
1446 TRACE("(%p)\n", hData
);
1453 /*****************************************************************
1454 * DdeFreeDataHandle (USER32.@)
1456 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1458 TRACE("(%p)\n", hData
);
1460 /* 1 is the handle value returned by an asynchronous operation. */
1461 if (hData
== (HDDEDATA
)1)
1464 return GlobalFree(hData
) == 0;
1467 /******************************************************************
1472 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1474 DDE_DATAHANDLE_HEAD
* pDdh
;
1477 pDdh
= GlobalLock(hData
);
1480 ret
= pDdh
->bAppOwned
;
1481 GlobalUnlock(hData
);
1486 /* ================================================================
1488 * Global <=> Data handle management
1490 * ================================================================ */
1492 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1494 * (bytes) (bits) comment
1495 * 0 16 bit fields for options (release, ackreq, response...)
1496 * 2 16 clipboard format
1497 * 4 ? data to be used
1499 HDDEDATA
WDML_Global2DataHandle(WDML_CONV
* pConv
, HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1507 pDd
= GlobalLock(hMem
);
1508 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1511 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1512 switch (pDd
->cfFormat
)
1515 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1516 pDd
->cfFormat
, hMem
);
1520 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1523 if (size
>= sizeof(BITMAP
))
1525 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1526 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1527 if (size
>= sizeof(BITMAP
) + count
)
1531 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1532 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1533 pDd
->Value
+ sizeof(BITMAP
))))
1535 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1536 0, 0, CF_BITMAP
, 0);
1538 else ERR("Can't create bmp\n");
1542 ERR("Wrong count: %u / %d\n", size
, count
);
1544 } else ERR("No bitmap header\n");
1553 /******************************************************************
1554 * WDML_DataHandle2Global
1558 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1559 BOOL fDeferUpd
, BOOL fAckReq
)
1561 DDE_DATAHANDLE_HEAD
* pDdh
;
1565 dwSize
= GlobalSize(hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1566 pDdh
= GlobalLock(hDdeData
);
1569 WINE_DDEHEAD
* wdh
= NULL
;
1571 switch (pDdh
->cfFormat
)
1574 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1575 pDdh
->cfFormat
, hDdeData
);
1579 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1580 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1582 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1586 if (dwSize
>= sizeof(HBITMAP
))
1590 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1592 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1594 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1595 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1596 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1597 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1599 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1600 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1609 wdh
->fResponse
= fResponse
;
1610 wdh
->fRelease
= fRelease
;
1611 wdh
->fDeferUpd
= fDeferUpd
;
1612 wdh
->fAckReq
= fAckReq
;
1613 wdh
->cfFormat
= pDdh
->cfFormat
;
1616 GlobalUnlock(hDdeData
);
1622 /* ================================================================
1626 * ================================================================ */
1628 /******************************************************************
1633 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1635 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','*','x',')',0};
1636 WDML_SERVER
* pServer
;
1640 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1641 if (pServer
== NULL
) return NULL
;
1643 pServer
->hszService
= hszService
;
1644 WDML_IncHSZ(pInstance
, hszService
);
1646 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1647 snprintfW(buf2
, 256, fmtW
, buf1
, 2*sizeof(ULONG_PTR
), GetCurrentProcessId());
1648 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1650 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1651 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1653 pServer
->filterOn
= TRUE
;
1655 pServer
->next
= pInstance
->servers
;
1656 pInstance
->servers
= pServer
;
1660 /******************************************************************
1665 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1667 WDML_SERVER
* pPrev
= NULL
;
1668 WDML_SERVER
* pServer
= NULL
;
1670 WDML_CONV
* pConvNext
;
1672 pServer
= pInstance
->servers
;
1674 while (pServer
!= NULL
)
1676 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1678 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1679 pServer
->atomService
, pServer
->atomServiceSpec
);
1680 /* terminate all conversations for given topic */
1681 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1683 pConvNext
= pConv
->next
;
1684 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1686 HWND client
= pConv
->hwndClient
, server
= pConv
->hwndServer
;
1687 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1688 /* don't care about return code (whether client window is present or not) */
1689 PostMessageW(client
, WM_DDE_TERMINATE
, (WPARAM
)server
, 0);
1692 if (pServer
== pInstance
->servers
)
1694 pInstance
->servers
= pServer
->next
;
1698 pPrev
->next
= pServer
->next
;
1701 DestroyWindow(pServer
->hwndServer
);
1702 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1703 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1705 GlobalDeleteAtom(pServer
->atomService
);
1706 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1708 HeapFree(GetProcessHeap(), 0, pServer
);
1713 pServer
= pServer
->next
;
1717 /*****************************************************************************
1720 * generic routine to return a pointer to the relevant ServiceNode
1721 * for a given service name, or NULL if the entry does not exist
1724 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1726 WDML_SERVER
* pServer
;
1728 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1730 if (hszService
== pServer
->hszService
)
1735 TRACE("Service name missing\n");
1739 /* ================================================================
1741 * Conversation management
1743 * ================================================================ */
1745 /******************************************************************
1750 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1751 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
1755 /* no conversation yet, add it */
1756 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
1757 if (!pConv
) return NULL
;
1759 pConv
->instance
= pInstance
;
1760 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
1761 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
1762 pConv
->magic
= WDML_CONV_MAGIC
;
1763 pConv
->hwndServer
= hwndServer
;
1764 pConv
->hwndClient
= hwndClient
;
1765 pConv
->transactions
= NULL
;
1767 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
1768 pConv
->wStatus
|= pInstance
->wStatus
;
1769 /* check if both side of the conversation are of the same instance */
1770 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
1771 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
1773 pConv
->wStatus
|= ST_ISSELF
;
1775 pConv
->wConvst
= XST_NULL
;
1777 pConv
->next
= pInstance
->convs
[side
];
1778 pInstance
->convs
[side
] = pConv
;
1780 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv
->wStatus
, pInstance
);
1785 /******************************************************************
1790 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1791 HSZ hszService
, HSZ hszTopic
)
1793 WDML_CONV
* pCurrent
= NULL
;
1795 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1797 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
1798 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
1807 /******************************************************************
1812 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
1814 WDML_CONV
* pPrev
= NULL
;
1815 WDML_CONV
* pCurrent
;
1817 WDML_XACT
* pXActNext
;
1823 /* remove any pending transaction */
1824 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
1826 pXActNext
= pXAct
->next
;
1827 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
1830 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
1832 /* FIXME: should we keep the window around ? it seems so (at least on client side
1833 * to let QueryConvInfo work after conv termination, but also to implement
1836 /* destroy conversation window, but first remove pConv from hWnd.
1837 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1839 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
1840 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
1842 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
1844 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
1845 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
1847 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
1849 if (pCurrent
== pRef
)
1851 if (pCurrent
== pRef
->instance
->convs
[side
])
1853 pRef
->instance
->convs
[side
] = pCurrent
->next
;
1857 pPrev
->next
= pCurrent
->next
;
1859 pCurrent
->magic
= 0;
1860 HeapFree(GetProcessHeap(), 0, pCurrent
);
1866 /******************************************************************
1867 * WDML_EnableCallback
1869 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
1871 if (wCmd
== EC_DISABLE
)
1873 pConv
->wStatus
|= ST_BLOCKED
;
1874 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1878 if (wCmd
== EC_QUERYWAITING
)
1879 return pConv
->transactions
? TRUE
: FALSE
;
1881 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
1883 FIXME("Unknown command code %04x\n", wCmd
);
1887 if (wCmd
== EC_ENABLEALL
)
1889 pConv
->wStatus
&= ~ST_BLOCKED
;
1890 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1893 while (pConv
->transactions
)
1895 WDML_XACT
*pXAct
= pConv
->transactions
;
1897 if (pConv
->wStatus
& ST_CLIENT
)
1899 /* transaction should be in the queue until handled */
1900 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
1901 WDML_UnQueueTransaction(pConv
, pXAct
);
1905 /* transaction should be removed from the queue before handling */
1906 WDML_UnQueueTransaction(pConv
, pXAct
);
1907 WDML_ServerHandle(pConv
, pXAct
);
1910 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1912 if (wCmd
== EC_ENABLEONE
) break;
1917 /*****************************************************************
1918 * DdeEnableCallback (USER32.@)
1920 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
1925 TRACE("(%d, %p, %04x)\n", idInst
, hConv
, wCmd
);
1929 pConv
= WDML_GetConv(hConv
, TRUE
);
1931 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
1932 ret
= WDML_EnableCallback(pConv
, wCmd
);
1936 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
1941 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
1942 pInstance
->wStatus
|= wCmd
;
1944 if (wCmd
== EC_DISABLE
)
1946 pInstance
->wStatus
|= ST_BLOCKED
;
1947 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1949 else if (wCmd
== EC_ENABLEALL
)
1951 pInstance
->wStatus
&= ~ST_BLOCKED
;
1952 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1957 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
1959 ret
= WDML_EnableCallback(pConv
, wCmd
);
1960 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
1967 /******************************************************************
1972 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
1974 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
1976 /* FIXME: should do better checking */
1977 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
1979 if (!pConv
->instance
)
1981 WARN("wrong thread ID, no instance\n");
1985 if (pConv
->instance
->threadID
!= GetCurrentThreadId())
1987 WARN("wrong thread ID\n");
1988 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
1992 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
1994 WARN("found conv but ain't connected\n");
1995 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
2002 /******************************************************************
2003 * WDML_GetConvFromWnd
2007 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
2009 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
2012 /******************************************************************
2017 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
2018 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
2023 if (side
== WDML_SERVER_SIDE
)
2025 from
= pConv
->hwndServer
;
2026 to
= pConv
->hwndClient
;
2030 to
= pConv
->hwndServer
;
2031 from
= pConv
->hwndClient
;
2034 ddeAck
.bAppReturnCode
= appRetCode
;
2035 ddeAck
.reserved
= 0;
2036 ddeAck
.fBusy
= fBusy
;
2039 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
2041 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
2042 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
2043 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
2045 pConv
->wStatus
&= ~ST_CONNECTED
;
2046 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
2047 FreeDDElParam(WM_DDE_ACK
, lParam
);
2053 /*****************************************************************
2054 * DdeSetUserHandle (USER32.@)
2056 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2060 pConv
= WDML_GetConv(hConv
, FALSE
);
2066 pConv
->hUser
= hUser
;
2072 pXAct
= WDML_FindTransaction(pConv
, id
);
2075 pXAct
->hUser
= hUser
;
2079 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2086 /******************************************************************
2087 * WDML_GetLocalConvInfo
2091 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2097 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2098 ci
->hszSvcPartner
= pConv
->hszService
;
2099 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2100 ci
->hszTopic
= pConv
->hszTopic
;
2101 ci
->wStatus
= pConv
->wStatus
;
2103 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2105 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2107 if (pLink
->hConv
== (HCONV
)pConv
)
2109 ci
->wStatus
|= ST_ADVISE
;
2114 /* FIXME: non handled status flags:
2120 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2122 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2124 ci
->ConvCtxt
= pConv
->convContext
;
2125 if (ci
->wStatus
& ST_CLIENT
)
2127 ci
->hwnd
= pConv
->hwndClient
;
2128 ci
->hwndPartner
= pConv
->hwndServer
;
2132 ci
->hwnd
= pConv
->hwndServer
;
2133 ci
->hwndPartner
= pConv
->hwndClient
;
2137 ci
->hUser
= pConv
->hUser
;
2146 pXAct
= WDML_FindTransaction(pConv
, id
);
2149 ci
->hUser
= pXAct
->hUser
;
2150 ci
->hszItem
= pXAct
->hszItem
;
2151 ci
->wFmt
= pXAct
->wFmt
;
2152 ci
->wType
= pXAct
->wType
;
2157 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2163 /******************************************************************
2164 * DdeQueryConvInfo (USER32.@)
2166 * FIXME: Set last DDE error on failure.
2168 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2170 UINT ret
= lpConvInfo
->cb
;
2174 TRACE("(%p,%x,%p)\n", hConv
, id
, lpConvInfo
);
2178 FIXME("hConv is NULL\n");
2182 pConv
= WDML_GetConv(hConv
, FALSE
);
2185 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2190 if ((ULONG_PTR
)hConv
& 1)
2192 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2194 FIXME("Request on remote conversation information is not implemented yet\n");
2200 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2204 /* ================================================================
2206 * Link (hot & warm) management
2208 * ================================================================ */
2210 /******************************************************************
2215 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2216 UINT wType
, HSZ hszItem
, UINT wFmt
)
2220 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
2227 pLink
->hConv
= hConv
;
2228 pLink
->transactionType
= wType
;
2229 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
2231 pLink
->next
= pInstance
->links
[side
];
2232 pInstance
->links
[side
] = pLink
;
2235 /******************************************************************
2240 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2241 HSZ hszItem
, UINT uFmt
)
2243 WDML_LINK
* pPrev
= NULL
;
2244 WDML_LINK
* pCurrent
= NULL
;
2246 pCurrent
= pInstance
->links
[side
];
2248 while (pCurrent
!= NULL
)
2250 if (pCurrent
->hConv
== hConv
&&
2251 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2252 pCurrent
->uFmt
== uFmt
)
2254 if (pCurrent
== pInstance
->links
[side
])
2256 pInstance
->links
[side
] = pCurrent
->next
;
2260 pPrev
->next
= pCurrent
->next
;
2263 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2264 HeapFree(GetProcessHeap(), 0, pCurrent
);
2269 pCurrent
= pCurrent
->next
;
2273 /* this function is called to remove all links related to the conv.
2274 It should be called from both client and server when terminating
2277 /******************************************************************
2278 * WDML_RemoveAllLinks
2282 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
2284 WDML_LINK
* pPrev
= NULL
;
2285 WDML_LINK
* pCurrent
= NULL
;
2286 WDML_LINK
* pNext
= NULL
;
2288 pCurrent
= pInstance
->links
[side
];
2290 while (pCurrent
!= NULL
)
2292 if (pCurrent
->hConv
== (HCONV
)pConv
)
2294 if (pCurrent
== pInstance
->links
[side
])
2296 pInstance
->links
[side
] = pCurrent
->next
;
2297 pNext
= pCurrent
->next
;
2301 pPrev
->next
= pCurrent
->next
;
2302 pNext
= pCurrent
->next
;
2305 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2307 HeapFree(GetProcessHeap(), 0, pCurrent
);
2314 pCurrent
= pCurrent
->next
;
2323 /******************************************************************
2328 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2329 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
2331 WDML_LINK
* pCurrent
= NULL
;
2333 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2335 /* we don't need to check for transaction type as it can be altered */
2337 if (pCurrent
->hConv
== hConv
&&
2338 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2339 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
2349 /* ================================================================
2351 * Transaction management
2353 * ================================================================ */
2355 /******************************************************************
2356 * WDML_AllocTransaction
2358 * Alloc a transaction structure for handling the message ddeMsg
2360 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
2361 UINT wFmt
, HSZ hszItem
)
2364 static WORD tid
= 1; /* FIXME: wrap around */
2366 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
2369 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
2373 pXAct
->xActID
= tid
++;
2374 pXAct
->ddeMsg
= ddeMsg
;
2375 pXAct
->hDdeData
= 0;
2380 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
2388 /******************************************************************
2389 * WDML_QueueTransaction
2391 * Adds a transaction to the list of transaction
2393 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2397 /* advance to last in queue */
2398 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
2402 /******************************************************************
2403 * WDML_UnQueueTransaction
2407 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2411 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
2422 /******************************************************************
2423 * WDML_FreeTransaction
2427 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
2429 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2430 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
2432 GlobalFree(pXAct
->hMem
);
2434 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
2436 HeapFree(GetProcessHeap(), 0, pXAct
);
2439 /******************************************************************
2440 * WDML_FindTransaction
2444 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2449 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2451 if (pXAct
->xActID
== tid
)
2457 /* ================================================================
2459 * Information broadcast across DDEML implementations
2461 * ================================================================ */
2463 struct tagWDML_BroadcastPmt
2471 /******************************************************************
2472 * WDML_BroadcastEnumProc
2476 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2478 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2481 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2482 lstrcmpiW(buffer
, s
->clsName
) == 0)
2484 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2489 /******************************************************************
2490 * WDML_BroadcastDDEWindows
2494 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2496 struct tagWDML_BroadcastPmt s
;
2498 s
.clsName
= clsName
;
2502 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);