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

  1. /**************************************************************************
  2.  *    LOCINIT    version 1.00
  3.  *
  4.  * This program tests the LocalInit function, which initializes the local
  5.  * heap at the specified segment location with the specified amount of
  6.  * memory for the heap.
  7.  *
  8.  **************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12.  
  13. #define    HEAPSIZE    1024
  14. #define    HEAPSEG    0
  15.  
  16. char    szPurpose[80] = "This is a program to initialize the local heap";
  17. HANDLE  hLocMem;
  18.  
  19. /**************************************************************************/
  20.  
  21. int     PASCAL  WinMain (HANDLE, HANDLE, LPSTR, int);
  22. BOOL              LocInit (HANDLE);
  23. long    FAR PASCAL      LocInitWndProc (HWND, unsigned, WORD, LONG);
  24.  
  25. /***************************************************************************/
  26.  
  27. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  28. HANDLE  hInstance;
  29. HANDLE  hPrevInstance;
  30. LPSTR   lpszCmdLine;
  31. int    cmdShow;
  32.   {
  33.   MSG    msg;
  34.   BOOL    Init;
  35.   HANDLE    hInst;
  36.   HWND    hWnd;
  37.   char    MesBuf[80];
  38.   WORD    wMemsize;
  39.  
  40.   MessageBox (GetFocus (), (LPSTR)szPurpose, (LPSTR)"LOCINIT", MB_OK);
  41.  
  42.   if (!hPrevInstance)
  43. /* Ensure that windows knows where to find parts of this
  44.          * task on disk by Registering a window class. Registering
  45.                  * a class binds an executable name to an internal name,
  46.          * known to Windows. */
  47.     if (LocInit (hInstance) == FALSE)
  48.       return FALSE;
  49.  
  50.   hInst = hInstance;
  51.  
  52.   hWnd = CreateWindow ( (LPSTR) "locinit", /* Window class name */
  53.   (LPSTR) "locinit", /* Window title */
  54.   WS_OVERLAPPEDWINDOW,
  55.       CW_USEDEFAULT,
  56.       CW_USEDEFAULT,
  57.       CW_USEDEFAULT,
  58.       CW_USEDEFAULT,
  59.       (HWND)NULL,               /* No parent */
  60.   (HMENU)NULL,       /* Use the class menu */
  61.   (HANDLE)hInstance,  /* .EXE file for Class */
  62.   (LPSTR)NULL           /* No Parameters */
  63.  );
  64.  
  65.   ShowWindow (hWnd, cmdShow);
  66.   UpdateWindow (hWnd);             /* Paint the client area */
  67.  
  68.   wMemsize = LocalSize (hLocMem);
  69.  
  70.   sprintf (MesBuf, "New size of local heap: %d\nLocated at Segment: %d",
  71.       HEAPSIZE, HEAPSEG);
  72.  
  73.   MessageBox (hWnd, (LPSTR)MesBuf, (LPSTR)"LocalInit", MB_OK);
  74.  
  75.   MessageBox (hWnd, (LPSTR)"Initialized and Accessed Local Heap Succesfully",
  76.       (LPSTR)"LocalInit", MB_OK);
  77.  
  78.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  79.     {
  80.     TranslateMessage (&msg);
  81.     DispatchMessage (&msg);
  82.     }
  83.   return (msg.wParam);
  84.   }
  85.  
  86.  
  87. /* WINMAIN */
  88. /***************************************************************************/
  89.  
  90. BOOL    LocInit (hInstance)
  91. HANDLE    hInstance;
  92.   {
  93.   NPWNDCLASS    npLocInitClass;
  94.   BOOL            bLocInit;
  95.  
  96.   bLocInit = LocalInit ( (WORD)HEAPSEG, (WORD)0, (char *)HEAPSIZE);
  97.  
  98.   if (!bLocInit)
  99.     MessageBox (GetFocus (), (LPSTR)"Local Initialization of Heap failed",
  100.         (LPSTR)"LOCINIT", MB_ICONHAND | MB_OK);
  101.   return FALSE;
  102.  
  103.   hLocMem = LocalAlloc (LMEM_DISCARDABLE, sizeof (WNDCLASS));
  104.   npLocInitClass = (NPWNDCLASS)LocalLock (hLocMem);
  105.  
  106.   npLocInitClass->hCursor       = LoadCursor (NULL, IDC_ARROW);
  107.   npLocInitClass->hIcon         = NULL;
  108.   npLocInitClass->lpszMenuName  = (LPSTR)NULL;
  109.   npLocInitClass->lpszClassName = (LPSTR)"LocInit";
  110.   npLocInitClass->hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  111.   npLocInitClass->hInstance     = hInstance;
  112.   npLocInitClass->style         = CS_HREDRAW | CS_VREDRAW;
  113.   npLocInitClass->lpfnWndProc   = LocInitWndProc;
  114.   npLocInitClass->cbClsExtra    = 0;
  115.   npLocInitClass->cbWndExtra    = 0;
  116.  
  117.   if (!RegisterClass ( (NPWNDCLASS)npLocInitClass))
  118. /* Initialization failed.
  119.          * Windows will automatically deallocate all allocated memory.
  120.          */
  121.     return (FALSE);
  122.  
  123.   return (TRUE);
  124.  
  125.   }
  126.  
  127.  
  128. /* END locinitINIT */
  129.  
  130. /***************************** WINDOWPROC *****************************/
  131. /*    Every message dispatched to any window of type "locinit" will
  132.  *    result in a call to this procedure.
  133.  */
  134. long    FAR PASCAL LocInitWndProc (hWnd, identifier, wParam, lParam)
  135. HWND        hWnd;        /* Intended window */
  136. unsigned    identifier;    /* Message Number */
  137. WORD        wParam;        /* 16 bit param */
  138. LONG        lParam;        /* 32 bit param */
  139.   {
  140.   switch (identifier)
  141.     {
  142.     default:
  143.       return DefWindowProc (hWnd, identifier, wParam, lParam);
  144.       break;
  145.     }
  146.  
  147.   return (0L);
  148.   } /* windowproc */
  149.