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
26 #include "user_private.h"
27 #include "dde_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
32 /* convert between ATOM and HSZ avoiding compiler warnings */
33 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
34 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
36 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
37 static LONG WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
38 const WCHAR WDML_szEventClass
[] = L
"WineDdeEventClass";
40 /* protection for instance list */
41 static CRITICAL_SECTION WDML_CritSect
;
42 static CRITICAL_SECTION_DEBUG critsect_debug
=
45 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
46 0, 0, { (DWORD_PTR
)(__FILE__
": WDML_CritSect") }
48 static CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
50 /* ================================================================
52 * Pure DDE (non DDEML) management
54 * ================================================================ */
57 /*****************************************************************
58 * PackDDElParam (USER32.@)
63 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
74 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
76 ERR("GlobalAlloc failed\n");
79 if (!(params
= GlobalLock(hMem
)))
81 ERR("GlobalLock failed (%p)\n", hMem
);
93 return MAKELONG(uiLo
, uiHi
);
98 /*****************************************************************
99 * UnpackDDElParam (USER32.@)
105 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
106 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
116 if (!lParam
|| !(params
= GlobalLock((HGLOBAL
)lParam
)))
122 if (uiLo
) *uiLo
= params
[0];
123 if (uiHi
) *uiHi
= params
[1];
124 GlobalUnlock( (HGLOBAL
)lParam
);
129 if (uiHi
) *uiHi
= lParam
;
133 if (uiLo
) *uiLo
= LOWORD(lParam
);
134 if (uiHi
) *uiHi
= HIWORD(lParam
);
140 /*****************************************************************
141 * FreeDDElParam (USER32.@)
147 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
155 /* first check if it's a global handle */
156 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
157 return !GlobalFree( (HGLOBAL
)lParam
);
165 /*****************************************************************
166 * ReuseDDElParam (USER32.@)
171 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
172 UINT_PTR uiLo
, UINT_PTR uiHi
)
188 if (!lParam
) return 0;
189 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
191 ERR("GlobalLock failed\n");
196 TRACE("Reusing pack %08Ix %08Ix\n", uiLo
, uiHi
);
197 GlobalUnlock( (HGLOBAL
)lParam
);
201 FreeDDElParam( msgIn
, lParam
);
205 FreeDDElParam( msgIn
, lParam
);
206 return MAKELPARAM(uiLo
, uiHi
);
210 return PackDDElParam( msgOut
, uiLo
, uiHi
);
214 /*****************************************************************
215 * ImpersonateDdeClientWindow (USER32.@)
218 * hWndClient [I] handle to DDE client window
219 * hWndServer [I] handle to DDE server window
221 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
223 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
227 /*****************************************************************
228 * DdeSetQualityOfService (USER32.@)
231 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, const SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
232 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
234 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
238 /* ================================================================
240 * WDML Error management
242 * ================================================================ */
244 /******************************************************************************
245 * DdeGetLastError [USER32.@] Gets most recent error code
248 * idInst [I] Instance identifier
253 UINT WINAPI
DdeGetLastError(DWORD idInst
)
256 WDML_INSTANCE
* pInstance
;
258 /* First check instance
260 pInstance
= WDML_GetInstance(idInst
);
261 if (pInstance
== NULL
)
263 error_code
= DMLERR_INVALIDPARAMETER
;
267 error_code
= pInstance
->lastError
;
268 pInstance
->lastError
= 0;
274 /******************************************************************
275 * WDML_SetAllLastError
279 static void WDML_SetAllLastError(DWORD lastError
)
282 WDML_INSTANCE
* pInstance
;
283 threadID
= GetCurrentThreadId();
284 pInstance
= WDML_InstanceList
;
287 if (pInstance
->threadID
== threadID
)
288 pInstance
->lastError
= lastError
;
289 pInstance
= pInstance
->next
;
293 /* ================================================================
297 * ================================================================ */
300 /******************************************************************
305 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
309 if (pInstance
== NULL
) return NULL
;
311 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
313 if (pNode
->hsz
== hsz
) break;
315 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
319 /******************************************************************
320 * WDML_MakeAtomFromHsz
322 * Creates a global atom from an existing HSZ
323 * Generally used before sending an HSZ as an atom to a remote app
325 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
327 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
329 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
330 return GlobalAddAtomW(nameBuffer
);
331 WARN("HSZ %p not found\n", hsz
);
335 /******************************************************************
336 * WDML_MakeHszFromAtom
338 * Creates a HSZ from an existing global atom
339 * Generally used while receiving a global atom and transforming it
342 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
344 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
346 if (!atom
) return NULL
;
348 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
350 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
351 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
353 WARN("ATOM 0x%x not found\n", atom
);
357 /******************************************************************
362 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
366 pNode
= WDML_FindNode(pInstance
, hsz
);
367 if (!pNode
) return FALSE
;
373 /******************************************************************************
374 * WDML_DecHSZ (INTERNAL)
376 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
378 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
380 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
382 HSZNode
* pPrev
= NULL
;
385 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
387 /* If we found the node we were looking for and its ref count is one,
390 if (pCurrent
->hsz
== hsz
)
392 if (--pCurrent
->refCount
== 0)
394 if (pCurrent
== pInstance
->nodeList
)
396 pInstance
->nodeList
= pCurrent
->next
;
400 pPrev
->next
= pCurrent
->next
;
402 HeapFree(GetProcessHeap(), 0, pCurrent
);
403 DeleteAtom(HSZ2ATOM(hsz
));
408 WARN("HSZ %p not found\n", hsz
);
413 /******************************************************************************
414 * WDML_FreeAllHSZ (INTERNAL)
416 * Frees up all the strings still allocated in the list and
417 * remove all the nodes from the list of HSZ nodes.
419 static void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
421 /* Free any strings created in this instance.
423 while (pInstance
->nodeList
!= NULL
)
425 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
429 /******************************************************************************
430 * InsertHSZNode (INTERNAL)
432 * Insert a node to the head of the list.
434 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
438 HSZNode
* pNew
= NULL
;
439 /* Create a new node for this HSZ.
441 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
445 pNew
->next
= pInstance
->nodeList
;
447 pInstance
->nodeList
= pNew
;
451 ERR("Primary HSZ Node allocation failed - out of memory\n");
456 /******************************************************************
461 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
464 WCHAR pString
[MAX_BUFFER_LEN
];
466 /* If psz is null, we have to return only the length
472 cchMax
= MAX_BUFFER_LEN
;
475 /* if there is no input windows returns a NULL string */
486 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
489 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
492 ERR("Unknown code page %d\n", codepage
);
498 /*****************************************************************
499 * DdeQueryStringA [USER32.@]
501 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
504 WDML_INSTANCE
* pInstance
;
506 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
508 /* First check instance
510 pInstance
= WDML_GetInstance(idInst
);
511 if (pInstance
!= NULL
)
513 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
514 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
517 TRACE("returning %ld (%s)\n", ret
, debugstr_a(psz
));
521 /*****************************************************************
522 * DdeQueryStringW [USER32.@]
525 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
528 WDML_INSTANCE
* pInstance
;
530 TRACE("(%ld, %p, %p, %ld, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
532 /* First check instance
534 pInstance
= WDML_GetInstance(idInst
);
535 if (pInstance
!= NULL
)
537 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
538 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
541 TRACE("returning %ld (%s)\n", ret
, debugstr_w(psz
));
545 /******************************************************************
550 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
557 hsz
= ATOM2HSZ(AddAtomA(ptr
));
558 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
561 hsz
= ATOM2HSZ(AddAtomW(ptr
));
562 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
565 ERR("Unknown code page %d\n", codepage
);
568 WDML_InsertHSZNode(pInstance
, hsz
);
572 /*****************************************************************
573 * DdeCreateStringHandleA [USER32.@]
575 * See DdeCreateStringHandleW.
577 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
580 WDML_INSTANCE
* pInstance
;
582 TRACE("(%ld,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
584 pInstance
= WDML_GetInstance(idInst
);
585 if (pInstance
== NULL
)
586 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
589 if (codepage
== 0) codepage
= CP_WINANSI
;
590 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
597 /******************************************************************************
598 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
601 * idInst [I] Instance identifier
602 * psz [I] Pointer to string
603 * codepage [I] Code page identifier
605 * Success: String handle
608 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
610 WDML_INSTANCE
* pInstance
;
613 pInstance
= WDML_GetInstance(idInst
);
614 if (pInstance
== NULL
)
615 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
618 if (codepage
== 0) codepage
= CP_WINUNICODE
;
619 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
625 /*****************************************************************
626 * DdeFreeStringHandle (USER32.@)
631 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
633 WDML_INSTANCE
* pInstance
;
636 TRACE("(%ld,%p):\n", idInst
, hsz
);
638 /* First check instance
640 pInstance
= WDML_GetInstance(idInst
);
642 ret
= WDML_DecHSZ(pInstance
, hsz
);
647 /*****************************************************************
648 * DdeKeepStringHandle (USER32.@)
654 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
656 WDML_INSTANCE
* pInstance
;
659 TRACE("(%ld,%p):\n", idInst
, hsz
);
661 /* First check instance
663 pInstance
= WDML_GetInstance(idInst
);
665 ret
= WDML_IncHSZ(pInstance
, hsz
);
670 /*****************************************************************
671 * DdeCmpStringHandles (USER32.@)
673 * Compares the value of two string handles. This comparison is
674 * not case sensitive.
677 * hsz1 [I] Handle to the first string
678 * hsz2 [I] Handle to the second string
681 * -1 The value of hsz1 is zero or less than hsz2
682 * 0 The values of hsz 1 and 2 are the same or both zero.
683 * 1 The value of hsz2 is zero of less than hsz1
685 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
687 WCHAR psz1
[MAX_BUFFER_LEN
];
688 WCHAR psz2
[MAX_BUFFER_LEN
];
692 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
693 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
695 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
697 /* Make sure we found both strings. */
698 if (ret1
== 0 && ret2
== 0)
700 /* If both are not found, return both "zero strings". */
705 /* If hsz1 is a not found, return hsz1 is "zero string". */
710 /* If hsz2 is a not found, return hsz2 is "zero string". */
715 /* Compare the two strings we got (case insensitive). */
716 ret
= lstrcmpiW(psz1
, psz2
);
717 /* Since strcmp returns any number smaller than
718 * 0 when the first string is found to be less than
719 * the second one we must make sure we are returning
735 /* ================================================================
737 * Instance management
739 * ================================================================ */
741 /******************************************************************************
742 * IncrementInstanceId
744 * generic routine to increment the max instance Id and allocate a new application instance
746 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
748 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
750 pInstance
->instanceID
= id
;
751 TRACE("New instance id %ld allocated\n", id
);
754 /******************************************************************
759 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
761 WDML_INSTANCE
* pInstance
;
766 case WM_WDML_REGISTER
:
767 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
768 /* try calling the Callback */
769 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
771 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
772 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
773 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
774 WDML_DecHSZ(pInstance
, hsz1
);
775 WDML_DecHSZ(pInstance
, hsz2
);
779 case WM_WDML_UNREGISTER
:
780 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
781 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
783 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
784 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
785 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
786 WDML_DecHSZ(pInstance
, hsz1
);
787 WDML_DecHSZ(pInstance
, hsz2
);
791 case WM_WDML_CONNECT_CONFIRM
:
792 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
793 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
796 /* confirm connection...
797 * lookup for this conv handle
799 HWND client
= WIN_GetFullHandle( (HWND
)wParam
);
800 HWND server
= WIN_GetFullHandle( (HWND
)lParam
);
801 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
803 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
808 pConv
->wStatus
|= ST_ISLOCAL
;
810 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
811 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
812 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
817 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
822 /******************************************************************
827 static UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
828 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
)
830 WDML_INSTANCE
* pInstance
;
831 WDML_INSTANCE
* reference_inst
;
833 WNDCLASSEXW wndclass
;
835 TRACE("(%p,%p,0x%lx,%ld,0x%x)\n",
836 pidInst
, pfnCallback
, afCmd
, ulRes
, bUnicode
);
840 ERR("Reserved value not zero? What does this mean?\n");
841 /* trap this and no more until we know more */
842 return DMLERR_NO_ERROR
;
845 /* grab enough heap for one control struct - not really necessary for re-initialise
846 * but allows us to use same validation routines */
847 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
848 if (pInstance
== NULL
)
850 /* catastrophe !! warn user & abort */
851 ERR("Instance create failed - out of memory\n");
852 return DMLERR_SYS_ERROR
;
854 pInstance
->next
= NULL
;
855 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
857 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
859 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
860 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
861 pInstance
->threadID
= GetCurrentThreadId();
862 pInstance
->callback
= *pfnCallback
;
863 pInstance
->unicode
= bUnicode
;
864 pInstance
->nodeList
= NULL
; /* node will be added later */
865 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
866 pInstance
->wStatus
= 0;
867 pInstance
->lastError
= DMLERR_NO_ERROR
;
868 pInstance
->servers
= NULL
;
869 pInstance
->convs
[0] = NULL
;
870 pInstance
->convs
[1] = NULL
;
871 pInstance
->links
[0] = NULL
;
872 pInstance
->links
[1] = NULL
;
874 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
876 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
878 if (!pInstance
->clientOnly
)
880 /* Check for other way of setting Client-only !! */
881 pInstance
->clientOnly
=
882 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
885 TRACE("instance created - checking validity\n");
889 /* Initialisation of new Instance Identifier */
890 TRACE("new instance, callback %p flags %lX\n", pfnCallback
, afCmd
);
892 EnterCriticalSection(&WDML_CritSect
);
894 if (WDML_InstanceList
== NULL
)
896 /* can't be another instance in this case, assign to the base pointer */
897 WDML_InstanceList
= pInstance
;
899 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
901 * ------------------------------- NOTE NOTE NOTE --------------------------
903 * the manual is not clear if this condition
904 * applies to the first call to DdeInitialize from an application, or the
905 * first call for a given callback !!!
908 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
909 TRACE("First application instance detected OK\n");
910 /* allocate new instance ID */
911 WDML_IncrementInstanceId(pInstance
);
915 /* really need to chain the new one in to the latest here, but after checking conditions
916 * such as trying to start a conversation from an application trying to monitor */
917 reference_inst
= WDML_InstanceList
;
918 TRACE("Subsequent application instance - starting checks\n");
919 while (reference_inst
->next
!= NULL
)
922 * This set of tests will work if application uses same instance Id
923 * at application level once allocated - which is what manual implies
924 * should happen. If someone tries to be
925 * clever (lazy ?) it will fail to pick up that later calls are for
926 * the same application - should we trust them ?
928 if (pInstance
->instanceID
== reference_inst
->instanceID
)
930 /* Check 1 - must be same Client-only state */
932 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
934 ret
= DMLERR_DLL_USAGE
;
938 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
940 if (pInstance
->monitor
!= reference_inst
->monitor
)
942 ret
= DMLERR_INVALIDPARAMETER
;
946 /* Check 3 - must supply different callback address */
948 if (pInstance
->callback
== reference_inst
->callback
)
950 ret
= DMLERR_DLL_USAGE
;
954 reference_inst
= reference_inst
->next
;
956 /* All cleared, add to chain */
958 TRACE("Application Instance checks finished\n");
959 WDML_IncrementInstanceId(pInstance
);
960 reference_inst
->next
= pInstance
;
962 LeaveCriticalSection(&WDML_CritSect
);
964 *pidInst
= pInstance
->instanceID
;
966 /* for deadlock issues, windows must always be created when outside the critical section */
967 wndclass
.cbSize
= sizeof(wndclass
);
969 wndclass
.lpfnWndProc
= WDML_EventProc
;
970 wndclass
.cbClsExtra
= 0;
971 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
972 wndclass
.hInstance
= 0;
974 wndclass
.hCursor
= 0;
975 wndclass
.hbrBackground
= 0;
976 wndclass
.lpszMenuName
= NULL
;
977 wndclass
.lpszClassName
= WDML_szEventClass
;
978 wndclass
.hIconSm
= 0;
980 RegisterClassExW(&wndclass
);
982 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
983 WS_POPUP
, 0, 0, 0, 0,
986 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
988 TRACE("New application instance processing finished OK\n");
992 /* Reinitialisation situation --- FIX */
993 TRACE("reinitialisation of (%p,%p,0x%lx,%ld): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
995 EnterCriticalSection(&WDML_CritSect
);
997 if (WDML_InstanceList
== NULL
)
999 ret
= DMLERR_INVALIDPARAMETER
;
1002 /* can't reinitialise if we have initialised nothing !! */
1003 reference_inst
= WDML_InstanceList
;
1004 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
1006 * MS allows initialisation without specifying a callback, should we allow addition of the
1007 * callback by a later call to initialise ? - if so this lot will have to change
1009 while (reference_inst
->next
!= NULL
)
1011 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
1013 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
1015 if (reference_inst
->clientOnly
)
1017 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
1019 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
1021 if (!(afCmd
& APPCMD_CLIENTONLY
))
1023 ret
= DMLERR_INVALIDPARAMETER
;
1028 /* Check 2 - cannot change monitor modes */
1030 if (pInstance
->monitor
!= reference_inst
->monitor
)
1032 ret
= DMLERR_INVALIDPARAMETER
;
1036 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
1038 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
1040 ret
= DMLERR_INVALIDPARAMETER
;
1045 reference_inst
= reference_inst
->next
;
1047 if (reference_inst
->next
== NULL
)
1049 ret
= DMLERR_INVALIDPARAMETER
;
1052 /* All checked - change relevant flags */
1054 reference_inst
->CBFflags
= pInstance
->CBFflags
;
1055 reference_inst
->clientOnly
= pInstance
->clientOnly
;
1056 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
1058 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
1060 LeaveCriticalSection(&WDML_CritSect
);
1063 return DMLERR_NO_ERROR
;
1065 HeapFree(GetProcessHeap(), 0, pInstance
);
1066 LeaveCriticalSection(&WDML_CritSect
);
1070 /******************************************************************************
1071 * DdeInitializeA (USER32.@)
1073 * See DdeInitializeW.
1075 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1076 DWORD afCmd
, DWORD ulRes
)
1078 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
);
1081 /******************************************************************************
1082 * DdeInitializeW [USER32.@]
1083 * Registers an application with the DDEML
1086 * pidInst [I] Pointer to instance identifier
1087 * pfnCallback [I] Pointer to callback function
1088 * afCmd [I] Set of command and filter flags
1089 * ulRes [I] Reserved
1092 * Success: DMLERR_NO_ERROR
1093 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
1095 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1096 DWORD afCmd
, DWORD ulRes
)
1098 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
);
1101 /*****************************************************************
1102 * DdeUninitialize [USER32.@] Frees DDEML resources
1105 * idInst [I] Instance identifier
1112 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
1114 /* Stage one - check if we have a handle for this instance
1116 WDML_INSTANCE
* pInstance
;
1118 WDML_CONV
* pConvNext
;
1120 TRACE("(%ld)\n", idInst
);
1122 /* First check instance
1124 pInstance
= WDML_GetInstance(idInst
);
1125 if (pInstance
== NULL
)
1128 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
1133 /* first terminate all conversations client side
1134 * this shall close existing links...
1136 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1138 pConvNext
= pConv
->next
;
1139 DdeDisconnect((HCONV
)pConv
);
1141 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
1142 FIXME("still pending conversations\n");
1144 /* then unregister all known service names */
1145 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
1147 /* Free the nodes that were not freed by this instance
1148 * and remove the nodes from the list of HSZ nodes.
1150 WDML_FreeAllHSZ(pInstance
);
1152 NtUserDestroyWindow( pInstance
->hwndEvent
);
1154 /* OK now delete the instance handle itself */
1156 if (WDML_InstanceList
== pInstance
)
1158 /* special case - the first/only entry */
1159 WDML_InstanceList
= pInstance
->next
;
1163 /* general case, remove entry */
1164 WDML_INSTANCE
* inst
;
1166 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
1167 inst
->next
= pInstance
->next
;
1169 /* release the heap entry
1171 HeapFree(GetProcessHeap(), 0, pInstance
);
1176 /******************************************************************
1177 * WDML_NotifyThreadExit
1181 void WDML_NotifyThreadDetach(void)
1183 WDML_INSTANCE
* pInstance
;
1184 WDML_INSTANCE
* next
;
1185 DWORD tid
= GetCurrentThreadId();
1187 EnterCriticalSection(&WDML_CritSect
);
1188 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
1190 next
= pInstance
->next
;
1191 if (pInstance
->threadID
== tid
)
1193 LeaveCriticalSection(&WDML_CritSect
);
1194 DdeUninitialize(pInstance
->instanceID
);
1195 EnterCriticalSection(&WDML_CritSect
);
1198 LeaveCriticalSection(&WDML_CritSect
);
1201 /******************************************************************
1202 * WDML_InvokeCallback
1206 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
1207 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
1208 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
1212 if (pInstance
== NULL
)
1215 TRACE("invoking CB[%p] (%x %x %p %p %p %p %Ix %Ix)\n",
1216 pInstance
->callback
, uType
, uFmt
,
1217 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1218 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1219 TRACE("done => %p\n", ret
);
1223 /*****************************************************************************
1226 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
1227 * for an instance Id, or NULL if the entry does not exist
1230 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
1232 WDML_INSTANCE
* pInstance
;
1234 EnterCriticalSection(&WDML_CritSect
);
1236 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
1238 if (pInstance
->instanceID
== instId
)
1240 if (GetCurrentThreadId() != pInstance
->threadID
)
1242 FIXME("Tried to get instance from wrong thread\n");
1249 LeaveCriticalSection(&WDML_CritSect
);
1252 WARN("Instance entry missing for id %04lx\n", instId
);
1256 /******************************************************************
1257 * WDML_GetInstanceFromWnd
1261 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
1263 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
1266 /* ================================================================
1268 * Data handle management
1270 * ================================================================ */
1272 /*****************************************************************
1273 * DdeCreateDataHandle (USER32.@)
1275 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1276 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1279 /* Other than check for validity we will ignore for now idInst, hszItem.
1280 * The purpose of these arguments still need to be investigated.
1283 WDML_INSTANCE
* pInstance
;
1286 DDE_DATAHANDLE_HEAD
* pDdh
;
1287 WCHAR psz
[MAX_BUFFER_LEN
];
1289 pInstance
= WDML_GetInstance(idInst
);
1290 if (pInstance
== NULL
)
1292 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1296 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1298 psz
[0] = HSZ2ATOM(hszItem
);
1302 TRACE("(%ld,%p,cb %ld, cbOff %ld,%p <%s>,fmt %04x,%x)\n",
1303 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1305 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1308 /* we use the first 4 bytes to store the size */
1309 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1311 ERR("GlobalAlloc failed\n");
1315 pDdh
= GlobalLock(hMem
);
1322 pDdh
->cfFormat
= wFmt
;
1323 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1325 pByte
= (LPBYTE
)(pDdh
+ 1);
1328 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1332 TRACE("=> %p\n", hMem
);
1336 /*****************************************************************
1338 * DdeAddData (USER32.@)
1340 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1342 DWORD old_sz
, new_sz
;
1345 TRACE("(%p,%p,cb %ld, cbOff %ld)\n", hData
, pSrc
, cb
, cbOff
);
1347 pDst
= DdeAccessData(hData
, &old_sz
);
1348 if (!pDst
) return 0;
1350 new_sz
= cb
+ cbOff
;
1351 if (new_sz
> old_sz
)
1353 DdeUnaccessData(hData
);
1354 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1355 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1356 pDst
= DdeAccessData(hData
, &old_sz
);
1359 if (!pDst
) return 0;
1361 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1362 DdeUnaccessData(hData
);
1366 /******************************************************************************
1367 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1371 * hData [I] Handle to DDE object
1372 * pDst [I] Pointer to destination buffer
1373 * cbMax [I] Amount of data to copy
1374 * cbOff [I] Offset to beginning of data
1377 * Size of memory object associated with handle
1379 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1381 DWORD dwSize
, dwRet
;
1384 TRACE("(%p,%p,%ld,%ld)\n", hData
, pDst
, cbMax
, cbOff
);
1386 pByte
= DdeAccessData(hData
, &dwSize
);
1394 else if (cbOff
+ cbMax
< dwSize
)
1398 else if (cbOff
< dwSize
)
1400 dwRet
= dwSize
- cbOff
;
1406 if (pDst
&& dwRet
!= 0)
1408 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1410 DdeUnaccessData(hData
);
1419 /*****************************************************************
1420 * DdeAccessData (USER32.@)
1422 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1424 HGLOBAL hMem
= hData
;
1425 DDE_DATAHANDLE_HEAD
* pDdh
;
1427 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1429 pDdh
= GlobalLock(hMem
);
1432 ERR("Failed on GlobalLock(%p)\n", hMem
);
1436 if (pcbDataSize
!= NULL
)
1438 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1440 TRACE("=> %p (%Iu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1441 return (LPBYTE
)(pDdh
+ 1);
1444 /*****************************************************************
1445 * DdeUnaccessData (USER32.@)
1447 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1449 HGLOBAL hMem
= hData
;
1451 TRACE("(%p)\n", hData
);
1458 /*****************************************************************
1459 * DdeFreeDataHandle (USER32.@)
1461 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1463 TRACE("(%p)\n", hData
);
1465 /* 1 is the handle value returned by an asynchronous operation. */
1466 if (hData
== (HDDEDATA
)1)
1469 return GlobalFree(hData
) == 0;
1472 /******************************************************************
1477 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1479 DDE_DATAHANDLE_HEAD
* pDdh
;
1482 pDdh
= GlobalLock(hData
);
1485 ret
= pDdh
->bAppOwned
;
1486 GlobalUnlock(hData
);
1491 /* ================================================================
1493 * Global <=> Data handle management
1495 * ================================================================ */
1497 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1499 * (bytes) (bits) comment
1500 * 0 16 bit fields for options (release, ackreq, response...)
1501 * 2 16 clipboard format
1502 * 4 ? data to be used
1504 HDDEDATA
WDML_Global2DataHandle(WDML_CONV
* pConv
, HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1512 pDd
= GlobalLock(hMem
);
1513 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1516 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1517 switch (pDd
->cfFormat
)
1520 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1521 pDd
->cfFormat
, hMem
);
1525 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1528 if (size
>= sizeof(BITMAP
))
1530 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1531 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1532 if (size
>= sizeof(BITMAP
) + count
)
1536 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1537 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1538 pDd
->Value
+ sizeof(BITMAP
))))
1540 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1541 0, 0, CF_BITMAP
, 0);
1543 else ERR("Can't create bmp\n");
1547 ERR("Wrong count: %lu / %d\n", size
, count
);
1549 } else ERR("No bitmap header\n");
1558 /******************************************************************
1559 * WDML_DataHandle2Global
1563 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1564 BOOL fDeferUpd
, BOOL fAckReq
)
1566 DDE_DATAHANDLE_HEAD
* pDdh
;
1570 dwSize
= GlobalSize(hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1571 pDdh
= GlobalLock(hDdeData
);
1574 WINE_DDEHEAD
* wdh
= NULL
;
1576 switch (pDdh
->cfFormat
)
1579 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1580 pDdh
->cfFormat
, hDdeData
);
1584 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1585 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1587 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1591 if (dwSize
>= sizeof(HBITMAP
))
1595 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1597 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1599 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1600 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1601 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1602 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1604 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1605 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1614 wdh
->fResponse
= fResponse
;
1615 wdh
->fRelease
= fRelease
;
1616 wdh
->fDeferUpd
= fDeferUpd
;
1617 wdh
->fAckReq
= fAckReq
;
1618 wdh
->cfFormat
= pDdh
->cfFormat
;
1621 GlobalUnlock(hDdeData
);
1627 /* ================================================================
1631 * ================================================================ */
1633 /******************************************************************
1638 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1640 WDML_SERVER
* pServer
;
1644 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1645 if (pServer
== NULL
) return NULL
;
1647 pServer
->hszService
= hszService
;
1648 WDML_IncHSZ(pInstance
, hszService
);
1650 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1651 swprintf(buf2
, 256, L
"%s(0x%*x)", buf1
, 2*sizeof(ULONG_PTR
), GetCurrentProcessId());
1652 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1654 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1655 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1657 pServer
->filterOn
= TRUE
;
1659 pServer
->next
= pInstance
->servers
;
1660 pInstance
->servers
= pServer
;
1664 /******************************************************************
1669 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1671 WDML_SERVER
* pPrev
= NULL
;
1672 WDML_SERVER
* pServer
= NULL
;
1674 WDML_CONV
* pConvNext
;
1676 pServer
= pInstance
->servers
;
1678 while (pServer
!= NULL
)
1680 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1682 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1683 pServer
->atomService
, pServer
->atomServiceSpec
);
1684 /* terminate all conversations for given topic */
1685 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1687 pConvNext
= pConv
->next
;
1688 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1690 HWND client
= pConv
->hwndClient
, server
= pConv
->hwndServer
;
1691 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1692 /* don't care about return code (whether client window is present or not) */
1693 PostMessageW(client
, WM_DDE_TERMINATE
, (WPARAM
)server
, 0);
1696 if (pServer
== pInstance
->servers
)
1698 pInstance
->servers
= pServer
->next
;
1702 pPrev
->next
= pServer
->next
;
1705 NtUserDestroyWindow(pServer
->hwndServer
);
1706 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1707 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1709 GlobalDeleteAtom(pServer
->atomService
);
1710 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1712 HeapFree(GetProcessHeap(), 0, pServer
);
1717 pServer
= pServer
->next
;
1721 /*****************************************************************************
1724 * generic routine to return a pointer to the relevant ServiceNode
1725 * for a given service name, or NULL if the entry does not exist
1728 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1730 WDML_SERVER
* pServer
;
1732 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1734 if (hszService
== pServer
->hszService
)
1739 TRACE("Service name missing\n");
1743 /* ================================================================
1745 * Link (hot & warm) management
1747 * ================================================================ */
1749 /******************************************************************
1754 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1755 UINT wType
, HSZ hszItem
, UINT wFmt
)
1759 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
1766 pLink
->hConv
= hConv
;
1767 pLink
->transactionType
= wType
;
1768 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
1770 pLink
->next
= pInstance
->links
[side
];
1771 pInstance
->links
[side
] = pLink
;
1774 /******************************************************************
1779 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1780 HSZ hszItem
, UINT uFmt
)
1782 WDML_LINK
* pPrev
= NULL
;
1783 WDML_LINK
* pCurrent
= NULL
;
1785 pCurrent
= pInstance
->links
[side
];
1787 while (pCurrent
!= NULL
)
1789 if (pCurrent
->hConv
== hConv
&&
1790 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1791 pCurrent
->uFmt
== uFmt
)
1793 if (pCurrent
== pInstance
->links
[side
])
1795 pInstance
->links
[side
] = pCurrent
->next
;
1799 pPrev
->next
= pCurrent
->next
;
1802 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1803 HeapFree(GetProcessHeap(), 0, pCurrent
);
1808 pCurrent
= pCurrent
->next
;
1812 /* this function is called to remove all links related to the conv.
1813 It should be called from both client and server when terminating
1816 /******************************************************************
1817 * WDML_RemoveAllLinks
1821 static void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
1823 WDML_LINK
* pPrev
= NULL
;
1824 WDML_LINK
* pCurrent
= NULL
;
1825 WDML_LINK
* pNext
= NULL
;
1827 pCurrent
= pInstance
->links
[side
];
1829 while (pCurrent
!= NULL
)
1831 if (pCurrent
->hConv
== (HCONV
)pConv
)
1833 if (pCurrent
== pInstance
->links
[side
])
1835 pInstance
->links
[side
] = pCurrent
->next
;
1836 pNext
= pCurrent
->next
;
1840 pPrev
->next
= pCurrent
->next
;
1841 pNext
= pCurrent
->next
;
1844 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1846 HeapFree(GetProcessHeap(), 0, pCurrent
);
1853 pCurrent
= pCurrent
->next
;
1862 /******************************************************************
1867 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1868 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
1870 WDML_LINK
* pCurrent
;
1872 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1874 /* we don't need to check for transaction type as it can be altered */
1876 if (pCurrent
->hConv
== hConv
&&
1877 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1878 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
1888 /* ================================================================
1890 * Transaction management
1892 * ================================================================ */
1894 /******************************************************************
1895 * WDML_AllocTransaction
1897 * Alloc a transaction structure for handling the message ddeMsg
1899 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
1900 UINT wFmt
, HSZ hszItem
)
1903 static WORD tid
= 1; /* FIXME: wrap around */
1905 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
1908 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
1912 pXAct
->xActID
= tid
++;
1913 pXAct
->ddeMsg
= ddeMsg
;
1914 pXAct
->hDdeData
= 0;
1919 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
1927 /******************************************************************
1928 * WDML_QueueTransaction
1930 * Adds a transaction to the list of transaction
1932 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1936 /* advance to last in queue */
1937 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
1941 /******************************************************************
1942 * WDML_UnQueueTransaction
1946 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1950 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
1961 /******************************************************************
1962 * WDML_FreeTransaction
1966 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
1968 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
1969 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
1971 GlobalFree(pXAct
->hMem
);
1973 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
1975 HeapFree(GetProcessHeap(), 0, pXAct
);
1978 /******************************************************************
1979 * WDML_FindTransaction
1983 static WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
1988 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1990 if (pXAct
->xActID
== tid
)
1996 /* ================================================================
1998 * Conversation management
2000 * ================================================================ */
2002 /******************************************************************
2007 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2008 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
2012 /* no conversation yet, add it */
2013 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
2014 if (!pConv
) return NULL
;
2016 pConv
->instance
= pInstance
;
2017 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
2018 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
2019 pConv
->magic
= WDML_CONV_MAGIC
;
2020 pConv
->hwndServer
= hwndServer
;
2021 pConv
->hwndClient
= hwndClient
;
2022 pConv
->transactions
= NULL
;
2024 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
2025 pConv
->wStatus
|= pInstance
->wStatus
;
2026 /* check if both side of the conversation are of the same instance */
2027 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
2028 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
2030 pConv
->wStatus
|= ST_ISSELF
;
2032 pConv
->wConvst
= XST_NULL
;
2034 pConv
->next
= pInstance
->convs
[side
];
2035 pInstance
->convs
[side
] = pConv
;
2037 TRACE("pConv->wStatus %04lx pInstance(%p)\n", pConv
->wStatus
, pInstance
);
2042 /******************************************************************
2047 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2048 HSZ hszService
, HSZ hszTopic
)
2050 WDML_CONV
* pCurrent
;
2052 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2054 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
2055 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
2064 /******************************************************************
2069 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
2071 WDML_CONV
* pPrev
= NULL
;
2072 WDML_CONV
* pCurrent
;
2074 WDML_XACT
* pXActNext
;
2080 /* remove any pending transaction */
2081 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
2083 pXActNext
= pXAct
->next
;
2084 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
2087 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
2089 /* FIXME: should we keep the window around ? it seems so (at least on client side
2090 * to let QueryConvInfo work after conv termination, but also to implement
2093 /* destroy conversation window, but first remove pConv from hWnd.
2094 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
2096 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
2097 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
2099 NtUserDestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
2101 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
2102 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
2104 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
2106 if (pCurrent
== pRef
)
2108 if (pCurrent
== pRef
->instance
->convs
[side
])
2110 pRef
->instance
->convs
[side
] = pCurrent
->next
;
2114 pPrev
->next
= pCurrent
->next
;
2116 /* Ensure compiler doesn't optimize out the assignment with 0. */
2117 SecureZeroMemory(&pCurrent
->magic
, sizeof(pCurrent
->magic
));
2118 HeapFree(GetProcessHeap(), 0, pCurrent
);
2124 /******************************************************************
2125 * WDML_EnableCallback
2127 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
2129 if (wCmd
== EC_DISABLE
)
2131 pConv
->wStatus
|= ST_BLOCKED
;
2132 TRACE("EC_DISABLE: conv %p status flags %04lx\n", pConv
, pConv
->wStatus
);
2136 if (wCmd
== EC_QUERYWAITING
)
2137 return pConv
->transactions
!= NULL
;
2139 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
2141 FIXME("Unknown command code %04x\n", wCmd
);
2145 if (wCmd
== EC_ENABLEALL
)
2147 pConv
->wStatus
&= ~ST_BLOCKED
;
2148 TRACE("EC_ENABLEALL: conv %p status flags %04lx\n", pConv
, pConv
->wStatus
);
2151 while (pConv
->transactions
)
2153 WDML_XACT
*pXAct
= pConv
->transactions
;
2155 if (pConv
->wStatus
& ST_CLIENT
)
2157 /* transaction should be in the queue until handled */
2158 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
2159 WDML_UnQueueTransaction(pConv
, pXAct
);
2163 /* transaction should be removed from the queue before handling */
2164 WDML_UnQueueTransaction(pConv
, pXAct
);
2165 WDML_ServerHandle(pConv
, pXAct
);
2168 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
2170 if (wCmd
== EC_ENABLEONE
) break;
2175 /*****************************************************************
2176 * DdeEnableCallback (USER32.@)
2178 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
2183 TRACE("(%ld, %p, %04x)\n", idInst
, hConv
, wCmd
);
2187 pConv
= WDML_GetConv(hConv
, TRUE
);
2189 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
2190 ret
= WDML_EnableCallback(pConv
, wCmd
);
2194 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
2199 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
2200 pInstance
->wStatus
|= wCmd
;
2202 if (wCmd
== EC_DISABLE
)
2204 pInstance
->wStatus
|= ST_BLOCKED
;
2205 TRACE("EC_DISABLE: inst %p status flags %04lx\n", pInstance
, pInstance
->wStatus
);
2207 else if (wCmd
== EC_ENABLEALL
)
2209 pInstance
->wStatus
&= ~ST_BLOCKED
;
2210 TRACE("EC_ENABLEALL: inst %p status flags %04lx\n", pInstance
, pInstance
->wStatus
);
2215 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
2217 ret
= WDML_EnableCallback(pConv
, wCmd
);
2218 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
2225 /******************************************************************
2230 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
2232 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
2234 /* FIXME: should do better checking */
2235 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
2237 if (!pConv
->instance
)
2239 WARN("wrong thread ID, no instance\n");
2243 if (pConv
->instance
->threadID
!= GetCurrentThreadId())
2245 WARN("wrong thread ID\n");
2246 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
2250 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
2252 WARN("found conv but ain't connected\n");
2253 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
2260 /******************************************************************
2261 * WDML_GetConvFromWnd
2265 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
2267 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
2270 /******************************************************************
2275 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
2276 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
2281 if (side
== WDML_SERVER_SIDE
)
2283 from
= pConv
->hwndServer
;
2284 to
= pConv
->hwndClient
;
2288 to
= pConv
->hwndServer
;
2289 from
= pConv
->hwndClient
;
2292 ddeAck
.bAppReturnCode
= appRetCode
;
2293 ddeAck
.reserved
= 0;
2294 ddeAck
.fBusy
= fBusy
;
2297 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
2299 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
2300 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
2301 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
2303 pConv
->wStatus
&= ~ST_CONNECTED
;
2304 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
2305 FreeDDElParam(WM_DDE_ACK
, lParam
);
2311 /*****************************************************************
2312 * DdeSetUserHandle (USER32.@)
2314 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2318 pConv
= WDML_GetConv(hConv
, FALSE
);
2324 pConv
->hUser
= hUser
;
2330 pXAct
= WDML_FindTransaction(pConv
, id
);
2333 pXAct
->hUser
= hUser
;
2337 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2344 /******************************************************************
2345 * WDML_GetLocalConvInfo
2349 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2355 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2356 ci
->hszSvcPartner
= pConv
->hszService
;
2357 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2358 ci
->hszTopic
= pConv
->hszTopic
;
2359 ci
->wStatus
= pConv
->wStatus
;
2361 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2363 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2365 if (pLink
->hConv
== (HCONV
)pConv
)
2367 ci
->wStatus
|= ST_ADVISE
;
2372 /* FIXME: non handled status flags:
2378 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2380 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2382 ci
->ConvCtxt
= pConv
->convContext
;
2383 if (ci
->wStatus
& ST_CLIENT
)
2385 ci
->hwnd
= pConv
->hwndClient
;
2386 ci
->hwndPartner
= pConv
->hwndServer
;
2390 ci
->hwnd
= pConv
->hwndServer
;
2391 ci
->hwndPartner
= pConv
->hwndClient
;
2395 ci
->hUser
= pConv
->hUser
;
2404 pXAct
= WDML_FindTransaction(pConv
, id
);
2407 ci
->hUser
= pXAct
->hUser
;
2408 ci
->hszItem
= pXAct
->hszItem
;
2409 ci
->wFmt
= pXAct
->wFmt
;
2410 ci
->wType
= pXAct
->wType
;
2415 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2421 /******************************************************************
2422 * DdeQueryConvInfo (USER32.@)
2424 * FIXME: Set last DDE error on failure.
2426 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2428 UINT ret
= lpConvInfo
->cb
;
2432 TRACE("(%p,%lx,%p)\n", hConv
, id
, lpConvInfo
);
2436 FIXME("hConv is NULL\n");
2440 pConv
= WDML_GetConv(hConv
, FALSE
);
2443 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2448 if ((ULONG_PTR
)hConv
& 1)
2450 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2452 FIXME("Request on remote conversation information is not implemented yet\n");
2459 ci
.cb
= lpConvInfo
->cb
;
2460 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2465 /* ================================================================
2467 * Information broadcast across DDEML implementations
2469 * ================================================================ */
2471 struct tagWDML_BroadcastPmt
2479 /******************************************************************
2480 * WDML_BroadcastEnumProc
2484 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2486 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2489 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2490 lstrcmpiW(buffer
, s
->clsName
) == 0)
2492 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2497 /******************************************************************
2498 * WDML_BroadcastDDEWindows
2502 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2504 struct tagWDML_BroadcastPmt s
;
2506 s
.clsName
= clsName
;
2510 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);