home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / memory / swaparea.c next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  132 lines

  1. /*
  2. Name               : swaparea.c
  3. Date               : 07-05-88
  4. Function(s)
  5. demonstrated in
  6. this program       : SetSwapAreaSize(), GlobalCompact().
  7. Windows version    : 2.03
  8. Compiler version   : 5.1
  9. Description        : SetSwapAreaSize() increases the amount of memory that
  10.                      an application uses for its code segment.  The memory
  11.                      used for this application may not be allocated for
  12.                      data.
  13. */
  14.  
  15. #include <windows.h>
  16. #include "swaparea.h"
  17.  
  18. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  19. int sprintf(PSTR, PSTR, int);
  20.  
  21. static int ErrorCheck;
  22. static char szResName [] = "ResMenu";
  23.  
  24. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  25.      HANDLE      hInstance, hPrevInstance ;
  26.      LPSTR       lpszCmdLine ;
  27.      int         nCmdShow ;
  28.      {
  29.      static char szAppName [] = "swaparea" ;
  30.      HWND        hWnd ;
  31.      WNDCLASS    wndclass ;
  32.      MSG msg;
  33.  
  34.      if (!hPrevInstance) 
  35.           {
  36.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  37.           wndclass.lpfnWndProc   = WndProc ;
  38.           wndclass.cbClsExtra    = 0 ;
  39.           wndclass.cbWndExtra    = 0 ;
  40.           wndclass.hInstance     = hInstance ;
  41.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  42.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  43.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  44.           wndclass.lpszMenuName  = NULL ;
  45.           wndclass.lpszClassName = szAppName ;
  46.  
  47.           if (!RegisterClass (&wndclass))
  48.                return FALSE ;
  49.           }
  50.  
  51.      hWnd = CreateWindow (szAppName,            /* window class name       */
  52.                     "The Joy of Swapping",       /* window caption          */
  53.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  54.                     CW_USEDEFAULT,              /* initial x position      */
  55.                     0,                          /* initial y position      */
  56.                     CW_USEDEFAULT,              /* initial x size          */
  57.                     0,                          /* initial y size          */
  58.                     NULL,                       /* parent window handle    */
  59.                     NULL,                       /* window menu handle      */
  60.                     hInstance,                  /* program instance handle */
  61.                     NULL) ;                     /* create parameters       */
  62.  
  63.      ShowWindow (hWnd, nCmdShow) ;
  64.  
  65.      UpdateWindow (hWnd) ;
  66.  
  67.      while (GetMessage(&msg, NULL, 0, 0))
  68.      {
  69.       TranslateMessage(&msg);
  70.       DispatchMessage(&msg);
  71.      } 
  72.      return (msg.wParam) ;     
  73.      }
  74.  
  75. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  76. HWND     hWnd ;
  77. unsigned iMessage ;
  78. WORD     wParam ;
  79. LONG     lParam ;
  80. {
  81.  static HANDLE hInstance;
  82.  DWORD TotalFree;
  83.  PAINTSTRUCT ps;
  84.  char szBuf[50];
  85.  HMENU hMenu;
  86.  switch(iMessage)
  87.  {
  88.   case WM_CREATE:
  89.   {
  90.    hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);
  91.    hMenu = LoadMenu(hInstance, "ResMenu");
  92.    SetMenu(hWnd, hMenu);
  93.    DrawMenuBar(hWnd);
  94.    break;
  95.   }
  96.   case WM_PAINT:
  97.   {
  98.    BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  99.    EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  100.    break;
  101.   }
  102.   case WM_COMMAND:
  103.   {
  104.    switch(wParam)
  105.    {
  106.     case IDM_EXECUTE:
  107.     {
  108.      /* Compact the memory to maximize the amount of free space, then
  109.         allocate 1/2 the total available for this applications code
  110.         segment.  This can only be done once.
  111.      */
  112.      TotalFree = (GlobalCompact(NULL)/(16*1024))/2;    
  113.      ErrorCheck = SetSwapAreaSize((WORD)TotalFree);
  114.      sprintf(szBuf, "%i paragraphs that have been obtained for code segment use.", ErrorCheck);
  115.      MessageBox(GetFocus(), szBuf, "There are", MB_OK);
  116.     }
  117.    }
  118.    break;
  119.   }
  120.   case WM_DESTROY:
  121.   {
  122.    PostQuitMessage(0);
  123.    break;
  124.   }
  125.   default:
  126.   {
  127.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  128.   }
  129.  }
  130.  return (0L); 
  131. }
  132.