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

  1. /*
  2. Function(s) demonstrated in this program: GlobalLock
  3. Compiler version: 5.10
  4. Description: When the right mouse button is clicked, this program will
  5.          attempt to allocate and lock a small block of global memory.
  6.          As a check, an arithmetic operation is performed on data
  7.          stored in this global memory.
  8. */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12.  
  13. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  14.  
  15. typedef struct {     /* structure we are going */
  16.           int x;     /* to allocate and lock   */
  17.           int y;
  18.           } MYSTRUCT;
  19.  
  20. typedef MYSTRUCT far *lpMyPtr;   /* far pointer to MYSTRUCT type */
  21.  
  22. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  23. HANDLE    hInstance, hPrevInstance;
  24. LPSTR     lpszCmdLine;
  25. int       nCmdShow;
  26. {
  27.    static char szAppName [] = "globlock";
  28.    HWND     hWnd;
  29.    WNDCLASS wndclass;
  30.    MSG      msg;
  31.  
  32.    if (!hPrevInstance)
  33.       {
  34.       wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  35.       wndclass.lpfnWndProc   = WndProc;
  36.       wndclass.cbClsExtra    = 0;
  37.       wndclass.cbWndExtra    = 0;
  38.       wndclass.hInstance     = hInstance;
  39.       wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  40.       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  41.       wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  42.       wndclass.lpszMenuName  = NULL;
  43.       wndclass.lpszClassName = szAppName;
  44.  
  45.       if (!RegisterClass (&wndclass))
  46.          return FALSE;
  47.       }
  48.  
  49.    hWnd = CreateWindow (szAppName,   /* window class name   */
  50.                         "Using GlobalLock (Click the Right Mouse Button for Demo)",
  51.                         WS_OVERLAPPEDWINDOW,  /* window style            */
  52.                         CW_USEDEFAULT,        /* initial x position      */
  53.                         0,                    /* initial y position      */
  54.                         CW_USEDEFAULT,        /* initial x size          */
  55.                         0,                    /* initial y size          */
  56.                         NULL,                 /* parent window handle    */
  57.                         NULL,                 /* window menu handle      */
  58.                         hInstance,            /* program instance handle */
  59.                         NULL);                /* create parameters       */
  60.  
  61.    ShowWindow (hWnd, nCmdShow);
  62.    UpdateWindow (hWnd);
  63.  
  64.    while (GetMessage(&msg, NULL, 0, 0))
  65.       {
  66.       TranslateMessage(&msg);
  67.       DispatchMessage(&msg);
  68.       }
  69.    return (msg.wParam);
  70. }
  71.  
  72. long FAR PASCAL WndProc(hWnd, iMessage, wParam, lParam)
  73. HWND     hWnd;
  74. unsigned iMessage;
  75. WORD     wParam;
  76. LONG     lParam;
  77. {
  78.     HANDLE  hMemBlock;      /* Handle to memory block */
  79.     lpMyPtr    ThisPtr;      /* far pointer to MYSTRUCT structure */
  80.     char    szBuff[150];      /* buffer for message box */
  81.  
  82.     switch(iMessage)
  83.        {
  84.        case WM_RBUTTONDOWN:
  85.           {
  86.           hMemBlock = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE,
  87.                                   (long)sizeof(MYSTRUCT));
  88.  
  89.           /* if memory allocated properly */
  90.           if (hMemBlock)
  91.              {
  92.              /* lock memory into current address */
  93.              ThisPtr = (lpMyPtr)GlobalLock(hMemBlock);
  94.  
  95.              /* if lock worked */
  96.              if (ThisPtr)
  97.                 {
  98.                 MessageBox(GetFocus(),
  99.                            (LPSTR)"The lock worked properly",
  100.                            (LPSTR)"GlobalLock",
  101.                            MB_OK);
  102.                 /* use memory from global heap */
  103.                 ThisPtr->x = 4;
  104.                 ThisPtr->y = 4;
  105.                 ThisPtr->x = ThisPtr->x * ThisPtr->y;
  106.                 sprintf(szBuff, "%s%d", "The values referenced by these pointers are located\
  107.  in the global heap. \n\nThisPtr->x * ThisPtr->y = ", ThisPtr->x);
  108.                 MessageBox(GetFocus(), (LPSTR)szBuff,
  109.                            (LPSTR)"Info from GlobalLock'ed memory", MB_OK);
  110.                 GlobalUnlock(hMemBlock);   /* unlock memory */
  111.                 GlobalFree(hMemBlock);     /* free memory   */
  112.                 }
  113.              else
  114.                 {
  115.                 MessageBox(GetFocus(),
  116.                            (LPSTR)"The lock DID NOT work properly",
  117.                            (LPSTR)"GlobalLock", MB_OK);
  118.                 GlobalFree(hMemBlock);     /* free memory   */
  119.                 }
  120.              }
  121.           break;
  122.           }
  123.       case WM_DESTROY:
  124.          {
  125.          PostQuitMessage(0);
  126.          break;
  127.          }
  128.       default:
  129.          return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  130.    }
  131.    return (0L);
  132. }
  133.