Application Can Allocate Memory with DdeCreateDataHandle

Last reviewed: November 2, 1995
Article ID: Q85680
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

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 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrDde


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.