The following code fragment shows how to create a shared-memory object for a DDE transaction. The parameters include the destination window for the DDE message, item name for the transaction, status word, format of the data, actual data to be transferred (if any), and the length of the data. The allocated object must be big enough to hold the DDESTRUCT data structure, item name, and the actual data to be transferred. The sample returns a pointer (PDDESTRUCT) to a shared-memory object that is ready to post as part of a DDE message.

/*  Get some sharable memory */
DosAllocSharedMem((PVOID)&mem,
                  NULL,
                  sizeof(DDESTRUCT)+21,
                  PAG_COMMIT |
                  PAG_READ |
                  PAG_WRITE |
                  OBJ_GIVEABLE);

/*  Get the server's ID and give it access to the */
/*  shared memory                                 */
WinQueryWindowProcess(hServerWnd, &pid, &tid);
DosGiveSharedMem(&mem, pid, PAG_READ | PAG_WRITE);

/*  Setup DDE data structures                                */
/*  (11 byte name length, 10 plus NULL, 10 byte data length) */
pDDEdata           = (PDDESTRUCT)mem;
pDDEdata->cbData   = 10;              /* Data length         */
pDDEdata->fsStatus = 0;               /* Status              */
pDDEdata->usFormat = DDEFMT_TEXT;     /* Text format         */

/*  Go past end of data structure for the name  */
pDDEdata->offszItemName = sizeof(DDESTRUCT);

/*  Go past end of structure (plus past the name)  */
/*  for the data                                   */
pDDEdata->offabData = sizeof(DDESTRUCT)+11;
strcpy((BYTE *)(pDDEdata+(pDDEdata->offszItemName)),
                "STATUS");

/* Post our request to the server program */
WinDdePostMsg(hServerWnd,
              hwnd,
              WM_DDE_REQUEST,
              pDDEdata,
              DDEPM_RETRY);


[Back] [Next]