Application Can Allocate Memory with DdeCreateDataHandle (85680)



The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) 3.1
  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 3.5
    • the operating system: Microsoft Windows NT 3.51
    • Microsoft Windows 95

This article was previously published under Q85680

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.

Modification Type:MajorLast Reviewed:3/16/2004
Keywords:KB85680