INF: Application Can Allocate Memory with DdeCreateDataHandle

ID Number: Q85680

3.10

WINDOWS

Summary:

An application can use the DdeCreateDataHandle function to create a

handle to a block of data. The application can use the handle to pass

the data to another application in a dynamic data exchange (DDE)

conversation using the Dynamic Data Exchange Management Libraries

(DDEML). All DDEML functions refer to blocks of memory using data

handles.

An application can allocate memory and manually create a data handle

associated with the memory (using method 1 below), or automatically by

using the DdeCreateDataHandle function (method 2 below).

Method 1

--------

1. Obtain a block of memory using the GlobalAlloc or LocalAlloc

function or by declaring a variable in your application.

2. Fill the block of memory with the desired data.

3. Call the DdeCreateDataHandle function to create a data handle

associated with the block of memory.

Method 2

--------

1. Call the DdeCreateDataHandle function with the lpvSrcBuf parameter

set to NULL, the cbInitData parameter set to zero, and the

offSrcBuf parameter set to the number of bytes of memory required.

2. To retrieve a handle to the memory block, specify the data handle

returned by DdeCreateDataHandle as the hData parameter of the

DdeAccessData function. This operation is similar to calling the

GlobalLock function on a handle returned from GlobalAlloc.

3. Use the pointer to fill the memory block with data.

4. Call DdeUnaccessData to unaccess the object. This operation is

similar to calling the GlobalUnlock function on a handle returned

from GlobalAlloc.

The following code fragment demonstrates method 2:

// Retrieve the length of the data to be stored

cbLen = lstrlen("This is a test") + 1;

// Create the data handle and allocate the memory

hData = DdeCreateDataHandle(idInst, NULL, 0, cbLen,

hszItem, wFmt, 0);

// Access the data handle

lpstrData = (LPSTR)DdeAccessData(hData, NULL);

// Fill the block of memory

lstrcpy(lpstrData, "This is a test");

// Unaccess the data handle

DdeUnaccessData(hData);

When an application obtains a data handle from DdeCreateDataHandle,

the application should next call DdeAccessData with the handle. If a

data handle is first specified as a parameter to a DDEML function

other than DdeAccessData, when the application later calls

DdeAccessData, the application receives only read access to the

associated memory block.

Additional reference words: 3.10