4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
8 * Copyright 2001 Eric Pouech
9 * Copyright 2003, 2004, 2005 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "dde_private.h"
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddeml
);
33 /* convert between ATOM and HSZ avoiding compiler warnings */
34 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
35 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
37 static WDML_INSTANCE
* WDML_InstanceList
= NULL
;
38 static LONG WDML_MaxInstanceID
= 0; /* OK for present, have to worry about wrap-around later */
39 const WCHAR WDML_szEventClass
[] = L
"DDEMLEvent";
41 /* protection for instance list */
42 CRITICAL_SECTION WDML_CritSect
;
43 static CRITICAL_SECTION_DEBUG critsect_debug
=
46 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
47 0, 0, { (DWORD_PTR
)(__FILE__
": WDML_CritSect") }
49 CRITICAL_SECTION WDML_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
51 /* ================================================================
53 * Pure DDE (non DDEML) management
55 * ================================================================ */
58 /*****************************************************************
59 * PackDDElParam (USER32.@)
64 LPARAM WINAPI
PackDDElParam(UINT msg
, UINT_PTR uiLo
, UINT_PTR uiHi
)
75 if (!(hMem
= GlobalAlloc(GMEM_DDESHARE
, sizeof(UINT_PTR
) * 2)))
77 ERR("GlobalAlloc failed\n");
80 if (!(params
= GlobalLock(hMem
)))
82 ERR("GlobalLock failed (%p)\n", hMem
);
94 return MAKELONG(uiLo
, uiHi
);
99 /*****************************************************************
100 * UnpackDDElParam (USER32.@)
106 BOOL WINAPI
UnpackDDElParam(UINT msg
, LPARAM lParam
,
107 PUINT_PTR uiLo
, PUINT_PTR uiHi
)
117 if (!lParam
|| !(params
= GlobalLock((HGLOBAL
)lParam
)))
123 if (uiLo
) *uiLo
= params
[0];
124 if (uiHi
) *uiHi
= params
[1];
125 GlobalUnlock( (HGLOBAL
)lParam
);
130 if (uiHi
) *uiHi
= lParam
;
134 if (uiLo
) *uiLo
= LOWORD(lParam
);
135 if (uiHi
) *uiHi
= HIWORD(lParam
);
141 /*****************************************************************
142 * FreeDDElParam (USER32.@)
148 BOOL WINAPI
FreeDDElParam(UINT msg
, LPARAM lParam
)
156 /* first check if it's a global handle */
157 if (!GlobalHandle( (LPVOID
)lParam
)) return TRUE
;
158 return !GlobalFree( (HGLOBAL
)lParam
);
166 /*****************************************************************
167 * ReuseDDElParam (USER32.@)
172 LPARAM WINAPI
ReuseDDElParam(LPARAM lParam
, UINT msgIn
, UINT msgOut
,
173 UINT_PTR uiLo
, UINT_PTR uiHi
)
189 if (!lParam
) return 0;
190 if (!(params
= GlobalLock( (HGLOBAL
)lParam
)))
192 ERR("GlobalLock failed\n");
197 TRACE("Reusing pack %08lx %08lx\n", uiLo
, uiHi
);
198 GlobalUnlock( (HGLOBAL
)lParam
);
202 FreeDDElParam( msgIn
, lParam
);
206 FreeDDElParam( msgIn
, lParam
);
207 return MAKELPARAM(uiLo
, uiHi
);
211 return PackDDElParam( msgOut
, uiLo
, uiHi
);
215 /*****************************************************************
216 * ImpersonateDdeClientWindow (USER32.@)
219 * hWndClient [I] handle to DDE client window
220 * hWndServer [I] handle to DDE server window
222 BOOL WINAPI
ImpersonateDdeClientWindow(HWND hWndClient
, HWND hWndServer
)
224 FIXME("(%p %p): stub\n", hWndClient
, hWndServer
);
228 /*****************************************************************
229 * DdeSetQualityOfService (USER32.@)
232 BOOL WINAPI
DdeSetQualityOfService(HWND hwndClient
, const SECURITY_QUALITY_OF_SERVICE
*pqosNew
,
233 PSECURITY_QUALITY_OF_SERVICE pqosPrev
)
235 FIXME("(%p %p %p): stub\n", hwndClient
, pqosNew
, pqosPrev
);
239 /* ================================================================
241 * WDML Error management
243 * ================================================================ */
245 /******************************************************************************
246 * DdeGetLastError [USER32.@] Gets most recent error code
249 * idInst [I] Instance identifier
254 UINT WINAPI
DdeGetLastError(DWORD idInst
)
257 WDML_INSTANCE
* pInstance
;
259 /* First check instance
261 pInstance
= WDML_GetInstance(idInst
);
262 if (pInstance
== NULL
)
264 error_code
= DMLERR_INVALIDPARAMETER
;
268 error_code
= pInstance
->lastError
;
269 pInstance
->lastError
= 0;
275 /******************************************************************
276 * WDML_SetAllLastError
280 static void WDML_SetAllLastError(DWORD lastError
)
283 WDML_INSTANCE
* pInstance
;
284 threadID
= GetCurrentThreadId();
285 pInstance
= WDML_InstanceList
;
288 if (pInstance
->threadID
== threadID
)
289 pInstance
->lastError
= lastError
;
290 pInstance
= pInstance
->next
;
294 /* ================================================================
298 * ================================================================ */
301 /******************************************************************
306 static HSZNode
* WDML_FindNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
310 if (pInstance
== NULL
) return NULL
;
312 for (pNode
= pInstance
->nodeList
; pNode
!= NULL
; pNode
= pNode
->next
)
314 if (pNode
->hsz
== hsz
) break;
316 if (!pNode
) WARN("HSZ %p not found\n", hsz
);
320 /******************************************************************
321 * WDML_MakeAtomFromHsz
323 * Creates a global atom from an existing HSZ
324 * Generally used before sending an HSZ as an atom to a remote app
326 ATOM
WDML_MakeAtomFromHsz(HSZ hsz
)
328 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
330 if (GetAtomNameW(HSZ2ATOM(hsz
), nameBuffer
, MAX_BUFFER_LEN
))
331 return GlobalAddAtomW(nameBuffer
);
332 WARN("HSZ %p not found\n", hsz
);
336 /******************************************************************
337 * WDML_MakeHszFromAtom
339 * Creates a HSZ from an existing global atom
340 * Generally used while receiving a global atom and transforming it
343 HSZ
WDML_MakeHszFromAtom(const WDML_INSTANCE
* pInstance
, ATOM atom
)
345 WCHAR nameBuffer
[MAX_BUFFER_LEN
];
347 if (!atom
) return NULL
;
349 if (GlobalGetAtomNameW(atom
, nameBuffer
, MAX_BUFFER_LEN
))
351 TRACE("%x => %s\n", atom
, debugstr_w(nameBuffer
));
352 return DdeCreateStringHandleW(pInstance
->instanceID
, nameBuffer
, CP_WINUNICODE
);
354 WARN("ATOM 0x%x not found\n", atom
);
358 /******************************************************************
363 BOOL
WDML_IncHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
367 pNode
= WDML_FindNode(pInstance
, hsz
);
368 if (!pNode
) return FALSE
;
374 /******************************************************************************
375 * WDML_DecHSZ (INTERNAL)
377 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
379 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
381 BOOL
WDML_DecHSZ(WDML_INSTANCE
* pInstance
, HSZ hsz
)
383 HSZNode
* pPrev
= NULL
;
386 for (pCurrent
= pInstance
->nodeList
; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
388 /* If we found the node we were looking for and its ref count is one,
391 if (pCurrent
->hsz
== hsz
)
393 if (--pCurrent
->refCount
== 0)
395 if (pCurrent
== pInstance
->nodeList
)
397 pInstance
->nodeList
= pCurrent
->next
;
401 pPrev
->next
= pCurrent
->next
;
403 HeapFree(GetProcessHeap(), 0, pCurrent
);
404 DeleteAtom(HSZ2ATOM(hsz
));
409 WARN("HSZ %p not found\n", hsz
);
414 /******************************************************************************
415 * WDML_FreeAllHSZ (INTERNAL)
417 * Frees up all the strings still allocated in the list and
418 * remove all the nodes from the list of HSZ nodes.
420 void WDML_FreeAllHSZ(WDML_INSTANCE
* pInstance
)
422 /* Free any strings created in this instance.
424 while (pInstance
->nodeList
!= NULL
)
426 DdeFreeStringHandle(pInstance
->instanceID
, pInstance
->nodeList
->hsz
);
430 /******************************************************************************
431 * InsertHSZNode (INTERNAL)
433 * Insert a node to the head of the list.
435 static void WDML_InsertHSZNode(WDML_INSTANCE
* pInstance
, HSZ hsz
)
439 HSZNode
* pNew
= NULL
;
440 /* Create a new node for this HSZ.
442 pNew
= HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode
));
446 pNew
->next
= pInstance
->nodeList
;
448 pInstance
->nodeList
= pNew
;
452 ERR("Primary HSZ Node allocation failed - out of memory\n");
457 /******************************************************************
462 static int WDML_QueryString(WDML_INSTANCE
* pInstance
, HSZ hsz
, LPVOID ptr
, DWORD cchMax
,
465 WCHAR pString
[MAX_BUFFER_LEN
];
467 /* If psz is null, we have to return only the length
473 cchMax
= MAX_BUFFER_LEN
;
476 /* if there is no input windows returns a NULL string */
487 ret
= GetAtomNameA(HSZ2ATOM(hsz
), ptr
, cchMax
);
490 ret
= GetAtomNameW(HSZ2ATOM(hsz
), ptr
, cchMax
);
493 ERR("Unknown code page %d\n", codepage
);
499 /*****************************************************************
500 * DdeQueryStringA [USER32.@]
502 DWORD WINAPI
DdeQueryStringA(DWORD idInst
, HSZ hsz
, LPSTR psz
, DWORD cchMax
, INT iCodePage
)
505 WDML_INSTANCE
* pInstance
;
507 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
509 /* First check instance
511 pInstance
= WDML_GetInstance(idInst
);
512 if (pInstance
!= NULL
)
514 if (iCodePage
== 0) iCodePage
= CP_WINANSI
;
515 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
518 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
522 /*****************************************************************
523 * DdeQueryStringW [USER32.@]
526 DWORD WINAPI
DdeQueryStringW(DWORD idInst
, HSZ hsz
, LPWSTR psz
, DWORD cchMax
, INT iCodePage
)
529 WDML_INSTANCE
* pInstance
;
531 TRACE("(%d, %p, %p, %d, %d)\n", idInst
, hsz
, psz
, cchMax
, iCodePage
);
533 /* First check instance
535 pInstance
= WDML_GetInstance(idInst
);
536 if (pInstance
!= NULL
)
538 if (iCodePage
== 0) iCodePage
= CP_WINUNICODE
;
539 ret
= WDML_QueryString(pInstance
, hsz
, psz
, cchMax
, iCodePage
);
542 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
546 /******************************************************************
551 static HSZ
WDML_CreateString(WDML_INSTANCE
* pInstance
, LPCVOID ptr
, int codepage
)
558 hsz
= ATOM2HSZ(AddAtomA(ptr
));
559 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr
), hsz
);
562 hsz
= ATOM2HSZ(AddAtomW(ptr
));
563 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr
), hsz
);
566 ERR("Unknown code page %d\n", codepage
);
569 WDML_InsertHSZNode(pInstance
, hsz
);
573 /*****************************************************************
574 * DdeCreateStringHandleA [USER32.@]
576 * See DdeCreateStringHandleW.
578 HSZ WINAPI
DdeCreateStringHandleA(DWORD idInst
, LPCSTR psz
, INT codepage
)
581 WDML_INSTANCE
* pInstance
;
583 TRACE("(%d,%s,%d)\n", idInst
, debugstr_a(psz
), codepage
);
585 pInstance
= WDML_GetInstance(idInst
);
586 if (pInstance
== NULL
)
587 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
590 if (codepage
== 0) codepage
= CP_WINANSI
;
591 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
598 /******************************************************************************
599 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
602 * idInst [I] Instance identifier
603 * psz [I] Pointer to string
604 * codepage [I] Code page identifier
606 * Success: String handle
609 HSZ WINAPI
DdeCreateStringHandleW(DWORD idInst
, LPCWSTR psz
, INT codepage
)
611 WDML_INSTANCE
* pInstance
;
614 pInstance
= WDML_GetInstance(idInst
);
615 if (pInstance
== NULL
)
616 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
619 if (codepage
== 0) codepage
= CP_WINUNICODE
;
620 hsz
= WDML_CreateString(pInstance
, psz
, codepage
);
626 /*****************************************************************
627 * DdeFreeStringHandle (USER32.@)
632 BOOL WINAPI
DdeFreeStringHandle(DWORD idInst
, HSZ hsz
)
634 WDML_INSTANCE
* pInstance
;
637 TRACE("(%d,%p):\n", idInst
, hsz
);
639 /* First check instance
641 pInstance
= WDML_GetInstance(idInst
);
643 ret
= WDML_DecHSZ(pInstance
, hsz
);
648 /*****************************************************************
649 * DdeKeepStringHandle (USER32.@)
655 BOOL WINAPI
DdeKeepStringHandle(DWORD idInst
, HSZ hsz
)
657 WDML_INSTANCE
* pInstance
;
660 TRACE("(%d,%p):\n", idInst
, hsz
);
662 /* First check instance
664 pInstance
= WDML_GetInstance(idInst
);
666 ret
= WDML_IncHSZ(pInstance
, hsz
);
671 /*****************************************************************
672 * DdeCmpStringHandles (USER32.@)
674 * Compares the value of two string handles. This comparison is
675 * not case sensitive.
678 * hsz1 [I] Handle to the first string
679 * hsz2 [I] Handle to the second string
682 * -1 The value of hsz1 is zero or less than hsz2
683 * 0 The values of hsz 1 and 2 are the same or both zero.
684 * 1 The value of hsz2 is zero of less than hsz1
686 INT WINAPI
DdeCmpStringHandles(HSZ hsz1
, HSZ hsz2
)
688 WCHAR psz1
[MAX_BUFFER_LEN
];
689 WCHAR psz2
[MAX_BUFFER_LEN
];
693 ret1
= GetAtomNameW(HSZ2ATOM(hsz1
), psz1
, MAX_BUFFER_LEN
);
694 ret2
= GetAtomNameW(HSZ2ATOM(hsz2
), psz2
, MAX_BUFFER_LEN
);
696 TRACE("(%p<%s> %p<%s>);\n", hsz1
, debugstr_w(psz1
), hsz2
, debugstr_w(psz2
));
698 /* Make sure we found both strings. */
699 if (ret1
== 0 && ret2
== 0)
701 /* If both are not found, return both "zero strings". */
706 /* If hsz1 is a not found, return hsz1 is "zero string". */
711 /* If hsz2 is a not found, return hsz2 is "zero string". */
716 /* Compare the two strings we got (case insensitive). */
717 ret
= lstrcmpiW(psz1
, psz2
);
718 /* Since strcmp returns any number smaller than
719 * 0 when the first string is found to be less than
720 * the second one we must make sure we are returning
736 /* ================================================================
738 * Instance management
740 * ================================================================ */
742 /******************************************************************************
743 * IncrementInstanceId
745 * generic routine to increment the max instance Id and allocate a new application instance
747 static void WDML_IncrementInstanceId(WDML_INSTANCE
* pInstance
)
749 DWORD id
= InterlockedIncrement(&WDML_MaxInstanceID
);
751 pInstance
->instanceID
= id
;
752 TRACE("New instance id %d allocated\n", id
);
755 /******************************************************************
760 static LRESULT CALLBACK
WDML_EventProc(HWND hwndEvent
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
762 WDML_INSTANCE
* pInstance
;
767 case WM_WDML_REGISTER
:
768 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
769 /* try calling the Callback */
770 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_REGISTRATIONS
))
772 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
773 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
774 WDML_InvokeCallback(pInstance
, XTYP_REGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
775 WDML_DecHSZ(pInstance
, hsz1
);
776 WDML_DecHSZ(pInstance
, hsz2
);
780 case WM_WDML_UNREGISTER
:
781 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
782 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_UNREGISTRATIONS
))
784 hsz1
= WDML_MakeHszFromAtom(pInstance
, wParam
);
785 hsz2
= WDML_MakeHszFromAtom(pInstance
, lParam
);
786 WDML_InvokeCallback(pInstance
, XTYP_UNREGISTER
, 0, 0, hsz1
, hsz2
, 0, 0, 0);
787 WDML_DecHSZ(pInstance
, hsz1
);
788 WDML_DecHSZ(pInstance
, hsz2
);
792 case WM_WDML_CONNECT_CONFIRM
:
793 pInstance
= WDML_GetInstanceFromWnd(hwndEvent
);
794 if (pInstance
&& !(pInstance
->CBFflags
& CBF_SKIP_CONNECT_CONFIRMS
))
797 /* confirm connection...
798 * lookup for this conv handle
800 HWND client
= WIN_GetFullHandle( (HWND
)wParam
);
801 HWND server
= WIN_GetFullHandle( (HWND
)lParam
);
802 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
804 if (pConv
->hwndClient
== client
&& pConv
->hwndServer
== server
)
809 pConv
->wStatus
|= ST_ISLOCAL
;
811 WDML_InvokeCallback(pInstance
, XTYP_CONNECT_CONFIRM
, 0, (HCONV
)pConv
,
812 pConv
->hszTopic
, pConv
->hszService
, 0, 0,
813 (pConv
->wStatus
& ST_ISSELF
) ? 1 : 0);
818 return DefWindowProcW(hwndEvent
, uMsg
, wParam
, lParam
);
823 /******************************************************************
828 UINT
WDML_Initialize(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
829 DWORD afCmd
, DWORD ulRes
, BOOL bUnicode
)
831 WDML_INSTANCE
* pInstance
;
832 WDML_INSTANCE
* reference_inst
;
834 WNDCLASSEXW wndclass
;
836 TRACE("(%p,%p,0x%x,%d,0x%x)\n",
837 pidInst
, pfnCallback
, afCmd
, ulRes
, bUnicode
);
841 ERR("Reserved value not zero? What does this mean?\n");
842 /* trap this and no more until we know more */
843 return DMLERR_NO_ERROR
;
846 /* grab enough heap for one control struct - not really necessary for re-initialise
847 * but allows us to use same validation routines */
848 pInstance
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE
));
849 if (pInstance
== NULL
)
851 /* catastrophe !! warn user & abort */
852 ERR("Instance create failed - out of memory\n");
853 return DMLERR_SYS_ERROR
;
855 pInstance
->next
= NULL
;
856 pInstance
->monitor
= (afCmd
| APPCLASS_MONITOR
);
858 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
860 pInstance
->clientOnly
= afCmd
& APPCMD_CLIENTONLY
;
861 pInstance
->instanceID
= *pidInst
; /* May need to add calling proc Id */
862 pInstance
->threadID
= GetCurrentThreadId();
863 pInstance
->callback
= *pfnCallback
;
864 pInstance
->unicode
= bUnicode
;
865 pInstance
->nodeList
= NULL
; /* node will be added later */
866 pInstance
->monitorFlags
= afCmd
& MF_MASK
;
867 pInstance
->wStatus
= 0;
868 pInstance
->lastError
= DMLERR_NO_ERROR
;
869 pInstance
->servers
= NULL
;
870 pInstance
->convs
[0] = NULL
;
871 pInstance
->convs
[1] = NULL
;
872 pInstance
->links
[0] = NULL
;
873 pInstance
->links
[1] = NULL
;
875 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
877 pInstance
->CBFflags
= afCmd
^((afCmd
&MF_MASK
)|((afCmd
&APPCMD_MASK
)|(afCmd
&APPCLASS_MASK
)));
879 if (!pInstance
->clientOnly
)
881 /* Check for other way of setting Client-only !! */
882 pInstance
->clientOnly
=
883 (pInstance
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) == CBF_FAIL_ALLSVRXACTIONS
;
886 TRACE("instance created - checking validity\n");
890 /* Initialisation of new Instance Identifier */
891 TRACE("new instance, callback %p flags %X\n",pfnCallback
,afCmd
);
893 EnterCriticalSection(&WDML_CritSect
);
895 if (WDML_InstanceList
== NULL
)
897 /* can't be another instance in this case, assign to the base pointer */
898 WDML_InstanceList
= pInstance
;
900 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
902 * ------------------------------- NOTE NOTE NOTE --------------------------
904 * the manual is not clear if this condition
905 * applies to the first call to DdeInitialize from an application, or the
906 * first call for a given callback !!!
909 pInstance
->CBFflags
= pInstance
->CBFflags
|APPCMD_FILTERINITS
;
910 TRACE("First application instance detected OK\n");
911 /* allocate new instance ID */
912 WDML_IncrementInstanceId(pInstance
);
916 /* really need to chain the new one in to the latest here, but after checking conditions
917 * such as trying to start a conversation from an application trying to monitor */
918 reference_inst
= WDML_InstanceList
;
919 TRACE("Subsequent application instance - starting checks\n");
920 while (reference_inst
->next
!= NULL
)
923 * This set of tests will work if application uses same instance Id
924 * at application level once allocated - which is what manual implies
925 * should happen. If someone tries to be
926 * clever (lazy ?) it will fail to pick up that later calls are for
927 * the same application - should we trust them ?
929 if (pInstance
->instanceID
== reference_inst
->instanceID
)
931 /* Check 1 - must be same Client-only state */
933 if (pInstance
->clientOnly
!= reference_inst
->clientOnly
)
935 ret
= DMLERR_DLL_USAGE
;
939 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
941 if (pInstance
->monitor
!= reference_inst
->monitor
)
943 ret
= DMLERR_INVALIDPARAMETER
;
947 /* Check 3 - must supply different callback address */
949 if (pInstance
->callback
== reference_inst
->callback
)
951 ret
= DMLERR_DLL_USAGE
;
955 reference_inst
= reference_inst
->next
;
957 /* All cleared, add to chain */
959 TRACE("Application Instance checks finished\n");
960 WDML_IncrementInstanceId(pInstance
);
961 reference_inst
->next
= pInstance
;
963 LeaveCriticalSection(&WDML_CritSect
);
965 *pidInst
= pInstance
->instanceID
;
967 /* for deadlock issues, windows must always be created when outside the critical section */
968 wndclass
.cbSize
= sizeof(wndclass
);
970 wndclass
.lpfnWndProc
= WDML_EventProc
;
971 wndclass
.cbClsExtra
= 0;
972 wndclass
.cbWndExtra
= sizeof(ULONG_PTR
);
973 wndclass
.hInstance
= 0;
975 wndclass
.hCursor
= 0;
976 wndclass
.hbrBackground
= 0;
977 wndclass
.lpszMenuName
= NULL
;
978 wndclass
.lpszClassName
= WDML_szEventClass
;
979 wndclass
.hIconSm
= 0;
981 RegisterClassExW(&wndclass
);
983 pInstance
->hwndEvent
= CreateWindowW(WDML_szEventClass
, NULL
,
984 WS_POPUP
, 0, 0, 0, 0,
987 SetWindowLongPtrW(pInstance
->hwndEvent
, GWL_WDML_INSTANCE
, (ULONG_PTR
)pInstance
);
989 TRACE("New application instance processing finished OK\n");
993 /* Reinitialisation situation --- FIX */
994 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst
, pfnCallback
, afCmd
, ulRes
);
996 EnterCriticalSection(&WDML_CritSect
);
998 if (WDML_InstanceList
== NULL
)
1000 ret
= DMLERR_INVALIDPARAMETER
;
1003 /* can't reinitialise if we have initialised nothing !! */
1004 reference_inst
= WDML_InstanceList
;
1005 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
1007 * MS allows initialisation without specifying a callback, should we allow addition of the
1008 * callback by a later call to initialise ? - if so this lot will have to change
1010 while (reference_inst
->next
!= NULL
)
1012 if (*pidInst
== reference_inst
->instanceID
&& pfnCallback
== reference_inst
->callback
)
1014 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
1016 if (reference_inst
->clientOnly
)
1018 if ((reference_inst
->CBFflags
& CBF_FAIL_ALLSVRXACTIONS
) != CBF_FAIL_ALLSVRXACTIONS
)
1020 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
1022 if (!(afCmd
& APPCMD_CLIENTONLY
))
1024 ret
= DMLERR_INVALIDPARAMETER
;
1029 /* Check 2 - cannot change monitor modes */
1031 if (pInstance
->monitor
!= reference_inst
->monitor
)
1033 ret
= DMLERR_INVALIDPARAMETER
;
1037 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
1039 if ((afCmd
&APPCMD_CLIENTONLY
) && !reference_inst
->clientOnly
)
1041 ret
= DMLERR_INVALIDPARAMETER
;
1046 reference_inst
= reference_inst
->next
;
1048 if (reference_inst
->next
== NULL
)
1050 ret
= DMLERR_INVALIDPARAMETER
;
1053 /* All checked - change relevant flags */
1055 reference_inst
->CBFflags
= pInstance
->CBFflags
;
1056 reference_inst
->clientOnly
= pInstance
->clientOnly
;
1057 reference_inst
->monitorFlags
= pInstance
->monitorFlags
;
1059 HeapFree(GetProcessHeap(), 0, pInstance
); /* finished - release heap space used as work store */
1061 LeaveCriticalSection(&WDML_CritSect
);
1064 return DMLERR_NO_ERROR
;
1066 HeapFree(GetProcessHeap(), 0, pInstance
);
1067 LeaveCriticalSection(&WDML_CritSect
);
1071 /******************************************************************************
1072 * DdeInitializeA (USER32.@)
1074 * See DdeInitializeW.
1076 UINT WINAPI
DdeInitializeA(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1077 DWORD afCmd
, DWORD ulRes
)
1079 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, FALSE
);
1082 /******************************************************************************
1083 * DdeInitializeW [USER32.@]
1084 * Registers an application with the DDEML
1087 * pidInst [I] Pointer to instance identifier
1088 * pfnCallback [I] Pointer to callback function
1089 * afCmd [I] Set of command and filter flags
1090 * ulRes [I] Reserved
1093 * Success: DMLERR_NO_ERROR
1094 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
1096 UINT WINAPI
DdeInitializeW(LPDWORD pidInst
, PFNCALLBACK pfnCallback
,
1097 DWORD afCmd
, DWORD ulRes
)
1099 return WDML_Initialize(pidInst
, pfnCallback
, afCmd
, ulRes
, TRUE
);
1102 /*****************************************************************
1103 * DdeUninitialize [USER32.@] Frees DDEML resources
1106 * idInst [I] Instance identifier
1113 BOOL WINAPI
DdeUninitialize(DWORD idInst
)
1115 /* Stage one - check if we have a handle for this instance
1117 WDML_INSTANCE
* pInstance
;
1119 WDML_CONV
* pConvNext
;
1121 TRACE("(%d)\n", idInst
);
1123 /* First check instance
1125 pInstance
= WDML_GetInstance(idInst
);
1126 if (pInstance
== NULL
)
1129 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
1134 /* first terminate all conversations client side
1135 * this shall close existing links...
1137 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1139 pConvNext
= pConv
->next
;
1140 DdeDisconnect((HCONV
)pConv
);
1142 if (pInstance
->convs
[WDML_CLIENT_SIDE
])
1143 FIXME("still pending conversations\n");
1145 /* then unregister all known service names */
1146 DdeNameService(idInst
, 0, 0, DNS_UNREGISTER
);
1148 /* Free the nodes that were not freed by this instance
1149 * and remove the nodes from the list of HSZ nodes.
1151 WDML_FreeAllHSZ(pInstance
);
1153 DestroyWindow(pInstance
->hwndEvent
);
1155 /* OK now delete the instance handle itself */
1157 if (WDML_InstanceList
== pInstance
)
1159 /* special case - the first/only entry */
1160 WDML_InstanceList
= pInstance
->next
;
1164 /* general case, remove entry */
1165 WDML_INSTANCE
* inst
;
1167 for (inst
= WDML_InstanceList
; inst
->next
!= pInstance
; inst
= inst
->next
);
1168 inst
->next
= pInstance
->next
;
1170 /* release the heap entry
1172 HeapFree(GetProcessHeap(), 0, pInstance
);
1177 /******************************************************************
1178 * WDML_NotifyThreadExit
1182 void WDML_NotifyThreadDetach(void)
1184 WDML_INSTANCE
* pInstance
;
1185 WDML_INSTANCE
* next
;
1186 DWORD tid
= GetCurrentThreadId();
1188 EnterCriticalSection(&WDML_CritSect
);
1189 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= next
)
1191 next
= pInstance
->next
;
1192 if (pInstance
->threadID
== tid
)
1194 LeaveCriticalSection(&WDML_CritSect
);
1195 DdeUninitialize(pInstance
->instanceID
);
1196 EnterCriticalSection(&WDML_CritSect
);
1199 LeaveCriticalSection(&WDML_CritSect
);
1202 /******************************************************************
1203 * WDML_InvokeCallback
1207 HDDEDATA
WDML_InvokeCallback(WDML_INSTANCE
* pInstance
, UINT uType
, UINT uFmt
, HCONV hConv
,
1208 HSZ hsz1
, HSZ hsz2
, HDDEDATA hdata
,
1209 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
1213 if (pInstance
== NULL
)
1216 TRACE("invoking CB[%p] (%x %x %p %p %p %p %lx %lx)\n",
1217 pInstance
->callback
, uType
, uFmt
,
1218 hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1219 ret
= pInstance
->callback(uType
, uFmt
, hConv
, hsz1
, hsz2
, hdata
, dwData1
, dwData2
);
1220 TRACE("done => %p\n", ret
);
1224 /*****************************************************************************
1227 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
1228 * for an instance Id, or NULL if the entry does not exist
1231 WDML_INSTANCE
* WDML_GetInstance(DWORD instId
)
1233 WDML_INSTANCE
* pInstance
;
1235 EnterCriticalSection(&WDML_CritSect
);
1237 for (pInstance
= WDML_InstanceList
; pInstance
!= NULL
; pInstance
= pInstance
->next
)
1239 if (pInstance
->instanceID
== instId
)
1241 if (GetCurrentThreadId() != pInstance
->threadID
)
1243 FIXME("Tried to get instance from wrong thread\n");
1250 LeaveCriticalSection(&WDML_CritSect
);
1253 WARN("Instance entry missing for id %04x\n", instId
);
1257 /******************************************************************
1258 * WDML_GetInstanceFromWnd
1262 WDML_INSTANCE
* WDML_GetInstanceFromWnd(HWND hWnd
)
1264 return (WDML_INSTANCE
*)GetWindowLongPtrW(hWnd
, GWL_WDML_INSTANCE
);
1267 /* ================================================================
1269 * Data handle management
1271 * ================================================================ */
1273 /*****************************************************************
1274 * DdeCreateDataHandle (USER32.@)
1276 HDDEDATA WINAPI
DdeCreateDataHandle(DWORD idInst
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
,
1277 HSZ hszItem
, UINT wFmt
, UINT afCmd
)
1280 /* Other than check for validity we will ignore for now idInst, hszItem.
1281 * The purpose of these arguments still need to be investigated.
1284 WDML_INSTANCE
* pInstance
;
1287 DDE_DATAHANDLE_HEAD
* pDdh
;
1288 WCHAR psz
[MAX_BUFFER_LEN
];
1290 pInstance
= WDML_GetInstance(idInst
);
1291 if (pInstance
== NULL
)
1293 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER
);
1297 if (!GetAtomNameW(HSZ2ATOM(hszItem
), psz
, MAX_BUFFER_LEN
))
1299 psz
[0] = HSZ2ATOM(hszItem
);
1303 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1304 idInst
, pSrc
, cb
, cbOff
, hszItem
, debugstr_w(psz
), wFmt
, afCmd
);
1306 if (afCmd
!= 0 && afCmd
!= HDATA_APPOWNED
)
1309 /* we use the first 4 bytes to store the size */
1310 if (!(hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, cb
+ cbOff
+ sizeof(DDE_DATAHANDLE_HEAD
))))
1312 ERR("GlobalAlloc failed\n");
1316 pDdh
= GlobalLock(hMem
);
1323 pDdh
->cfFormat
= wFmt
;
1324 pDdh
->bAppOwned
= (afCmd
== HDATA_APPOWNED
);
1326 pByte
= (LPBYTE
)(pDdh
+ 1);
1329 memcpy(pByte
, pSrc
+ cbOff
, cb
);
1333 TRACE("=> %p\n", hMem
);
1337 /*****************************************************************
1339 * DdeAddData (USER32.@)
1341 HDDEDATA WINAPI
DdeAddData(HDDEDATA hData
, LPBYTE pSrc
, DWORD cb
, DWORD cbOff
)
1343 DWORD old_sz
, new_sz
;
1346 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData
, pSrc
, cb
, cbOff
);
1348 pDst
= DdeAccessData(hData
, &old_sz
);
1349 if (!pDst
) return 0;
1351 new_sz
= cb
+ cbOff
;
1352 if (new_sz
> old_sz
)
1354 DdeUnaccessData(hData
);
1355 hData
= GlobalReAlloc(hData
, new_sz
+ sizeof(DDE_DATAHANDLE_HEAD
),
1356 GMEM_MOVEABLE
| GMEM_DDESHARE
);
1357 pDst
= DdeAccessData(hData
, &old_sz
);
1360 if (!pDst
) return 0;
1362 memcpy(pDst
+ cbOff
, pSrc
, cb
);
1363 DdeUnaccessData(hData
);
1367 /******************************************************************************
1368 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1372 * hData [I] Handle to DDE object
1373 * pDst [I] Pointer to destination buffer
1374 * cbMax [I] Amount of data to copy
1375 * cbOff [I] Offset to beginning of data
1378 * Size of memory object associated with handle
1380 DWORD WINAPI
DdeGetData(HDDEDATA hData
, LPBYTE pDst
, DWORD cbMax
, DWORD cbOff
)
1382 DWORD dwSize
, dwRet
;
1385 TRACE("(%p,%p,%d,%d)\n", hData
, pDst
, cbMax
, cbOff
);
1387 pByte
= DdeAccessData(hData
, &dwSize
);
1395 else if (cbOff
+ cbMax
< dwSize
)
1399 else if (cbOff
< dwSize
)
1401 dwRet
= dwSize
- cbOff
;
1407 if (pDst
&& dwRet
!= 0)
1409 memcpy(pDst
, pByte
+ cbOff
, dwRet
);
1411 DdeUnaccessData(hData
);
1420 /*****************************************************************
1421 * DdeAccessData (USER32.@)
1423 LPBYTE WINAPI
DdeAccessData(HDDEDATA hData
, LPDWORD pcbDataSize
)
1425 HGLOBAL hMem
= hData
;
1426 DDE_DATAHANDLE_HEAD
* pDdh
;
1428 TRACE("(%p,%p)\n", hData
, pcbDataSize
);
1430 pDdh
= GlobalLock(hMem
);
1433 ERR("Failed on GlobalLock(%p)\n", hMem
);
1437 if (pcbDataSize
!= NULL
)
1439 *pcbDataSize
= GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
);
1441 TRACE("=> %p (%lu) fmt %04x\n", pDdh
+ 1, GlobalSize(hMem
) - sizeof(DDE_DATAHANDLE_HEAD
), pDdh
->cfFormat
);
1442 return (LPBYTE
)(pDdh
+ 1);
1445 /*****************************************************************
1446 * DdeUnaccessData (USER32.@)
1448 BOOL WINAPI
DdeUnaccessData(HDDEDATA hData
)
1450 HGLOBAL hMem
= hData
;
1452 TRACE("(%p)\n", hData
);
1459 /*****************************************************************
1460 * DdeFreeDataHandle (USER32.@)
1462 BOOL WINAPI
DdeFreeDataHandle(HDDEDATA hData
)
1464 TRACE("(%p)\n", hData
);
1466 /* 1 is the handle value returned by an asynchronous operation. */
1467 if (hData
== (HDDEDATA
)1)
1470 return GlobalFree(hData
) == 0;
1473 /******************************************************************
1478 BOOL
WDML_IsAppOwned(HDDEDATA hData
)
1480 DDE_DATAHANDLE_HEAD
* pDdh
;
1483 pDdh
= GlobalLock(hData
);
1486 ret
= pDdh
->bAppOwned
;
1487 GlobalUnlock(hData
);
1492 /* ================================================================
1494 * Global <=> Data handle management
1496 * ================================================================ */
1498 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1500 * (bytes) (bits) comment
1501 * 0 16 bit fields for options (release, ackreq, response...)
1502 * 2 16 clipboard format
1503 * 4 ? data to be used
1505 HDDEDATA
WDML_Global2DataHandle(WDML_CONV
* pConv
, HGLOBAL hMem
, WINE_DDEHEAD
* p
)
1513 pDd
= GlobalLock(hMem
);
1514 size
= GlobalSize(hMem
) - sizeof(WINE_DDEHEAD
);
1517 if (p
) memcpy(p
, pDd
, sizeof(WINE_DDEHEAD
));
1518 switch (pDd
->cfFormat
)
1521 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1522 pDd
->cfFormat
, hMem
);
1526 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, pDd
->Value
, size
, 0, 0, pDd
->cfFormat
, 0);
1529 if (size
>= sizeof(BITMAP
))
1531 BITMAP
* bmp
= (BITMAP
*)pDd
->Value
;
1532 int count
= bmp
->bmWidthBytes
* bmp
->bmHeight
* bmp
->bmPlanes
;
1533 if (size
>= sizeof(BITMAP
) + count
)
1537 if ((hbmp
= CreateBitmap(bmp
->bmWidth
, bmp
->bmHeight
,
1538 bmp
->bmPlanes
, bmp
->bmBitsPixel
,
1539 pDd
->Value
+ sizeof(BITMAP
))))
1541 ret
= DdeCreateDataHandle(pConv
->instance
->instanceID
, (LPBYTE
)&hbmp
, sizeof(hbmp
),
1542 0, 0, CF_BITMAP
, 0);
1544 else ERR("Can't create bmp\n");
1548 ERR("Wrong count: %u / %d\n", size
, count
);
1550 } else ERR("No bitmap header\n");
1559 /******************************************************************
1560 * WDML_DataHandle2Global
1564 HGLOBAL
WDML_DataHandle2Global(HDDEDATA hDdeData
, BOOL fResponse
, BOOL fRelease
,
1565 BOOL fDeferUpd
, BOOL fAckReq
)
1567 DDE_DATAHANDLE_HEAD
* pDdh
;
1571 dwSize
= GlobalSize(hDdeData
) - sizeof(DDE_DATAHANDLE_HEAD
);
1572 pDdh
= GlobalLock(hDdeData
);
1575 WINE_DDEHEAD
* wdh
= NULL
;
1577 switch (pDdh
->cfFormat
)
1580 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1581 pDdh
->cfFormat
, hDdeData
);
1585 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
, sizeof(WINE_DDEHEAD
) + dwSize
);
1586 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1588 memcpy(wdh
+ 1, pDdh
+ 1, dwSize
);
1592 if (dwSize
>= sizeof(HBITMAP
))
1596 HBITMAP hbmp
= *(HBITMAP
*)(pDdh
+ 1);
1598 if (GetObjectW(hbmp
, sizeof(bmp
), &bmp
))
1600 count
= bmp
.bmWidthBytes
* bmp
.bmHeight
;
1601 hMem
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
,
1602 sizeof(WINE_DDEHEAD
) + sizeof(bmp
) + count
);
1603 if (hMem
&& (wdh
= GlobalLock(hMem
)))
1605 memcpy(wdh
+ 1, &bmp
, sizeof(bmp
));
1606 GetBitmapBits(hbmp
, count
, ((char*)(wdh
+ 1)) + sizeof(bmp
));
1615 wdh
->fResponse
= fResponse
;
1616 wdh
->fRelease
= fRelease
;
1617 wdh
->fDeferUpd
= fDeferUpd
;
1618 wdh
->fAckReq
= fAckReq
;
1619 wdh
->cfFormat
= pDdh
->cfFormat
;
1622 GlobalUnlock(hDdeData
);
1628 /* ================================================================
1632 * ================================================================ */
1634 /******************************************************************
1639 WDML_SERVER
* WDML_AddServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1641 static const WCHAR fmtW
[] = {'%','s','(','0','x','%','*','x',')',0};
1642 WDML_SERVER
* pServer
;
1646 pServer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER
));
1647 if (pServer
== NULL
) return NULL
;
1649 pServer
->hszService
= hszService
;
1650 WDML_IncHSZ(pInstance
, hszService
);
1652 DdeQueryStringW(pInstance
->instanceID
, hszService
, buf1
, 256, CP_WINUNICODE
);
1653 snprintfW(buf2
, 256, fmtW
, buf1
, 2*sizeof(ULONG_PTR
), GetCurrentProcessId());
1654 pServer
->hszServiceSpec
= DdeCreateStringHandleW(pInstance
->instanceID
, buf2
, CP_WINUNICODE
);
1656 pServer
->atomService
= WDML_MakeAtomFromHsz(pServer
->hszService
);
1657 pServer
->atomServiceSpec
= WDML_MakeAtomFromHsz(pServer
->hszServiceSpec
);
1659 pServer
->filterOn
= TRUE
;
1661 pServer
->next
= pInstance
->servers
;
1662 pInstance
->servers
= pServer
;
1666 /******************************************************************
1671 void WDML_RemoveServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1673 WDML_SERVER
* pPrev
= NULL
;
1674 WDML_SERVER
* pServer
= NULL
;
1676 WDML_CONV
* pConvNext
;
1678 pServer
= pInstance
->servers
;
1680 while (pServer
!= NULL
)
1682 if (DdeCmpStringHandles(pServer
->hszService
, hszService
) == 0)
1684 WDML_BroadcastDDEWindows(WDML_szEventClass
, WM_WDML_UNREGISTER
,
1685 pServer
->atomService
, pServer
->atomServiceSpec
);
1686 /* terminate all conversations for given topic */
1687 for (pConv
= pInstance
->convs
[WDML_SERVER_SIDE
]; pConv
!= NULL
; pConv
= pConvNext
)
1689 pConvNext
= pConv
->next
;
1690 if (DdeCmpStringHandles(pConv
->hszService
, hszService
) == 0)
1692 HWND client
= pConv
->hwndClient
, server
= pConv
->hwndServer
;
1693 WDML_RemoveConv(pConv
, WDML_SERVER_SIDE
);
1694 /* don't care about return code (whether client window is present or not) */
1695 PostMessageW(client
, WM_DDE_TERMINATE
, (WPARAM
)server
, 0);
1698 if (pServer
== pInstance
->servers
)
1700 pInstance
->servers
= pServer
->next
;
1704 pPrev
->next
= pServer
->next
;
1707 DestroyWindow(pServer
->hwndServer
);
1708 WDML_DecHSZ(pInstance
, pServer
->hszServiceSpec
);
1709 WDML_DecHSZ(pInstance
, pServer
->hszService
);
1711 GlobalDeleteAtom(pServer
->atomService
);
1712 GlobalDeleteAtom(pServer
->atomServiceSpec
);
1714 HeapFree(GetProcessHeap(), 0, pServer
);
1719 pServer
= pServer
->next
;
1723 /*****************************************************************************
1726 * generic routine to return a pointer to the relevant ServiceNode
1727 * for a given service name, or NULL if the entry does not exist
1730 WDML_SERVER
* WDML_FindServer(WDML_INSTANCE
* pInstance
, HSZ hszService
, HSZ hszTopic
)
1732 WDML_SERVER
* pServer
;
1734 for (pServer
= pInstance
->servers
; pServer
!= NULL
; pServer
= pServer
->next
)
1736 if (hszService
== pServer
->hszService
)
1741 TRACE("Service name missing\n");
1745 /* ================================================================
1747 * Link (hot & warm) management
1749 * ================================================================ */
1751 /******************************************************************
1756 void WDML_AddLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1757 UINT wType
, HSZ hszItem
, UINT wFmt
)
1761 pLink
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK
));
1768 pLink
->hConv
= hConv
;
1769 pLink
->transactionType
= wType
;
1770 WDML_IncHSZ(pInstance
, pLink
->hszItem
= hszItem
);
1772 pLink
->next
= pInstance
->links
[side
];
1773 pInstance
->links
[side
] = pLink
;
1776 /******************************************************************
1781 void WDML_RemoveLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1782 HSZ hszItem
, UINT uFmt
)
1784 WDML_LINK
* pPrev
= NULL
;
1785 WDML_LINK
* pCurrent
= NULL
;
1787 pCurrent
= pInstance
->links
[side
];
1789 while (pCurrent
!= NULL
)
1791 if (pCurrent
->hConv
== hConv
&&
1792 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1793 pCurrent
->uFmt
== uFmt
)
1795 if (pCurrent
== pInstance
->links
[side
])
1797 pInstance
->links
[side
] = pCurrent
->next
;
1801 pPrev
->next
= pCurrent
->next
;
1804 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1805 HeapFree(GetProcessHeap(), 0, pCurrent
);
1810 pCurrent
= pCurrent
->next
;
1814 /* this function is called to remove all links related to the conv.
1815 It should be called from both client and server when terminating
1818 /******************************************************************
1819 * WDML_RemoveAllLinks
1823 void WDML_RemoveAllLinks(WDML_INSTANCE
* pInstance
, WDML_CONV
* pConv
, WDML_SIDE side
)
1825 WDML_LINK
* pPrev
= NULL
;
1826 WDML_LINK
* pCurrent
= NULL
;
1827 WDML_LINK
* pNext
= NULL
;
1829 pCurrent
= pInstance
->links
[side
];
1831 while (pCurrent
!= NULL
)
1833 if (pCurrent
->hConv
== (HCONV
)pConv
)
1835 if (pCurrent
== pInstance
->links
[side
])
1837 pInstance
->links
[side
] = pCurrent
->next
;
1838 pNext
= pCurrent
->next
;
1842 pPrev
->next
= pCurrent
->next
;
1843 pNext
= pCurrent
->next
;
1846 WDML_DecHSZ(pInstance
, pCurrent
->hszItem
);
1848 HeapFree(GetProcessHeap(), 0, pCurrent
);
1855 pCurrent
= pCurrent
->next
;
1864 /******************************************************************
1869 WDML_LINK
* WDML_FindLink(WDML_INSTANCE
* pInstance
, HCONV hConv
, WDML_SIDE side
,
1870 HSZ hszItem
, BOOL use_fmt
, UINT uFmt
)
1872 WDML_LINK
* pCurrent
= NULL
;
1874 for (pCurrent
= pInstance
->links
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
1876 /* we don't need to check for transaction type as it can be altered */
1878 if (pCurrent
->hConv
== hConv
&&
1879 DdeCmpStringHandles(pCurrent
->hszItem
, hszItem
) == 0 &&
1880 (!use_fmt
|| pCurrent
->uFmt
== uFmt
))
1890 /* ================================================================
1892 * Transaction management
1894 * ================================================================ */
1896 /******************************************************************
1897 * WDML_AllocTransaction
1899 * Alloc a transaction structure for handling the message ddeMsg
1901 WDML_XACT
* WDML_AllocTransaction(WDML_INSTANCE
* pInstance
, UINT ddeMsg
,
1902 UINT wFmt
, HSZ hszItem
)
1905 static WORD tid
= 1; /* FIXME: wrap around */
1907 pXAct
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT
));
1910 pInstance
->lastError
= DMLERR_MEMORY_ERROR
;
1914 pXAct
->xActID
= tid
++;
1915 pXAct
->ddeMsg
= ddeMsg
;
1916 pXAct
->hDdeData
= 0;
1921 if ((pXAct
->hszItem
= hszItem
)) WDML_IncHSZ(pInstance
, pXAct
->hszItem
);
1929 /******************************************************************
1930 * WDML_QueueTransaction
1932 * Adds a transaction to the list of transaction
1934 void WDML_QueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1938 /* advance to last in queue */
1939 for (pt
= &pConv
->transactions
; *pt
!= NULL
; pt
= &(*pt
)->next
);
1943 /******************************************************************
1944 * WDML_UnQueueTransaction
1948 BOOL
WDML_UnQueueTransaction(WDML_CONV
* pConv
, WDML_XACT
* pXAct
)
1952 for (pt
= &pConv
->transactions
; *pt
; pt
= &(*pt
)->next
)
1963 /******************************************************************
1964 * WDML_FreeTransaction
1968 void WDML_FreeTransaction(WDML_INSTANCE
* pInstance
, WDML_XACT
* pXAct
, BOOL doFreePmt
)
1970 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
1971 if (doFreePmt
&& (ULONG_PTR
)pXAct
->hMem
> 1)
1973 GlobalFree(pXAct
->hMem
);
1975 if (pXAct
->hszItem
) WDML_DecHSZ(pInstance
, pXAct
->hszItem
);
1977 HeapFree(GetProcessHeap(), 0, pXAct
);
1980 /******************************************************************
1981 * WDML_FindTransaction
1985 WDML_XACT
* WDML_FindTransaction(WDML_CONV
* pConv
, DWORD tid
)
1990 for (pXAct
= pConv
->transactions
; pXAct
; pXAct
= pXAct
->next
)
1992 if (pXAct
->xActID
== tid
)
1998 /* ================================================================
2000 * Conversation management
2002 * ================================================================ */
2004 /******************************************************************
2009 WDML_CONV
* WDML_AddConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2010 HSZ hszService
, HSZ hszTopic
, HWND hwndClient
, HWND hwndServer
)
2014 /* no conversation yet, add it */
2015 pConv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV
));
2016 if (!pConv
) return NULL
;
2018 pConv
->instance
= pInstance
;
2019 WDML_IncHSZ(pInstance
, pConv
->hszService
= hszService
);
2020 WDML_IncHSZ(pInstance
, pConv
->hszTopic
= hszTopic
);
2021 pConv
->magic
= WDML_CONV_MAGIC
;
2022 pConv
->hwndServer
= hwndServer
;
2023 pConv
->hwndClient
= hwndClient
;
2024 pConv
->transactions
= NULL
;
2026 pConv
->wStatus
= (side
== WDML_CLIENT_SIDE
) ? ST_CLIENT
: 0L;
2027 pConv
->wStatus
|= pInstance
->wStatus
;
2028 /* check if both side of the conversation are of the same instance */
2029 if (GetWindowThreadProcessId(hwndClient
, NULL
) == GetWindowThreadProcessId(hwndServer
, NULL
) &&
2030 WDML_GetInstanceFromWnd(hwndClient
) == WDML_GetInstanceFromWnd(hwndServer
))
2032 pConv
->wStatus
|= ST_ISSELF
;
2034 pConv
->wConvst
= XST_NULL
;
2036 pConv
->next
= pInstance
->convs
[side
];
2037 pInstance
->convs
[side
] = pConv
;
2039 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv
->wStatus
, pInstance
);
2044 /******************************************************************
2049 WDML_CONV
* WDML_FindConv(WDML_INSTANCE
* pInstance
, WDML_SIDE side
,
2050 HSZ hszService
, HSZ hszTopic
)
2052 WDML_CONV
* pCurrent
= NULL
;
2054 for (pCurrent
= pInstance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= pCurrent
->next
)
2056 if (DdeCmpStringHandles(pCurrent
->hszService
, hszService
) == 0 &&
2057 DdeCmpStringHandles(pCurrent
->hszTopic
, hszTopic
) == 0)
2066 /******************************************************************
2071 void WDML_RemoveConv(WDML_CONV
* pRef
, WDML_SIDE side
)
2073 WDML_CONV
* pPrev
= NULL
;
2074 WDML_CONV
* pCurrent
;
2076 WDML_XACT
* pXActNext
;
2082 /* remove any pending transaction */
2083 for (pXAct
= pRef
->transactions
; pXAct
!= NULL
; pXAct
= pXActNext
)
2085 pXActNext
= pXAct
->next
;
2086 WDML_FreeTransaction(pRef
->instance
, pXAct
, TRUE
);
2089 WDML_RemoveAllLinks(pRef
->instance
, pRef
, side
);
2091 /* FIXME: should we keep the window around ? it seems so (at least on client side
2092 * to let QueryConvInfo work after conv termination, but also to implement
2095 /* destroy conversation window, but first remove pConv from hWnd.
2096 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
2098 hWnd
= (side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
;
2099 SetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
, 0);
2101 DestroyWindow((side
== WDML_CLIENT_SIDE
) ? pRef
->hwndClient
: pRef
->hwndServer
);
2103 WDML_DecHSZ(pRef
->instance
, pRef
->hszService
);
2104 WDML_DecHSZ(pRef
->instance
, pRef
->hszTopic
);
2106 for (pCurrent
= pRef
->instance
->convs
[side
]; pCurrent
!= NULL
; pCurrent
= (pPrev
= pCurrent
)->next
)
2108 if (pCurrent
== pRef
)
2110 if (pCurrent
== pRef
->instance
->convs
[side
])
2112 pRef
->instance
->convs
[side
] = pCurrent
->next
;
2116 pPrev
->next
= pCurrent
->next
;
2118 pCurrent
->magic
= 0;
2119 HeapFree(GetProcessHeap(), 0, pCurrent
);
2125 /******************************************************************
2126 * WDML_EnableCallback
2128 static BOOL
WDML_EnableCallback(WDML_CONV
*pConv
, UINT wCmd
)
2130 if (wCmd
== EC_DISABLE
)
2132 pConv
->wStatus
|= ST_BLOCKED
;
2133 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
2137 if (wCmd
== EC_QUERYWAITING
)
2138 return pConv
->transactions
!= NULL
;
2140 if (wCmd
!= EC_ENABLEALL
&& wCmd
!= EC_ENABLEONE
)
2142 FIXME("Unknown command code %04x\n", wCmd
);
2146 if (wCmd
== EC_ENABLEALL
)
2148 pConv
->wStatus
&= ~ST_BLOCKED
;
2149 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv
, pConv
->wStatus
);
2152 while (pConv
->transactions
)
2154 WDML_XACT
*pXAct
= pConv
->transactions
;
2156 if (pConv
->wStatus
& ST_CLIENT
)
2158 /* transaction should be in the queue until handled */
2159 WDML_ClientHandle(pConv
, pXAct
, 0, NULL
);
2160 WDML_UnQueueTransaction(pConv
, pXAct
);
2164 /* transaction should be removed from the queue before handling */
2165 WDML_UnQueueTransaction(pConv
, pXAct
);
2166 WDML_ServerHandle(pConv
, pXAct
);
2169 WDML_FreeTransaction(pConv
->instance
, pXAct
, TRUE
);
2171 if (wCmd
== EC_ENABLEONE
) break;
2176 /*****************************************************************
2177 * DdeEnableCallback (USER32.@)
2179 BOOL WINAPI
DdeEnableCallback(DWORD idInst
, HCONV hConv
, UINT wCmd
)
2184 TRACE("(%d, %p, %04x)\n", idInst
, hConv
, wCmd
);
2188 pConv
= WDML_GetConv(hConv
, TRUE
);
2190 if (pConv
&& pConv
->instance
->instanceID
== idInst
)
2191 ret
= WDML_EnableCallback(pConv
, wCmd
);
2195 WDML_INSTANCE
*pInstance
= WDML_GetInstance(idInst
);
2200 TRACE("adding flags %04x to instance %p\n", wCmd
, pInstance
);
2201 pInstance
->wStatus
|= wCmd
;
2203 if (wCmd
== EC_DISABLE
)
2205 pInstance
->wStatus
|= ST_BLOCKED
;
2206 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
2208 else if (wCmd
== EC_ENABLEALL
)
2210 pInstance
->wStatus
&= ~ST_BLOCKED
;
2211 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance
, pInstance
->wStatus
);
2216 for (pConv
= pInstance
->convs
[WDML_CLIENT_SIDE
]; pConv
!= NULL
; pConv
= pConv
->next
)
2218 ret
= WDML_EnableCallback(pConv
, wCmd
);
2219 if (ret
&& wCmd
== EC_QUERYWAITING
) break;
2226 /******************************************************************
2231 WDML_CONV
* WDML_GetConv(HCONV hConv
, BOOL checkConnected
)
2233 WDML_CONV
* pConv
= (WDML_CONV
*)hConv
;
2235 /* FIXME: should do better checking */
2236 if (pConv
== NULL
|| pConv
->magic
!= WDML_CONV_MAGIC
) return NULL
;
2238 if (!pConv
->instance
)
2240 WARN("wrong thread ID, no instance\n");
2244 if (pConv
->instance
->threadID
!= GetCurrentThreadId())
2246 WARN("wrong thread ID\n");
2247 pConv
->instance
->lastError
= DMLERR_INVALIDPARAMETER
; /* FIXME: check */
2251 if (checkConnected
&& !(pConv
->wStatus
& ST_CONNECTED
))
2253 WARN("found conv but ain't connected\n");
2254 pConv
->instance
->lastError
= DMLERR_NO_CONV_ESTABLISHED
;
2261 /******************************************************************
2262 * WDML_GetConvFromWnd
2266 WDML_CONV
* WDML_GetConvFromWnd(HWND hWnd
)
2268 return (WDML_CONV
*)GetWindowLongPtrW(hWnd
, GWL_WDML_CONVERSATION
);
2271 /******************************************************************
2276 BOOL
WDML_PostAck(WDML_CONV
* pConv
, WDML_SIDE side
, WORD appRetCode
,
2277 BOOL fBusy
, BOOL fAck
, UINT_PTR pmt
, LPARAM lParam
, UINT oldMsg
)
2282 if (side
== WDML_SERVER_SIDE
)
2284 from
= pConv
->hwndServer
;
2285 to
= pConv
->hwndClient
;
2289 to
= pConv
->hwndServer
;
2290 from
= pConv
->hwndClient
;
2293 ddeAck
.bAppReturnCode
= appRetCode
;
2294 ddeAck
.reserved
= 0;
2295 ddeAck
.fBusy
= fBusy
;
2298 TRACE("Posting a %s ack\n", ddeAck
.fAck
? "positive" : "negative");
2300 lParam
= (lParam
) ? ReuseDDElParam(lParam
, oldMsg
, WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
) :
2301 PackDDElParam(WM_DDE_ACK
, *(WORD
*)&ddeAck
, pmt
);
2302 if (!PostMessageW(to
, WM_DDE_ACK
, (WPARAM
)from
, lParam
))
2304 pConv
->wStatus
&= ~ST_CONNECTED
;
2305 pConv
->instance
->lastError
= DMLERR_POSTMSG_FAILED
;
2306 FreeDDElParam(WM_DDE_ACK
, lParam
);
2312 /*****************************************************************
2313 * DdeSetUserHandle (USER32.@)
2315 BOOL WINAPI
DdeSetUserHandle(HCONV hConv
, DWORD id
, DWORD hUser
)
2319 pConv
= WDML_GetConv(hConv
, FALSE
);
2325 pConv
->hUser
= hUser
;
2331 pXAct
= WDML_FindTransaction(pConv
, id
);
2334 pXAct
->hUser
= hUser
;
2338 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2345 /******************************************************************
2346 * WDML_GetLocalConvInfo
2350 static BOOL
WDML_GetLocalConvInfo(WDML_CONV
* pConv
, CONVINFO
* ci
, DWORD id
)
2356 ci
->hConvPartner
= (pConv
->wStatus
& ST_ISLOCAL
) ? (HCONV
)((ULONG_PTR
)pConv
| 1) : 0;
2357 ci
->hszSvcPartner
= pConv
->hszService
;
2358 ci
->hszServiceReq
= pConv
->hszService
; /* FIXME: they shouldn't be the same, should they ? */
2359 ci
->hszTopic
= pConv
->hszTopic
;
2360 ci
->wStatus
= pConv
->wStatus
;
2362 side
= (pConv
->wStatus
& ST_CLIENT
) ? WDML_CLIENT_SIDE
: WDML_SERVER_SIDE
;
2364 for (pLink
= pConv
->instance
->links
[side
]; pLink
!= NULL
; pLink
= pLink
->next
)
2366 if (pLink
->hConv
== (HCONV
)pConv
)
2368 ci
->wStatus
|= ST_ADVISE
;
2373 /* FIXME: non handled status flags:
2379 ci
->wConvst
= pConv
->wConvst
; /* FIXME */
2381 ci
->wLastError
= 0; /* FIXME: note it's not the instance last error */
2383 ci
->ConvCtxt
= pConv
->convContext
;
2384 if (ci
->wStatus
& ST_CLIENT
)
2386 ci
->hwnd
= pConv
->hwndClient
;
2387 ci
->hwndPartner
= pConv
->hwndServer
;
2391 ci
->hwnd
= pConv
->hwndServer
;
2392 ci
->hwndPartner
= pConv
->hwndClient
;
2396 ci
->hUser
= pConv
->hUser
;
2405 pXAct
= WDML_FindTransaction(pConv
, id
);
2408 ci
->hUser
= pXAct
->hUser
;
2409 ci
->hszItem
= pXAct
->hszItem
;
2410 ci
->wFmt
= pXAct
->wFmt
;
2411 ci
->wType
= pXAct
->wType
;
2416 pConv
->instance
->lastError
= DMLERR_UNFOUND_QUEUE_ID
;
2422 /******************************************************************
2423 * DdeQueryConvInfo (USER32.@)
2425 * FIXME: Set last DDE error on failure.
2427 UINT WINAPI
DdeQueryConvInfo(HCONV hConv
, DWORD id
, PCONVINFO lpConvInfo
)
2429 UINT ret
= lpConvInfo
->cb
;
2433 TRACE("(%p,%x,%p)\n", hConv
, id
, lpConvInfo
);
2437 FIXME("hConv is NULL\n");
2441 pConv
= WDML_GetConv(hConv
, FALSE
);
2444 if (!WDML_GetLocalConvInfo(pConv
, &ci
, id
))
2449 if ((ULONG_PTR
)hConv
& 1)
2451 pConv
= WDML_GetConv((HCONV
)((ULONG_PTR
)hConv
& ~1), FALSE
);
2453 FIXME("Request on remote conversation information is not implemented yet\n");
2459 memcpy(lpConvInfo
, &ci
, min((size_t)lpConvInfo
->cb
, sizeof(ci
)));
2463 /* ================================================================
2465 * Information broadcast across DDEML implementations
2467 * ================================================================ */
2469 struct tagWDML_BroadcastPmt
2477 /******************************************************************
2478 * WDML_BroadcastEnumProc
2482 static BOOL CALLBACK
WDML_BroadcastEnumProc(HWND hWnd
, LPARAM lParam
)
2484 struct tagWDML_BroadcastPmt
* s
= (struct tagWDML_BroadcastPmt
*)lParam
;
2487 if (GetClassNameW(hWnd
, buffer
, 128) > 0 &&
2488 lstrcmpiW(buffer
, s
->clsName
) == 0)
2490 PostMessageW(hWnd
, s
->uMsg
, s
->wParam
, s
->lParam
);
2495 /******************************************************************
2496 * WDML_BroadcastDDEWindows
2500 void WDML_BroadcastDDEWindows(LPCWSTR clsName
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2502 struct tagWDML_BroadcastPmt s
;
2504 s
.clsName
= clsName
;
2508 EnumWindows(WDML_BroadcastEnumProc
, (LPARAM
)&s
);