Platform SDK: Interprocess Communications |
To retrieve an item from the server, the client sends the server a WM_DDE_REQUEST message specifying the item and format to retrieve, as shown in the following example.
if ((atomItem = GlobalAddAtom(szItemName)) != 0) { if (!PostMessage(hwndServerDDE, WM_DDE_REQUEST, (WPARAM) hwndClientDDE, PackDDElParam(WM_DDE_REQUEST, CF_TEXT, atomItem))) { GlobalDeleteAtom(atomItem); } } if (atomItem == 0) { // Handle errors. }
In this example, the client specifies the clipboard format CF_TEXT as the preferred format for the requested data item.
The receiver (server) of the WM_DDE_REQUEST message typically must delete the item atom, but if the PostMessage call fails, the client must delete the atom.
If the server has access to the requested item and can render it in the requested format, the server copies the item value as a shared memory object and sends the client a WM_DDE_DATA message, as illustrated in the following example.
// Allocate the size of the DDE data header, plus the data: a // string,<CR><LF><NULL>. The byte for the string's terminating // null character is counted by DDEDATA.Value[1]. if (!(hData = GlobalAlloc(GMEM_MOVEABLE, (LONG) sizeof(DDEDATA) + lstrlen(szItemValue) + 2))) { return; } if (!(lpData = (DDEDATA FAR*) GlobalLock(hData))) { GlobalFree(hData); return; } lpData->cfFormat = CF_TEXT; lstrcpy((LPSTR) lpData->Value, (LPSTR) szItemValue); // Each line of CF_TEXT data is terminated by CR/LF. lstrcat((LPSTR) lpData->Value, (LPSTR) "\r\n"); GlobalUnlock(hData); if ((atomItem = GlobalAddAtom((LPSTR) szItemName)) != 0) { lParam = PackDDElParam(WM_DDE_ACK, (UINT) hData, atomItem); if (!PostMessage(hwndClientDDE, WM_DDE_DATA, (WPARAM) hwndServerDDE, lParam)) { GlobalFree(hData); GlobalDeleteAtom(atomItem); FreeDDElParam(WM_DDE_ACK, lParam); } } if (atomItem == 0) { // Handle errors. }
In this example, the server application allocates a memory object to contain the data item. The data object is initialized as a DDEDATA structure.
The server application then sets the cfFormat member of the structure to CF_TEXT to inform the client application that the data is in text format. The client responds by copying the value of the requested data into the Value member of the DDEDATA structure. After the server has filled the data object, the server unlocks the data and creates a global atom containing the name of the data item.
Finally, the server issues the WM_DDE_DATA message by calling PostMessage. The handle to the data object and the atom containing the item name are packed into the lParam parameter of the message by the PackDDElParam function.
If PostMessage fails, the server must use the FreeDDElParam function to free the packed lParam parameter. The server must also free the packed lParam parameter for the WM_DDE_REQUEST message it received.
If the server cannot satisfy the request, it sends a negative WM_DDE_ACK message to the client, as shown in the following example.
// Negative acknowledgment. PostMessage(hwndClientDDE, WM_DDE_ACK, (WPARAM) hwndServerDDE, PackDDElParam(WM_DDE_ACK, 0, atomItem));
Upon receiving a WM_DDE_DATA message, the client processes the data-item value as appropriate. Then, if the fAckReq member pointed to in the WM_DDE_DATA message is 1, the client must send the server a positive WM_DDE_ACK message, as shown in the following example.
UnpackDDElParam(WM_DDE_DATA, lParam, (PUINT) &hData, (PUINT) &atomItem); if (!(lpDDEData = (DDEDATA FAR*) GlobalLock(hData)) || (lpDDEData->cfFormat != CF_TEXT)) { PostMessage(hwndServerDDE, WM_DDE_ACK, (WPARAM) hwndClientDDE, PackDDElParam(WM_DDE_ACK, 0, atomItem)); // Negative ACK. } // Copy data from lpDDEData here. if (lpDDEData->fAckReq) { PostMessage(hwndServerDDE, WM_DDE_ACK, (WPARAM) hwndClientDDE, PackDDElParam(WM_DDE_ACK, 0x8000, atomItem)); // Positive ACK } bRelease = lpDDEData->fRelease; GlobalUnlock(hData); if (bRelease) GlobalFree(hData);
In this example, the client examines the format of the data. If the format is not CF_TEXT (or if the client cannot lock the memory for the data), the client sends a negative WM_DDE_ACK message to indicate that it cannot process the data. If the client cannot lock a data handle because the handle contains the fAckReq member, the client should not send a negative WM_DDE_ACK message. Instead, the client should terminate the conversation.
If a client sends a negative acknowledgement in response to a WM_DDE_DATA message, the server is responsible for freeing the memory (but not the lParam parameter) referenced by the WM_DDE_DATA message associated with the negative acknowledgement.
If it can process the data, the client examines the fAckReq member of the DDEDATA structure to determine whether the server requested that it be informed that the client received and processed the data successfully. If the server did request this information, the client sends the server a positive WM_DDE_ACK message.
Because unlocking data invalidates the pointer to the data, the client saves the value of the fRelease member before unlocking the data object. After saving the value, the client then examines it to determine whether the server application requested the client to free the memory containing the data; the client acts accordingly.
Upon receiving a negative WM_DDE_ACK message, the client can ask for the same item value again, specifying a different clipboard format. Typically, a client will first ask for the most complex format it can support, then step down if necessary through progressively simpler formats until it finds one the server can provide.
If the server supports the Formats item of the system topic, the client can determine once what clipboard formats the server supports, instead of determining them each time the client requests an item. For more information about the system topic, see The System Topic.