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

  1. /*
  2. Name               : valfresp.c
  3. Function(s)
  4. demonstrated in
  5. this program       : ValidateFreeSpaces()
  6. Windows version    : 2.03
  7. Compiler version   : 5.1
  8. Description        : ValidateFreeSpaces() checks free memory for valid
  9.                      contents, returning the address of the first invalid
  10.                      byte or in the event of no invalid bytes, NULL
  11. */
  12.  
  13. #include <windows.h>
  14. #include "valfresp.h"
  15.  
  16. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  17. int sprintf(PSTR, PSTR, int);
  18.  
  19. static int ErrorCheck;
  20. static char szResName [] = "ResMenu";
  21.  
  22. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  23.      HANDLE      hInstance, hPrevInstance ;
  24.      LPSTR       lpszCmdLine ;
  25.      int         nCmdShow ;
  26.      {
  27.      static char szAppName [] = "valfresp" ;
  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.                     "ValidateFreeSpaces",       /* window caption          */
  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.  
  63.      UpdateWindow (hWnd) ;
  64.  
  65.      while (GetMessage(&msg, NULL, 0, 0))
  66.      {
  67.       TranslateMessage(&msg);
  68.       DispatchMessage(&msg);
  69.      } 
  70.      return (msg.wParam) ;     
  71.      }
  72.  
  73. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  74. HWND     hWnd ;
  75. unsigned iMessage ;
  76. WORD     wParam ;
  77. LONG     lParam ;
  78. {
  79.  static HANDLE hInstance;
  80.  DWORD TotalFree;
  81.  PAINTSTRUCT ps;
  82.  char szBuf[50];
  83.  LPSTR BoneBad;
  84.  HMENU hMenu;
  85.  switch(iMessage)
  86.  {
  87.   case WM_CREATE:
  88.   {
  89.    hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);
  90.    hMenu = LoadMenu(hInstance, "ResMenu");
  91.    SetMenu(hWnd, hMenu);
  92.    DrawMenuBar(hWnd);
  93.    break;
  94.   }
  95.   case WM_PAINT:
  96.   {
  97.    BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  98.    EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  99.    break;
  100.   }
  101.   case WM_COMMAND:
  102.   {
  103.    switch(wParam)
  104.    {
  105.     case IDM_EXECUTE:
  106.     {
  107.      BoneBad = ValidateFreeSpaces();
  108.      if (BoneBad!=NULL)
  109.      {
  110.       sprintf(szBuf, "address %iH contains an invalid byte",(unsigned)BoneBad);
  111.       MessageBox(GetFocus(), szBuf, "ABORTING!", MB_OK);
  112.      } else
  113.      {
  114.       MessageBox(GetFocus(), "No invalid bytes encountered", "FINISHED", MB_OK);
  115.      }
  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.