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

  1. /*
  2.  *   GlobalUnlock
  3.  *
  4.  *   This program demonstrates the use of the GlobalUnlock function. The
  5.  *   GlobalUnlock function decrements the reference count of a block of
  6.  *   memory allocated in the Global heap. GlobalUnlock returns zero if the
  7.  *   reference count of the block is zero. GlobalUnlock was called from
  8.  *   WinMain in this sample application.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"
  13.  
  14. int sprintf();
  15.  
  16. typedef struct {        /* structure we are going */
  17.         int x;        /* to allocate and lock   */
  18.         int y;
  19.            } MYSTRUCT;
  20.  
  21. typedef MYSTRUCT far *lpMyPtr;    /* far pointer to MYSTRUCT type */
  22.  
  23. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  24. HANDLE hInstance, hPrevInstance;
  25. LPSTR lpszCmdLine;
  26. int cmdShow;
  27. {
  28.     HANDLE hMemBlock;      /* Handle to memory block */
  29.     lpMyPtr  ThisPtr;      /* Pointer to myStruct    */
  30.     char szBuff[30];      /* buffer for message box */
  31.     BOOL unlocked;      /* return value from GlobalUnlock */
  32.  
  33. /* allocate space in global heap for a MYSTRUCT structure */
  34.     hMemBlock = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE,
  35.             (long)sizeof(MYSTRUCT));
  36.  
  37. /* if memory allocated properly */
  38.     if (hMemBlock != NULL)
  39.     {
  40.  
  41.     /* lock memory into current address */
  42.     ThisPtr = (lpMyPtr)GlobalLock(hMemBlock);
  43.  
  44.     /* if lock worked */
  45.     if (ThisPtr != NULL)
  46.         {
  47.         MessageBox(NULL,(LPSTR)"The lock worked properly",
  48.                (LPSTR)" ",MB_OK);
  49.         /* use memory from global heap and output results*/
  50.         ThisPtr->x = 4;
  51.         ThisPtr->y = 4;
  52.         ThisPtr->x = ThisPtr->x*ThisPtr->y;
  53.         sprintf(szBuff,"ThisPtr->x * ThisPtr->y = %d",ThisPtr->x);
  54.         MessageBox(NULL,(LPSTR)szBuff,
  55.                 (LPSTR)"Info from GlobalLock'ed memory",
  56.                 MB_OK);
  57.  
  58.         /* unlock the block (decrement reference count) */
  59.         unlocked = GlobalUnlock(hMemBlock);
  60.         if (unlocked == 0)      /* if reference count == 0 */
  61.         MessageBox (NULL,(LPSTR)"The block was properly unlocked",
  62.                  (LPSTR)" ",MB_OK);
  63.         else    /* if (reference count != 0) */
  64.         MessageBox (NULL,(LPSTR)"The block was NOT unlocked",
  65.                  (LPSTR)" ",MB_OK);
  66.  
  67.         GlobalFree(hMemBlock);    /* free memory     */
  68.         }
  69.     else
  70.         MessageBox(NULL,(LPSTR)"The lock DID NOT work properly",
  71.                (LPSTR)" ",MB_OK);
  72.     }
  73.     return 0;
  74. }
  75.