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 MAKELONG(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 * WDML Error management
255 * ================================================================ */
257 /******************************************************************************
258 * DdeGetLastError [USER32.@] Gets most recent error code
261 * idInst [I] Instance identifier
266 UINT WINAPI
DdeGetLastError(DWORD idInst
)
269 WDML_INSTANCE
* pInstance
;
271 /* First check instance
273 pInstance
= WDML_GetInstance(idInst
);
274 if (pInstance
== NULL
)
276 error_code
= DMLERR_INVALIDPARAMETER
;
280 error_code
= pInstance
->lastError
;
281 pInstance
->lastError
= 0;
287 /******************************************************************
288 * WDML_SetAllLastError
292 static void WDML_SetAllLastError(DWORD lastError
)
295 WDML_INSTANCE
* pInstance
;
296 threadID
= GetCurrentThreadId();
297 pInstance
= WDML_InstanceList
;
300 if (pInstance
->threadID
== threadID
)
301 pInstance
->lastError
= lastError
;
302 pInstance
= pInstance
->next
;
306 /* ================================================================
310 * ================================================================ */
313 /******************************************************************
318 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
322 if (pInstance
== NULL
) return NULL
;
324 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
326 if (pNode
->hsz
== hsz
) break;
328 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
332 /******************************************************************
333 * WDML_MakeAtomFromHsz
335 * Creates a global atom from an existing HSZ
336 * Generally used before sending an HSZ as an atom to a remote app
338 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
340 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
342 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
343 return GlobalAddAtomW(nameBuffer
);
344 WARN("HSZ %p not found\n", hsz
);
348 /******************************************************************
349 * WDML_MakeHszFromAtom
351 * Creates a HSZ from an existing global atom
352 * Generally used while receiving a global atom and transforming it
355 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
357 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
359 if (!atom
) return NULL
;
361 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
363 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
364 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
366 WARN("ATOM 0x%x not found\n", atom
);
370 /******************************************************************
375 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
379 pNode
= WDML_FindNode(pInstance
, hsz
);
380 if (!pNode
) return FALSE
;
386 /******************************************************************************
387 * WDML_DecHSZ (INTERNAL)
389 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
391 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
393 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
395 HSZNode
* pPrev
= NULL
;
398 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
400 /* If we found the node we were looking for and its ref count is one,
403 if (pCurrent
->hsz
== hsz
)
405 if (--pCurrent
->refCount
== 0)
407 if (pCurrent
== pInstance
->nodeList
)
409 pInstance
->nodeList
= pCurrent
->next
;
413 pPrev
->next
= pCurrent
->next
;
415 HeapFree(GetProcessHeap(), 0, pCurrent
);
416 DeleteAtom(HSZ2ATOM(hsz
));
421 WARN("HSZ %p not found\n", hsz
);
426 /******************************************************************************
427 * WDML_FreeAllHSZ (INTERNAL)
429 * Frees up all the strings still allocated in the list and
430 * remove all the nodes from the list of HSZ nodes.
432 static void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
434 /* Free any strings created in this instance.
436 while (pInstance
->nodeList
!= NULL
)
438 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
442 /******************************************************************************
443 * InsertHSZNode (INTERNAL)
445 * Insert a node to the head of the list.
447 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
451 HSZNode
* pNew
= NULL
;
452 /* Create a new node for this HSZ.
454 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
458 pNew
->next
= pInstance
->nodeList
;
460 pInstance
->nodeList
= pNew
;
464 ERR("Primary HSZ Node allocation failed - out of memory\n");
469 /******************************************************************
474 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
477 WCHAR pString
[MAX_BUFFER_LEN
];
479 /* If psz is null, we have to return only the length
485 cchMax
= MAX_BUFFER_LEN
;
488 /* if there is no input windows returns a NULL string */
499 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
502 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
505 ERR("Unknown code page %d\n", codepage
);
511 /*****************************************************************
512 * DdeQueryStringA [USER32.@]
514 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
517 WDML_INSTANCE
* pInstance
;
519 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
521 /* First check instance
523 pInstance
= WDML_GetInstance(idInst
);
524 if (pInstance
!= NULL
)
526 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
527 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
530 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
534 /*****************************************************************
535 * DdeQueryStringW [USER32.@]
538 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
541 WDML_INSTANCE
* pInstance
;
543 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
545 /* First check instance
547 pInstance
= WDML_GetInstance(idInst
);
548 if (pInstance
!= NULL
)
550 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
551 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
554 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
558 /******************************************************************
563 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
570 hsz
= ATOM2HSZ(AddAtomA(ptr
));
571 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
574 hsz
= ATOM2HSZ(AddAtomW(ptr
));
575 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
578 ERR("Unknown code page %d\n", codepage
);
581 WDML_InsertHSZNode(pInstance
, hsz
);
585 /*****************************************************************
586 * DdeCreateStringHandleA [USER32.@]
588 * See DdeCreateStringHandleW.
590 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
593 WDML_INSTANCE
* pInstance
;
595 TRACE("(%d,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
597 pInstance
= WDML_GetInstance(idInst
);
598 if (pInstance
== NULL
)
599 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
602 if (codepage
== 0) codepage
= CP_WINANSI
;
603 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
610 /******************************************************************************
611 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
614 * idInst [I] Instance identifier
615 * psz [I] Pointer to string
616 * codepage [I] Code page identifier
618 * Success: String handle
621 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
623 WDML_INSTANCE
* pInstance
;
626 pInstance
= WDML_GetInstance(idInst
);
627 if (pInstance
== NULL
)
628 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
631 if (codepage
== 0) codepage
= CP_WINUNICODE
;
632 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
638 /*****************************************************************
639 * DdeFreeStringHandle (USER32.@)
644 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
646 WDML_INSTANCE
* pInstance
;
649 TRACE("(%d,%p):\n", idInst
, hsz
);
651 /* First check instance
653 pInstance
= WDML_GetInstance(idInst
);
655 ret
= WDML_DecHSZ(pInstance
, hsz
);
660 /*****************************************************************
661 * DdeKeepStringHandle (USER32.@)
667 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
669 WDML_INSTANCE
* pInstance
;
672 TRACE("(%d,%p):\n", idInst
, hsz
);
674 /* First check instance
676 pInstance
= WDML_GetInstance(idInst
);
678 ret
= WDML_IncHSZ(pInstance
, hsz
);
683 /*****************************************************************
684 * DdeCmpStringHandles (USER32.@)
686 * Compares the value of two string handles. This comparison is
687 * not case sensitive.
690 * hsz1 [I] Handle to the first string
691 * hsz2 [I] Handle to the second string
694 * -1 The value of hsz1 is zero or less than hsz2
695 * 0 The values of hsz 1 and 2 are the same or both zero.
696 * 1 The value of hsz2 is zero of less than hsz1
698 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
700 WCHAR psz1
[MAX_BUFFER_LEN
];
701 WCHAR psz2
[MAX_BUFFER_LEN
];
705 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
706 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
708 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
710 /* Make sure we found both strings. */
711 if (ret1
== 0 && ret2
== 0)
713 /* If both are not found, return both "zero strings". */
718 /* If hsz1 is a not found, return hsz1 is "zero string". */
723 /* If hsz2 is a not found, return hsz2 is "zero string". */
728 /* Compare the two strings we got (case insensitive). */
729 ret
= lstrcmpiW(psz1
, psz2
);
730 /* Since strcmp returns any number smaller than
731 * 0 when the first string is found to be less than
732 * the second one we must make sure we are returning
748 /* ================================================================
750 * Instance management
752 * ================================================================ */
754 /******************************************************************************
755 * IncrementInstanceId
757 * generic routine to increment the max instance Id and allocate a new application instance
759 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
761 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
763 pInstance
->instanceID
= id
;
764 TRACE("New instance id %d allocated\n", id
);
767 /******************************************************************
772 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
774 WDML_INSTANCE
* pInstance
;
779 case WM_WDML_REGISTER
:
780 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
781 /* try calling the Callback */
782 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
784 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
785 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
786 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
787 WDML_DecHSZ(pInstance
, hsz1
);
788 WDML_DecHSZ(pInstance
, hsz2
);
792 case WM_WDML_UNREGISTER
:
793 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
794 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
796 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
797 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
798 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
799 WDML_DecHSZ(pInstance
, hsz1
);
800 WDML_DecHSZ(pInstance
, hsz2
);
804 case WM_WDML_CONNECT_CONFIRM
:
805 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
806 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
809 /* confirm connection...
810 * lookup for this conv handle
812 HWND client
= WIN_GetFullHandle( (HWND
)wParam
);
813 HWND server
= WIN_GetFullHandle( (HWND
)lParam
);
814 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
816 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
821 pConv
->wStatus
|= ST_ISLOCAL
;
823 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
824 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
825 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
830 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
835 /******************************************************************
840 static UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
841 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
)
843 WDML_INSTANCE
* pInstance
;
844 WDML_INSTANCE
* reference_inst
;
846 WNDCLASSEXW wndclass
;
848 TRACE("(%p,%p,0x%x,%d,0x%x)\n",
849 pidInst
, pfnCallback
, afCmd
, ulRes
, bUnicode
);
853 ERR("Reserved value not zero? What does this mean?\n");
854 /* trap this and no more until we know more */
855 return DMLERR_NO_ERROR
;
858 /* grab enough heap for one control struct - not really necessary for re-initialise
859 * but allows us to use same validation routines */
860 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
861 if (pInstance
== NULL
)
863 /* catastrophe !! warn user & abort */
864 ERR("Instance create failed - out of memory\n");
865 return DMLERR_SYS_ERROR
;
867 pInstance
->next
= NULL
;
868 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
870 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
872 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
873 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
874 pInstance
->threadID
= GetCurrentThreadId();
875 pInstance
->callback
= *pfnCallback
;
876 pInstance
->unicode
= bUnicode
;
877 pInstance
->nodeList
= NULL
; /* node will be added later */
878 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
879 pInstance
->wStatus
= 0;
880 pInstance
->lastError
= DMLERR_NO_ERROR
;
881 pInstance
->servers
= NULL
;
882 pInstance
->convs
[0] = NULL
;
883 pInstance
->convs
[1] = NULL
;
884 pInstance
->links
[0] = NULL
;
885 pInstance
->links
[1] = NULL
;
887 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
889 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
891 if (!pInstance
->clientOnly
)
893 /* Check for other way of setting Client-only !! */
894 pInstance
->clientOnly
=
895 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
898 TRACE("instance created - checking validity\n");
902 /* Initialisation of new Instance Identifier */
903 TRACE("new instance, callback %p flags %X\n",pfnCallback
,afCmd
);
905 EnterCriticalSection(&WDML_CritSect
);
907 if (WDML_InstanceList
== NULL
)
909 /* can't be another instance in this case, assign to the base pointer */
910 WDML_InstanceList
= pInstance
;
912 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
914 * ------------------------------- NOTE NOTE NOTE --------------------------
916 * the manual is not clear if this condition
917 * applies to the first call to DdeInitialize from an application, or the
918 * first call for a given callback !!!
921 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
922 TRACE("First application instance detected OK\n");
923 /* allocate new instance ID */
924 WDML_IncrementInstanceId(pInstance
);
928 /* really need to chain the new one in to the latest here, but after checking conditions
929 * such as trying to start a conversation from an application trying to monitor */
930 reference_inst
= WDML_InstanceList
;
931 TRACE("Subsequent application instance - starting checks\n");
932 while (reference_inst
->next
!= NULL
)
935 * This set of tests will work if application uses same instance Id
936 * at application level once allocated - which is what manual implies
937 * should happen. If someone tries to be
938 * clever (lazy ?) it will fail to pick up that later calls are for
939 * the same application - should we trust them ?
941 if (pInstance
->instanceID
== reference_inst
->instanceID
)
943 /* Check 1 - must be same Client-only state */
945 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
947 ret
= DMLERR_DLL_USAGE
;
951 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
953 if (pInstance
->monitor
!= reference_inst
->monitor
)
955 ret
= DMLERR_INVALIDPARAMETER
;
959 /* Check 3 - must supply different callback address */
961 if (pInstance
->callback
== reference_inst
->callback
)
963 ret
= DMLERR_DLL_USAGE
;
967 reference_inst
= reference_inst
->next
;
969 /* All cleared, add to chain */
971 TRACE("Application Instance checks finished\n");
972 WDML_IncrementInstanceId(pInstance
);
973 reference_inst
->next
= pInstance
;
975 LeaveCriticalSection(&WDML_CritSect
);
977 *pidInst
= pInstance
->instanceID
;
979 /* for deadlock issues, windows must always be created when outside the critical section */
980 wndclass
.cbSize
= sizeof(wndclass
);
982 wndclass
.lpfnWndProc
= WDML_EventProc
;
983 wndclass
.cbClsExtra
= 0;
984 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
985 wndclass
.hInstance
= 0;
987 wndclass
.hCursor
= 0;
988 wndclass
.hbrBackground
= 0;
989 wndclass
.lpszMenuName
= NULL
;
990 wndclass
.lpszClassName
= WDML_szEventClass
;
991 wndclass
.hIconSm
= 0;
993 RegisterClassExW(&wndclass
);
995 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
996 WS_POPUP
, 0, 0, 0, 0,
999 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
1001 TRACE("New application instance processing finished OK\n");
1005 /* Reinitialisation situation --- FIX */
1006 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
1008 EnterCriticalSection(&WDML_CritSect
);
1010 if (WDML_InstanceList
== NULL
)
1012 ret
= DMLERR_INVALIDPARAMETER
;
1015 /* can't reinitialise if we have initialised nothing !! */
1016 reference_inst
= WDML_InstanceList
;
1017 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
1019 * MS allows initialisation without specifying a callback, should we allow addition of the
1020 * callback by a later call to initialise ? - if so this lot will have to change
1022 while (reference_inst
->next
!= NULL
)
1024 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
1026 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
1028 if (reference_inst
->clientOnly
)
1030 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
1032 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
1034 if (!(afCmd
& APPCMD_CLIENTONLY
))
1036 ret
= DMLERR_INVALIDPARAMETER
;
1041 /* Check 2 - cannot change monitor modes */
1043 if (pInstance
->monitor
!= reference_inst
->monitor
)
1045 ret
= DMLERR_INVALIDPARAMETER
;
1049 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
1051 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
1053 ret
= DMLERR_INVALIDPARAMETER
;
1058 reference_inst
= reference_inst
->next
;
1060 if (reference_inst
->next
== NULL
)
1062 ret
= DMLERR_INVALIDPARAMETER
;
1065 /* All checked - change relevant flags */
1067 reference_inst
->CBFflags
= pInstance
->CBFflags
;
1068 reference_inst
->clientOnly
= pInstance
->clientOnly
;
1069 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
1071 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
1073 LeaveCriticalSection(&WDML_CritSect
);
1076 return DMLERR_NO_ERROR
;
1078 HeapFree(GetProcessHeap(), 0, pInstance
);
1079 LeaveCriticalSection(&WDML_CritSect
);
1083 /******************************************************************************
1084 * DdeInitializeA (USER32.@)
1086 * See DdeInitializeW.
1088 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1089 DWORD afCmd
, DWORD ulRes
)
1091 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
);
1094 /******************************************************************************
1095 * DdeInitializeW [USER32.@]
1096 * Registers an application with the DDEML
1099 * pidInst [I] Pointer to instance identifier
1100 * pfnCallback [I] Pointer to callback function
1101 * afCmd [I] Set of command and filter flags
1102 * ulRes [I] Reserved
1105 * Success: DMLERR_NO_ERROR
1106 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
1108 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1109 DWORD afCmd
, DWORD ulRes
)
1111 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
);
1114 /*****************************************************************
1115 * DdeUninitialize [USER32.@] Frees DDEML resources
1118 * idInst [I] Instance identifier
1125 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
1127 /* Stage one - check if we have a handle for this instance
1129 WDML_INSTANCE
* pInstance
;
1131 WDML_CONV
* pConvNext
;
1133 TRACE("(%d)\n", idInst
);
1135 /* First check instance
1137 pInstance
= WDML_GetInstance(idInst
);
1138 if (pInstance
== NULL
)
1141 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
1146 /* first terminate all conversations client side
1147 * this shall close existing links...
1149 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1151 pConvNext
= pConv
->next
;
1152 DdeDisconnect((HCONV
)pConv
);
1154 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
1155 FIXME("still pending conversations\n");
1157 /* then unregister all known service names */
1158 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
1160 /* Free the nodes that were not freed by this instance
1161 * and remove the nodes from the list of HSZ nodes.
1163 WDML_FreeAllHSZ(pInstance
);
1165 DestroyWindow(pInstance
->hwndEvent
);
1167 /* OK now delete the instance handle itself */
1169 if (WDML_InstanceList
== pInstance
)
1171 /* special case - the first/only entry */
1172 WDML_InstanceList
= pInstance
->next
;
1176 /* general case, remove entry */
1177 WDML_INSTANCE
* inst
;
1179 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
1180 inst
->next
= pInstance
->next
;
1182 /* release the heap entry
1184 HeapFree(GetProcessHeap(), 0, pInstance
);
1189 /******************************************************************
1190 * WDML_NotifyThreadExit
1194 void WDML_NotifyThreadDetach(void)
1196 WDML_INSTANCE
* pInstance
;
1197 WDML_INSTANCE
* next
;
1198 DWORD tid
= GetCurrentThreadId();
1200 EnterCriticalSection(&WDML_CritSect
);
1201 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
1203 next
= pInstance
->next
;
1204 if (pInstance
->threadID
== tid
)
1206 LeaveCriticalSection(&WDML_CritSect
);
1207 DdeUninitialize(pInstance
->instanceID
);
1208 EnterCriticalSection(&WDML_CritSect
);
1211 LeaveCriticalSection(&WDML_CritSect
);
1214 /******************************************************************
1215 * WDML_InvokeCallback
1219 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
1220 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
1221 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
1225 if (pInstance
== NULL
)
1228 TRACE("invoking CB[%p] (%x %x %p %p %p %p %lx %lx)\n",
1229 pInstance
->callback
, uType
, uFmt
,
1230 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1231 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1232 TRACE("done => %p\n", ret
);
1236 /*****************************************************************************
1239 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
1240 * for an instance Id, or NULL if the entry does not exist
1243 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
1245 WDML_INSTANCE
* pInstance
;
1247 EnterCriticalSection(&WDML_CritSect
);
1249 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
1251 if (pInstance
->instanceID
== instId
)
1253 if (GetCurrentThreadId() != pInstance
->threadID
)
1255 FIXME("Tried to get instance from wrong thread\n");
1262 LeaveCriticalSection(&WDML_CritSect
);
1265 WARN("Instance entry missing for id %04x\n", instId
);
1269 /******************************************************************
1270 * WDML_GetInstanceFromWnd
1274 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
1276 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
1279 /* ================================================================
1281 * Data handle management
1283 * ================================================================ */
1285 /*****************************************************************
1286 * DdeCreateDataHandle (USER32.@)
1288 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1289 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1292 /* Other than check for validity we will ignore for now idInst, hszItem.
1293 * The purpose of these arguments still need to be investigated.
1296 WDML_INSTANCE
* pInstance
;
1299 DDE_DATAHANDLE_HEAD
* pDdh
;
1300 WCHAR psz
[MAX_BUFFER_LEN
];
1302 pInstance
= WDML_GetInstance(idInst
);
1303 if (pInstance
== NULL
)
1305 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1309 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1311 psz
[0] = HSZ2ATOM(hszItem
);
1315 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1316 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1318 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1321 /* we use the first 4 bytes to store the size */
1322 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1324 ERR("GlobalAlloc failed\n");
1328 pDdh
= GlobalLock(hMem
);
1335 pDdh
->cfFormat
= wFmt
;
1336 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1338 pByte
= (LPBYTE
)(pDdh
+ 1);
1341 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1345 TRACE("=> %p\n", hMem
);
1349 /*****************************************************************
1351 * DdeAddData (USER32.@)
1353 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1355 DWORD old_sz
, new_sz
;
1358 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData
, pSrc
, cb
, cbOff
);
1360 pDst
= DdeAccessData(hData
, &old_sz
);
1361 if (!pDst
) return 0;
1363 new_sz
= cb
+ cbOff
;
1364 if (new_sz
> old_sz
)
1366 DdeUnaccessData(hData
);
1367 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1368 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1369 pDst
= DdeAccessData(hData
, &old_sz
);
1372 if (!pDst
) return 0;
1374 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1375 DdeUnaccessData(hData
);
1379 /******************************************************************************
1380 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1384 * hData [I] Handle to DDE object
1385 * pDst [I] Pointer to destination buffer
1386 * cbMax [I] Amount of data to copy
1387 * cbOff [I] Offset to beginning of data
1390 * Size of memory object associated with handle
1392 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1394 DWORD dwSize
, dwRet
;
1397 TRACE("(%p,%p,%d,%d)\n", hData
, pDst
, cbMax
, cbOff
);
1399 pByte
= DdeAccessData(hData
, &dwSize
);
1407 else if (cbOff
+ cbMax
< dwSize
)
1411 else if (cbOff
< dwSize
)
1413 dwRet
= dwSize
- cbOff
;
1419 if (pDst
&& dwRet
!= 0)
1421 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1423 DdeUnaccessData(hData
);
1432 /*****************************************************************
1433 * DdeAccessData (USER32.@)
1435 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1437 HGLOBAL hMem
= hData
;
1438 DDE_DATAHANDLE_HEAD
* pDdh
;
1440 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1442 pDdh
= GlobalLock(hMem
);
1445 ERR("Failed on GlobalLock(%p)\n", hMem
);
1449 if (pcbDataSize
!= NULL
)
1451 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1453 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1454 return (LPBYTE
)(pDdh
+ 1);
1457 /*****************************************************************
1458 * DdeUnaccessData (USER32.@)
1460 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1462 HGLOBAL hMem
= hData
;
1464 TRACE("(%p)\n", hData
);
1471 /*****************************************************************
1472 * DdeFreeDataHandle (USER32.@)
1474 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1476 TRACE("(%p)\n", hData
);
1478 /* 1 is the handle value returned by an asynchronous operation. */
1479 if (hData
== (HDDEDATA
)1)
1482 return GlobalFree(hData
) == 0;
1485 /******************************************************************
1490 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1492 DDE_DATAHANDLE_HEAD
* pDdh
;
1495 pDdh
= GlobalLock(hData
);
1498 ret
= pDdh
->bAppOwned
;
1499 GlobalUnlock(hData
);
1504 /* ================================================================
1506 * Global <=> Data handle management
1508 * ================================================================ */
1510 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1512 * (bytes) (bits) comment
1513 * 0 16 bit fields for options (release, ackreq, response...)
1514 * 2 16 clipboard format
1515 * 4 ? data to be used
1517 HDDEDATA
WDML_Global2DataHandle(WDML_CONV
* pConv
, HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1525 pDd
= GlobalLock(hMem
);
1526 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1529 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1530 switch (pDd
->cfFormat
)
1533 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1534 pDd
->cfFormat
, hMem
);
1538 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1541 if (size
>= sizeof(BITMAP
))
1543 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1544 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1545 if (size
>= sizeof(BITMAP
) + count
)
1549 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1550 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1551 pDd
->Value
+ sizeof(BITMAP
))))
1553 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1554 0, 0, CF_BITMAP
, 0);
1556 else ERR("Can't create bmp\n");
1560 ERR("Wrong count: %u / %d\n", size
, count
);
1562 } else ERR("No bitmap header\n");
1571 /******************************************************************
1572 * WDML_DataHandle2Global
1576 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1577 BOOL fDeferUpd
, BOOL fAckReq
)
1579 DDE_DATAHANDLE_HEAD
* pDdh
;
1583 dwSize
= GlobalSize(hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1584 pDdh
= GlobalLock(hDdeData
);
1587 WINE_DDEHEAD
* wdh
= NULL
;
1589 switch (pDdh
->cfFormat
)
1592 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1593 pDdh
->cfFormat
, hDdeData
);
1597 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1598 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1600 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1604 if (dwSize
>= sizeof(HBITMAP
))
1608 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1610 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1612 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1613 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1614 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1615 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1617 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1618 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1627 wdh
->fResponse
= fResponse
;
1628 wdh
->fRelease
= fRelease
;
1629 wdh
->fDeferUpd
= fDeferUpd
;
1630 wdh
->fAckReq
= fAckReq
;
1631 wdh
->cfFormat
= pDdh
->cfFormat
;
1634 GlobalUnlock(hDdeData
);
1640 /* ================================================================
1644 * ================================================================ */
1646 /******************************************************************
1651 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1653 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','*','x',')',0};
1654 WDML_SERVER
* pServer
;
1658 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1659 if (pServer
== NULL
) return NULL
;
1661 pServer
->hszService
= hszService
;
1662 WDML_IncHSZ(pInstance
, hszService
);
1664 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1665 snprintfW(buf2
, 256, fmtW
, buf1
, 2*sizeof(ULONG_PTR
), GetCurrentProcessId());
1666 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1668 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1669 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1671 pServer
->filterOn
= TRUE
;
1673 pServer
->next
= pInstance
->servers
;
1674 pInstance
->servers
= pServer
;
1678 /******************************************************************
1683 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1685 WDML_SERVER
* pPrev
= NULL
;
1686 WDML_SERVER
* pServer
= NULL
;
1688 WDML_CONV
* pConvNext
;
1690 pServer
= pInstance
->servers
;
1692 while (pServer
!= NULL
)
1694 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1696 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1697 pServer
->atomService
, pServer
->atomServiceSpec
);
1698 /* terminate all conversations for given topic */
1699 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1701 pConvNext
= pConv
->next
;
1702 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1704 HWND client
= pConv
->hwndClient
, server
= pConv
->hwndServer
;
1705 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1706 /* don't care about return code (whether client window is present or not) */
1707 PostMessageW(client
, WM_DDE_TERMINATE
, (WPARAM
)server
, 0);
1710 if (pServer
== pInstance
->servers
)
1712 pInstance
->servers
= pServer
->next
;
1716 pPrev
->next
= pServer
->next
;
1719 DestroyWindow(pServer
->hwndServer
);
1720 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1721 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1723 GlobalDeleteAtom(pServer
->atomService
);
1724 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1726 HeapFree(GetProcessHeap(), 0, pServer
);
1731 pServer
= pServer
->next
;
1735 /*****************************************************************************
1738 * generic routine to return a pointer to the relevant ServiceNode
1739 * for a given service name, or NULL if the entry does not exist
1742 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1744 WDML_SERVER
* pServer
;
1746 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1748 if (hszService
== pServer
->hszService
)
1753 TRACE("Service name missing\n");
1757 /* ================================================================
1759 * Link (hot & warm) management
1761 * ================================================================ */
1763 /******************************************************************
1768 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1769 UINT wType
, HSZ hszItem
, UINT wFmt
)
1773 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
1780 pLink
->hConv
= hConv
;
1781 pLink
->transactionType
= wType
;
1782 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
1784 pLink
->next
= pInstance
->links
[side
];
1785 pInstance
->links
[side
] = pLink
;
1788 /******************************************************************
1793 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1794 HSZ hszItem
, UINT uFmt
)
1796 WDML_LINK
* pPrev
= NULL
;
1797 WDML_LINK
* pCurrent
= NULL
;
1799 pCurrent
= pInstance
->links
[side
];
1801 while (pCurrent
!= NULL
)
1803 if (pCurrent
->hConv
== hConv
&&
1804 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1805 pCurrent
->uFmt
== uFmt
)
1807 if (pCurrent
== pInstance
->links
[side
])
1809 pInstance
->links
[side
] = pCurrent
->next
;
1813 pPrev
->next
= pCurrent
->next
;
1816 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1817 HeapFree(GetProcessHeap(), 0, pCurrent
);
1822 pCurrent
= pCurrent
->next
;
1826 /* this function is called to remove all links related to the conv.
1827 It should be called from both client and server when terminating
1830 /******************************************************************
1831 * WDML_RemoveAllLinks
1835 static void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
1837 WDML_LINK
* pPrev
= NULL
;
1838 WDML_LINK
* pCurrent
= NULL
;
1839 WDML_LINK
* pNext
= NULL
;
1841 pCurrent
= pInstance
->links
[side
];
1843 while (pCurrent
!= NULL
)
1845 if (pCurrent
->hConv
== (HCONV
)pConv
)
1847 if (pCurrent
== pInstance
->links
[side
])
1849 pInstance
->links
[side
] = pCurrent
->next
;
1850 pNext
= pCurrent
->next
;
1854 pPrev
->next
= pCurrent
->next
;
1855 pNext
= pCurrent
->next
;
1858 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1860 HeapFree(GetProcessHeap(), 0, pCurrent
);
1867 pCurrent
= pCurrent
->next
;
1876 /******************************************************************
1881 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1882 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
1884 WDML_LINK
* pCurrent
= NULL
;
1886 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1888 /* we don't need to check for transaction type as it can be altered */
1890 if (pCurrent
->hConv
== hConv
&&
1891 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1892 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
1902 /* ================================================================
1904 * Transaction management
1906 * ================================================================ */
1908 /******************************************************************
1909 * WDML_AllocTransaction
1911 * Alloc a transaction structure for handling the message ddeMsg
1913 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
1914 UINT wFmt
, HSZ hszItem
)
1917 static WORD tid
= 1; /* FIXME: wrap around */
1919 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
1922 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
1926 pXAct
->xActID
= tid
++;
1927 pXAct
->ddeMsg
= ddeMsg
;
1928 pXAct
->hDdeData
= 0;
1933 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
1941 /******************************************************************
1942 * WDML_QueueTransaction
1944 * Adds a transaction to the list of transaction
1946 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1950 /* advance to last in queue */
1951 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
1955 /******************************************************************
1956 * WDML_UnQueueTransaction
1960 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1964 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
1975 /******************************************************************
1976 * WDML_FreeTransaction
1980 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
1982 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
1983 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
1985 GlobalFree(pXAct
->hMem
);
1987 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
1989 HeapFree(GetProcessHeap(), 0, pXAct
);
1992 /******************************************************************
1993 * WDML_FindTransaction
1997 static WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
2002 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
2004 if (pXAct
->xActID
== tid
)
2010 /* ================================================================
2012 * Conversation management
2014 * ================================================================ */
2016 /******************************************************************
2021 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2022 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
2026 /* no conversation yet, add it */
2027 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
2028 if (!pConv
) return NULL
;
2030 pConv
->instance
= pInstance
;
2031 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
2032 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
2033 pConv
->magic
= WDML_CONV_MAGIC
;
2034 pConv
->hwndServer
= hwndServer
;
2035 pConv
->hwndClient
= hwndClient
;
2036 pConv
->transactions
= NULL
;
2038 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
2039 pConv
->wStatus
|= pInstance
->wStatus
;
2040 /* check if both side of the conversation are of the same instance */
2041 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
2042 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
2044 pConv
->wStatus
|= ST_ISSELF
;
2046 pConv
->wConvst
= XST_NULL
;
2048 pConv
->next
= pInstance
->convs
[side
];
2049 pInstance
->convs
[side
] = pConv
;
2051 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv
->wStatus
, pInstance
);
2056 /******************************************************************
2061 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2062 HSZ hszService
, HSZ hszTopic
)
2064 WDML_CONV
* pCurrent
= NULL
;
2066 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2068 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
2069 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
2078 /******************************************************************
2083 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
2085 WDML_CONV
* pPrev
= NULL
;
2086 WDML_CONV
* pCurrent
;
2088 WDML_XACT
* pXActNext
;
2094 /* remove any pending transaction */
2095 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
2097 pXActNext
= pXAct
->next
;
2098 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
2101 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
2103 /* FIXME: should we keep the window around ? it seems so (at least on client side
2104 * to let QueryConvInfo work after conv termination, but also to implement
2107 /* destroy conversation window, but first remove pConv from hWnd.
2108 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
2110 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
2111 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
2113 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
2115 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
2116 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
2118 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
2120 if (pCurrent
== pRef
)
2122 if (pCurrent
== pRef
->instance
->convs
[side
])
2124 pRef
->instance
->convs
[side
] = pCurrent
->next
;
2128 pPrev
->next
= pCurrent
->next
;
2130 pCurrent
->magic
= 0;
2131 HeapFree(GetProcessHeap(), 0, pCurrent
);
2137 /******************************************************************
2138 * WDML_EnableCallback
2140 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
2142 if (wCmd
== EC_DISABLE
)
2144 pConv
->wStatus
|= ST_BLOCKED
;
2145 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
2149 if (wCmd
== EC_QUERYWAITING
)
2150 return pConv
->transactions
!= NULL
;
2152 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
2154 FIXME("Unknown command code %04x\n", wCmd
);
2158 if (wCmd
== EC_ENABLEALL
)
2160 pConv
->wStatus
&= ~ST_BLOCKED
;
2161 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
2164 while (pConv
->transactions
)
2166 WDML_XACT
*pXAct
= pConv
->transactions
;
2168 if (pConv
->wStatus
& ST_CLIENT
)
2170 /* transaction should be in the queue until handled */
2171 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
2172 WDML_UnQueueTransaction(pConv
, pXAct
);
2176 /* transaction should be removed from the queue before handling */
2177 WDML_UnQueueTransaction(pConv
, pXAct
);
2178 WDML_ServerHandle(pConv
, pXAct
);
2181 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
2183 if (wCmd
== EC_ENABLEONE
) break;
2188 /*****************************************************************
2189 * DdeEnableCallback (USER32.@)
2191 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
2196 TRACE("(%d, %p, %04x)\n", idInst
, hConv
, wCmd
);
2200 pConv
= WDML_GetConv(hConv
, TRUE
);
2202 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
2203 ret
= WDML_EnableCallback(pConv
, wCmd
);
2207 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
2212 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
2213 pInstance
->wStatus
|= wCmd
;
2215 if (wCmd
== EC_DISABLE
)
2217 pInstance
->wStatus
|= ST_BLOCKED
;
2218 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
2220 else if (wCmd
== EC_ENABLEALL
)
2222 pInstance
->wStatus
&= ~ST_BLOCKED
;
2223 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
2228 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
2230 ret
= WDML_EnableCallback(pConv
, wCmd
);
2231 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
2238 /******************************************************************
2243 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
2245 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
2247 /* FIXME: should do better checking */
2248 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
2250 if (!pConv
->instance
)
2252 WARN("wrong thread ID, no instance\n");
2256 if (pConv
->instance
->threadID
!= GetCurrentThreadId())
2258 WARN("wrong thread ID\n");
2259 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
2263 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
2265 WARN("found conv but ain't connected\n");
2266 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
2273 /******************************************************************
2274 * WDML_GetConvFromWnd
2278 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
2280 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
2283 /******************************************************************
2288 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
2289 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
2294 if (side
== WDML_SERVER_SIDE
)
2296 from
= pConv
->hwndServer
;
2297 to
= pConv
->hwndClient
;
2301 to
= pConv
->hwndServer
;
2302 from
= pConv
->hwndClient
;
2305 ddeAck
.bAppReturnCode
= appRetCode
;
2306 ddeAck
.reserved
= 0;
2307 ddeAck
.fBusy
= fBusy
;
2310 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
2312 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
2313 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
2314 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
2316 pConv
->wStatus
&= ~ST_CONNECTED
;
2317 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
2318 FreeDDElParam(WM_DDE_ACK
, lParam
);
2324 /*****************************************************************
2325 * DdeSetUserHandle (USER32.@)
2327 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2331 pConv
= WDML_GetConv(hConv
, FALSE
);
2337 pConv
->hUser
= hUser
;
2343 pXAct
= WDML_FindTransaction(pConv
, id
);
2346 pXAct
->hUser
= hUser
;
2350 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2357 /******************************************************************
2358 * WDML_GetLocalConvInfo
2362 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2368 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2369 ci
->hszSvcPartner
= pConv
->hszService
;
2370 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2371 ci
->hszTopic
= pConv
->hszTopic
;
2372 ci
->wStatus
= pConv
->wStatus
;
2374 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2376 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2378 if (pLink
->hConv
== (HCONV
)pConv
)
2380 ci
->wStatus
|= ST_ADVISE
;
2385 /* FIXME: non handled status flags:
2391 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2393 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2395 ci
->ConvCtxt
= pConv
->convContext
;
2396 if (ci
->wStatus
& ST_CLIENT
)
2398 ci
->hwnd
= pConv
->hwndClient
;
2399 ci
->hwndPartner
= pConv
->hwndServer
;
2403 ci
->hwnd
= pConv
->hwndServer
;
2404 ci
->hwndPartner
= pConv
->hwndClient
;
2408 ci
->hUser
= pConv
->hUser
;
2417 pXAct
= WDML_FindTransaction(pConv
, id
);
2420 ci
->hUser
= pXAct
->hUser
;
2421 ci
->hszItem
= pXAct
->hszItem
;
2422 ci
->wFmt
= pXAct
->wFmt
;
2423 ci
->wType
= pXAct
->wType
;
2428 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2434 /******************************************************************
2435 * DdeQueryConvInfo (USER32.@)
2437 * FIXME: Set last DDE error on failure.
2439 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2441 UINT ret
= lpConvInfo
->cb
;
2445 TRACE("(%p,%x,%p)\n", hConv
, id
, lpConvInfo
);
2449 FIXME("hConv is NULL\n");
2453 pConv
= WDML_GetConv(hConv
, FALSE
);
2456 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2461 if ((ULONG_PTR
)hConv
& 1)
2463 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2465 FIXME("Request on remote conversation information is not implemented yet\n");
2471 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2475 /* ================================================================
2477 * Information broadcast across DDEML implementations
2479 * ================================================================ */
2481 struct tagWDML_BroadcastPmt
2489 /******************************************************************
2490 * WDML_BroadcastEnumProc
2494 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2496 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2499 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2500 lstrcmpiW(buffer
, s
->clsName
) == 0)
2502 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2507 /******************************************************************
2508 * WDML_BroadcastDDEWindows
2512 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2514 struct tagWDML_BroadcastPmt s
;
2516 s
.clsName
= clsName
;
2520 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);