4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
8 * Copyright 2001 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/config.h"
26 #include "wine/port.h"
38 #include "wine/debug.h"
39 #include "dde_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
43 /* convert between ATOM and HSZ avoiding compiler warnings */
44 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
45 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
47 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
48 static DWORD WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
49 const char WDML_szEventClass
[] = "DdeEventClass";
51 static CRITICAL_SECTION_DEBUG critsect_debug
=
54 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
55 0, 0, { 0, (DWORD
)(__FILE__
": WDML_CritSect") }
57 CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
59 /* ================================================================
61 * Pure DDE (non DDEML) management
63 * ================================================================ */
66 /*****************************************************************
67 * PackDDElParam (USER32.@)
72 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
83 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
85 ERR("GlobalAlloc failed\n");
88 if (!(params
= GlobalLock(hMem
)))
90 ERR("GlobalLock failed (%p)\n", hMem
);
102 return MAKELPARAM(uiLo
, uiHi
);
107 /*****************************************************************
108 * UnpackDDElParam (USER32.@)
114 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
115 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
125 if (!lParam
) return FALSE
;
126 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
128 ERR("GlobalLock failed (%lx)\n", lParam
);
131 TRACE("unpacked: low %08x, high %08x\n", params
[0], params
[1]);
132 if (uiLo
) *uiLo
= params
[0];
133 if (uiHi
) *uiHi
= params
[1];
134 GlobalUnlock( (HGLOBAL
)lParam
);
139 if (uiHi
) *uiHi
= lParam
;
143 if (uiLo
) *uiLo
= LOWORD(lParam
);
144 if (uiHi
) *uiHi
= HIWORD(lParam
);
150 /*****************************************************************
151 * FreeDDElParam (USER32.@)
157 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
165 /* first check if it's a global handle */
166 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
167 return !GlobalFree( (HGLOBAL
)lParam
);
175 /*****************************************************************
176 * ReuseDDElParam (USER32.@)
181 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
182 UINT_PTR uiLo
, UINT_PTR uiHi
)
198 if (!lParam
) return 0;
199 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
201 ERR("GlobalLock failed\n");
206 TRACE("Reusing pack %08x %08x\n", uiLo
, uiHi
);
207 GlobalLock( (HGLOBAL
)lParam
);
211 FreeDDElParam( msgIn
, lParam
);
215 FreeDDElParam( msgIn
, lParam
);
216 return MAKELPARAM(uiLo
, uiHi
);
220 return PackDDElParam( msgOut
, uiLo
, uiHi
);
224 /*****************************************************************
225 * ImpersonateDdeClientWindow (USER32.@)
228 * hWndClient [I] handle to DDE client window
229 * hWndServer [I] handle to DDE server window
231 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
233 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
237 /*****************************************************************
238 * DdeSetQualityOfService (USER32.@)
241 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, CONST SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
242 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
244 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
248 /* ================================================================
250 * Instance management
252 * ================================================================ */
254 /******************************************************************************
255 * IncrementInstanceId
257 * generic routine to increment the max instance Id and allocate a new application instance
259 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
261 DWORD id
= InterlockedIncrement((PLONG
)&WDML_MaxInstanceID
);
263 pInstance
->instanceID
= id
;
264 TRACE("New instance id %ld allocated\n", id
);
267 /******************************************************************
272 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
274 WDML_INSTANCE
* pInstance
;
279 case WM_WDML_REGISTER
:
280 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
281 /* try calling the Callback */
282 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
284 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
285 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
286 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
287 WDML_DecHSZ(pInstance
, hsz1
);
288 WDML_DecHSZ(pInstance
, hsz2
);
292 case WM_WDML_UNREGISTER
:
293 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
294 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
296 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
297 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
298 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
299 WDML_DecHSZ(pInstance
, hsz1
);
300 WDML_DecHSZ(pInstance
, hsz2
);
304 case WM_WDML_CONNECT_CONFIRM
:
305 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
306 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
309 /* confirm connection...
310 * lookup for this conv handle
312 HWND client
= (HWND
)wParam
;
313 HWND server
= (HWND
)lParam
;
314 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
316 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
321 pConv
->wStatus
|= ST_ISLOCAL
;
323 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
324 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
325 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
330 return DefWindowProcA(hwndEvent
, uMsg
, wParam
, lParam
);
335 /******************************************************************
340 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
341 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
, BOOL b16
)
343 WDML_INSTANCE
* pInstance
;
344 WDML_INSTANCE
* reference_inst
;
346 WNDCLASSEXA wndclass
;
348 TRACE("(%p,%p,0x%lx,%ld)\n",
349 pidInst
, pfnCallback
, afCmd
, ulRes
);
353 ERR("Reserved value not zero? What does this mean?\n");
354 /* trap this and no more until we know more */
355 return DMLERR_NO_ERROR
;
358 /* grab enough heap for one control struct - not really necessary for re-initialise
359 * but allows us to use same validation routines */
360 pInstance
= (WDML_INSTANCE
*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
361 if (pInstance
== NULL
)
363 /* catastrophe !! warn user & abort */
364 ERR("Instance create failed - out of memory\n");
365 return DMLERR_SYS_ERROR
;
367 pInstance
->next
= NULL
;
368 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
370 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
372 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
373 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
374 pInstance
->threadID
= GetCurrentThreadId();
375 pInstance
->callback
= *pfnCallback
;
376 pInstance
->unicode
= bUnicode
;
377 pInstance
->win16
= b16
;
378 pInstance
->nodeList
= NULL
; /* node will be added later */
379 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
380 pInstance
->servers
= NULL
;
381 pInstance
->convs
[0] = NULL
;
382 pInstance
->convs
[1] = NULL
;
383 pInstance
->links
[0] = NULL
;
384 pInstance
->links
[1] = NULL
;
386 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
388 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
390 if (!pInstance
->clientOnly
)
392 /* Check for other way of setting Client-only !! */
393 pInstance
->clientOnly
=
394 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
397 TRACE("instance created - checking validity \n");
401 /* Initialisation of new Instance Identifier */
402 TRACE("new instance, callback %p flags %lX\n",pfnCallback
,afCmd
);
404 EnterCriticalSection(&WDML_CritSect
);
406 if (WDML_InstanceList
== NULL
)
408 /* can't be another instance in this case, assign to the base pointer */
409 WDML_InstanceList
= pInstance
;
411 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
413 * ------------------------------- NOTE NOTE NOTE --------------------------
415 * the manual is not clear if this condition
416 * applies to the first call to DdeInitialize from an application, or the
417 * first call for a given callback !!!
420 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
421 TRACE("First application instance detected OK\n");
422 /* allocate new instance ID */
423 WDML_IncrementInstanceId(pInstance
);
427 /* really need to chain the new one in to the latest here, but after checking conditions
428 * such as trying to start a conversation from an application trying to monitor */
429 reference_inst
= WDML_InstanceList
;
430 TRACE("Subsequent application instance - starting checks\n");
431 while (reference_inst
->next
!= NULL
)
434 * This set of tests will work if application uses same instance Id
435 * at application level once allocated - which is what manual implies
436 * should happen. If someone tries to be
437 * clever (lazy ?) it will fail to pick up that later calls are for
438 * the same application - should we trust them ?
440 if (pInstance
->instanceID
== reference_inst
->instanceID
)
442 /* Check 1 - must be same Client-only state */
444 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
446 ret
= DMLERR_DLL_USAGE
;
450 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
452 if (pInstance
->monitor
!= reference_inst
->monitor
)
454 ret
= DMLERR_INVALIDPARAMETER
;
458 /* Check 3 - must supply different callback address */
460 if (pInstance
->callback
== reference_inst
->callback
)
462 ret
= DMLERR_DLL_USAGE
;
466 reference_inst
= reference_inst
->next
;
468 /* All cleared, add to chain */
470 TRACE("Application Instance checks finished\n");
471 WDML_IncrementInstanceId(pInstance
);
472 reference_inst
->next
= pInstance
;
474 LeaveCriticalSection(&WDML_CritSect
);
476 *pidInst
= pInstance
->instanceID
;
478 /* for deadlock issues, windows must always be created when outside the critical section */
479 wndclass
.cbSize
= sizeof(wndclass
);
481 wndclass
.lpfnWndProc
= WDML_EventProc
;
482 wndclass
.cbClsExtra
= 0;
483 wndclass
.cbWndExtra
= sizeof(DWORD
);
484 wndclass
.hInstance
= 0;
486 wndclass
.hCursor
= 0;
487 wndclass
.hbrBackground
= 0;
488 wndclass
.lpszMenuName
= NULL
;
489 wndclass
.lpszClassName
= WDML_szEventClass
;
490 wndclass
.hIconSm
= 0;
492 RegisterClassExA(&wndclass
);
494 pInstance
->hwndEvent
= CreateWindowA(WDML_szEventClass
, NULL
,
495 WS_POPUP
, 0, 0, 0, 0,
498 SetWindowLongA(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (DWORD
)pInstance
);
500 TRACE("New application instance processing finished OK\n");
504 /* Reinitialisation situation --- FIX */
505 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
507 EnterCriticalSection(&WDML_CritSect
);
509 if (WDML_InstanceList
== NULL
)
511 ret
= DMLERR_INVALIDPARAMETER
;
514 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
515 /* can't reinitialise if we have initialised nothing !! */
516 reference_inst
= WDML_InstanceList
;
517 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
519 * MS allows initialisation without specifying a callback, should we allow addition of the
520 * callback by a later call to initialise ? - if so this lot will have to change
522 while (reference_inst
->next
!= NULL
)
524 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
526 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
528 if (reference_inst
->clientOnly
)
530 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
532 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
534 if (!(afCmd
& APPCMD_CLIENTONLY
))
536 ret
= DMLERR_INVALIDPARAMETER
;
541 /* Check 2 - cannot change monitor modes */
543 if (pInstance
->monitor
!= reference_inst
->monitor
)
545 ret
= DMLERR_INVALIDPARAMETER
;
549 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
551 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
553 ret
= DMLERR_INVALIDPARAMETER
;
558 reference_inst
= reference_inst
->next
;
560 if (reference_inst
->next
== NULL
)
562 ret
= DMLERR_INVALIDPARAMETER
;
565 /* All checked - change relevant flags */
567 reference_inst
->CBFflags
= pInstance
->CBFflags
;
568 reference_inst
->clientOnly
= pInstance
->clientOnly
;
569 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
570 LeaveCriticalSection(&WDML_CritSect
);
573 return DMLERR_NO_ERROR
;
575 HeapFree(GetProcessHeap(), 0, pInstance
);
576 LeaveCriticalSection(&WDML_CritSect
);
580 /******************************************************************************
581 * DdeInitializeA (USER32.@)
583 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
584 DWORD afCmd
, DWORD ulRes
)
586 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
, FALSE
);
589 /******************************************************************************
590 * DdeInitializeW [USER32.@]
591 * Registers an application with the DDEML
594 * pidInst [I] Pointer to instance identifier
595 * pfnCallback [I] Pointer to callback function
596 * afCmd [I] Set of command and filter flags
600 * Success: DMLERR_NO_ERROR
601 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
603 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
604 DWORD afCmd
, DWORD ulRes
)
606 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
, FALSE
);
609 /*****************************************************************
610 * DdeUninitialize [USER32.@] Frees DDEML resources
613 * idInst [I] Instance identifier
620 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
622 /* Stage one - check if we have a handle for this instance
624 WDML_INSTANCE
* pInstance
;
626 WDML_CONV
* pConvNext
;
628 TRACE("(%ld)\n", idInst
);
630 EnterCriticalSection(&WDML_CritSect
);
632 /* First check instance
634 pInstance
= WDML_GetInstance(idInst
);
635 if (pInstance
== NULL
)
637 LeaveCriticalSection(&WDML_CritSect
);
639 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
644 /* first terminate all conversations client side
645 * this shall close existing links...
647 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
649 pConvNext
= pConv
->next
;
650 DdeDisconnect((HCONV
)pConv
);
652 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
653 FIXME("still pending conversations\n");
655 /* then unregister all known service names */
656 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
658 /* Free the nodes that were not freed by this instance
659 * and remove the nodes from the list of HSZ nodes.
661 WDML_FreeAllHSZ(pInstance
);
663 DestroyWindow(pInstance
->hwndEvent
);
665 /* OK now delete the instance handle itself */
667 if (WDML_InstanceList
== pInstance
)
669 /* special case - the first/only entry */
670 WDML_InstanceList
= pInstance
->next
;
674 /* general case, remove entry */
677 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
678 inst
->next
= pInstance
->next
;
680 /* leave crit sect and release the heap entry
682 HeapFree(GetProcessHeap(), 0, pInstance
);
683 LeaveCriticalSection(&WDML_CritSect
);
687 /******************************************************************
688 * WDML_NotifyThreadExit
692 void WDML_NotifyThreadDetach(void)
694 WDML_INSTANCE
* pInstance
;
696 DWORD tid
= GetCurrentThreadId();
698 EnterCriticalSection(&WDML_CritSect
);
699 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
701 next
= pInstance
->next
;
702 if (pInstance
->threadID
== tid
)
704 DdeUninitialize(pInstance
->instanceID
);
707 LeaveCriticalSection(&WDML_CritSect
);
710 /******************************************************************
711 * WDML_InvokeCallback
715 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
716 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
717 DWORD dwData1
, DWORD dwData2
)
721 if (pInstance
== NULL
)
723 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
724 pInstance
->win16
? 16 : 32, pInstance
->callback
, uType
, uFmt
,
725 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
726 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
727 TRACE("done => %p\n", ret
);
731 /*****************************************************************************
734 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
735 * for an instance Id, or NULL if the entry does not exist
738 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
740 WDML_INSTANCE
* pInstance
;
742 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
744 if (pInstance
->instanceID
== instId
)
746 if (GetCurrentThreadId() != pInstance
->threadID
)
748 FIXME("Tried to get instance from wrong thread\n");
754 TRACE("Instance entry missing\n");
758 /******************************************************************
759 * WDML_GetInstanceFromWnd
763 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
765 return (WDML_INSTANCE
*)GetWindowLongA(hWnd
, GWL_WDML_INSTANCE
);
768 /******************************************************************************
769 * DdeGetLastError [USER32.@] Gets most recent error code
772 * idInst [I] Instance identifier
777 UINT WINAPI
DdeGetLastError(DWORD idInst
)
780 WDML_INSTANCE
* pInstance
;
782 EnterCriticalSection(&WDML_CritSect
);
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;
797 LeaveCriticalSection(&WDML_CritSect
);
801 /* ================================================================
805 * ================================================================ */
808 /******************************************************************
813 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
817 if (pInstance
== NULL
) return NULL
;
819 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
821 if (pNode
->hsz
== hsz
) break;
823 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
827 /******************************************************************
828 * WDML_MakeAtomFromHsz
830 * Creates a global atom from an existing HSZ
831 * Generally used before sending an HSZ as an atom to a remote app
833 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
835 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
837 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
838 return GlobalAddAtomW(nameBuffer
);
839 WARN("HSZ %p not found\n", hsz
);
843 /******************************************************************
844 * WDML_MakeHszFromAtom
846 * Creates a HSZ from an existing global atom
847 * Generally used while receiving a global atom and transforming it
850 HSZ
WDML_MakeHszFromAtom(WDML_INSTANCE
* pInstance
, ATOM atom
)
852 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
854 if (!atom
) return NULL
;
856 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
858 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
859 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
861 WARN("ATOM 0x%x not found\n", atom
);
865 /******************************************************************
870 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
874 pNode
= WDML_FindNode(pInstance
, hsz
);
875 if (!pNode
) return FALSE
;
881 /******************************************************************************
882 * WDML_DecHSZ (INTERNAL)
884 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
886 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
888 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
890 HSZNode
* pPrev
= NULL
;
893 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
895 /* If we found the node we were looking for and its ref count is one,
898 if (pCurrent
->hsz
== hsz
)
900 if (--pCurrent
->refCount
== 0)
902 if (pCurrent
== pInstance
->nodeList
)
904 pInstance
->nodeList
= pCurrent
->next
;
908 pPrev
->next
= pCurrent
->next
;
910 HeapFree(GetProcessHeap(), 0, pCurrent
);
911 DeleteAtom(HSZ2ATOM(hsz
));
916 WARN("HSZ %p not found\n", hsz
);
921 /******************************************************************************
922 * WDML_FreeAllHSZ (INTERNAL)
924 * Frees up all the strings still allocated in the list and
925 * remove all the nodes from the list of HSZ nodes.
927 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
929 /* Free any strings created in this instance.
931 while (pInstance
->nodeList
!= NULL
)
933 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
937 /******************************************************************************
938 * InsertHSZNode (INTERNAL)
940 * Insert a node to the head of the list.
942 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
946 HSZNode
* pNew
= NULL
;
947 /* Create a new node for this HSZ.
949 pNew
= (HSZNode
*)HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
953 pNew
->next
= pInstance
->nodeList
;
955 pInstance
->nodeList
= pNew
;
959 ERR("Primary HSZ Node allocation failed - out of memory\n");
964 /******************************************************************
969 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
972 WCHAR pString
[MAX_BUFFER_LEN
];
974 /* If psz is null, we have to return only the length
980 cchMax
= MAX_BUFFER_LEN
;
986 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
989 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
992 ERR("Unknown code page %d\n", codepage
);
998 /*****************************************************************
999 * DdeQueryStringA [USER32.@]
1001 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
1004 WDML_INSTANCE
* pInstance
;
1006 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1008 EnterCriticalSection(&WDML_CritSect
);
1010 /* First check instance
1012 pInstance
= WDML_GetInstance(idInst
);
1013 if (pInstance
!= NULL
)
1015 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
1016 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1018 LeaveCriticalSection(&WDML_CritSect
);
1020 TRACE("returning %ld (%s)\n", ret
, debugstr_a(psz
));
1024 /*****************************************************************
1025 * DdeQueryStringW [USER32.@]
1028 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
1031 WDML_INSTANCE
* pInstance
;
1033 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1035 EnterCriticalSection(&WDML_CritSect
);
1037 /* First check instance
1039 pInstance
= WDML_GetInstance(idInst
);
1040 if (pInstance
!= NULL
)
1042 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
1043 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1045 LeaveCriticalSection(&WDML_CritSect
);
1047 TRACE("returning %ld (%s)\n", ret
, debugstr_w(psz
));
1051 /******************************************************************
1056 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
1063 hsz
= ATOM2HSZ(AddAtomA(ptr
));
1064 TRACE("added atom %s with HSZ %p, \n", debugstr_a(ptr
), hsz
);
1067 hsz
= ATOM2HSZ(AddAtomW(ptr
));
1068 TRACE("added atom %s with HSZ %p, \n", debugstr_w(ptr
), hsz
);
1071 ERR("Unknown code page %d\n", codepage
);
1074 WDML_InsertHSZNode(pInstance
, hsz
);
1078 /*****************************************************************
1079 * DdeCreateStringHandleA [USER32.@]
1082 * Success: String handle
1085 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPSTR psz
, INT codepage
)
1088 WDML_INSTANCE
* pInstance
;
1090 TRACE("(%ld,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
1092 EnterCriticalSection(&WDML_CritSect
);
1094 pInstance
= WDML_GetInstance(idInst
);
1097 if (codepage
== 0) codepage
= CP_WINANSI
;
1098 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1101 LeaveCriticalSection(&WDML_CritSect
);
1106 /******************************************************************************
1107 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1110 * idInst [I] Instance identifier
1111 * psz [I] Pointer to string
1112 * codepage [I] Code page identifier
1114 * Success: String handle
1117 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPWSTR psz
, INT codepage
)
1119 WDML_INSTANCE
* pInstance
;
1122 TRACE("(%ld,%s,%d)\n", idInst
, debugstr_w(psz
), codepage
);
1124 EnterCriticalSection(&WDML_CritSect
);
1126 pInstance
= WDML_GetInstance(idInst
);
1129 if (codepage
== 0) codepage
= CP_WINUNICODE
;
1130 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1132 LeaveCriticalSection(&WDML_CritSect
);
1137 /*****************************************************************
1138 * DdeFreeStringHandle (USER32.@)
1139 * RETURNS: success: nonzero
1142 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
1144 WDML_INSTANCE
* pInstance
;
1147 TRACE("(%ld,%p): \n", idInst
, hsz
);
1149 EnterCriticalSection(&WDML_CritSect
);
1151 /* First check instance
1153 pInstance
= WDML_GetInstance(idInst
);
1155 ret
= WDML_DecHSZ(pInstance
, hsz
);
1157 LeaveCriticalSection(&WDML_CritSect
);
1162 /*****************************************************************
1163 * DdeKeepStringHandle (USER32.@)
1165 * RETURNS: success: nonzero
1168 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
1170 WDML_INSTANCE
* pInstance
;
1173 TRACE("(%ld,%p): \n", idInst
, hsz
);
1175 EnterCriticalSection(&WDML_CritSect
);
1177 /* First check instance
1179 pInstance
= WDML_GetInstance(idInst
);
1181 ret
= WDML_IncHSZ(pInstance
, hsz
);
1183 LeaveCriticalSection(&WDML_CritSect
);
1187 /*****************************************************************
1188 * DdeCmpStringHandles (USER32.@)
1190 * Compares the value of two string handles. This comparison is
1191 * not case sensitive.
1194 * -1 The value of hsz1 is zero or less than hsz2
1195 * 0 The values of hsz 1 and 2 are the same or both zero.
1196 * 1 The value of hsz2 is zero of less than hsz1
1198 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
1200 WCHAR psz1
[MAX_BUFFER_LEN
];
1201 WCHAR psz2
[MAX_BUFFER_LEN
];
1205 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
1206 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
1208 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
1210 /* Make sure we found both strings. */
1211 if (ret1
== 0 && ret2
== 0)
1213 /* If both are not found, return both "zero strings". */
1218 /* If hsz1 is a not found, return hsz1 is "zero string". */
1223 /* If hsz2 is a not found, return hsz2 is "zero string". */
1228 /* Compare the two strings we got (case insensitive). */
1229 ret
= lstrcmpiW(psz1
, psz2
);
1230 /* Since strcmp returns any number smaller than
1231 * 0 when the first string is found to be less than
1232 * the second one we must make sure we are returning
1233 * the proper values.
1248 /* ================================================================
1250 * Data handle management
1252 * ================================================================ */
1254 /*****************************************************************
1255 * DdeCreateDataHandle (USER32.@)
1257 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1258 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1260 /* For now, we ignore idInst, hszItem.
1261 * The purpose of these arguments still need to be investigated.
1266 DDE_DATAHANDLE_HEAD
* pDdh
;
1267 WCHAR psz
[MAX_BUFFER_LEN
];
1269 GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
);
1271 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,%x,%x)\n",
1272 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1274 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1277 /* we use the first 4 bytes to store the size */
1278 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1280 ERR("GlobalAlloc failed\n");
1284 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1291 pDdh
->cfFormat
= wFmt
;
1292 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1294 pByte
= (LPBYTE
)(pDdh
+ 1);
1297 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1301 return (HDDEDATA
)hMem
;
1304 /*****************************************************************
1306 * DdeAddData (USER32.@)
1308 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1310 DWORD old_sz
, new_sz
;
1313 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData
, pSrc
, cb
, cbOff
);
1315 pDst
= DdeAccessData(hData
, &old_sz
);
1316 if (!pDst
) return 0;
1318 new_sz
= cb
+ cbOff
;
1319 if (new_sz
> old_sz
)
1321 DdeUnaccessData(hData
);
1322 hData
= GlobalReAlloc((HGLOBAL
)hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1323 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1324 pDst
= DdeAccessData(hData
, &old_sz
);
1327 if (!pDst
) return 0;
1329 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1330 DdeUnaccessData(hData
);
1334 /******************************************************************************
1335 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1339 * hData [I] Handle to DDE object
1340 * pDst [I] Pointer to destination buffer
1341 * cbMax [I] Amount of data to copy
1342 * cbOff [I] Offset to beginning of data
1345 * Size of memory object associated with handle
1347 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1349 DWORD dwSize
, dwRet
;
1352 TRACE("(%p,%p,%ld,%ld)\n", hData
, pDst
, cbMax
, cbOff
);
1354 pByte
= DdeAccessData(hData
, &dwSize
);
1362 else if (cbOff
+ cbMax
< dwSize
)
1366 else if (cbOff
< dwSize
)
1368 dwRet
= dwSize
- cbOff
;
1374 if (pDst
&& dwRet
!= 0)
1376 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1378 DdeUnaccessData(hData
);
1387 /*****************************************************************
1388 * DdeAccessData (USER32.@)
1390 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1392 HGLOBAL hMem
= (HGLOBAL
)hData
;
1393 DDE_DATAHANDLE_HEAD
* pDdh
;
1395 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1397 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1400 ERR("Failed on GlobalLock(%p)\n", hMem
);
1404 if (pcbDataSize
!= NULL
)
1406 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1408 TRACE("=> %p (%lu)\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
));
1409 return (LPBYTE
)(pDdh
+ 1);
1412 /*****************************************************************
1413 * DdeUnaccessData (USER32.@)
1415 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1417 HGLOBAL hMem
= (HGLOBAL
)hData
;
1419 TRACE("(%p)\n", hData
);
1426 /*****************************************************************
1427 * DdeFreeDataHandle (USER32.@)
1429 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1431 TRACE("(%p)\n", hData
);
1432 return GlobalFree((HGLOBAL
)hData
) == 0;
1435 /******************************************************************
1440 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1442 DDE_DATAHANDLE_HEAD
* pDdh
;
1445 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hData
);
1448 ret
= pDdh
->bAppOwned
;
1449 GlobalUnlock((HGLOBAL
)hData
);
1454 /* ================================================================
1456 * Global <=> Data handle management
1458 * ================================================================ */
1460 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1462 * (bytes) (bits) comment
1463 * 0 16 bit fields for options (release, ackreq, response...)
1464 * 2 16 clipboard format
1465 * 4 ? data to be used
1467 HDDEDATA
WDML_Global2DataHandle(HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1475 pDd
= GlobalLock(hMem
);
1476 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1479 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1480 switch (pDd
->cfFormat
)
1483 FIXME("Unsupported format (%d) for data... assuming raw information\n",
1488 ret
= DdeCreateDataHandle(0, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1491 if (size
>= sizeof(BITMAP
))
1493 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1494 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1495 if (size
>= sizeof(BITMAP
) + count
)
1499 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1500 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1501 pDd
->Value
+ sizeof(BITMAP
))))
1503 ret
= DdeCreateDataHandle(0, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1504 0, 0, CF_BITMAP
, 0);
1506 else ERR("Can't create bmp\n");
1510 ERR("Wrong count: %lu / %d\n", size
, sizeof(BITMAP
) + count
);
1512 } else ERR("No bitmap header\n");
1521 /******************************************************************
1522 * WDML_DataHandle2Global
1526 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1527 BOOL fDeferUpd
, BOOL fAckReq
)
1529 DDE_DATAHANDLE_HEAD
* pDdh
;
1533 dwSize
= GlobalSize((HGLOBAL
)hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1534 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hDdeData
);
1537 WINE_DDEHEAD
* wdh
= NULL
;
1539 switch (pDdh
->cfFormat
)
1542 FIXME("Unsupported format (%d) for data... passing raw information\n", pDdh
->cfFormat
);
1546 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1547 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1549 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1553 if (dwSize
>= sizeof(HBITMAP
))
1557 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1559 if (GetObjectA(hbmp
, sizeof(bmp
), &bmp
))
1561 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1562 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1563 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1564 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1566 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1567 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1576 wdh
->fResponse
= fResponse
;
1577 wdh
->fRelease
= fRelease
;
1578 wdh
->fDeferUpd
= fDeferUpd
;
1579 wdh
->fAckReq
= fAckReq
;
1580 wdh
->cfFormat
= pDdh
->cfFormat
;
1583 GlobalUnlock((HGLOBAL
)hDdeData
);
1589 /* ================================================================
1593 * ================================================================ */
1595 /******************************************************************
1600 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1602 WDML_SERVER
* pServer
;
1606 pServer
= (WDML_SERVER
*)HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1607 if (pServer
== NULL
) return NULL
;
1609 WDML_IncHSZ(pInstance
, pServer
->hszService
= hszService
);
1611 DdeQueryStringA(pInstance
->instanceID
, hszService
, buf1
, sizeof(buf1
), CP_WINANSI
);
1612 _snprintf(buf2
, sizeof(buf2
), "%s(0x%08lx)", buf1
, GetCurrentProcessId());
1613 pServer
->hszServiceSpec
= DdeCreateStringHandleA(pInstance
->instanceID
, buf2
, CP_WINANSI
);
1615 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1616 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1618 pServer
->filterOn
= TRUE
;
1620 pServer
->next
= pInstance
->servers
;
1621 pInstance
->servers
= pServer
;
1625 /******************************************************************
1630 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1632 WDML_SERVER
* pPrev
= NULL
;
1633 WDML_SERVER
* pServer
= NULL
;
1635 WDML_CONV
* pConvNext
;
1637 pServer
= pInstance
->servers
;
1639 while (pServer
!= NULL
)
1641 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1643 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1644 pServer
->atomService
, pServer
->atomServiceSpec
);
1645 /* terminate all conversations for given topic */
1646 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1648 pConvNext
= pConv
->next
;
1649 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1651 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1652 /* don't care about return code (whether client window is present or not) */
1653 PostMessageA(pConv
->hwndClient
, WM_DDE_TERMINATE
, (WPARAM
)pConv
->hwndServer
, 0L);
1656 if (pServer
== pInstance
->servers
)
1658 pInstance
->servers
= pServer
->next
;
1662 pPrev
->next
= pServer
->next
;
1665 DestroyWindow(pServer
->hwndServer
);
1666 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1667 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1669 GlobalDeleteAtom(pServer
->atomService
);
1670 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1672 HeapFree(GetProcessHeap(), 0, pServer
);
1677 pServer
= pServer
->next
;
1681 /*****************************************************************************
1684 * generic routine to return a pointer to the relevant ServiceNode
1685 * for a given service name, or NULL if the entry does not exist
1688 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1690 WDML_SERVER
* pServer
;
1692 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1694 if (hszService
== pServer
->hszService
)
1699 TRACE("Service name missing\n");
1703 /* ================================================================
1705 * Conversation management
1707 * ================================================================ */
1709 /******************************************************************
1714 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1715 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
1719 /* no converstation yet, add it */
1720 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
1721 if (!pConv
) return NULL
;
1723 pConv
->instance
= pInstance
;
1724 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
1725 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
1726 pConv
->hwndServer
= hwndServer
;
1727 pConv
->hwndClient
= hwndClient
;
1728 pConv
->transactions
= NULL
;
1730 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
1731 /* check if both side of the conversation are of the same instance */
1732 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
1733 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
1735 pConv
->wStatus
|= ST_ISSELF
;
1737 pConv
->wConvst
= XST_NULL
;
1739 pConv
->next
= pInstance
->convs
[side
];
1740 pInstance
->convs
[side
] = pConv
;
1745 /******************************************************************
1750 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1751 HSZ hszService
, HSZ hszTopic
)
1753 WDML_CONV
* pCurrent
= NULL
;
1755 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1757 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
1758 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
1767 /******************************************************************
1772 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
1774 WDML_CONV
* pPrev
= NULL
;
1775 WDML_CONV
* pCurrent
;
1777 WDML_XACT
* pXActNext
;
1783 /* remove any pending transaction */
1784 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
1786 pXActNext
= pXAct
->next
;
1787 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
1790 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
1792 /* FIXME: should we keep the window around ? it seems so (at least on client side
1793 * to let QueryConvInfo work after conv termination, but also to implement
1796 /* destroy conversation window, but first remove pConv from hWnd.
1797 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1799 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
1800 SetWindowLongA(hWnd
, GWL_WDML_CONVERSATION
, 0);
1802 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
1804 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
1805 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
1807 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
1809 if (pCurrent
== pRef
)
1811 if (pCurrent
== pRef
->instance
->convs
[side
])
1813 pRef
->instance
->convs
[side
] = pCurrent
->next
;
1817 pPrev
->next
= pCurrent
->next
;
1820 HeapFree(GetProcessHeap(), 0, pCurrent
);
1826 /******************************************************************
1827 * WDML_EnableCallback
1829 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
1831 if (wCmd
== EC_DISABLE
)
1833 FIXME("EC_DISABLE is not implemented\n");
1837 if (wCmd
== EC_QUERYWAITING
)
1838 return pConv
->transactions
? TRUE
: FALSE
;
1840 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
1842 FIXME("Unknown command code %04x\n", wCmd
);
1846 while (pConv
->transactions
)
1848 WDML_XACT
*pXAct
= pConv
->transactions
;
1849 WDML_UnQueueTransaction(pConv
, pXAct
);
1851 if (pConv
->wStatus
& ST_CLIENT
)
1853 /*WDML_ClientHandle(pConv, pXAct);*/
1854 FIXME("Client delayed transaction queue handling is not supported\n");
1857 WDML_ServerHandle(pConv
, pXAct
);
1859 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1861 if (wCmd
== EC_ENABLEONE
) break;
1866 /*****************************************************************
1867 * DdeEnableCallback (USER32.@)
1869 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
1874 TRACE("(%ld, %p, %04x)\n", idInst
, hConv
, wCmd
);
1876 EnterCriticalSection(&WDML_CritSect
);
1878 pConv
= WDML_GetConv(hConv
, TRUE
);
1880 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
1881 ret
= WDML_EnableCallback(pConv
, wCmd
);
1883 LeaveCriticalSection(&WDML_CritSect
);
1887 /******************************************************************
1892 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
1894 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
1896 /* FIXME: should do better checking */
1897 if (pConv
== NULL
) return NULL
;
1899 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
1901 FIXME("found conv but ain't connected\n");
1904 if (GetCurrentThreadId() != pConv
->instance
->threadID
)
1906 FIXME("wrong thread ID\n");
1913 /******************************************************************
1914 * WDML_GetConvFromWnd
1918 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
1920 return (WDML_CONV
*)GetWindowLongA(hWnd
, GWL_WDML_CONVERSATION
);
1923 /******************************************************************
1928 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
1929 BOOL fBusy
, BOOL fAck
, UINT pmt
, LPARAM lParam
, UINT oldMsg
)
1934 if (side
== WDML_SERVER_SIDE
)
1936 from
= pConv
->hwndServer
;
1937 to
= pConv
->hwndClient
;
1941 to
= pConv
->hwndServer
;
1942 from
= pConv
->hwndClient
;
1945 ddeAck
.bAppReturnCode
= appRetCode
;
1946 ddeAck
.reserved
= 0;
1947 ddeAck
.fBusy
= fBusy
;
1950 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
1952 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
1953 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
1954 if (!PostMessageA(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
1956 pConv
->wStatus
&= ~ST_CONNECTED
;
1957 FreeDDElParam(WM_DDE_ACK
, lParam
);
1963 /*****************************************************************
1964 * DdeSetUserHandle (USER32.@)
1966 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
1971 TRACE("(%p,%lx,%lx)\n", hConv
, id
, hUser
);
1973 EnterCriticalSection(&WDML_CritSect
);
1975 pConv
= WDML_GetConv(hConv
, FALSE
);
1983 pConv
->hUser
= hUser
;
1989 pXAct
= WDML_FindTransaction(pConv
, id
);
1992 pXAct
->hUser
= hUser
;
1996 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2001 LeaveCriticalSection(&WDML_CritSect
);
2005 /******************************************************************
2006 * WDML_GetLocalConvInfo
2010 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2016 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((DWORD
)pConv
| 1) : 0;
2017 ci
->hszSvcPartner
= pConv
->hszService
;
2018 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2019 ci
->hszTopic
= pConv
->hszTopic
;
2020 ci
->wStatus
= pConv
->wStatus
;
2022 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2024 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2026 if (pLink
->hConv
== (HCONV
)pConv
)
2028 ci
->wStatus
|= ST_ADVISE
;
2033 /* FIXME: non handled status flags:
2039 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2041 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2043 ci
->ConvCtxt
= pConv
->convContext
;
2044 if (ci
->wStatus
& ST_CLIENT
)
2046 ci
->hwnd
= pConv
->hwndClient
;
2047 ci
->hwndPartner
= pConv
->hwndServer
;
2051 ci
->hwnd
= pConv
->hwndServer
;
2052 ci
->hwndPartner
= pConv
->hwndClient
;
2056 ci
->hUser
= pConv
->hUser
;
2065 pXAct
= WDML_FindTransaction(pConv
, id
);
2068 ci
->hUser
= pXAct
->hUser
;
2069 ci
->hszItem
= pXAct
->hszItem
;
2070 ci
->wFmt
= pXAct
->wFmt
;
2071 ci
->wType
= pXAct
->wType
;
2076 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2082 /******************************************************************
2083 * DdeQueryConvInfo (USER32.@)
2086 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2088 UINT ret
= lpConvInfo
->cb
;
2092 TRACE("(%p,%lx,%p)\n", hConv
, id
, lpConvInfo
);
2096 FIXME("hConv is NULL\n");
2100 EnterCriticalSection(&WDML_CritSect
);
2102 pConv
= WDML_GetConv(hConv
, FALSE
);
2103 if (pConv
!= NULL
&& !WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2107 else if ((DWORD
)hConv
& 1)
2109 pConv
= WDML_GetConv((HCONV
)((DWORD
)hConv
& ~1), FALSE
);
2112 FIXME("Request on remote conversation information is not implemented yet\n");
2116 LeaveCriticalSection(&WDML_CritSect
);
2118 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2122 /* ================================================================
2124 * Link (hot & warm) management
2126 * ================================================================ */
2128 /******************************************************************
2133 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2134 UINT wType
, HSZ hszItem
, UINT wFmt
)
2138 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
2145 pLink
->hConv
= hConv
;
2146 pLink
->transactionType
= wType
;
2147 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
2149 pLink
->next
= pInstance
->links
[side
];
2150 pInstance
->links
[side
] = pLink
;
2153 /******************************************************************
2158 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2159 HSZ hszItem
, UINT uFmt
)
2161 WDML_LINK
* pPrev
= NULL
;
2162 WDML_LINK
* pCurrent
= NULL
;
2164 pCurrent
= pInstance
->links
[side
];
2166 while (pCurrent
!= NULL
)
2168 if (pCurrent
->hConv
== hConv
&&
2169 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2170 pCurrent
->uFmt
== uFmt
)
2172 if (pCurrent
== pInstance
->links
[side
])
2174 pInstance
->links
[side
] = pCurrent
->next
;
2178 pPrev
->next
= pCurrent
->next
;
2181 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2182 HeapFree(GetProcessHeap(), 0, pCurrent
);
2187 pCurrent
= pCurrent
->next
;
2191 /* this function is called to remove all links related to the conv.
2192 It should be called from both client and server when terminating
2195 /******************************************************************
2196 * WDML_RemoveAllLinks
2200 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
2202 WDML_LINK
* pPrev
= NULL
;
2203 WDML_LINK
* pCurrent
= NULL
;
2204 WDML_LINK
* pNext
= NULL
;
2206 pCurrent
= pInstance
->links
[side
];
2208 while (pCurrent
!= NULL
)
2210 if (pCurrent
->hConv
== (HCONV
)pConv
)
2212 if (pCurrent
== pInstance
->links
[side
])
2214 pInstance
->links
[side
] = pCurrent
->next
;
2215 pNext
= pCurrent
->next
;
2219 pPrev
->next
= pCurrent
->next
;
2220 pNext
= pCurrent
->next
;
2223 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2225 HeapFree(GetProcessHeap(), 0, pCurrent
);
2232 pCurrent
= pCurrent
->next
;
2241 /******************************************************************
2246 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2247 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
2249 WDML_LINK
* pCurrent
= NULL
;
2251 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2253 /* we don't need to check for transaction type as it can be altered */
2255 if (pCurrent
->hConv
== hConv
&&
2256 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2257 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
2267 /* ================================================================
2269 * Transaction management
2271 * ================================================================ */
2273 /******************************************************************
2274 * WDML_AllocTransaction
2276 * Alloc a transaction structure for handling the message ddeMsg
2278 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
2279 UINT wFmt
, HSZ hszItem
)
2282 static WORD tid
= 1; /* FIXME: wrap around */
2284 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
2287 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
2291 pXAct
->xActID
= tid
++;
2292 pXAct
->ddeMsg
= ddeMsg
;
2293 pXAct
->hDdeData
= 0;
2298 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
2306 /******************************************************************
2307 * WDML_QueueTransaction
2309 * Adds a transaction to the list of transaction
2311 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2315 /* advance to last in queue */
2316 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
2320 /******************************************************************
2321 * WDML_UnQueueTransaction
2325 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2329 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
2340 /******************************************************************
2341 * WDML_FreeTransaction
2345 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
2347 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2348 if (doFreePmt
&& (DWORD
)pXAct
->hMem
> 1)
2350 GlobalFree(pXAct
->hMem
);
2352 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
2354 HeapFree(GetProcessHeap(), 0, pXAct
);
2357 /******************************************************************
2358 * WDML_FindTransaction
2362 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2367 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2369 if (pXAct
->xActID
== tid
)
2375 /* ================================================================
2377 * Information broadcast across DDEML implementations
2379 * ================================================================ */
2381 struct tagWDML_BroadcastPmt
2389 /******************************************************************
2390 * WDML_BroadcastEnumProc
2394 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2396 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2399 if (GetClassNameA(hWnd
, buffer
, sizeof(buffer
)) > 0 &&
2400 strcmp(buffer
, s
->clsName
) == 0)
2402 PostMessageA(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2407 /******************************************************************
2408 * WDML_BroadcastDDEWindows
2412 void WDML_BroadcastDDEWindows(const char* clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2414 struct tagWDML_BroadcastPmt s
;
2416 s
.clsName
= clsName
;
2420 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);