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 "wine/port.h"
39 #include "dde_private.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
45 /* convert between ATOM and HSZ avoiding compiler warnings */
46 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
47 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
49 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
50 static LONG WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
51 const WCHAR WDML_szEventClass
[] = {'W','i','n','e','D','d','e','E','v','e','n','t','C','l','a','s','s',0};
53 /* protection for instance list */
54 static CRITICAL_SECTION WDML_CritSect
;
55 static CRITICAL_SECTION_DEBUG critsect_debug
=
58 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
59 0, 0, { (DWORD_PTR
)(__FILE__
": WDML_CritSect") }
61 static CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
63 /* ================================================================
65 * Pure DDE (non DDEML) management
67 * ================================================================ */
70 /*****************************************************************
71 * PackDDElParam (USER32.@)
76 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
87 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
89 ERR("GlobalAlloc failed\n");
92 if (!(params
= GlobalLock(hMem
)))
94 ERR("GlobalLock failed (%p)\n", hMem
);
106 return MAKELPARAM(uiLo
, uiHi
);
111 /*****************************************************************
112 * UnpackDDElParam (USER32.@)
118 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
119 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
129 if (!lParam
|| !(params
= GlobalLock((HGLOBAL
)lParam
)))
135 if (uiLo
) *uiLo
= params
[0];
136 if (uiHi
) *uiHi
= params
[1];
137 GlobalUnlock( (HGLOBAL
)lParam
);
142 if (uiHi
) *uiHi
= lParam
;
146 if (uiLo
) *uiLo
= LOWORD(lParam
);
147 if (uiHi
) *uiHi
= HIWORD(lParam
);
153 /*****************************************************************
154 * FreeDDElParam (USER32.@)
160 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
168 /* first check if it's a global handle */
169 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
170 return !GlobalFree( (HGLOBAL
)lParam
);
178 /*****************************************************************
179 * ReuseDDElParam (USER32.@)
184 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
185 UINT_PTR uiLo
, UINT_PTR uiHi
)
201 if (!lParam
) return 0;
202 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
204 ERR("GlobalLock failed\n");
209 TRACE("Reusing pack %08lx %08lx\n", uiLo
, uiHi
);
210 GlobalUnlock( (HGLOBAL
)lParam
);
214 FreeDDElParam( msgIn
, lParam
);
218 FreeDDElParam( msgIn
, lParam
);
219 return MAKELPARAM(uiLo
, uiHi
);
223 return PackDDElParam( msgOut
, uiLo
, uiHi
);
227 /*****************************************************************
228 * ImpersonateDdeClientWindow (USER32.@)
231 * hWndClient [I] handle to DDE client window
232 * hWndServer [I] handle to DDE server window
234 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
236 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
240 /*****************************************************************
241 * DdeSetQualityOfService (USER32.@)
244 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, CONST SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
245 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
247 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
251 /* ================================================================
253 * Instance management
255 * ================================================================ */
257 /******************************************************************************
258 * IncrementInstanceId
260 * generic routine to increment the max instance Id and allocate a new application instance
262 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
264 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
266 pInstance
->instanceID
= id
;
267 TRACE("New instance id %d allocated\n", id
);
270 /******************************************************************
275 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
277 WDML_INSTANCE
* pInstance
;
282 case WM_WDML_REGISTER
:
283 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
284 /* try calling the Callback */
285 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
287 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
288 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
289 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
290 WDML_DecHSZ(pInstance
, hsz1
);
291 WDML_DecHSZ(pInstance
, hsz2
);
295 case WM_WDML_UNREGISTER
:
296 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
297 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
299 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
300 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
301 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
302 WDML_DecHSZ(pInstance
, hsz1
);
303 WDML_DecHSZ(pInstance
, hsz2
);
307 case WM_WDML_CONNECT_CONFIRM
:
308 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
309 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
312 /* confirm connection...
313 * lookup for this conv handle
315 HWND client
= WIN_GetFullHandle( (HWND
)wParam
);
316 HWND server
= WIN_GetFullHandle( (HWND
)lParam
);
317 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
319 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
324 pConv
->wStatus
|= ST_ISLOCAL
;
326 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
327 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
328 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
333 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
338 /******************************************************************
343 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
344 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
, BOOL b16
)
346 WDML_INSTANCE
* pInstance
;
347 WDML_INSTANCE
* reference_inst
;
349 WNDCLASSEXW wndclass
;
351 TRACE("(%p,%p,0x%x,%d)\n",
352 pidInst
, pfnCallback
, afCmd
, ulRes
);
356 ERR("Reserved value not zero? What does this mean?\n");
357 /* trap this and no more until we know more */
358 return DMLERR_NO_ERROR
;
361 /* grab enough heap for one control struct - not really necessary for re-initialise
362 * but allows us to use same validation routines */
363 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
364 if (pInstance
== NULL
)
366 /* catastrophe !! warn user & abort */
367 ERR("Instance create failed - out of memory\n");
368 return DMLERR_SYS_ERROR
;
370 pInstance
->next
= NULL
;
371 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
373 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
375 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
376 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
377 pInstance
->threadID
= GetCurrentThreadId();
378 pInstance
->callback
= *pfnCallback
;
379 pInstance
->unicode
= bUnicode
;
380 pInstance
->win16
= b16
;
381 pInstance
->nodeList
= NULL
; /* node will be added later */
382 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
383 pInstance
->wStatus
= 0;
384 pInstance
->servers
= NULL
;
385 pInstance
->convs
[0] = NULL
;
386 pInstance
->convs
[1] = NULL
;
387 pInstance
->links
[0] = NULL
;
388 pInstance
->links
[1] = NULL
;
390 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
392 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
394 if (!pInstance
->clientOnly
)
396 /* Check for other way of setting Client-only !! */
397 pInstance
->clientOnly
=
398 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
401 TRACE("instance created - checking validity\n");
405 /* Initialisation of new Instance Identifier */
406 TRACE("new instance, callback %p flags %X\n",pfnCallback
,afCmd
);
408 EnterCriticalSection(&WDML_CritSect
);
410 if (WDML_InstanceList
== NULL
)
412 /* can't be another instance in this case, assign to the base pointer */
413 WDML_InstanceList
= pInstance
;
415 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
417 * ------------------------------- NOTE NOTE NOTE --------------------------
419 * the manual is not clear if this condition
420 * applies to the first call to DdeInitialize from an application, or the
421 * first call for a given callback !!!
424 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
425 TRACE("First application instance detected OK\n");
426 /* allocate new instance ID */
427 WDML_IncrementInstanceId(pInstance
);
431 /* really need to chain the new one in to the latest here, but after checking conditions
432 * such as trying to start a conversation from an application trying to monitor */
433 reference_inst
= WDML_InstanceList
;
434 TRACE("Subsequent application instance - starting checks\n");
435 while (reference_inst
->next
!= NULL
)
438 * This set of tests will work if application uses same instance Id
439 * at application level once allocated - which is what manual implies
440 * should happen. If someone tries to be
441 * clever (lazy ?) it will fail to pick up that later calls are for
442 * the same application - should we trust them ?
444 if (pInstance
->instanceID
== reference_inst
->instanceID
)
446 /* Check 1 - must be same Client-only state */
448 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
450 ret
= DMLERR_DLL_USAGE
;
454 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
456 if (pInstance
->monitor
!= reference_inst
->monitor
)
458 ret
= DMLERR_INVALIDPARAMETER
;
462 /* Check 3 - must supply different callback address */
464 if (pInstance
->callback
== reference_inst
->callback
)
466 ret
= DMLERR_DLL_USAGE
;
470 reference_inst
= reference_inst
->next
;
472 /* All cleared, add to chain */
474 TRACE("Application Instance checks finished\n");
475 WDML_IncrementInstanceId(pInstance
);
476 reference_inst
->next
= pInstance
;
478 LeaveCriticalSection(&WDML_CritSect
);
480 *pidInst
= pInstance
->instanceID
;
482 /* for deadlock issues, windows must always be created when outside the critical section */
483 wndclass
.cbSize
= sizeof(wndclass
);
485 wndclass
.lpfnWndProc
= WDML_EventProc
;
486 wndclass
.cbClsExtra
= 0;
487 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
488 wndclass
.hInstance
= 0;
490 wndclass
.hCursor
= 0;
491 wndclass
.hbrBackground
= 0;
492 wndclass
.lpszMenuName
= NULL
;
493 wndclass
.lpszClassName
= WDML_szEventClass
;
494 wndclass
.hIconSm
= 0;
496 RegisterClassExW(&wndclass
);
498 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
499 WS_POPUP
, 0, 0, 0, 0,
502 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
504 TRACE("New application instance processing finished OK\n");
508 /* Reinitialisation situation --- FIX */
509 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
511 EnterCriticalSection(&WDML_CritSect
);
513 if (WDML_InstanceList
== NULL
)
515 ret
= DMLERR_INVALIDPARAMETER
;
518 /* can't reinitialise if we have initialised nothing !! */
519 reference_inst
= WDML_InstanceList
;
520 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
522 * MS allows initialisation without specifying a callback, should we allow addition of the
523 * callback by a later call to initialise ? - if so this lot will have to change
525 while (reference_inst
->next
!= NULL
)
527 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
529 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
531 if (reference_inst
->clientOnly
)
533 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
535 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
537 if (!(afCmd
& APPCMD_CLIENTONLY
))
539 ret
= DMLERR_INVALIDPARAMETER
;
544 /* Check 2 - cannot change monitor modes */
546 if (pInstance
->monitor
!= reference_inst
->monitor
)
548 ret
= DMLERR_INVALIDPARAMETER
;
552 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
554 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
556 ret
= DMLERR_INVALIDPARAMETER
;
561 reference_inst
= reference_inst
->next
;
563 if (reference_inst
->next
== NULL
)
565 ret
= DMLERR_INVALIDPARAMETER
;
568 /* All checked - change relevant flags */
570 reference_inst
->CBFflags
= pInstance
->CBFflags
;
571 reference_inst
->clientOnly
= pInstance
->clientOnly
;
572 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
574 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
576 LeaveCriticalSection(&WDML_CritSect
);
579 return DMLERR_NO_ERROR
;
581 HeapFree(GetProcessHeap(), 0, pInstance
);
582 LeaveCriticalSection(&WDML_CritSect
);
586 /******************************************************************************
587 * DdeInitializeA (USER32.@)
589 * See DdeInitializeW.
591 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
592 DWORD afCmd
, DWORD ulRes
)
594 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
, FALSE
);
597 /******************************************************************************
598 * DdeInitializeW [USER32.@]
599 * Registers an application with the DDEML
602 * pidInst [I] Pointer to instance identifier
603 * pfnCallback [I] Pointer to callback function
604 * afCmd [I] Set of command and filter flags
608 * Success: DMLERR_NO_ERROR
609 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
611 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
612 DWORD afCmd
, DWORD ulRes
)
614 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
, FALSE
);
617 /*****************************************************************
618 * DdeUninitialize [USER32.@] Frees DDEML resources
621 * idInst [I] Instance identifier
628 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
630 /* Stage one - check if we have a handle for this instance
632 WDML_INSTANCE
* pInstance
;
634 WDML_CONV
* pConvNext
;
636 TRACE("(%d)\n", idInst
);
638 /* First check instance
640 pInstance
= WDML_GetInstance(idInst
);
641 if (pInstance
== NULL
)
644 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
649 /* first terminate all conversations client side
650 * this shall close existing links...
652 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
654 pConvNext
= pConv
->next
;
655 DdeDisconnect((HCONV
)pConv
);
657 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
658 FIXME("still pending conversations\n");
660 /* then unregister all known service names */
661 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
663 /* Free the nodes that were not freed by this instance
664 * and remove the nodes from the list of HSZ nodes.
666 WDML_FreeAllHSZ(pInstance
);
668 DestroyWindow(pInstance
->hwndEvent
);
670 /* OK now delete the instance handle itself */
672 if (WDML_InstanceList
== pInstance
)
674 /* special case - the first/only entry */
675 WDML_InstanceList
= pInstance
->next
;
679 /* general case, remove entry */
682 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
683 inst
->next
= pInstance
->next
;
685 /* release the heap entry
687 HeapFree(GetProcessHeap(), 0, pInstance
);
692 /******************************************************************
693 * WDML_NotifyThreadExit
697 void WDML_NotifyThreadDetach(void)
699 WDML_INSTANCE
* pInstance
;
701 DWORD tid
= GetCurrentThreadId();
703 EnterCriticalSection(&WDML_CritSect
);
704 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
706 next
= pInstance
->next
;
707 if (pInstance
->threadID
== tid
)
709 LeaveCriticalSection(&WDML_CritSect
);
710 DdeUninitialize(pInstance
->instanceID
);
711 EnterCriticalSection(&WDML_CritSect
);
714 LeaveCriticalSection(&WDML_CritSect
);
717 /******************************************************************
718 * WDML_InvokeCallback
722 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
723 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
724 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
728 if (pInstance
== NULL
)
731 TRACE("invoking CB%d[%p] (%x %x %p %p %p %p %lx %lx)\n",
732 pInstance
->win16
? 16 : 32, pInstance
->callback
, uType
, uFmt
,
733 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
734 if (pInstance
->win16
)
736 ret
= WDML_InvokeCallback16(pInstance
->callback
, uType
, uFmt
, hConv
,
737 hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
741 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
743 TRACE("done => %p\n", ret
);
747 /*****************************************************************************
750 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
751 * for an instance Id, or NULL if the entry does not exist
754 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
756 WDML_INSTANCE
* pInstance
;
758 EnterCriticalSection(&WDML_CritSect
);
760 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
762 if (pInstance
->instanceID
== instId
)
764 if (GetCurrentThreadId() != pInstance
->threadID
)
766 FIXME("Tried to get instance from wrong thread\n");
773 LeaveCriticalSection(&WDML_CritSect
);
776 WARN("Instance entry missing for id %04x\n", instId
);
780 /******************************************************************
781 * WDML_GetInstanceFromWnd
785 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
787 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
790 /******************************************************************************
791 * DdeGetLastError [USER32.@] Gets most recent error code
794 * idInst [I] Instance identifier
799 UINT WINAPI
DdeGetLastError(DWORD idInst
)
802 WDML_INSTANCE
* pInstance
;
804 /* First check instance
806 pInstance
= WDML_GetInstance(idInst
);
807 if (pInstance
== NULL
)
809 error_code
= DMLERR_INVALIDPARAMETER
;
813 error_code
= pInstance
->lastError
;
814 pInstance
->lastError
= 0;
820 /* ================================================================
824 * ================================================================ */
827 /******************************************************************
832 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
836 if (pInstance
== NULL
) return NULL
;
838 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
840 if (pNode
->hsz
== hsz
) break;
842 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
846 /******************************************************************
847 * WDML_MakeAtomFromHsz
849 * Creates a global atom from an existing HSZ
850 * Generally used before sending an HSZ as an atom to a remote app
852 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
854 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
856 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
857 return GlobalAddAtomW(nameBuffer
);
858 WARN("HSZ %p not found\n", hsz
);
862 /******************************************************************
863 * WDML_MakeHszFromAtom
865 * Creates a HSZ from an existing global atom
866 * Generally used while receiving a global atom and transforming it
869 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
871 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
873 if (!atom
) return NULL
;
875 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
877 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
878 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
880 WARN("ATOM 0x%x not found\n", atom
);
884 /******************************************************************
889 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
893 pNode
= WDML_FindNode(pInstance
, hsz
);
894 if (!pNode
) return FALSE
;
900 /******************************************************************************
901 * WDML_DecHSZ (INTERNAL)
903 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
905 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
907 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
909 HSZNode
* pPrev
= NULL
;
912 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
914 /* If we found the node we were looking for and its ref count is one,
917 if (pCurrent
->hsz
== hsz
)
919 if (--pCurrent
->refCount
== 0)
921 if (pCurrent
== pInstance
->nodeList
)
923 pInstance
->nodeList
= pCurrent
->next
;
927 pPrev
->next
= pCurrent
->next
;
929 HeapFree(GetProcessHeap(), 0, pCurrent
);
930 DeleteAtom(HSZ2ATOM(hsz
));
935 WARN("HSZ %p not found\n", hsz
);
940 /******************************************************************************
941 * WDML_FreeAllHSZ (INTERNAL)
943 * Frees up all the strings still allocated in the list and
944 * remove all the nodes from the list of HSZ nodes.
946 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
948 /* Free any strings created in this instance.
950 while (pInstance
->nodeList
!= NULL
)
952 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
956 /******************************************************************************
957 * InsertHSZNode (INTERNAL)
959 * Insert a node to the head of the list.
961 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
965 HSZNode
* pNew
= NULL
;
966 /* Create a new node for this HSZ.
968 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
972 pNew
->next
= pInstance
->nodeList
;
974 pInstance
->nodeList
= pNew
;
978 ERR("Primary HSZ Node allocation failed - out of memory\n");
983 /******************************************************************
988 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
991 WCHAR pString
[MAX_BUFFER_LEN
];
993 /* If psz is null, we have to return only the length
999 cchMax
= MAX_BUFFER_LEN
;
1005 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
1008 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
1011 ERR("Unknown code page %d\n", codepage
);
1017 /*****************************************************************
1018 * DdeQueryStringA [USER32.@]
1020 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
1023 WDML_INSTANCE
* pInstance
;
1025 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1027 /* First check instance
1029 pInstance
= WDML_GetInstance(idInst
);
1030 if (pInstance
!= NULL
)
1032 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
1033 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1036 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
1040 /*****************************************************************
1041 * DdeQueryStringW [USER32.@]
1044 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
1047 WDML_INSTANCE
* pInstance
;
1049 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
1051 /* First check instance
1053 pInstance
= WDML_GetInstance(idInst
);
1054 if (pInstance
!= NULL
)
1056 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
1057 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
1060 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
1064 /******************************************************************
1069 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
1076 hsz
= ATOM2HSZ(AddAtomA(ptr
));
1077 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
1080 hsz
= ATOM2HSZ(AddAtomW(ptr
));
1081 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
1084 ERR("Unknown code page %d\n", codepage
);
1087 WDML_InsertHSZNode(pInstance
, hsz
);
1091 /*****************************************************************
1092 * DdeCreateStringHandleA [USER32.@]
1094 * See DdeCreateStringHandleW.
1096 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
1099 WDML_INSTANCE
* pInstance
;
1101 TRACE("(%d,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
1103 pInstance
= WDML_GetInstance(idInst
);
1106 if (codepage
== 0) codepage
= CP_WINANSI
;
1107 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1114 /******************************************************************************
1115 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
1118 * idInst [I] Instance identifier
1119 * psz [I] Pointer to string
1120 * codepage [I] Code page identifier
1122 * Success: String handle
1125 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
1127 WDML_INSTANCE
* pInstance
;
1130 pInstance
= WDML_GetInstance(idInst
);
1133 if (codepage
== 0) codepage
= CP_WINUNICODE
;
1134 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
1140 /*****************************************************************
1141 * DdeFreeStringHandle (USER32.@)
1146 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
1148 WDML_INSTANCE
* pInstance
;
1151 TRACE("(%d,%p):\n", idInst
, hsz
);
1153 /* First check instance
1155 pInstance
= WDML_GetInstance(idInst
);
1157 ret
= WDML_DecHSZ(pInstance
, hsz
);
1162 /*****************************************************************
1163 * DdeKeepStringHandle (USER32.@)
1169 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
1171 WDML_INSTANCE
* pInstance
;
1174 TRACE("(%d,%p):\n", idInst
, hsz
);
1176 /* First check instance
1178 pInstance
= WDML_GetInstance(idInst
);
1180 ret
= WDML_IncHSZ(pInstance
, hsz
);
1185 /*****************************************************************
1186 * DdeCmpStringHandles (USER32.@)
1188 * Compares the value of two string handles. This comparison is
1189 * not case sensitive.
1192 * hsz1 [I] Handle to the first string
1193 * hsz2 [I] Handle to the second string
1196 * -1 The value of hsz1 is zero or less than hsz2
1197 * 0 The values of hsz 1 and 2 are the same or both zero.
1198 * 1 The value of hsz2 is zero of less than hsz1
1200 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
1202 WCHAR psz1
[MAX_BUFFER_LEN
];
1203 WCHAR psz2
[MAX_BUFFER_LEN
];
1207 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
1208 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
1210 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
1212 /* Make sure we found both strings. */
1213 if (ret1
== 0 && ret2
== 0)
1215 /* If both are not found, return both "zero strings". */
1220 /* If hsz1 is a not found, return hsz1 is "zero string". */
1225 /* If hsz2 is a not found, return hsz2 is "zero string". */
1230 /* Compare the two strings we got (case insensitive). */
1231 ret
= lstrcmpiW(psz1
, psz2
);
1232 /* Since strcmp returns any number smaller than
1233 * 0 when the first string is found to be less than
1234 * the second one we must make sure we are returning
1235 * the proper values.
1250 /* ================================================================
1252 * Data handle management
1254 * ================================================================ */
1256 /*****************************************************************
1257 * DdeCreateDataHandle (USER32.@)
1259 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1260 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1262 /* For now, we ignore idInst, hszItem.
1263 * The purpose of these arguments still need to be investigated.
1268 DDE_DATAHANDLE_HEAD
* pDdh
;
1269 WCHAR psz
[MAX_BUFFER_LEN
];
1271 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1273 psz
[0] = HSZ2ATOM(hszItem
);
1277 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1278 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1280 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1283 /* we use the first 4 bytes to store the size */
1284 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1286 ERR("GlobalAlloc failed\n");
1290 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1297 pDdh
->cfFormat
= wFmt
;
1298 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1300 pByte
= (LPBYTE
)(pDdh
+ 1);
1303 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1307 TRACE("=> %p\n", hMem
);
1308 return (HDDEDATA
)hMem
;
1311 /*****************************************************************
1313 * DdeAddData (USER32.@)
1315 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1317 DWORD old_sz
, new_sz
;
1320 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData
, pSrc
, cb
, cbOff
);
1322 pDst
= DdeAccessData(hData
, &old_sz
);
1323 if (!pDst
) return 0;
1325 new_sz
= cb
+ cbOff
;
1326 if (new_sz
> old_sz
)
1328 DdeUnaccessData(hData
);
1329 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1330 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1331 pDst
= DdeAccessData(hData
, &old_sz
);
1334 if (!pDst
) return 0;
1336 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1337 DdeUnaccessData(hData
);
1341 /******************************************************************************
1342 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1346 * hData [I] Handle to DDE object
1347 * pDst [I] Pointer to destination buffer
1348 * cbMax [I] Amount of data to copy
1349 * cbOff [I] Offset to beginning of data
1352 * Size of memory object associated with handle
1354 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1356 DWORD dwSize
, dwRet
;
1359 TRACE("(%p,%p,%d,%d)\n", hData
, pDst
, cbMax
, cbOff
);
1361 pByte
= DdeAccessData(hData
, &dwSize
);
1369 else if (cbOff
+ cbMax
< dwSize
)
1373 else if (cbOff
< dwSize
)
1375 dwRet
= dwSize
- cbOff
;
1381 if (pDst
&& dwRet
!= 0)
1383 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1385 DdeUnaccessData(hData
);
1394 /*****************************************************************
1395 * DdeAccessData (USER32.@)
1397 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1399 HGLOBAL hMem
= hData
;
1400 DDE_DATAHANDLE_HEAD
* pDdh
;
1402 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1404 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hMem
);
1407 ERR("Failed on GlobalLock(%p)\n", hMem
);
1411 if (pcbDataSize
!= NULL
)
1413 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1415 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1416 return (LPBYTE
)(pDdh
+ 1);
1419 /*****************************************************************
1420 * DdeUnaccessData (USER32.@)
1422 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1424 HGLOBAL hMem
= hData
;
1426 TRACE("(%p)\n", hData
);
1433 /*****************************************************************
1434 * DdeFreeDataHandle (USER32.@)
1436 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1438 TRACE("(%p)\n", hData
);
1439 return GlobalFree(hData
) == 0;
1442 /******************************************************************
1447 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1449 DDE_DATAHANDLE_HEAD
* pDdh
;
1452 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock(hData
);
1455 ret
= pDdh
->bAppOwned
;
1456 GlobalUnlock(hData
);
1461 /* ================================================================
1463 * Global <=> Data handle management
1465 * ================================================================ */
1467 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1469 * (bytes) (bits) comment
1470 * 0 16 bit fields for options (release, ackreq, response...)
1471 * 2 16 clipboard format
1472 * 4 ? data to be used
1474 HDDEDATA
WDML_Global2DataHandle(HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1482 pDd
= GlobalLock(hMem
);
1483 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1486 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1487 switch (pDd
->cfFormat
)
1490 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1491 pDd
->cfFormat
, hMem
);
1495 ret
= DdeCreateDataHandle(0, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1498 if (size
>= sizeof(BITMAP
))
1500 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1501 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1502 if (size
>= sizeof(BITMAP
) + count
)
1506 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1507 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1508 pDd
->Value
+ sizeof(BITMAP
))))
1510 ret
= DdeCreateDataHandle(0, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1511 0, 0, CF_BITMAP
, 0);
1513 else ERR("Can't create bmp\n");
1517 ERR("Wrong count: %u / %d\n", size
, count
);
1519 } else ERR("No bitmap header\n");
1528 /******************************************************************
1529 * WDML_DataHandle2Global
1533 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1534 BOOL fDeferUpd
, BOOL fAckReq
)
1536 DDE_DATAHANDLE_HEAD
* pDdh
;
1540 dwSize
= GlobalSize((HGLOBAL
)hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1541 pDdh
= (DDE_DATAHANDLE_HEAD
*)GlobalLock((HGLOBAL
)hDdeData
);
1544 WINE_DDEHEAD
* wdh
= NULL
;
1546 switch (pDdh
->cfFormat
)
1549 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1550 pDdh
->cfFormat
, hDdeData
);
1554 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1555 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1557 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1561 if (dwSize
>= sizeof(HBITMAP
))
1565 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1567 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1569 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1570 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1571 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1572 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1574 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1575 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1584 wdh
->fResponse
= fResponse
;
1585 wdh
->fRelease
= fRelease
;
1586 wdh
->fDeferUpd
= fDeferUpd
;
1587 wdh
->fAckReq
= fAckReq
;
1588 wdh
->cfFormat
= pDdh
->cfFormat
;
1591 GlobalUnlock((HGLOBAL
)hDdeData
);
1597 /* ================================================================
1601 * ================================================================ */
1603 /******************************************************************
1608 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1610 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','0','8','l','x',')',0};
1611 WDML_SERVER
* pServer
;
1615 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1616 if (pServer
== NULL
) return NULL
;
1618 pServer
->hszService
= hszService
;
1619 WDML_IncHSZ(pInstance
, hszService
);
1621 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1622 snprintfW(buf2
, 256, fmtW
, buf1
, GetCurrentProcessId());
1623 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1625 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1626 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1628 pServer
->filterOn
= TRUE
;
1630 pServer
->next
= pInstance
->servers
;
1631 pInstance
->servers
= pServer
;
1635 /******************************************************************
1640 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1642 WDML_SERVER
* pPrev
= NULL
;
1643 WDML_SERVER
* pServer
= NULL
;
1645 WDML_CONV
* pConvNext
;
1647 pServer
= pInstance
->servers
;
1649 while (pServer
!= NULL
)
1651 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1653 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1654 pServer
->atomService
, pServer
->atomServiceSpec
);
1655 /* terminate all conversations for given topic */
1656 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1658 pConvNext
= pConv
->next
;
1659 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1661 HWND client
= pConv
->hwndClient
, server
= pConv
->hwndServer
;
1662 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1663 /* don't care about return code (whether client window is present or not) */
1664 PostMessageW(client
, WM_DDE_TERMINATE
, (WPARAM
)server
, 0);
1667 if (pServer
== pInstance
->servers
)
1669 pInstance
->servers
= pServer
->next
;
1673 pPrev
->next
= pServer
->next
;
1676 DestroyWindow(pServer
->hwndServer
);
1677 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1678 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1680 GlobalDeleteAtom(pServer
->atomService
);
1681 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1683 HeapFree(GetProcessHeap(), 0, pServer
);
1688 pServer
= pServer
->next
;
1692 /*****************************************************************************
1695 * generic routine to return a pointer to the relevant ServiceNode
1696 * for a given service name, or NULL if the entry does not exist
1699 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1701 WDML_SERVER
* pServer
;
1703 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1705 if (hszService
== pServer
->hszService
)
1710 TRACE("Service name missing\n");
1714 /* ================================================================
1716 * Conversation management
1718 * ================================================================ */
1720 /******************************************************************
1725 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1726 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
1730 /* no conversation yet, add it */
1731 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
1732 if (!pConv
) return NULL
;
1734 pConv
->instance
= pInstance
;
1735 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
1736 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
1737 pConv
->magic
= WDML_CONV_MAGIC
;
1738 pConv
->hwndServer
= hwndServer
;
1739 pConv
->hwndClient
= hwndClient
;
1740 pConv
->transactions
= NULL
;
1742 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
1743 pConv
->wStatus
|= pInstance
->wStatus
;
1744 /* check if both side of the conversation are of the same instance */
1745 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
1746 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
1748 pConv
->wStatus
|= ST_ISSELF
;
1750 pConv
->wConvst
= XST_NULL
;
1752 pConv
->next
= pInstance
->convs
[side
];
1753 pInstance
->convs
[side
] = pConv
;
1755 TRACE("pConv->wStatus %04x\n", pConv
->wStatus
);
1760 /******************************************************************
1765 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
1766 HSZ hszService
, HSZ hszTopic
)
1768 WDML_CONV
* pCurrent
= NULL
;
1770 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1772 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
1773 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
1782 /******************************************************************
1787 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
1789 WDML_CONV
* pPrev
= NULL
;
1790 WDML_CONV
* pCurrent
;
1792 WDML_XACT
* pXActNext
;
1798 /* remove any pending transaction */
1799 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
1801 pXActNext
= pXAct
->next
;
1802 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
1805 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
1807 /* FIXME: should we keep the window around ? it seems so (at least on client side
1808 * to let QueryConvInfo work after conv termination, but also to implement
1811 /* destroy conversation window, but first remove pConv from hWnd.
1812 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
1814 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
1815 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
1817 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
1819 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
1820 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
1822 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
1824 if (pCurrent
== pRef
)
1826 if (pCurrent
== pRef
->instance
->convs
[side
])
1828 pRef
->instance
->convs
[side
] = pCurrent
->next
;
1832 pPrev
->next
= pCurrent
->next
;
1834 pCurrent
->magic
= 0;
1835 HeapFree(GetProcessHeap(), 0, pCurrent
);
1841 /******************************************************************
1842 * WDML_EnableCallback
1844 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
1846 if (wCmd
== EC_DISABLE
)
1848 pConv
->wStatus
|= ST_BLOCKED
;
1849 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1853 if (wCmd
== EC_QUERYWAITING
)
1854 return pConv
->transactions
? TRUE
: FALSE
;
1856 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
1858 FIXME("Unknown command code %04x\n", wCmd
);
1862 if (wCmd
== EC_ENABLEALL
)
1864 pConv
->wStatus
&= ~ST_BLOCKED
;
1865 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
1868 while (pConv
->transactions
)
1870 WDML_XACT
*pXAct
= pConv
->transactions
;
1872 if (pConv
->wStatus
& ST_CLIENT
)
1874 /* transaction should be in the queue until handled */
1875 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
1876 WDML_UnQueueTransaction(pConv
, pXAct
);
1880 /* transaction should be removed from the queue before handling */
1881 WDML_UnQueueTransaction(pConv
, pXAct
);
1882 WDML_ServerHandle(pConv
, pXAct
);
1885 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
1887 if (wCmd
== EC_ENABLEONE
) break;
1892 /*****************************************************************
1893 * DdeEnableCallback (USER32.@)
1895 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
1900 TRACE("(%d, %p, %04x)\n", idInst
, hConv
, wCmd
);
1904 pConv
= WDML_GetConv(hConv
, TRUE
);
1906 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
1907 ret
= WDML_EnableCallback(pConv
, wCmd
);
1911 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
1916 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
1917 pInstance
->wStatus
|= wCmd
;
1919 if (wCmd
== EC_DISABLE
)
1921 pInstance
->wStatus
|= ST_BLOCKED
;
1922 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1924 else if (wCmd
== EC_ENABLEALL
)
1926 pInstance
->wStatus
&= ~ST_BLOCKED
;
1927 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
1932 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
1934 ret
= WDML_EnableCallback(pConv
, wCmd
);
1935 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
1942 /******************************************************************
1947 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
1949 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
1951 /* FIXME: should do better checking */
1952 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
1954 if (!pConv
->instance
|| pConv
->instance
->threadID
!= GetCurrentThreadId())
1956 WARN("wrong thread ID\n");
1957 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
1961 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
1963 WARN("found conv but ain't connected\n");
1964 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
1971 /******************************************************************
1972 * WDML_GetConvFromWnd
1976 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
1978 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
1981 /******************************************************************
1986 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
1987 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
1992 if (side
== WDML_SERVER_SIDE
)
1994 from
= pConv
->hwndServer
;
1995 to
= pConv
->hwndClient
;
1999 to
= pConv
->hwndServer
;
2000 from
= pConv
->hwndClient
;
2003 ddeAck
.bAppReturnCode
= appRetCode
;
2004 ddeAck
.reserved
= 0;
2005 ddeAck
.fBusy
= fBusy
;
2008 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
2010 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
2011 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
2012 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
2014 pConv
->wStatus
&= ~ST_CONNECTED
;
2015 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
2016 FreeDDElParam(WM_DDE_ACK
, lParam
);
2022 /*****************************************************************
2023 * DdeSetUserHandle (USER32.@)
2025 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2029 pConv
= WDML_GetConv(hConv
, FALSE
);
2035 pConv
->hUser
= hUser
;
2041 pXAct
= WDML_FindTransaction(pConv
, id
);
2044 pXAct
->hUser
= hUser
;
2048 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2055 /******************************************************************
2056 * WDML_GetLocalConvInfo
2060 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2066 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2067 ci
->hszSvcPartner
= pConv
->hszService
;
2068 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2069 ci
->hszTopic
= pConv
->hszTopic
;
2070 ci
->wStatus
= pConv
->wStatus
;
2072 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2074 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2076 if (pLink
->hConv
== (HCONV
)pConv
)
2078 ci
->wStatus
|= ST_ADVISE
;
2083 /* FIXME: non handled status flags:
2089 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2091 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2093 ci
->ConvCtxt
= pConv
->convContext
;
2094 if (ci
->wStatus
& ST_CLIENT
)
2096 ci
->hwnd
= pConv
->hwndClient
;
2097 ci
->hwndPartner
= pConv
->hwndServer
;
2101 ci
->hwnd
= pConv
->hwndServer
;
2102 ci
->hwndPartner
= pConv
->hwndClient
;
2106 ci
->hUser
= pConv
->hUser
;
2115 pXAct
= WDML_FindTransaction(pConv
, id
);
2118 ci
->hUser
= pXAct
->hUser
;
2119 ci
->hszItem
= pXAct
->hszItem
;
2120 ci
->wFmt
= pXAct
->wFmt
;
2121 ci
->wType
= pXAct
->wType
;
2126 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2132 /******************************************************************
2133 * DdeQueryConvInfo (USER32.@)
2135 * FIXME: Set last DDE error on failure.
2137 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2139 UINT ret
= lpConvInfo
->cb
;
2143 TRACE("(%p,%x,%p)\n", hConv
, id
, lpConvInfo
);
2147 FIXME("hConv is NULL\n");
2151 pConv
= WDML_GetConv(hConv
, FALSE
);
2154 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2159 if ((ULONG_PTR
)hConv
& 1)
2161 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2163 FIXME("Request on remote conversation information is not implemented yet\n");
2169 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2173 /* ================================================================
2175 * Link (hot & warm) management
2177 * ================================================================ */
2179 /******************************************************************
2184 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2185 UINT wType
, HSZ hszItem
, UINT wFmt
)
2189 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
2196 pLink
->hConv
= hConv
;
2197 pLink
->transactionType
= wType
;
2198 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
2200 pLink
->next
= pInstance
->links
[side
];
2201 pInstance
->links
[side
] = pLink
;
2204 /******************************************************************
2209 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2210 HSZ hszItem
, UINT uFmt
)
2212 WDML_LINK
* pPrev
= NULL
;
2213 WDML_LINK
* pCurrent
= NULL
;
2215 pCurrent
= pInstance
->links
[side
];
2217 while (pCurrent
!= NULL
)
2219 if (pCurrent
->hConv
== hConv
&&
2220 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2221 pCurrent
->uFmt
== uFmt
)
2223 if (pCurrent
== pInstance
->links
[side
])
2225 pInstance
->links
[side
] = pCurrent
->next
;
2229 pPrev
->next
= pCurrent
->next
;
2232 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2233 HeapFree(GetProcessHeap(), 0, pCurrent
);
2238 pCurrent
= pCurrent
->next
;
2242 /* this function is called to remove all links related to the conv.
2243 It should be called from both client and server when terminating
2246 /******************************************************************
2247 * WDML_RemoveAllLinks
2251 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
2253 WDML_LINK
* pPrev
= NULL
;
2254 WDML_LINK
* pCurrent
= NULL
;
2255 WDML_LINK
* pNext
= NULL
;
2257 pCurrent
= pInstance
->links
[side
];
2259 while (pCurrent
!= NULL
)
2261 if (pCurrent
->hConv
== (HCONV
)pConv
)
2263 if (pCurrent
== pInstance
->links
[side
])
2265 pInstance
->links
[side
] = pCurrent
->next
;
2266 pNext
= pCurrent
->next
;
2270 pPrev
->next
= pCurrent
->next
;
2271 pNext
= pCurrent
->next
;
2274 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
2276 HeapFree(GetProcessHeap(), 0, pCurrent
);
2283 pCurrent
= pCurrent
->next
;
2292 /******************************************************************
2297 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
2298 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
2300 WDML_LINK
* pCurrent
= NULL
;
2302 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2304 /* we don't need to check for transaction type as it can be altered */
2306 if (pCurrent
->hConv
== hConv
&&
2307 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
2308 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
2318 /* ================================================================
2320 * Transaction management
2322 * ================================================================ */
2324 /******************************************************************
2325 * WDML_AllocTransaction
2327 * Alloc a transaction structure for handling the message ddeMsg
2329 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
2330 UINT wFmt
, HSZ hszItem
)
2333 static WORD tid
= 1; /* FIXME: wrap around */
2335 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
2338 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
2342 pXAct
->xActID
= tid
++;
2343 pXAct
->ddeMsg
= ddeMsg
;
2344 pXAct
->hDdeData
= 0;
2349 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
2357 /******************************************************************
2358 * WDML_QueueTransaction
2360 * Adds a transaction to the list of transaction
2362 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2366 /* advance to last in queue */
2367 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
2371 /******************************************************************
2372 * WDML_UnQueueTransaction
2376 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
2380 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
2391 /******************************************************************
2392 * WDML_FreeTransaction
2396 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
2398 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
2399 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
2401 GlobalFree(pXAct
->hMem
);
2403 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
2405 HeapFree(GetProcessHeap(), 0, pXAct
);
2408 /******************************************************************
2409 * WDML_FindTransaction
2413 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2418 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2420 if (pXAct
->xActID
== tid
)
2426 /* ================================================================
2428 * Information broadcast across DDEML implementations
2430 * ================================================================ */
2432 struct tagWDML_BroadcastPmt
2440 /******************************************************************
2441 * WDML_BroadcastEnumProc
2445 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2447 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2450 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2451 lstrcmpiW(buffer
, s
->clsName
) == 0)
2453 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2458 /******************************************************************
2459 * WDML_BroadcastDDEWindows
2463 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2465 struct tagWDML_BroadcastPmt s
;
2467 s
.clsName
= clsName
;
2471 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);