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

  1. /*
  2.  *   GlobalHandle
  3.  *   globlock.c
  4.  *
  5.  *   This program demonstrates the use of the GlobalHandle function. The
  6.  *   GlobalHandle function returns the handle of the specified global
  7.  *   memory block. GlobalHandle is called from WinMain in this sample
  8.  *   application.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"
  13. #include "dos.h"        /* FP_SEG located in dos.h */
  14.  
  15. int sprintf();
  16.  
  17. typedef struct {        /* structure we are going */
  18.         int x;        /* to allocate and lock   */
  19.         int y;
  20.            } MYSTRUCT;
  21.  
  22. typedef MYSTRUCT far *lpMyPtr;    /* far pointer to MYSTRUCT type */
  23.  
  24. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  25. HANDLE hInstance, hPrevInstance;
  26. LPSTR lpszCmdLine;
  27. int cmdShow;
  28. {
  29.     HANDLE hMemBlock1;  /* Handle to memory block */
  30.     DWORD hMemBlock2;   /* Handle to memory block returned from GlobalHandle */
  31.     lpMyPtr  FirstPtr;  /* Pointer to MYSTRUCT structure */
  32.     char szBuff[30];    /* buffer for message box     */
  33.  
  34. /* allocate space in global heap for a MYSTRUCT structure */
  35.     hMemBlock1 = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE,
  36.             (long)sizeof(MYSTRUCT));
  37.  
  38. /* if memory allocated properly */
  39.     if (hMemBlock1 != NULL)
  40.     {
  41.  
  42.     /* lock memory into current address */
  43.     FirstPtr = (lpMyPtr)GlobalLock(hMemBlock1);
  44.  
  45.     /* if lock worked */
  46.     if (FirstPtr != NULL)
  47.         {
  48.         /* get handle of segment pointed to by FirstPtr */
  49.         hMemBlock2 = GlobalHandle(FP_SEG(FirstPtr));
  50.  
  51.         /* if GlobalHandle returned original handle */
  52.         if ((HANDLE)hMemBlock2 == hMemBlock1)
  53.            MessageBox(NULL,(LPSTR)"DID return the correct handle",
  54.                    (LPSTR)"GlobalHandle...",MB_OK);
  55.         else
  56.            MessageBox(NULL,(LPSTR)"DID NOT return the correct handle",
  57.                    (LPSTR)"GlobalHandle...",MB_OK);
  58.  
  59.         GlobalUnlock(hMemBlock1);     /* unlock memory */
  60.         GlobalFree(hMemBlock1);     /* free memory   */
  61.         }
  62.     else
  63.         MessageBox(NULL,(LPSTR)"The lock DID NOT work properly",
  64.                (LPSTR)" ",MB_OK);
  65.     }
  66.     return 0;
  67. }
  68.