wined3d: Pass a wined3d_device_context to wined3d_cs_emit_set_light_enable().
[wine.git] / dlls / user32 / dde_misc.c
blob41181d099cc0da97e1d9ebc4f3181831e7375606
1 /*
2 * DDEML library
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Len White
6 * Copyright 1999 Keith Matthews
7 * Copyright 2000 Corel
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 <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "dde.h"
34 #include "ddeml.h"
35 #include "win.h"
36 #include "dde_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
41 /* convert between ATOM and HSZ avoiding compiler warnings */
42 #define ATOM2HSZ(atom) ((HSZ) (ULONG_PTR)(atom))
43 #define HSZ2ATOM(hsz) ((ATOM) (ULONG_PTR)(hsz))
45 static WDML_INSTANCE* WDML_InstanceList = NULL;
46 static LONG WDML_MaxInstanceID = 0; /* OK for present, have to worry about wrap-around later */
47 const WCHAR WDML_szEventClass[] = L"WineDdeEventClass";
49 /* protection for instance list */
50 static CRITICAL_SECTION WDML_CritSect;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &WDML_CritSect,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": WDML_CritSect") }
57 static CRITICAL_SECTION WDML_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /* ================================================================
61 * Pure DDE (non DDEML) management
63 * ================================================================ */
66 /*****************************************************************
67 * PackDDElParam (USER32.@)
69 * RETURNS
70 * the packed lParam
72 LPARAM WINAPI PackDDElParam(UINT msg, UINT_PTR uiLo, UINT_PTR uiHi)
74 HGLOBAL hMem;
75 UINT_PTR *params;
77 switch (msg)
79 case WM_DDE_ACK:
80 case WM_DDE_ADVISE:
81 case WM_DDE_DATA:
82 case WM_DDE_POKE:
83 if (!(hMem = GlobalAlloc(GMEM_DDESHARE, sizeof(UINT_PTR) * 2)))
85 ERR("GlobalAlloc failed\n");
86 return 0;
88 if (!(params = GlobalLock(hMem)))
90 ERR("GlobalLock failed (%p)\n", hMem);
91 return 0;
93 params[0] = uiLo;
94 params[1] = uiHi;
95 GlobalUnlock(hMem);
96 return (LPARAM)hMem;
98 case WM_DDE_EXECUTE:
99 return uiHi;
101 default:
102 return MAKELONG(uiLo, uiHi);
107 /*****************************************************************
108 * UnpackDDElParam (USER32.@)
110 * RETURNS
111 * success: nonzero
112 * failure: zero
114 BOOL WINAPI UnpackDDElParam(UINT msg, LPARAM lParam,
115 PUINT_PTR uiLo, PUINT_PTR uiHi)
117 UINT_PTR *params;
119 switch (msg)
121 case WM_DDE_ACK:
122 case WM_DDE_ADVISE:
123 case WM_DDE_DATA:
124 case WM_DDE_POKE:
125 if (!lParam || !(params = GlobalLock((HGLOBAL)lParam)))
127 if (uiLo) *uiLo = 0;
128 if (uiHi) *uiHi = 0;
129 return FALSE;
131 if (uiLo) *uiLo = params[0];
132 if (uiHi) *uiHi = params[1];
133 GlobalUnlock( (HGLOBAL)lParam );
134 return TRUE;
136 case WM_DDE_EXECUTE:
137 if (uiLo) *uiLo = 0;
138 if (uiHi) *uiHi = lParam;
139 return TRUE;
141 default:
142 if (uiLo) *uiLo = LOWORD(lParam);
143 if (uiHi) *uiHi = HIWORD(lParam);
144 return TRUE;
149 /*****************************************************************
150 * FreeDDElParam (USER32.@)
152 * RETURNS
153 * success: nonzero
154 * failure: zero
156 BOOL WINAPI FreeDDElParam(UINT msg, LPARAM lParam)
158 switch (msg)
160 case WM_DDE_ACK:
161 case WM_DDE_ADVISE:
162 case WM_DDE_DATA:
163 case WM_DDE_POKE:
164 /* first check if it's a global handle */
165 if (!GlobalHandle( (LPVOID)lParam )) return TRUE;
166 return !GlobalFree( (HGLOBAL)lParam );
168 default:
169 return TRUE;
174 /*****************************************************************
175 * ReuseDDElParam (USER32.@)
177 * RETURNS
178 * the packed lParam
180 LPARAM WINAPI ReuseDDElParam(LPARAM lParam, UINT msgIn, UINT msgOut,
181 UINT_PTR uiLo, UINT_PTR uiHi)
183 UINT_PTR *params;
185 switch (msgIn)
187 case WM_DDE_ACK:
188 case WM_DDE_ADVISE:
189 case WM_DDE_DATA:
190 case WM_DDE_POKE:
191 switch(msgOut)
193 case WM_DDE_ACK:
194 case WM_DDE_ADVISE:
195 case WM_DDE_DATA:
196 case WM_DDE_POKE:
197 if (!lParam) return 0;
198 if (!(params = GlobalLock( (HGLOBAL)lParam )))
200 ERR("GlobalLock failed\n");
201 return 0;
203 params[0] = uiLo;
204 params[1] = uiHi;
205 TRACE("Reusing pack %08lx %08lx\n", uiLo, uiHi);
206 GlobalUnlock( (HGLOBAL)lParam );
207 return lParam;
209 case WM_DDE_EXECUTE:
210 FreeDDElParam( msgIn, lParam );
211 return uiHi;
213 default:
214 FreeDDElParam( msgIn, lParam );
215 return MAKELPARAM(uiLo, uiHi);
218 default:
219 return PackDDElParam( msgOut, uiLo, uiHi );
223 /*****************************************************************
224 * ImpersonateDdeClientWindow (USER32.@)
226 * PARAMS
227 * hWndClient [I] handle to DDE client window
228 * hWndServer [I] handle to DDE server window
230 BOOL WINAPI ImpersonateDdeClientWindow(HWND hWndClient, HWND hWndServer)
232 FIXME("(%p %p): stub\n", hWndClient, hWndServer);
233 return FALSE;
236 /*****************************************************************
237 * DdeSetQualityOfService (USER32.@)
240 BOOL WINAPI DdeSetQualityOfService(HWND hwndClient, const SECURITY_QUALITY_OF_SERVICE *pqosNew,
241 PSECURITY_QUALITY_OF_SERVICE pqosPrev)
243 FIXME("(%p %p %p): stub\n", hwndClient, pqosNew, pqosPrev);
244 return TRUE;
247 /* ================================================================
249 * WDML Error management
251 * ================================================================ */
253 /******************************************************************************
254 * DdeGetLastError [USER32.@] Gets most recent error code
256 * PARAMS
257 * idInst [I] Instance identifier
259 * RETURNS
260 * Last error code
262 UINT WINAPI DdeGetLastError(DWORD idInst)
264 DWORD error_code;
265 WDML_INSTANCE* pInstance;
267 /* First check instance
269 pInstance = WDML_GetInstance(idInst);
270 if (pInstance == NULL)
272 error_code = DMLERR_INVALIDPARAMETER;
274 else
276 error_code = pInstance->lastError;
277 pInstance->lastError = 0;
280 return error_code;
283 /******************************************************************
284 * WDML_SetAllLastError
288 static void WDML_SetAllLastError(DWORD lastError)
290 DWORD threadID;
291 WDML_INSTANCE* pInstance;
292 threadID = GetCurrentThreadId();
293 pInstance = WDML_InstanceList;
294 while (pInstance)
296 if (pInstance->threadID == threadID)
297 pInstance->lastError = lastError;
298 pInstance = pInstance->next;
302 /* ================================================================
304 * String management
306 * ================================================================ */
309 /******************************************************************
310 * WDML_FindNode
314 static HSZNode* WDML_FindNode(WDML_INSTANCE* pInstance, HSZ hsz)
316 HSZNode* pNode;
318 if (pInstance == NULL) return NULL;
320 for (pNode = pInstance->nodeList; pNode != NULL; pNode = pNode->next)
322 if (pNode->hsz == hsz) break;
324 if (!pNode) WARN("HSZ %p not found\n", hsz);
325 return pNode;
328 /******************************************************************
329 * WDML_MakeAtomFromHsz
331 * Creates a global atom from an existing HSZ
332 * Generally used before sending an HSZ as an atom to a remote app
334 ATOM WDML_MakeAtomFromHsz(HSZ hsz)
336 WCHAR nameBuffer[MAX_BUFFER_LEN];
338 if (GetAtomNameW(HSZ2ATOM(hsz), nameBuffer, MAX_BUFFER_LEN))
339 return GlobalAddAtomW(nameBuffer);
340 WARN("HSZ %p not found\n", hsz);
341 return 0;
344 /******************************************************************
345 * WDML_MakeHszFromAtom
347 * Creates a HSZ from an existing global atom
348 * Generally used while receiving a global atom and transforming it
349 * into an HSZ
351 HSZ WDML_MakeHszFromAtom(const WDML_INSTANCE* pInstance, ATOM atom)
353 WCHAR nameBuffer[MAX_BUFFER_LEN];
355 if (!atom) return NULL;
357 if (GlobalGetAtomNameW(atom, nameBuffer, MAX_BUFFER_LEN))
359 TRACE("%x => %s\n", atom, debugstr_w(nameBuffer));
360 return DdeCreateStringHandleW(pInstance->instanceID, nameBuffer, CP_WINUNICODE);
362 WARN("ATOM 0x%x not found\n", atom);
363 return 0;
366 /******************************************************************
367 * WDML_IncHSZ
371 BOOL WDML_IncHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
373 HSZNode* pNode;
375 pNode = WDML_FindNode(pInstance, hsz);
376 if (!pNode) return FALSE;
378 pNode->refCount++;
379 return TRUE;
382 /******************************************************************************
383 * WDML_DecHSZ (INTERNAL)
385 * Decrease the ref count of an HSZ. If it reaches 0, the node is removed from the list
386 * of HSZ nodes
387 * Returns -1 is the HSZ isn't found, otherwise it's the current (after --) of the ref count
389 BOOL WDML_DecHSZ(WDML_INSTANCE* pInstance, HSZ hsz)
391 HSZNode* pPrev = NULL;
392 HSZNode* pCurrent;
394 for (pCurrent = pInstance->nodeList; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
396 /* If we found the node we were looking for and its ref count is one,
397 * we can remove it
399 if (pCurrent->hsz == hsz)
401 if (--pCurrent->refCount == 0)
403 if (pCurrent == pInstance->nodeList)
405 pInstance->nodeList = pCurrent->next;
407 else
409 pPrev->next = pCurrent->next;
411 HeapFree(GetProcessHeap(), 0, pCurrent);
412 DeleteAtom(HSZ2ATOM(hsz));
414 return TRUE;
417 WARN("HSZ %p not found\n", hsz);
419 return FALSE;
422 /******************************************************************************
423 * WDML_FreeAllHSZ (INTERNAL)
425 * Frees up all the strings still allocated in the list and
426 * remove all the nodes from the list of HSZ nodes.
428 static void WDML_FreeAllHSZ(WDML_INSTANCE* pInstance)
430 /* Free any strings created in this instance.
432 while (pInstance->nodeList != NULL)
434 DdeFreeStringHandle(pInstance->instanceID, pInstance->nodeList->hsz);
438 /******************************************************************************
439 * InsertHSZNode (INTERNAL)
441 * Insert a node to the head of the list.
443 static void WDML_InsertHSZNode(WDML_INSTANCE* pInstance, HSZ hsz)
445 if (hsz != 0)
447 HSZNode* pNew = NULL;
448 /* Create a new node for this HSZ.
450 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(HSZNode));
451 if (pNew != NULL)
453 pNew->hsz = hsz;
454 pNew->next = pInstance->nodeList;
455 pNew->refCount = 1;
456 pInstance->nodeList = pNew;
458 else
460 ERR("Primary HSZ Node allocation failed - out of memory\n");
465 /******************************************************************
466 * WDML_QueryString
470 static int WDML_QueryString(WDML_INSTANCE* pInstance, HSZ hsz, LPVOID ptr, DWORD cchMax,
471 int codepage)
473 WCHAR pString[MAX_BUFFER_LEN];
474 int ret;
475 /* If psz is null, we have to return only the length
476 * of the string.
478 if (ptr == NULL)
480 ptr = pString;
481 cchMax = MAX_BUFFER_LEN;
484 /* if there is no input windows returns a NULL string */
485 if (hsz == NULL)
487 CHAR *t_ptr = ptr;
488 *t_ptr = '\0';
489 return 1;
492 switch (codepage)
494 case CP_WINANSI:
495 ret = GetAtomNameA(HSZ2ATOM(hsz), ptr, cchMax);
496 break;
497 case CP_WINUNICODE:
498 ret = GetAtomNameW(HSZ2ATOM(hsz), ptr, cchMax);
499 break;
500 default:
501 ERR("Unknown code page %d\n", codepage);
502 ret = 0;
504 return ret;
507 /*****************************************************************
508 * DdeQueryStringA [USER32.@]
510 DWORD WINAPI DdeQueryStringA(DWORD idInst, HSZ hsz, LPSTR psz, DWORD cchMax, INT iCodePage)
512 DWORD ret = 0;
513 WDML_INSTANCE* pInstance;
515 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
517 /* First check instance
519 pInstance = WDML_GetInstance(idInst);
520 if (pInstance != NULL)
522 if (iCodePage == 0) iCodePage = CP_WINANSI;
523 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
526 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
527 return ret;
530 /*****************************************************************
531 * DdeQueryStringW [USER32.@]
534 DWORD WINAPI DdeQueryStringW(DWORD idInst, HSZ hsz, LPWSTR psz, DWORD cchMax, INT iCodePage)
536 DWORD ret = 0;
537 WDML_INSTANCE* pInstance;
539 TRACE("(%d, %p, %p, %d, %d)\n", idInst, hsz, psz, cchMax, iCodePage);
541 /* First check instance
543 pInstance = WDML_GetInstance(idInst);
544 if (pInstance != NULL)
546 if (iCodePage == 0) iCodePage = CP_WINUNICODE;
547 ret = WDML_QueryString(pInstance, hsz, psz, cchMax, iCodePage);
550 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
551 return ret;
554 /******************************************************************
555 * DML_CreateString
559 static HSZ WDML_CreateString(WDML_INSTANCE* pInstance, LPCVOID ptr, int codepage)
561 HSZ hsz;
563 switch (codepage)
565 case CP_WINANSI:
566 hsz = ATOM2HSZ(AddAtomA(ptr));
567 TRACE("added atom %s with HSZ %p,\n", debugstr_a(ptr), hsz);
568 break;
569 case CP_WINUNICODE:
570 hsz = ATOM2HSZ(AddAtomW(ptr));
571 TRACE("added atom %s with HSZ %p,\n", debugstr_w(ptr), hsz);
572 break;
573 default:
574 ERR("Unknown code page %d\n", codepage);
575 return 0;
577 WDML_InsertHSZNode(pInstance, hsz);
578 return hsz;
581 /*****************************************************************
582 * DdeCreateStringHandleA [USER32.@]
584 * See DdeCreateStringHandleW.
586 HSZ WINAPI DdeCreateStringHandleA(DWORD idInst, LPCSTR psz, INT codepage)
588 HSZ hsz = 0;
589 WDML_INSTANCE* pInstance;
591 TRACE("(%d,%s,%d)\n", idInst, debugstr_a(psz), codepage);
593 pInstance = WDML_GetInstance(idInst);
594 if (pInstance == NULL)
595 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
596 else
598 if (codepage == 0) codepage = CP_WINANSI;
599 hsz = WDML_CreateString(pInstance, psz, codepage);
602 return hsz;
606 /******************************************************************************
607 * DdeCreateStringHandleW [USER32.@] Creates handle to identify string
609 * PARAMS
610 * idInst [I] Instance identifier
611 * psz [I] Pointer to string
612 * codepage [I] Code page identifier
613 * RETURNS
614 * Success: String handle
615 * Failure: 0
617 HSZ WINAPI DdeCreateStringHandleW(DWORD idInst, LPCWSTR psz, INT codepage)
619 WDML_INSTANCE* pInstance;
620 HSZ hsz = 0;
622 pInstance = WDML_GetInstance(idInst);
623 if (pInstance == NULL)
624 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
625 else
627 if (codepage == 0) codepage = CP_WINUNICODE;
628 hsz = WDML_CreateString(pInstance, psz, codepage);
631 return hsz;
634 /*****************************************************************
635 * DdeFreeStringHandle (USER32.@)
636 * RETURNS
637 * success: nonzero
638 * fail: zero
640 BOOL WINAPI DdeFreeStringHandle(DWORD idInst, HSZ hsz)
642 WDML_INSTANCE* pInstance;
643 BOOL ret = FALSE;
645 TRACE("(%d,%p):\n", idInst, hsz);
647 /* First check instance
649 pInstance = WDML_GetInstance(idInst);
650 if (pInstance)
651 ret = WDML_DecHSZ(pInstance, hsz);
653 return ret;
656 /*****************************************************************
657 * DdeKeepStringHandle (USER32.@)
659 * RETURNS
660 * success: nonzero
661 * fail: zero
663 BOOL WINAPI DdeKeepStringHandle(DWORD idInst, HSZ hsz)
665 WDML_INSTANCE* pInstance;
666 BOOL ret = FALSE;
668 TRACE("(%d,%p):\n", idInst, hsz);
670 /* First check instance
672 pInstance = WDML_GetInstance(idInst);
673 if (pInstance)
674 ret = WDML_IncHSZ(pInstance, hsz);
676 return ret;
679 /*****************************************************************
680 * DdeCmpStringHandles (USER32.@)
682 * Compares the value of two string handles. This comparison is
683 * not case sensitive.
685 * PARAMS
686 * hsz1 [I] Handle to the first string
687 * hsz2 [I] Handle to the second string
689 * RETURNS
690 * -1 The value of hsz1 is zero or less than hsz2
691 * 0 The values of hsz 1 and 2 are the same or both zero.
692 * 1 The value of hsz2 is zero of less than hsz1
694 INT WINAPI DdeCmpStringHandles(HSZ hsz1, HSZ hsz2)
696 WCHAR psz1[MAX_BUFFER_LEN];
697 WCHAR psz2[MAX_BUFFER_LEN];
698 int ret = 0;
699 int ret1, ret2;
701 ret1 = GetAtomNameW(HSZ2ATOM(hsz1), psz1, MAX_BUFFER_LEN);
702 ret2 = GetAtomNameW(HSZ2ATOM(hsz2), psz2, MAX_BUFFER_LEN);
704 TRACE("(%p<%s> %p<%s>);\n", hsz1, debugstr_w(psz1), hsz2, debugstr_w(psz2));
706 /* Make sure we found both strings. */
707 if (ret1 == 0 && ret2 == 0)
709 /* If both are not found, return both "zero strings". */
710 ret = 0;
712 else if (ret1 == 0)
714 /* If hsz1 is a not found, return hsz1 is "zero string". */
715 ret = -1;
717 else if (ret2 == 0)
719 /* If hsz2 is a not found, return hsz2 is "zero string". */
720 ret = 1;
722 else
724 /* Compare the two strings we got (case insensitive). */
725 ret = lstrcmpiW(psz1, psz2);
726 /* Since strcmp returns any number smaller than
727 * 0 when the first string is found to be less than
728 * the second one we must make sure we are returning
729 * the proper values.
731 if (ret < 0)
733 ret = -1;
735 else if (ret > 0)
737 ret = 1;
741 return ret;
744 /* ================================================================
746 * Instance management
748 * ================================================================ */
750 /******************************************************************************
751 * IncrementInstanceId
753 * generic routine to increment the max instance Id and allocate a new application instance
755 static void WDML_IncrementInstanceId(WDML_INSTANCE* pInstance)
757 DWORD id = InterlockedIncrement(&WDML_MaxInstanceID);
759 pInstance->instanceID = id;
760 TRACE("New instance id %d allocated\n", id);
763 /******************************************************************
764 * WDML_EventProc
768 static LRESULT CALLBACK WDML_EventProc(HWND hwndEvent, UINT uMsg, WPARAM wParam, LPARAM lParam)
770 WDML_INSTANCE* pInstance;
771 HSZ hsz1, hsz2;
773 switch (uMsg)
775 case WM_WDML_REGISTER:
776 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
777 /* try calling the Callback */
778 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_REGISTRATIONS))
780 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
781 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
782 WDML_InvokeCallback(pInstance, XTYP_REGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
783 WDML_DecHSZ(pInstance, hsz1);
784 WDML_DecHSZ(pInstance, hsz2);
786 break;
788 case WM_WDML_UNREGISTER:
789 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
790 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_UNREGISTRATIONS))
792 hsz1 = WDML_MakeHszFromAtom(pInstance, wParam);
793 hsz2 = WDML_MakeHszFromAtom(pInstance, lParam);
794 WDML_InvokeCallback(pInstance, XTYP_UNREGISTER, 0, 0, hsz1, hsz2, 0, 0, 0);
795 WDML_DecHSZ(pInstance, hsz1);
796 WDML_DecHSZ(pInstance, hsz2);
798 break;
800 case WM_WDML_CONNECT_CONFIRM:
801 pInstance = WDML_GetInstanceFromWnd(hwndEvent);
802 if (pInstance && !(pInstance->CBFflags & CBF_SKIP_CONNECT_CONFIRMS))
804 WDML_CONV* pConv;
805 /* confirm connection...
806 * lookup for this conv handle
808 HWND client = WIN_GetFullHandle( (HWND)wParam );
809 HWND server = WIN_GetFullHandle( (HWND)lParam );
810 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConv->next)
812 if (pConv->hwndClient == client && pConv->hwndServer == server)
813 break;
815 if (pConv)
817 pConv->wStatus |= ST_ISLOCAL;
819 WDML_InvokeCallback(pInstance, XTYP_CONNECT_CONFIRM, 0, (HCONV)pConv,
820 pConv->hszTopic, pConv->hszService, 0, 0,
821 (pConv->wStatus & ST_ISSELF) ? 1 : 0);
824 break;
825 default:
826 return DefWindowProcW(hwndEvent, uMsg, wParam, lParam);
828 return 0;
831 /******************************************************************
832 * WDML_Initialize
836 static UINT WDML_Initialize(LPDWORD pidInst, PFNCALLBACK pfnCallback,
837 DWORD afCmd, DWORD ulRes, BOOL bUnicode)
839 WDML_INSTANCE* pInstance;
840 WDML_INSTANCE* reference_inst;
841 UINT ret;
842 WNDCLASSEXW wndclass;
844 TRACE("(%p,%p,0x%x,%d,0x%x)\n",
845 pidInst, pfnCallback, afCmd, ulRes, bUnicode);
847 if (ulRes)
849 ERR("Reserved value not zero? What does this mean?\n");
850 /* trap this and no more until we know more */
851 return DMLERR_NO_ERROR;
854 /* grab enough heap for one control struct - not really necessary for re-initialise
855 * but allows us to use same validation routines */
856 pInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_INSTANCE));
857 if (pInstance == NULL)
859 /* catastrophe !! warn user & abort */
860 ERR("Instance create failed - out of memory\n");
861 return DMLERR_SYS_ERROR;
863 pInstance->next = NULL;
864 pInstance->monitor = (afCmd | APPCLASS_MONITOR);
866 /* messy bit, spec implies that 'Client Only' can be set in 2 different ways, catch 1 here */
868 pInstance->clientOnly = afCmd & APPCMD_CLIENTONLY;
869 pInstance->instanceID = *pidInst; /* May need to add calling proc Id */
870 pInstance->threadID = GetCurrentThreadId();
871 pInstance->callback = *pfnCallback;
872 pInstance->unicode = bUnicode;
873 pInstance->nodeList = NULL; /* node will be added later */
874 pInstance->monitorFlags = afCmd & MF_MASK;
875 pInstance->wStatus = 0;
876 pInstance->lastError = DMLERR_NO_ERROR;
877 pInstance->servers = NULL;
878 pInstance->convs[0] = NULL;
879 pInstance->convs[1] = NULL;
880 pInstance->links[0] = NULL;
881 pInstance->links[1] = NULL;
883 /* isolate CBF flags in one go, expect this will go the way of all attempts to be clever !! */
885 pInstance->CBFflags = afCmd^((afCmd&MF_MASK)|((afCmd&APPCMD_MASK)|(afCmd&APPCLASS_MASK)));
887 if (!pInstance->clientOnly)
889 /* Check for other way of setting Client-only !! */
890 pInstance->clientOnly =
891 (pInstance->CBFflags & CBF_FAIL_ALLSVRXACTIONS) == CBF_FAIL_ALLSVRXACTIONS;
894 TRACE("instance created - checking validity\n");
896 if (*pidInst == 0)
898 /* Initialisation of new Instance Identifier */
899 TRACE("new instance, callback %p flags %X\n",pfnCallback,afCmd);
901 EnterCriticalSection(&WDML_CritSect);
903 if (WDML_InstanceList == NULL)
905 /* can't be another instance in this case, assign to the base pointer */
906 WDML_InstanceList = pInstance;
908 /* since first must force filter of XTYP_CONNECT and XTYP_WILDCONNECT for
909 * present
910 * ------------------------------- NOTE NOTE NOTE --------------------------
912 * the manual is not clear if this condition
913 * applies to the first call to DdeInitialize from an application, or the
914 * first call for a given callback !!!
917 pInstance->CBFflags = pInstance->CBFflags|APPCMD_FILTERINITS;
918 TRACE("First application instance detected OK\n");
919 /* allocate new instance ID */
920 WDML_IncrementInstanceId(pInstance);
922 else
924 /* really need to chain the new one in to the latest here, but after checking conditions
925 * such as trying to start a conversation from an application trying to monitor */
926 reference_inst = WDML_InstanceList;
927 TRACE("Subsequent application instance - starting checks\n");
928 while (reference_inst->next != NULL)
931 * This set of tests will work if application uses same instance Id
932 * at application level once allocated - which is what manual implies
933 * should happen. If someone tries to be
934 * clever (lazy ?) it will fail to pick up that later calls are for
935 * the same application - should we trust them ?
937 if (pInstance->instanceID == reference_inst->instanceID)
939 /* Check 1 - must be same Client-only state */
941 if (pInstance->clientOnly != reference_inst->clientOnly)
943 ret = DMLERR_DLL_USAGE;
944 goto theError;
947 /* Check 2 - cannot use 'Monitor' with any non-monitor modes */
949 if (pInstance->monitor != reference_inst->monitor)
951 ret = DMLERR_INVALIDPARAMETER;
952 goto theError;
955 /* Check 3 - must supply different callback address */
957 if (pInstance->callback == reference_inst->callback)
959 ret = DMLERR_DLL_USAGE;
960 goto theError;
963 reference_inst = reference_inst->next;
965 /* All cleared, add to chain */
967 TRACE("Application Instance checks finished\n");
968 WDML_IncrementInstanceId(pInstance);
969 reference_inst->next = pInstance;
971 LeaveCriticalSection(&WDML_CritSect);
973 *pidInst = pInstance->instanceID;
975 /* for deadlock issues, windows must always be created when outside the critical section */
976 wndclass.cbSize = sizeof(wndclass);
977 wndclass.style = 0;
978 wndclass.lpfnWndProc = WDML_EventProc;
979 wndclass.cbClsExtra = 0;
980 wndclass.cbWndExtra = sizeof(ULONG_PTR);
981 wndclass.hInstance = 0;
982 wndclass.hIcon = 0;
983 wndclass.hCursor = 0;
984 wndclass.hbrBackground = 0;
985 wndclass.lpszMenuName = NULL;
986 wndclass.lpszClassName = WDML_szEventClass;
987 wndclass.hIconSm = 0;
989 RegisterClassExW(&wndclass);
991 pInstance->hwndEvent = CreateWindowW(WDML_szEventClass, NULL,
992 WS_POPUP, 0, 0, 0, 0,
993 0, 0, 0, 0);
995 SetWindowLongPtrW(pInstance->hwndEvent, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance);
997 TRACE("New application instance processing finished OK\n");
999 else
1001 /* Reinitialisation situation --- FIX */
1002 TRACE("reinitialisation of (%p,%p,0x%x,%d): stub\n", pidInst, pfnCallback, afCmd, ulRes);
1004 EnterCriticalSection(&WDML_CritSect);
1006 if (WDML_InstanceList == NULL)
1008 ret = DMLERR_INVALIDPARAMETER;
1009 goto theError;
1011 /* can't reinitialise if we have initialised nothing !! */
1012 reference_inst = WDML_InstanceList;
1013 /* must first check if we have been given a valid instance to re-initialise !! how do we do that ? */
1015 * MS allows initialisation without specifying a callback, should we allow addition of the
1016 * callback by a later call to initialise ? - if so this lot will have to change
1018 while (reference_inst->next != NULL)
1020 if (*pidInst == reference_inst->instanceID && pfnCallback == reference_inst->callback)
1022 /* Check 1 - cannot change client-only mode if set via APPCMD_CLIENTONLY */
1024 if (reference_inst->clientOnly)
1026 if ((reference_inst->CBFflags & CBF_FAIL_ALLSVRXACTIONS) != CBF_FAIL_ALLSVRXACTIONS)
1028 /* i.e. Was set to Client-only and through APPCMD_CLIENTONLY */
1030 if (!(afCmd & APPCMD_CLIENTONLY))
1032 ret = DMLERR_INVALIDPARAMETER;
1033 goto theError;
1037 /* Check 2 - cannot change monitor modes */
1039 if (pInstance->monitor != reference_inst->monitor)
1041 ret = DMLERR_INVALIDPARAMETER;
1042 goto theError;
1045 /* Check 3 - trying to set Client-only via APPCMD when not set so previously */
1047 if ((afCmd&APPCMD_CLIENTONLY) && !reference_inst->clientOnly)
1049 ret = DMLERR_INVALIDPARAMETER;
1050 goto theError;
1052 break;
1054 reference_inst = reference_inst->next;
1056 if (reference_inst->next == NULL)
1058 ret = DMLERR_INVALIDPARAMETER;
1059 goto theError;
1061 /* All checked - change relevant flags */
1063 reference_inst->CBFflags = pInstance->CBFflags;
1064 reference_inst->clientOnly = pInstance->clientOnly;
1065 reference_inst->monitorFlags = pInstance->monitorFlags;
1067 HeapFree(GetProcessHeap(), 0, pInstance); /* finished - release heap space used as work store */
1069 LeaveCriticalSection(&WDML_CritSect);
1072 return DMLERR_NO_ERROR;
1073 theError:
1074 HeapFree(GetProcessHeap(), 0, pInstance);
1075 LeaveCriticalSection(&WDML_CritSect);
1076 return ret;
1079 /******************************************************************************
1080 * DdeInitializeA (USER32.@)
1082 * See DdeInitializeW.
1084 UINT WINAPI DdeInitializeA(LPDWORD pidInst, PFNCALLBACK pfnCallback,
1085 DWORD afCmd, DWORD ulRes)
1087 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, FALSE);
1090 /******************************************************************************
1091 * DdeInitializeW [USER32.@]
1092 * Registers an application with the DDEML
1094 * PARAMS
1095 * pidInst [I] Pointer to instance identifier
1096 * pfnCallback [I] Pointer to callback function
1097 * afCmd [I] Set of command and filter flags
1098 * ulRes [I] Reserved
1100 * RETURNS
1101 * Success: DMLERR_NO_ERROR
1102 * Failure: DMLERR_DLL_USAGE, DMLERR_INVALIDPARAMETER, DMLERR_SYS_ERROR
1104 UINT WINAPI DdeInitializeW(LPDWORD pidInst, PFNCALLBACK pfnCallback,
1105 DWORD afCmd, DWORD ulRes)
1107 return WDML_Initialize(pidInst, pfnCallback, afCmd, ulRes, TRUE);
1110 /*****************************************************************
1111 * DdeUninitialize [USER32.@] Frees DDEML resources
1113 * PARAMS
1114 * idInst [I] Instance identifier
1116 * RETURNS
1117 * Success: TRUE
1118 * Failure: FALSE
1121 BOOL WINAPI DdeUninitialize(DWORD idInst)
1123 /* Stage one - check if we have a handle for this instance
1125 WDML_INSTANCE* pInstance;
1126 WDML_CONV* pConv;
1127 WDML_CONV* pConvNext;
1129 TRACE("(%d)\n", idInst);
1131 /* First check instance
1133 pInstance = WDML_GetInstance(idInst);
1134 if (pInstance == NULL)
1137 * Needs something here to record NOT_INITIALIZED ready for DdeGetLastError
1139 return FALSE;
1142 /* first terminate all conversations client side
1143 * this shall close existing links...
1145 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConvNext)
1147 pConvNext = pConv->next;
1148 DdeDisconnect((HCONV)pConv);
1150 if (pInstance->convs[WDML_CLIENT_SIDE])
1151 FIXME("still pending conversations\n");
1153 /* then unregister all known service names */
1154 DdeNameService(idInst, 0, 0, DNS_UNREGISTER);
1156 /* Free the nodes that were not freed by this instance
1157 * and remove the nodes from the list of HSZ nodes.
1159 WDML_FreeAllHSZ(pInstance);
1161 DestroyWindow(pInstance->hwndEvent);
1163 /* OK now delete the instance handle itself */
1165 if (WDML_InstanceList == pInstance)
1167 /* special case - the first/only entry */
1168 WDML_InstanceList = pInstance->next;
1170 else
1172 /* general case, remove entry */
1173 WDML_INSTANCE* inst;
1175 for (inst = WDML_InstanceList; inst->next != pInstance; inst = inst->next);
1176 inst->next = pInstance->next;
1178 /* release the heap entry
1180 HeapFree(GetProcessHeap(), 0, pInstance);
1182 return TRUE;
1185 /******************************************************************
1186 * WDML_NotifyThreadExit
1190 void WDML_NotifyThreadDetach(void)
1192 WDML_INSTANCE* pInstance;
1193 WDML_INSTANCE* next;
1194 DWORD tid = GetCurrentThreadId();
1196 EnterCriticalSection(&WDML_CritSect);
1197 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = next)
1199 next = pInstance->next;
1200 if (pInstance->threadID == tid)
1202 LeaveCriticalSection(&WDML_CritSect);
1203 DdeUninitialize(pInstance->instanceID);
1204 EnterCriticalSection(&WDML_CritSect);
1207 LeaveCriticalSection(&WDML_CritSect);
1210 /******************************************************************
1211 * WDML_InvokeCallback
1215 HDDEDATA WDML_InvokeCallback(WDML_INSTANCE* pInstance, UINT uType, UINT uFmt, HCONV hConv,
1216 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
1217 ULONG_PTR dwData1, ULONG_PTR dwData2)
1219 HDDEDATA ret;
1221 if (pInstance == NULL)
1222 return NULL;
1224 TRACE("invoking CB[%p] (%x %x %p %p %p %p %lx %lx)\n",
1225 pInstance->callback, uType, uFmt,
1226 hConv, hsz1, hsz2, hdata, dwData1, dwData2);
1227 ret = pInstance->callback(uType, uFmt, hConv, hsz1, hsz2, hdata, dwData1, dwData2);
1228 TRACE("done => %p\n", ret);
1229 return ret;
1232 /*****************************************************************************
1233 * WDML_GetInstance
1235 * generic routine to return a pointer to the relevant DDE_HANDLE_ENTRY
1236 * for an instance Id, or NULL if the entry does not exist
1239 WDML_INSTANCE* WDML_GetInstance(DWORD instId)
1241 WDML_INSTANCE* pInstance;
1243 EnterCriticalSection(&WDML_CritSect);
1245 for (pInstance = WDML_InstanceList; pInstance != NULL; pInstance = pInstance->next)
1247 if (pInstance->instanceID == instId)
1249 if (GetCurrentThreadId() != pInstance->threadID)
1251 FIXME("Tried to get instance from wrong thread\n");
1252 continue;
1254 break;
1258 LeaveCriticalSection(&WDML_CritSect);
1260 if (!pInstance)
1261 WARN("Instance entry missing for id %04x\n", instId);
1262 return pInstance;
1265 /******************************************************************
1266 * WDML_GetInstanceFromWnd
1270 WDML_INSTANCE* WDML_GetInstanceFromWnd(HWND hWnd)
1272 return (WDML_INSTANCE*)GetWindowLongPtrW(hWnd, GWL_WDML_INSTANCE);
1275 /* ================================================================
1277 * Data handle management
1279 * ================================================================ */
1281 /*****************************************************************
1282 * DdeCreateDataHandle (USER32.@)
1284 HDDEDATA WINAPI DdeCreateDataHandle(DWORD idInst, LPBYTE pSrc, DWORD cb, DWORD cbOff,
1285 HSZ hszItem, UINT wFmt, UINT afCmd)
1288 /* Other than check for validity we will ignore for now idInst, hszItem.
1289 * The purpose of these arguments still need to be investigated.
1292 WDML_INSTANCE* pInstance;
1293 HGLOBAL hMem;
1294 LPBYTE pByte;
1295 DDE_DATAHANDLE_HEAD* pDdh;
1296 WCHAR psz[MAX_BUFFER_LEN];
1298 pInstance = WDML_GetInstance(idInst);
1299 if (pInstance == NULL)
1301 WDML_SetAllLastError(DMLERR_INVALIDPARAMETER);
1302 return NULL;
1305 if (!GetAtomNameW(HSZ2ATOM(hszItem), psz, MAX_BUFFER_LEN))
1307 psz[0] = HSZ2ATOM(hszItem);
1308 psz[1] = 0;
1311 TRACE("(%d,%p,cb %d, cbOff %d,%p <%s>,fmt %04x,%x)\n",
1312 idInst, pSrc, cb, cbOff, hszItem, debugstr_w(psz), wFmt, afCmd);
1314 if (afCmd != 0 && afCmd != HDATA_APPOWNED)
1315 return 0;
1317 /* we use the first 4 bytes to store the size */
1318 if (!(hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cb + cbOff + sizeof(DDE_DATAHANDLE_HEAD))))
1320 ERR("GlobalAlloc failed\n");
1321 return 0;
1324 pDdh = GlobalLock(hMem);
1325 if (!pDdh)
1327 GlobalFree(hMem);
1328 return 0;
1331 pDdh->cfFormat = wFmt;
1332 pDdh->bAppOwned = (afCmd == HDATA_APPOWNED);
1334 pByte = (LPBYTE)(pDdh + 1);
1335 if (pSrc)
1337 memcpy(pByte, pSrc + cbOff, cb);
1339 GlobalUnlock(hMem);
1341 TRACE("=> %p\n", hMem);
1342 return hMem;
1345 /*****************************************************************
1347 * DdeAddData (USER32.@)
1349 HDDEDATA WINAPI DdeAddData(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
1351 DWORD old_sz, new_sz;
1352 LPBYTE pDst;
1354 TRACE("(%p,%p,cb %d, cbOff %d)\n", hData, pSrc, cb, cbOff);
1356 pDst = DdeAccessData(hData, &old_sz);
1357 if (!pDst) return 0;
1359 new_sz = cb + cbOff;
1360 if (new_sz > old_sz)
1362 DdeUnaccessData(hData);
1363 hData = GlobalReAlloc(hData, new_sz + sizeof(DDE_DATAHANDLE_HEAD),
1364 GMEM_MOVEABLE | GMEM_DDESHARE);
1365 pDst = DdeAccessData(hData, &old_sz);
1368 if (!pDst) return 0;
1370 memcpy(pDst + cbOff, pSrc, cb);
1371 DdeUnaccessData(hData);
1372 return hData;
1375 /******************************************************************************
1376 * DdeGetData [USER32.@] Copies data from DDE object to local buffer
1379 * PARAMS
1380 * hData [I] Handle to DDE object
1381 * pDst [I] Pointer to destination buffer
1382 * cbMax [I] Amount of data to copy
1383 * cbOff [I] Offset to beginning of data
1385 * RETURNS
1386 * Size of memory object associated with handle
1388 DWORD WINAPI DdeGetData(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
1390 DWORD dwSize, dwRet;
1391 LPBYTE pByte;
1393 TRACE("(%p,%p,%d,%d)\n", hData, pDst, cbMax, cbOff);
1395 pByte = DdeAccessData(hData, &dwSize);
1397 if (pByte)
1399 if (!pDst)
1401 dwRet = dwSize;
1403 else if (cbOff + cbMax < dwSize)
1405 dwRet = cbMax;
1407 else if (cbOff < dwSize)
1409 dwRet = dwSize - cbOff;
1411 else
1413 dwRet = 0;
1415 if (pDst && dwRet != 0)
1417 memcpy(pDst, pByte + cbOff, dwRet);
1419 DdeUnaccessData(hData);
1421 else
1423 dwRet = 0;
1425 return dwRet;
1428 /*****************************************************************
1429 * DdeAccessData (USER32.@)
1431 LPBYTE WINAPI DdeAccessData(HDDEDATA hData, LPDWORD pcbDataSize)
1433 HGLOBAL hMem = hData;
1434 DDE_DATAHANDLE_HEAD* pDdh;
1436 TRACE("(%p,%p)\n", hData, pcbDataSize);
1438 pDdh = GlobalLock(hMem);
1439 if (pDdh == NULL)
1441 ERR("Failed on GlobalLock(%p)\n", hMem);
1442 return 0;
1445 if (pcbDataSize != NULL)
1447 *pcbDataSize = GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD);
1449 TRACE("=> %p (%lu) fmt %04x\n", pDdh + 1, GlobalSize(hMem) - sizeof(DDE_DATAHANDLE_HEAD), pDdh->cfFormat);
1450 return (LPBYTE)(pDdh + 1);
1453 /*****************************************************************
1454 * DdeUnaccessData (USER32.@)
1456 BOOL WINAPI DdeUnaccessData(HDDEDATA hData)
1458 HGLOBAL hMem = hData;
1460 TRACE("(%p)\n", hData);
1462 GlobalUnlock(hMem);
1464 return TRUE;
1467 /*****************************************************************
1468 * DdeFreeDataHandle (USER32.@)
1470 BOOL WINAPI DdeFreeDataHandle(HDDEDATA hData)
1472 TRACE("(%p)\n", hData);
1474 /* 1 is the handle value returned by an asynchronous operation. */
1475 if (hData == (HDDEDATA)1)
1476 return TRUE;
1478 return GlobalFree(hData) == 0;
1481 /******************************************************************
1482 * WDML_IsAppOwned
1486 BOOL WDML_IsAppOwned(HDDEDATA hData)
1488 DDE_DATAHANDLE_HEAD* pDdh;
1489 BOOL ret = FALSE;
1491 pDdh = GlobalLock(hData);
1492 if (pDdh != NULL)
1494 ret = pDdh->bAppOwned;
1495 GlobalUnlock(hData);
1497 return ret;
1500 /* ================================================================
1502 * Global <=> Data handle management
1504 * ================================================================ */
1506 /* Note: we use a DDEDATA, but layout of DDEDATA, DDEADVISE and DDEPOKE structures is similar:
1507 * offset size
1508 * (bytes) (bits) comment
1509 * 0 16 bit fields for options (release, ackreq, response...)
1510 * 2 16 clipboard format
1511 * 4 ? data to be used
1513 HDDEDATA WDML_Global2DataHandle(WDML_CONV* pConv, HGLOBAL hMem, WINE_DDEHEAD* p)
1515 DDEDATA* pDd;
1516 HDDEDATA ret = 0;
1517 DWORD size;
1519 if (hMem)
1521 pDd = GlobalLock(hMem);
1522 size = GlobalSize(hMem) - sizeof(WINE_DDEHEAD);
1523 if (pDd)
1525 if (p) memcpy(p, pDd, sizeof(WINE_DDEHEAD));
1526 switch (pDd->cfFormat)
1528 default:
1529 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1530 pDd->cfFormat, hMem);
1531 /* fall through */
1532 case 0:
1533 case CF_TEXT:
1534 ret = DdeCreateDataHandle(pConv->instance->instanceID, pDd->Value, size, 0, 0, pDd->cfFormat, 0);
1535 break;
1536 case CF_BITMAP:
1537 if (size >= sizeof(BITMAP))
1539 BITMAP* bmp = (BITMAP*)pDd->Value;
1540 int count = bmp->bmWidthBytes * bmp->bmHeight * bmp->bmPlanes;
1541 if (size >= sizeof(BITMAP) + count)
1543 HBITMAP hbmp;
1545 if ((hbmp = CreateBitmap(bmp->bmWidth, bmp->bmHeight,
1546 bmp->bmPlanes, bmp->bmBitsPixel,
1547 pDd->Value + sizeof(BITMAP))))
1549 ret = DdeCreateDataHandle(pConv->instance->instanceID, (LPBYTE)&hbmp, sizeof(hbmp),
1550 0, 0, CF_BITMAP, 0);
1552 else ERR("Can't create bmp\n");
1554 else
1556 ERR("Wrong count: %u / %d\n", size, count);
1558 } else ERR("No bitmap header\n");
1559 break;
1561 GlobalUnlock(hMem);
1564 return ret;
1567 /******************************************************************
1568 * WDML_DataHandle2Global
1572 HGLOBAL WDML_DataHandle2Global(HDDEDATA hDdeData, BOOL fResponse, BOOL fRelease,
1573 BOOL fDeferUpd, BOOL fAckReq)
1575 DDE_DATAHANDLE_HEAD* pDdh;
1576 DWORD dwSize;
1577 HGLOBAL hMem = 0;
1579 dwSize = GlobalSize(hDdeData) - sizeof(DDE_DATAHANDLE_HEAD);
1580 pDdh = GlobalLock(hDdeData);
1581 if (dwSize && pDdh)
1583 WINE_DDEHEAD* wdh = NULL;
1585 switch (pDdh->cfFormat)
1587 default:
1588 FIXME("Unsupported format (%04x) for data %p, passing raw information\n",
1589 pDdh->cfFormat, hDdeData);
1590 /* fall through */
1591 case 0:
1592 case CF_TEXT:
1593 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(WINE_DDEHEAD) + dwSize);
1594 if (hMem && (wdh = GlobalLock(hMem)))
1596 memcpy(wdh + 1, pDdh + 1, dwSize);
1598 break;
1599 case CF_BITMAP:
1600 if (dwSize >= sizeof(HBITMAP))
1602 BITMAP bmp;
1603 DWORD count;
1604 HBITMAP hbmp = *(HBITMAP*)(pDdh + 1);
1606 if (GetObjectW(hbmp, sizeof(bmp), &bmp))
1608 count = bmp.bmWidthBytes * bmp.bmHeight;
1609 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1610 sizeof(WINE_DDEHEAD) + sizeof(bmp) + count);
1611 if (hMem && (wdh = GlobalLock(hMem)))
1613 memcpy(wdh + 1, &bmp, sizeof(bmp));
1614 GetBitmapBits(hbmp, count, ((char*)(wdh + 1)) + sizeof(bmp));
1618 break;
1620 if (wdh)
1622 wdh->unused = 0;
1623 wdh->fResponse = fResponse;
1624 wdh->fRelease = fRelease;
1625 wdh->fDeferUpd = fDeferUpd;
1626 wdh->fAckReq = fAckReq;
1627 wdh->cfFormat = pDdh->cfFormat;
1628 GlobalUnlock(hMem);
1630 GlobalUnlock(hDdeData);
1633 return hMem;
1636 /* ================================================================
1638 * Server management
1640 * ================================================================ */
1642 /******************************************************************
1643 * WDML_AddServer
1647 WDML_SERVER* WDML_AddServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1649 WDML_SERVER* pServer;
1650 WCHAR buf1[256];
1651 WCHAR buf2[256];
1653 pServer = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_SERVER));
1654 if (pServer == NULL) return NULL;
1656 pServer->hszService = hszService;
1657 WDML_IncHSZ(pInstance, hszService);
1659 DdeQueryStringW(pInstance->instanceID, hszService, buf1, 256, CP_WINUNICODE);
1660 swprintf(buf2, 256, L"%s(0x%*x)", buf1, 2*sizeof(ULONG_PTR), GetCurrentProcessId());
1661 pServer->hszServiceSpec = DdeCreateStringHandleW(pInstance->instanceID, buf2, CP_WINUNICODE);
1663 pServer->atomService = WDML_MakeAtomFromHsz(pServer->hszService);
1664 pServer->atomServiceSpec = WDML_MakeAtomFromHsz(pServer->hszServiceSpec);
1666 pServer->filterOn = TRUE;
1668 pServer->next = pInstance->servers;
1669 pInstance->servers = pServer;
1670 return pServer;
1673 /******************************************************************
1674 * WDML_RemoveServer
1678 void WDML_RemoveServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1680 WDML_SERVER* pPrev = NULL;
1681 WDML_SERVER* pServer = NULL;
1682 WDML_CONV* pConv;
1683 WDML_CONV* pConvNext;
1685 pServer = pInstance->servers;
1687 while (pServer != NULL)
1689 if (DdeCmpStringHandles(pServer->hszService, hszService) == 0)
1691 WDML_BroadcastDDEWindows(WDML_szEventClass, WM_WDML_UNREGISTER,
1692 pServer->atomService, pServer->atomServiceSpec);
1693 /* terminate all conversations for given topic */
1694 for (pConv = pInstance->convs[WDML_SERVER_SIDE]; pConv != NULL; pConv = pConvNext)
1696 pConvNext = pConv->next;
1697 if (DdeCmpStringHandles(pConv->hszService, hszService) == 0)
1699 HWND client = pConv->hwndClient, server = pConv->hwndServer;
1700 WDML_RemoveConv(pConv, WDML_SERVER_SIDE);
1701 /* don't care about return code (whether client window is present or not) */
1702 PostMessageW(client, WM_DDE_TERMINATE, (WPARAM)server, 0);
1705 if (pServer == pInstance->servers)
1707 pInstance->servers = pServer->next;
1709 else
1711 pPrev->next = pServer->next;
1714 DestroyWindow(pServer->hwndServer);
1715 WDML_DecHSZ(pInstance, pServer->hszServiceSpec);
1716 WDML_DecHSZ(pInstance, pServer->hszService);
1718 GlobalDeleteAtom(pServer->atomService);
1719 GlobalDeleteAtom(pServer->atomServiceSpec);
1721 HeapFree(GetProcessHeap(), 0, pServer);
1722 break;
1725 pPrev = pServer;
1726 pServer = pServer->next;
1730 /*****************************************************************************
1731 * WDML_FindServer
1733 * generic routine to return a pointer to the relevant ServiceNode
1734 * for a given service name, or NULL if the entry does not exist
1737 WDML_SERVER* WDML_FindServer(WDML_INSTANCE* pInstance, HSZ hszService, HSZ hszTopic)
1739 WDML_SERVER* pServer;
1741 for (pServer = pInstance->servers; pServer != NULL; pServer = pServer->next)
1743 if (hszService == pServer->hszService)
1745 return pServer;
1748 TRACE("Service name missing\n");
1749 return NULL;
1752 /* ================================================================
1754 * Link (hot & warm) management
1756 * ================================================================ */
1758 /******************************************************************
1759 * WDML_AddLink
1763 void WDML_AddLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
1764 UINT wType, HSZ hszItem, UINT wFmt)
1766 WDML_LINK* pLink;
1768 pLink = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_LINK));
1769 if (pLink == NULL)
1771 ERR("OOM\n");
1772 return;
1775 pLink->hConv = hConv;
1776 pLink->transactionType = wType;
1777 WDML_IncHSZ(pInstance, pLink->hszItem = hszItem);
1778 pLink->uFmt = wFmt;
1779 pLink->next = pInstance->links[side];
1780 pInstance->links[side] = pLink;
1783 /******************************************************************
1784 * WDML_RemoveLink
1788 void WDML_RemoveLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
1789 HSZ hszItem, UINT uFmt)
1791 WDML_LINK* pPrev = NULL;
1792 WDML_LINK* pCurrent = NULL;
1794 pCurrent = pInstance->links[side];
1796 while (pCurrent != NULL)
1798 if (pCurrent->hConv == hConv &&
1799 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
1800 pCurrent->uFmt == uFmt)
1802 if (pCurrent == pInstance->links[side])
1804 pInstance->links[side] = pCurrent->next;
1806 else
1808 pPrev->next = pCurrent->next;
1811 WDML_DecHSZ(pInstance, pCurrent->hszItem);
1812 HeapFree(GetProcessHeap(), 0, pCurrent);
1813 break;
1816 pPrev = pCurrent;
1817 pCurrent = pCurrent->next;
1821 /* this function is called to remove all links related to the conv.
1822 It should be called from both client and server when terminating
1823 the conversation.
1825 /******************************************************************
1826 * WDML_RemoveAllLinks
1830 static void WDML_RemoveAllLinks(WDML_INSTANCE* pInstance, WDML_CONV* pConv, WDML_SIDE side)
1832 WDML_LINK* pPrev = NULL;
1833 WDML_LINK* pCurrent = NULL;
1834 WDML_LINK* pNext = NULL;
1836 pCurrent = pInstance->links[side];
1838 while (pCurrent != NULL)
1840 if (pCurrent->hConv == (HCONV)pConv)
1842 if (pCurrent == pInstance->links[side])
1844 pInstance->links[side] = pCurrent->next;
1845 pNext = pCurrent->next;
1847 else
1849 pPrev->next = pCurrent->next;
1850 pNext = pCurrent->next;
1853 WDML_DecHSZ(pInstance, pCurrent->hszItem);
1855 HeapFree(GetProcessHeap(), 0, pCurrent);
1856 pCurrent = NULL;
1859 if (pCurrent)
1861 pPrev = pCurrent;
1862 pCurrent = pCurrent->next;
1864 else
1866 pCurrent = pNext;
1871 /******************************************************************
1872 * WDML_FindLink
1876 WDML_LINK* WDML_FindLink(WDML_INSTANCE* pInstance, HCONV hConv, WDML_SIDE side,
1877 HSZ hszItem, BOOL use_fmt, UINT uFmt)
1879 WDML_LINK* pCurrent;
1881 for (pCurrent = pInstance->links[side]; pCurrent != NULL; pCurrent = pCurrent->next)
1883 /* we don't need to check for transaction type as it can be altered */
1885 if (pCurrent->hConv == hConv &&
1886 DdeCmpStringHandles(pCurrent->hszItem, hszItem) == 0 &&
1887 (!use_fmt || pCurrent->uFmt == uFmt))
1889 break;
1894 return pCurrent;
1897 /* ================================================================
1899 * Transaction management
1901 * ================================================================ */
1903 /******************************************************************
1904 * WDML_AllocTransaction
1906 * Alloc a transaction structure for handling the message ddeMsg
1908 WDML_XACT* WDML_AllocTransaction(WDML_INSTANCE* pInstance, UINT ddeMsg,
1909 UINT wFmt, HSZ hszItem)
1911 WDML_XACT* pXAct;
1912 static WORD tid = 1; /* FIXME: wrap around */
1914 pXAct = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_XACT));
1915 if (!pXAct)
1917 pInstance->lastError = DMLERR_MEMORY_ERROR;
1918 return NULL;
1921 pXAct->xActID = tid++;
1922 pXAct->ddeMsg = ddeMsg;
1923 pXAct->hDdeData = 0;
1924 pXAct->hUser = 0;
1925 pXAct->next = NULL;
1926 pXAct->wType = 0;
1927 pXAct->wFmt = wFmt;
1928 if ((pXAct->hszItem = hszItem)) WDML_IncHSZ(pInstance, pXAct->hszItem);
1929 pXAct->atom = 0;
1930 pXAct->hMem = 0;
1931 pXAct->lParam = 0;
1933 return pXAct;
1936 /******************************************************************
1937 * WDML_QueueTransaction
1939 * Adds a transaction to the list of transaction
1941 void WDML_QueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
1943 WDML_XACT** pt;
1945 /* advance to last in queue */
1946 for (pt = &pConv->transactions; *pt != NULL; pt = &(*pt)->next);
1947 *pt = pXAct;
1950 /******************************************************************
1951 * WDML_UnQueueTransaction
1955 BOOL WDML_UnQueueTransaction(WDML_CONV* pConv, WDML_XACT* pXAct)
1957 WDML_XACT** pt;
1959 for (pt = &pConv->transactions; *pt; pt = &(*pt)->next)
1961 if (*pt == pXAct)
1963 *pt = pXAct->next;
1964 return TRUE;
1967 return FALSE;
1970 /******************************************************************
1971 * WDML_FreeTransaction
1975 void WDML_FreeTransaction(WDML_INSTANCE* pInstance, WDML_XACT* pXAct, BOOL doFreePmt)
1977 /* free pmt(s) in pXAct too. check against one for not deleting TRUE return values */
1978 if (doFreePmt && (ULONG_PTR)pXAct->hMem > 1)
1980 GlobalFree(pXAct->hMem);
1982 if (pXAct->hszItem) WDML_DecHSZ(pInstance, pXAct->hszItem);
1984 HeapFree(GetProcessHeap(), 0, pXAct);
1987 /******************************************************************
1988 * WDML_FindTransaction
1992 static WDML_XACT* WDML_FindTransaction(WDML_CONV* pConv, DWORD tid)
1994 WDML_XACT* pXAct;
1996 tid = HIWORD(tid);
1997 for (pXAct = pConv->transactions; pXAct; pXAct = pXAct->next)
1999 if (pXAct->xActID == tid)
2000 break;
2002 return pXAct;
2005 /* ================================================================
2007 * Conversation management
2009 * ================================================================ */
2011 /******************************************************************
2012 * WDML_AddConv
2016 WDML_CONV* WDML_AddConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
2017 HSZ hszService, HSZ hszTopic, HWND hwndClient, HWND hwndServer)
2019 WDML_CONV* pConv;
2021 /* no conversation yet, add it */
2022 pConv = HeapAlloc(GetProcessHeap(), 0, sizeof(WDML_CONV));
2023 if (!pConv) return NULL;
2025 pConv->instance = pInstance;
2026 WDML_IncHSZ(pInstance, pConv->hszService = hszService);
2027 WDML_IncHSZ(pInstance, pConv->hszTopic = hszTopic);
2028 pConv->magic = WDML_CONV_MAGIC;
2029 pConv->hwndServer = hwndServer;
2030 pConv->hwndClient = hwndClient;
2031 pConv->transactions = NULL;
2032 pConv->hUser = 0;
2033 pConv->wStatus = (side == WDML_CLIENT_SIDE) ? ST_CLIENT : 0L;
2034 pConv->wStatus |= pInstance->wStatus;
2035 /* check if both side of the conversation are of the same instance */
2036 if (GetWindowThreadProcessId(hwndClient, NULL) == GetWindowThreadProcessId(hwndServer, NULL) &&
2037 WDML_GetInstanceFromWnd(hwndClient) == WDML_GetInstanceFromWnd(hwndServer))
2039 pConv->wStatus |= ST_ISSELF;
2041 pConv->wConvst = XST_NULL;
2043 pConv->next = pInstance->convs[side];
2044 pInstance->convs[side] = pConv;
2046 TRACE("pConv->wStatus %04x pInstance(%p)\n", pConv->wStatus, pInstance);
2048 return pConv;
2051 /******************************************************************
2052 * WDML_FindConv
2056 WDML_CONV* WDML_FindConv(WDML_INSTANCE* pInstance, WDML_SIDE side,
2057 HSZ hszService, HSZ hszTopic)
2059 WDML_CONV* pCurrent;
2061 for (pCurrent = pInstance->convs[side]; pCurrent != NULL; pCurrent = pCurrent->next)
2063 if (DdeCmpStringHandles(pCurrent->hszService, hszService) == 0 &&
2064 DdeCmpStringHandles(pCurrent->hszTopic, hszTopic) == 0)
2066 return pCurrent;
2070 return NULL;
2073 /******************************************************************
2074 * WDML_RemoveConv
2078 void WDML_RemoveConv(WDML_CONV* pRef, WDML_SIDE side)
2080 WDML_CONV* pPrev = NULL;
2081 WDML_CONV* pCurrent;
2082 WDML_XACT* pXAct;
2083 WDML_XACT* pXActNext;
2084 HWND hWnd;
2086 if (!pRef)
2087 return;
2089 /* remove any pending transaction */
2090 for (pXAct = pRef->transactions; pXAct != NULL; pXAct = pXActNext)
2092 pXActNext = pXAct->next;
2093 WDML_FreeTransaction(pRef->instance, pXAct, TRUE);
2096 WDML_RemoveAllLinks(pRef->instance, pRef, side);
2098 /* FIXME: should we keep the window around ? it seems so (at least on client side
2099 * to let QueryConvInfo work after conv termination, but also to implement
2100 * DdeReconnect...
2102 /* destroy conversation window, but first remove pConv from hWnd.
2103 * this would help the wndProc do appropriate handling upon a WM_DESTROY message
2105 hWnd = (side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer;
2106 SetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION, 0);
2108 DestroyWindow((side == WDML_CLIENT_SIDE) ? pRef->hwndClient : pRef->hwndServer);
2110 WDML_DecHSZ(pRef->instance, pRef->hszService);
2111 WDML_DecHSZ(pRef->instance, pRef->hszTopic);
2113 for (pCurrent = pRef->instance->convs[side]; pCurrent != NULL; pCurrent = (pPrev = pCurrent)->next)
2115 if (pCurrent == pRef)
2117 if (pCurrent == pRef->instance->convs[side])
2119 pRef->instance->convs[side] = pCurrent->next;
2121 else
2123 pPrev->next = pCurrent->next;
2125 pCurrent->magic = 0;
2126 HeapFree(GetProcessHeap(), 0, pCurrent);
2127 break;
2132 /******************************************************************
2133 * WDML_EnableCallback
2135 static BOOL WDML_EnableCallback(WDML_CONV *pConv, UINT wCmd)
2137 if (wCmd == EC_DISABLE)
2139 pConv->wStatus |= ST_BLOCKED;
2140 TRACE("EC_DISABLE: conv %p status flags %04x\n", pConv, pConv->wStatus);
2141 return TRUE;
2144 if (wCmd == EC_QUERYWAITING)
2145 return pConv->transactions != NULL;
2147 if (wCmd != EC_ENABLEALL && wCmd != EC_ENABLEONE)
2149 FIXME("Unknown command code %04x\n", wCmd);
2150 return FALSE;
2153 if (wCmd == EC_ENABLEALL)
2155 pConv->wStatus &= ~ST_BLOCKED;
2156 TRACE("EC_ENABLEALL: conv %p status flags %04x\n", pConv, pConv->wStatus);
2159 while (pConv->transactions)
2161 WDML_XACT *pXAct = pConv->transactions;
2163 if (pConv->wStatus & ST_CLIENT)
2165 /* transaction should be in the queue until handled */
2166 WDML_ClientHandle(pConv, pXAct, 0, NULL);
2167 WDML_UnQueueTransaction(pConv, pXAct);
2169 else
2171 /* transaction should be removed from the queue before handling */
2172 WDML_UnQueueTransaction(pConv, pXAct);
2173 WDML_ServerHandle(pConv, pXAct);
2176 WDML_FreeTransaction(pConv->instance, pXAct, TRUE);
2178 if (wCmd == EC_ENABLEONE) break;
2180 return TRUE;
2183 /*****************************************************************
2184 * DdeEnableCallback (USER32.@)
2186 BOOL WINAPI DdeEnableCallback(DWORD idInst, HCONV hConv, UINT wCmd)
2188 BOOL ret = FALSE;
2189 WDML_CONV *pConv;
2191 TRACE("(%d, %p, %04x)\n", idInst, hConv, wCmd);
2193 if (hConv)
2195 pConv = WDML_GetConv(hConv, TRUE);
2197 if (pConv && pConv->instance->instanceID == idInst)
2198 ret = WDML_EnableCallback(pConv, wCmd);
2200 else
2202 WDML_INSTANCE *pInstance = WDML_GetInstance(idInst);
2204 if (!pInstance)
2205 return FALSE;
2207 TRACE("adding flags %04x to instance %p\n", wCmd, pInstance);
2208 pInstance->wStatus |= wCmd;
2210 if (wCmd == EC_DISABLE)
2212 pInstance->wStatus |= ST_BLOCKED;
2213 TRACE("EC_DISABLE: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
2215 else if (wCmd == EC_ENABLEALL)
2217 pInstance->wStatus &= ~ST_BLOCKED;
2218 TRACE("EC_ENABLEALL: inst %p status flags %04x\n", pInstance, pInstance->wStatus);
2221 ret = TRUE;
2223 for (pConv = pInstance->convs[WDML_CLIENT_SIDE]; pConv != NULL; pConv = pConv->next)
2225 ret = WDML_EnableCallback(pConv, wCmd);
2226 if (ret && wCmd == EC_QUERYWAITING) break;
2230 return ret;
2233 /******************************************************************
2234 * WDML_GetConv
2238 WDML_CONV* WDML_GetConv(HCONV hConv, BOOL checkConnected)
2240 WDML_CONV* pConv = (WDML_CONV*)hConv;
2242 /* FIXME: should do better checking */
2243 if (pConv == NULL || pConv->magic != WDML_CONV_MAGIC) return NULL;
2245 if (!pConv->instance)
2247 WARN("wrong thread ID, no instance\n");
2248 return NULL;
2251 if (pConv->instance->threadID != GetCurrentThreadId())
2253 WARN("wrong thread ID\n");
2254 pConv->instance->lastError = DMLERR_INVALIDPARAMETER; /* FIXME: check */
2255 return NULL;
2258 if (checkConnected && !(pConv->wStatus & ST_CONNECTED))
2260 WARN("found conv but ain't connected\n");
2261 pConv->instance->lastError = DMLERR_NO_CONV_ESTABLISHED;
2262 return NULL;
2265 return pConv;
2268 /******************************************************************
2269 * WDML_GetConvFromWnd
2273 WDML_CONV* WDML_GetConvFromWnd(HWND hWnd)
2275 return (WDML_CONV*)GetWindowLongPtrW(hWnd, GWL_WDML_CONVERSATION);
2278 /******************************************************************
2279 * WDML_PostAck
2283 BOOL WDML_PostAck(WDML_CONV* pConv, WDML_SIDE side, WORD appRetCode,
2284 BOOL fBusy, BOOL fAck, UINT_PTR pmt, LPARAM lParam, UINT oldMsg)
2286 DDEACK ddeAck;
2287 HWND from, to;
2289 if (side == WDML_SERVER_SIDE)
2291 from = pConv->hwndServer;
2292 to = pConv->hwndClient;
2294 else
2296 to = pConv->hwndServer;
2297 from = pConv->hwndClient;
2300 ddeAck.bAppReturnCode = appRetCode;
2301 ddeAck.reserved = 0;
2302 ddeAck.fBusy = fBusy;
2303 ddeAck.fAck = fAck;
2305 TRACE("Posting a %s ack\n", ddeAck.fAck ? "positive" : "negative");
2307 lParam = (lParam) ? ReuseDDElParam(lParam, oldMsg, WM_DDE_ACK, *(WORD*)&ddeAck, pmt) :
2308 PackDDElParam(WM_DDE_ACK, *(WORD*)&ddeAck, pmt);
2309 if (!PostMessageW(to, WM_DDE_ACK, (WPARAM)from, lParam))
2311 pConv->wStatus &= ~ST_CONNECTED;
2312 pConv->instance->lastError = DMLERR_POSTMSG_FAILED;
2313 FreeDDElParam(WM_DDE_ACK, lParam);
2314 return FALSE;
2316 return TRUE;
2319 /*****************************************************************
2320 * DdeSetUserHandle (USER32.@)
2322 BOOL WINAPI DdeSetUserHandle(HCONV hConv, DWORD id, DWORD hUser)
2324 WDML_CONV* pConv;
2326 pConv = WDML_GetConv(hConv, FALSE);
2327 if (pConv == NULL)
2328 return FALSE;
2330 if (id == QID_SYNC)
2332 pConv->hUser = hUser;
2334 else
2336 WDML_XACT* pXAct;
2338 pXAct = WDML_FindTransaction(pConv, id);
2339 if (pXAct)
2341 pXAct->hUser = hUser;
2343 else
2345 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2346 return FALSE;
2349 return TRUE;
2352 /******************************************************************
2353 * WDML_GetLocalConvInfo
2357 static BOOL WDML_GetLocalConvInfo(WDML_CONV* pConv, CONVINFO* ci, DWORD id)
2359 BOOL ret = TRUE;
2360 WDML_LINK* pLink;
2361 WDML_SIDE side;
2363 ci->hConvPartner = (pConv->wStatus & ST_ISLOCAL) ? (HCONV)((ULONG_PTR)pConv | 1) : 0;
2364 ci->hszSvcPartner = pConv->hszService;
2365 ci->hszServiceReq = pConv->hszService; /* FIXME: they shouldn't be the same, should they ? */
2366 ci->hszTopic = pConv->hszTopic;
2367 ci->wStatus = pConv->wStatus;
2369 side = (pConv->wStatus & ST_CLIENT) ? WDML_CLIENT_SIDE : WDML_SERVER_SIDE;
2371 for (pLink = pConv->instance->links[side]; pLink != NULL; pLink = pLink->next)
2373 if (pLink->hConv == (HCONV)pConv)
2375 ci->wStatus |= ST_ADVISE;
2376 break;
2380 /* FIXME: non handled status flags:
2381 ST_BLOCKED
2382 ST_BLOCKNEXT
2383 ST_INLIST
2386 ci->wConvst = pConv->wConvst; /* FIXME */
2388 ci->wLastError = 0; /* FIXME: note it's not the instance last error */
2389 ci->hConvList = 0;
2390 ci->ConvCtxt = pConv->convContext;
2391 if (ci->wStatus & ST_CLIENT)
2393 ci->hwnd = pConv->hwndClient;
2394 ci->hwndPartner = pConv->hwndServer;
2396 else
2398 ci->hwnd = pConv->hwndServer;
2399 ci->hwndPartner = pConv->hwndClient;
2401 if (id == QID_SYNC)
2403 ci->hUser = pConv->hUser;
2404 ci->hszItem = 0;
2405 ci->wFmt = 0;
2406 ci->wType = 0;
2408 else
2410 WDML_XACT* pXAct;
2412 pXAct = WDML_FindTransaction(pConv, id);
2413 if (pXAct)
2415 ci->hUser = pXAct->hUser;
2416 ci->hszItem = pXAct->hszItem;
2417 ci->wFmt = pXAct->wFmt;
2418 ci->wType = pXAct->wType;
2420 else
2422 ret = 0;
2423 pConv->instance->lastError = DMLERR_UNFOUND_QUEUE_ID;
2426 return ret;
2429 /******************************************************************
2430 * DdeQueryConvInfo (USER32.@)
2432 * FIXME: Set last DDE error on failure.
2434 UINT WINAPI DdeQueryConvInfo(HCONV hConv, DWORD id, PCONVINFO lpConvInfo)
2436 UINT ret = lpConvInfo->cb;
2437 CONVINFO ci;
2438 WDML_CONV* pConv;
2440 TRACE("(%p,%x,%p)\n", hConv, id, lpConvInfo);
2442 if (!hConv)
2444 FIXME("hConv is NULL\n");
2445 return 0;
2448 pConv = WDML_GetConv(hConv, FALSE);
2449 if (pConv != NULL)
2451 if (!WDML_GetLocalConvInfo(pConv, &ci, id))
2452 ret = 0;
2454 else
2456 if ((ULONG_PTR)hConv & 1)
2458 pConv = WDML_GetConv((HCONV)((ULONG_PTR)hConv & ~1), FALSE);
2459 if (pConv != NULL)
2460 FIXME("Request on remote conversation information is not implemented yet\n");
2462 ret = 0;
2465 if (ret != 0)
2467 ci.cb = lpConvInfo->cb;
2468 memcpy(lpConvInfo, &ci, min((size_t)lpConvInfo->cb, sizeof(ci)));
2470 return ret;
2473 /* ================================================================
2475 * Information broadcast across DDEML implementations
2477 * ================================================================ */
2479 struct tagWDML_BroadcastPmt
2481 LPCWSTR clsName;
2482 UINT uMsg;
2483 WPARAM wParam;
2484 LPARAM lParam;
2487 /******************************************************************
2488 * WDML_BroadcastEnumProc
2492 static BOOL CALLBACK WDML_BroadcastEnumProc(HWND hWnd, LPARAM lParam)
2494 struct tagWDML_BroadcastPmt* s = (struct tagWDML_BroadcastPmt*)lParam;
2495 WCHAR buffer[128];
2497 if (GetClassNameW(hWnd, buffer, 128) > 0 &&
2498 lstrcmpiW(buffer, s->clsName) == 0)
2500 PostMessageW(hWnd, s->uMsg, s->wParam, s->lParam);
2502 return TRUE;
2505 /******************************************************************
2506 * WDML_BroadcastDDEWindows
2510 void WDML_BroadcastDDEWindows(LPCWSTR clsName, UINT uMsg, WPARAM wParam, LPARAM lParam)
2512 struct tagWDML_BroadcastPmt s;
2514 s.clsName = clsName;
2515 s.uMsg = uMsg;
2516 s.wParam = wParam;
2517 s.lParam = lParam;
2518 EnumWindows(WDML_BroadcastEnumProc, (LPARAM)&s);