当前位置:在线查询网 > 在线百科全书查询 > Dynamic Data Exchange

Dynamic Data Exchange_在线百科全书查询


请输入要查询的词条内容:

Dynamic Data Exchange


提供了应用程序间数据传输的若干种方法。其中一种就是使用动态数据交换(DDE)协议。DDE协议是一套消息和指示的集合。通过发送消息以及共享内存实现应用程序的数据共享和交换。应用程序可以使用DDE协议实现一次性数据传输以及持续的数据交换(当新数据可用时,应用程序发送更新通知给另一个应用程序)。



概念


Microsoft® Windows® provides several methods for transferring data between applications. One method is to use the dynamic data exchange (DDE) protocol. The DDE protocol is a set of messages and guidelines. It sends messages between applications that share data and uses shared memory to exchange data between applications. Applications can use the DDE protocol for one-time data transfers and for continuous exchanges in which applications send updates to one another as new data becomes available.

示例


The following example illustrates how the client initiates a conversation, where both the application and topic are specified.

static BOOL fInInitiate = FALSE;

char *szApplication;

char *szTopic;

atomApplication = *szApplication == 0 ?

NULL : GlobalAddAtom((LPSTR) szApplication);

atomTopic = *szTopic == 0 ?

NULL : GlobalAddAtom((LPSTR) szTopic);

fInInitiate = TRUE;

SendMessage((HWND) HWND_BROADCAST, // broadcasts message

WM_DDE_INITIATE, // initiates conversation

(WPARAM) hwndClientDDE, // handle to client DDE window

MAKELONG(atomApplication, // application-name atom

atomTopic)); // topic-name atom

fInInitiate = FALSE;

if (atomApplication != NULL)

GlobalDeleteAtom(atomApplication);

if (atomTopic != NULL)

GlobalDeleteAtom(atomTopic);

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\");

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);

相关分词: Dynamic Data Exchange