home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / memory / global / globwire.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  1.8 KB  |  58 lines

  1. /*
  2.  *   GlobalWire
  3.  *   globwire.c
  4.  *
  5.  *   This program demonstrates the use of the GlobalWire function. The
  6.  *   GlobalWire function locks a block into low memory. GlobalWire is
  7.  *   called from WinMain in this sample application. GlobalWire is often
  8.  *   used when a block is going to be locked in memory for a long time
  9.  *   so there will be larger continuous free spaces than if GlobalLock was
  10.  *   used. Before allocation  and after GlobalWire'ing, GlobalCompact is
  11.  *   called to show the affect of locking the block in memory has on the
  12.  *   largest continuous block
  13.  *
  14.  */
  15.  
  16. #include "windows.h"
  17.  
  18. int sprintf();
  19.  
  20. typedef struct {
  21.         char smallstruct[20];
  22.            } SMALLSTRUCT;          /* structure we're going to allocate */
  23.                         /*   space for      */
  24. typedef SMALLSTRUCT far *lpSMALLPtr;  /* pointer to SMALLSTRUCT structure */
  25.  
  26. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  27. HANDLE hInstance, hPrevInstance;
  28. LPSTR lpszCmdLine;
  29. int cmdShow;
  30. {
  31.     HANDLE       hMemBlock;         /* Handle to memory block        */
  32.     lpSMALLPtr       ThisPtr;         /* Pointer to myStruct        */
  33.     char       szBuff[80];         /* buffer for output        */
  34.     DWORD       ContigFreeBytes;  /* return value from GlobalCompact */
  35.  
  36. /* Allocate space for SMALLSTRUCT in global heap */
  37.     hMemBlock = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE,
  38.             (long)sizeof(SMALLSTRUCT));
  39. /* if memory allocated properly */
  40.     if (hMemBlock)
  41.     /* GlobalWire the block into low memory */
  42.     ThisPtr = (lpSMALLPtr)GlobalWire(hMemBlock);
  43.     if (ThisPtr == NULL)
  44.         MessageBox(NULL,(LPSTR)"GlobalWire Failure",
  45.                 (LPSTR)" ",MB_OK);
  46.     else
  47.         MessageBox(NULL,(LPSTR)"GlobalWire worked properly",
  48.                 (LPSTR)" ",MB_OK);
  49.  
  50. /* GlobalUnwire the block */
  51.     GlobalUnWire(hMemBlock);
  52.  
  53. /* Free the block */
  54.     GlobalFree(hMemBlock);
  55.  
  56.     return 0;
  57. }
  58.