/****************************************************************** * WDML_ClientQueueAdvise * * Creates and queue an WM_DDE_ADVISE transaction */ static WDML_XACT* WDML_ClientQueueAdvise(WDML_CONV* pConv, UINT wType, UINT wFmt, HSZ hszItem) { DDEADVISE* pDdeAdvise; WDML_XACT* pXAct; ATOM atom; TRACE("XTYP_ADVSTART (with%s data) transaction\n", (wType & XTYPF_NODATA) ? "out" : ""); atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) return NULL; pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_ADVISE, wFmt, hszItem); if (!pXAct) { GlobalDeleteAtom(atom); return NULL; } pXAct->wType = wType & ~0x0F; pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEADVISE)); /* FIXME: hMem is unfreed for now... should be deleted in server */ /* pack DdeAdvise */ pDdeAdvise = GlobalLock(pXAct->hMem); pDdeAdvise->fAckReq = (wType & XTYPF_ACKREQ) != 0; pDdeAdvise->fDeferUpd = (wType & XTYPF_NODATA) != 0; pDdeAdvise->cfFormat = wFmt; GlobalUnlock(pXAct->hMem); pXAct->lParam = PackDDElParam(WM_DDE_ADVISE, (UINT_PTR)pXAct->hMem, atom); return pXAct; }
/****************************************************************** * WDML_ClientQueuePoke * * */ static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPVOID pData, DWORD cbData, UINT wFmt, HSZ hszItem) { DDE_DATAHANDLE_HEAD *dh; WDML_XACT *pXAct; DDEPOKE *ddePoke; HGLOBAL hglobal; ATOM atom; TRACE("XTYP_POKE transaction\n"); atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) return NULL; pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem); if (!pXAct) { GlobalDeleteAtom(atom); return NULL; } if (cbData == (DWORD)-1) { hglobal = pData; dh = GlobalLock(hglobal); cbData = GlobalSize(hglobal) - sizeof(DDE_DATAHANDLE_HEAD); pData = dh + 1; GlobalUnlock(hglobal); } pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, FIELD_OFFSET(DDEPOKE, Value[cbData])); ddePoke = GlobalLock(pXAct->hMem); if (!ddePoke) { pConv->instance->lastError = DMLERR_MEMORY_ERROR; return NULL; } ddePoke->unused = 0; ddePoke->fRelease = TRUE; ddePoke->cfFormat = wFmt; memcpy(ddePoke->Value, pData, cbData); GlobalUnlock(pXAct->hMem); pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom); return pXAct; }
/****************************************************************** * WDML_ClientQueuePoke * * */ static WDML_XACT* WDML_ClientQueuePoke(WDML_CONV* pConv, LPCVOID pData, DWORD cbData, UINT wFmt, HSZ hszItem) { WDML_XACT* pXAct; ATOM atom; TRACE("XTYP_POKE transaction\n"); atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) return NULL; pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_POKE, wFmt, hszItem); if (!pXAct) { GlobalDeleteAtom(atom); return NULL; } if (cbData == (DWORD)-1) { pXAct->hMem = (HDDEDATA)pData; } else { DDEPOKE* ddePoke; pXAct->hMem = GlobalAlloc(GHND | GMEM_DDESHARE, sizeof(DDEPOKE) + cbData); ddePoke = GlobalLock(pXAct->hMem); if (ddePoke) { memcpy(ddePoke->Value, pData, cbData); ddePoke->fRelease = FALSE; /* FIXME: app owned ? */ ddePoke->cfFormat = wFmt; GlobalUnlock(pXAct->hMem); } } pXAct->lParam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)pXAct->hMem, atom); return pXAct; }
/****************************************************************** * WDML_ClientQueueRequest * * */ static WDML_XACT* WDML_ClientQueueRequest(WDML_CONV* pConv, UINT wFmt, HSZ hszItem) { WDML_XACT* pXAct; ATOM atom; TRACE("XTYP_REQUEST transaction\n"); atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) return NULL; pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_REQUEST, wFmt, hszItem); if (!pXAct) { GlobalDeleteAtom(atom); return NULL; } pXAct->lParam = PackDDElParam(WM_DDE_REQUEST, wFmt, atom); return pXAct; }
/****************************************************************** * WDML_ClientQueueUnadvise * * queues an unadvise transaction */ static WDML_XACT* WDML_ClientQueueUnadvise(WDML_CONV* pConv, UINT wFmt, HSZ hszItem) { WDML_XACT* pXAct; ATOM atom; TRACE("XTYP_ADVSTOP transaction\n"); atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) return NULL; pXAct = WDML_AllocTransaction(pConv->instance, WM_DDE_UNADVISE, wFmt, hszItem); if (!pXAct) { GlobalDeleteAtom(atom); return NULL; } /* end advise loop: post WM_DDE_UNADVISE to server to terminate link * on the specified item. */ pXAct->lParam = PackDDElParam(WM_DDE_UNADVISE, wFmt, atom); return pXAct; }
/****************************************************************************** * DdePostAdvise [USER32.@] Send transaction to DDE callback function. * * PARAMS * idInst [I] Instance identifier * hszTopic [I] Handle to topic name string * hszItem [I] Handle to item name string * * RETURNS * Success: TRUE * Failure: FALSE */ BOOL WINAPI DdePostAdvise(DWORD idInst, HSZ hszTopic, HSZ hszItem) { WDML_INSTANCE* pInstance = NULL; WDML_LINK* pLink = NULL; HDDEDATA hDdeData = 0; HGLOBAL hItemData = 0; WDML_CONV* pConv = NULL; ATOM atom = 0; UINT count; TRACE("(%ld,0x%x,0x%x)\n", idInst, hszTopic, hszItem); EnterCriticalSection(&WDML_CritSect); pInstance = WDML_GetInstance(idInst); if (pInstance == NULL || pInstance->links == NULL) { goto theError; } atom = WDML_MakeAtomFromHsz(hszItem); if (!atom) goto theError; /* first compute the number of links which will trigger a message */ count = 0; for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next) { if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0) { count++; } } if (count >= CADV_LATEACK) { FIXME("too high value for count\n"); count &= 0xFFFF; } for (pLink = pInstance->links[WDML_SERVER_SIDE]; pLink != NULL; pLink = pLink->next) { if (DdeCmpStringHandles(hszItem, pLink->hszItem) == 0) { hDdeData = WDML_InvokeCallback(pInstance, XTYP_ADVREQ, pLink->uFmt, pLink->hConv, hszTopic, hszItem, 0, count--, 0); if (hDdeData == (HDDEDATA)CBR_BLOCK) { /* MS doc is not consistent here */ FIXME("CBR_BLOCK returned for ADVREQ\n"); continue; } if (hDdeData) { if (pLink->transactionType & XTYPF_NODATA) { TRACE("no data\n"); hItemData = 0; } else { TRACE("with data\n"); hItemData = WDML_DataHandle2Global(hDdeData, FALSE, FALSE, FALSE, FALSE); } pConv = WDML_GetConv(pLink->hConv, TRUE); if (pConv == NULL) { if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData); goto theError; } if (!PostMessageA(pConv->hwndClient, WM_DDE_DATA, (WPARAM)pConv->hwndServer, PackDDElParam(WM_DDE_DATA, (UINT)hItemData, atom))) { ERR("post message failed\n"); pConv->wStatus &= ~ST_CONNECTED; if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData); GlobalFree(hItemData); goto theError; } if (!WDML_IsAppOwned(hDdeData)) DdeFreeDataHandle(hDdeData); } } } LeaveCriticalSection(&WDML_CritSect); return TRUE; theError: LeaveCriticalSection(&WDML_CritSect); if (atom) GlobalDeleteAtom(atom); return FALSE; }
/****************************************************************** * WDML_CreateServerConv * * */ static WDML_CONV* WDML_CreateServerConv(WDML_INSTANCE* pInstance, HWND hwndClient, HWND hwndServerName, HSZ hszApp, HSZ hszTopic) { HWND hwndServerConv; WDML_CONV* pConv; if (pInstance->unicode) { WNDCLASSEXW wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = 0; wndclass.lpfnWndProc = WDML_ServerConvProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 2 * sizeof(DWORD); wndclass.hInstance = 0; wndclass.hIcon = 0; wndclass.hCursor = 0; wndclass.hbrBackground = 0; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = WDML_szServerConvClassW; wndclass.hIconSm = 0; RegisterClassExW(&wndclass); hwndServerConv = CreateWindowW(WDML_szServerConvClassW, 0, WS_CHILD, 0, 0, 0, 0, hwndServerName, 0, 0, 0); } else { WNDCLASSEXA wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = 0; wndclass.lpfnWndProc = WDML_ServerConvProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 2 * sizeof(DWORD); wndclass.hInstance = 0; wndclass.hIcon = 0; wndclass.hCursor = 0; wndclass.hbrBackground = 0; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = WDML_szServerConvClassA; wndclass.hIconSm = 0; RegisterClassExA(&wndclass); hwndServerConv = CreateWindowA(WDML_szServerConvClassA, 0, WS_CHILD, 0, 0, 0, 0, hwndServerName, 0, 0, 0); } TRACE("Created convServer=%04x (nameServer=%04x) for instance=%08lx\n", hwndServerConv, hwndServerName, pInstance->instanceID); pConv = WDML_AddConv(pInstance, WDML_SERVER_SIDE, hszApp, hszTopic, hwndClient, hwndServerConv); if (pConv) { SetWindowLongA(hwndServerConv, GWL_WDML_INSTANCE, (DWORD)pInstance); SetWindowLongA(hwndServerConv, GWL_WDML_CONVERSATION, (DWORD)pConv); /* this should be the only place using SendMessage for WM_DDE_ACK */ /* note: sent messages shall not use packing */ SendMessageA(hwndClient, WM_DDE_ACK, (WPARAM)hwndServerConv, MAKELPARAM(WDML_MakeAtomFromHsz(hszApp), WDML_MakeAtomFromHsz(hszTopic))); /* we assume we're connected since we've sent an answer... * I'm not sure what we can do... it doesn't look like the return value * of SendMessage is used... sigh... */ pConv->wStatus |= ST_CONNECTED; } else { DestroyWindow(hwndServerConv); } return pConv; }
/***************************************************************** * DdeConnect (USER32.@) */ HCONV WINAPI DdeConnect(DWORD idInst, HSZ hszService, HSZ hszTopic, PCONVCONTEXT pCC) { HWND hwndClient; WDML_INSTANCE* pInstance; WDML_CONV* pConv; ATOM aSrv = 0, aTpc = 0; TRACE("(0x%x,%p,%p,%p)\n", idInst, hszService, hszTopic, pCC); pInstance = WDML_GetInstance(idInst); if (!pInstance) return NULL; /* make sure this conv is never created */ pConv = WDML_FindConv(pInstance, WDML_CLIENT_SIDE, hszService, hszTopic); if (pConv != NULL) { ERR("This Conv already exists: (%p)\n", pConv); return NULL; } /* we need to establish a conversation with server, so create a window for it */ if (pInstance->unicode) { WNDCLASSEXW wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = 0; wndclass.lpfnWndProc = WDML_ClientProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR); wndclass.hInstance = 0; wndclass.hIcon = 0; wndclass.hCursor = 0; wndclass.hbrBackground = 0; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = WDML_szClientConvClassW; wndclass.hIconSm = 0; RegisterClassExW(&wndclass); hwndClient = CreateWindowW(WDML_szClientConvClassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0); } else { WNDCLASSEXA wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = 0; wndclass.lpfnWndProc = WDML_ClientProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 2 * sizeof(ULONG_PTR); wndclass.hInstance = 0; wndclass.hIcon = 0; wndclass.hCursor = 0; wndclass.hbrBackground = 0; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = WDML_szClientConvClassA; wndclass.hIconSm = 0; RegisterClassExA(&wndclass); hwndClient = CreateWindowA(WDML_szClientConvClassA, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0); } SetWindowLongPtrW(hwndClient, GWL_WDML_INSTANCE, (ULONG_PTR)pInstance); if (hszService) { aSrv = WDML_MakeAtomFromHsz(hszService); if (!aSrv) goto theEnd; } if (hszTopic) { aTpc = WDML_MakeAtomFromHsz(hszTopic); if (!aTpc) goto theEnd; } /* note: sent messages shall not use packing */ SendMessageTimeoutW( HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc), SMTO_ABORTIFHUNG, 0, NULL ); pInstance = WDML_GetInstance(idInst); if (!pInstance) { goto theEnd; } /* At this point, Client WM_DDE_ACK should have saved hwndServer for this instance id and hwndClient if server responds. So get HCONV and return it. And add it to conv list */ pConv = WDML_GetConvFromWnd(hwndClient); if (pConv == NULL || pConv->hwndServer == 0) { WARN("Done with INITIATE, but no Server window available\n"); pConv = NULL; pInstance->lastError = DMLERR_NO_CONV_ESTABLISHED; goto theEnd; } TRACE("Connected to Server window (%p)\n", pConv->hwndServer); pConv->wConvst = XST_CONNECTED; /* finish init of pConv */ if (pCC != NULL) { pConv->convContext = *pCC; } else { memset(&pConv->convContext, 0, sizeof(pConv->convContext)); pConv->convContext.cb = sizeof(pConv->convContext); pConv->convContext.iCodePage = (pInstance->unicode) ? CP_WINUNICODE : CP_WINANSI; } theEnd: if (aSrv) GlobalDeleteAtom(aSrv); if (aTpc) GlobalDeleteAtom(aTpc); return (HCONV)pConv; }
/***************************************************************** * DdeReconnect (DDEML.37) * DdeReconnect (USER32.@) */ HCONV WINAPI DdeReconnect(HCONV hConv) { WDML_CONV* pConv; WDML_CONV* pNewConv = NULL; ATOM aSrv = 0, aTpc = 0; TRACE("(%p)\n", hConv); pConv = WDML_GetConv(hConv, FALSE); if (pConv != NULL && (pConv->wStatus & ST_CLIENT)) { BOOL ret; /* to reestablish a connection, we have to make sure that: * 1/ pConv is the conversation attached to the client window (it wouldn't be * if a call to DdeReconnect would have already been done...) * FIXME: is this really an error ??? * 2/ the pConv conversation had really been deconnected */ if (pConv == WDML_GetConvFromWnd(pConv->hwndClient) && (pConv->wStatus & ST_TERMINATED) && !(pConv->wStatus & ST_CONNECTED)) { HWND hwndClient = pConv->hwndClient; HWND hwndServer = pConv->hwndServer; SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, 0); aSrv = WDML_MakeAtomFromHsz(pConv->hszService); aTpc = WDML_MakeAtomFromHsz(pConv->hszTopic); if (!aSrv || !aTpc) goto theEnd; /* note: sent messages shall not use packing */ ret = SendMessageW(hwndServer, WM_DDE_INITIATE, (WPARAM)hwndClient, MAKELPARAM(aSrv, aTpc)); pConv = WDML_GetConv(hConv, FALSE); if (pConv == NULL) { FIXME("Should fail reconnection\n"); goto theEnd; } if (ret && (pNewConv = WDML_GetConvFromWnd(pConv->hwndClient)) != NULL) { /* re-establish all links... */ WDML_LINK* pLink; for (pLink = pConv->instance->links[WDML_CLIENT_SIDE]; pLink; pLink = pLink->next) { if (pLink->hConv == hConv) { /* try to reestablish the links... */ DdeClientTransaction(NULL, 0, (HCONV)pNewConv, pLink->hszItem, pLink->uFmt, pLink->transactionType, 1000, NULL); } } } else { /* reset the conversation as it was */ SetWindowLongPtrW(pConv->hwndClient, GWL_WDML_CONVERSATION, (ULONG_PTR)pConv); } } } theEnd: if (aSrv) GlobalDeleteAtom(aSrv); if (aTpc) GlobalDeleteAtom(aTpc); return (HCONV)pNewConv; }