home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / msjall.zip / MSJV2-1.ZIP / FREEMEM.ALL < prev   
Text File  |  1987-10-30  |  6KB  |  213 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 1; March, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     FREEMEM
  7.     pp. 33-40
  8.  
  9. Author(s): Charles Petzold
  10. Title:     Keep Track of Your Windows Memory With FREEMEM
  11.  
  12.  
  13.  
  14. ==============================================================================
  15. ==============================================================================
  16. Figure 1: WinMain Function of FREEMEM.C
  17.  
  18. ==============================================================================
  19.  
  20. /*  FreeMem.C -- Windows application that displays free memory */
  21.  
  22. #include <windows.h>    /* all Windows functions */
  23. #include <stdlib.h>     /* itoa */
  24. #include <string.h>     /* strcat & strlen */
  25.  
  26. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  27.  
  28. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  29.     HANDLE  hInstance, hPrevInstance ;
  30.     LPSTR   lpszCmdLine ;
  31.     int     nCmdShow ;
  32.     {
  33.     static  char szAppName [] = "FreeMem" ;
  34.     WNDCLASS WndClass ;
  35.     HWND    hWnd ;
  36.     MSG     msg ;
  37.  
  38.     if (hPrevInstance) 
  39.         return FALSE ;
  40.  
  41.     WndClass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  42.     WndClass.hIcon         = NULL ;
  43.     WndClass.cbClsExtra    = 0 ;
  44.     WndClass.cbWndExtra    = 0 ;
  45.     WndClass.lpszMenuName  = NULL ;
  46.     WndClass.lpszClassName = (LPSTR) szAppName ;
  47.     WndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  48.     WndClass.hInstance     = hInstance ;
  49.     WndClass.style         = CS_HREDRAW | CS_VREDRAW;
  50.     WndClass.lpfnWndProc   = WndProc ;
  51.  
  52.     if (!RegisterClass ((LPWNDCLASS) &WndClass))
  53.         return FALSE ;
  54.  
  55.     hWnd = CreateWindow ((LPSTR) szAppName,
  56.             (LPSTR) szAppName,
  57.             WS_TILEDWINDOW,
  58.             0, 0, 0, 0,
  59.             (HWND)   NULL,
  60.  
  61.             (HMENU)  NULL,
  62.             (HANDLE) hInstance,
  63.             (LPSTR)  NULL) ;
  64.  
  65.     if (!SetTimer (hWnd, 1, 1000, NULL))
  66.         return FALSE ;
  67.  
  68.     ShowWindow (hWnd, SHOW_ICONWINDOW) ;
  69.     UpdateWindow (hWnd) ;
  70.  
  71.     while (GetMessage ((LPMSG) &msg, NULL, 0, 0))
  72.         {
  73.         TranslateMessage ((LPMSG) &msg) ;
  74.         DispatchMessage ((LPMSG) &msg) ;
  75.         }
  76.     return (int) msg.wParam ;
  77.     }
  78. ==============================================================================
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. ==============================================================================
  91. ==============================================================================
  92. Figure 2: WndProc Function of FREEMEM.C
  93.  
  94. ==============================================================================
  95.  
  96. long FAR PASCAL WndProc (hWnd, message, wParam, lParam)
  97.     HWND    hWnd ;
  98.     unsigned message ;
  99.     WORD    wParam ;
  100.     LONG    lParam ;
  101.     {
  102.     static  int mem, lastmem ;
  103.     char    buffer [20] ;
  104.     PAINTSTRUCT ps ;
  105.     RECT    rect ;
  106.  
  107.     switch (message)
  108.         {
  109.         case WM_TIMER:
  110.             mem = (int) (GlobalCompact (0L) / 1024) ;
  111.             if (mem != lastmem)
  112.                 InvalidateRect (hWnd, NULL, TRUE) ;
  113.             lastmem = mem ;
  114.             break ;
  115.  
  116.         case WM_PAINT:
  117.             BeginPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
  118.             GetClientRect (hWnd, (LPRECT) &rect) ;
  119.             DrawText (ps.hdc, (LPSTR) buffer,
  120.                 strlen (strcat (itoa (mem, buffer, 10), "K Free")),
  121.                 (LPRECT) &rect, DT_WORDBREAK) ;
  122.             EndPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
  123.             break ;
  124.  
  125.         case WM_DESTROY:
  126.             KillTimer (hWnd, 1) ;
  127.             PostQuitMessage (0) ;
  128.             break ;
  129.  
  130.         default:
  131.             return DefWindowProc (hWnd, message, wParam, lParam) ;
  132.         }
  133.     return (long) 0 ;
  134.     }
  135. ==============================================================================
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. ==============================================================================
  151. ==============================================================================
  152. Figure 3: Alternate SetTimer Logic
  153.  
  154. ==============================================================================
  155.  
  156. if (!SetTimer (hWnd, 1, 1000, NULL)
  157.      {
  158.      MessageBox (hWnd, "Hey! Too many timers!", NULL, MB_OK) ;
  159.      return FALSE ;
  160.      }
  161. ==============================================================================
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. ==============================================================================
  174. ==============================================================================
  175. Figure 4: Module Definition File FREEMEM.DEF
  176.  
  177. ==============================================================================
  178.  
  179. NAME    FreeMem
  180.  
  181. DESCRIPTION 'Free Memory Display by Charles Petzold'
  182.  
  183. STUB    'WINSTUB.EXE'
  184.  
  185. CODE    MOVEABLE
  186. DATA    MOVEABLE MULTIPLE
  187.  
  188. HEAPSIZE  1024
  189. STACKSIZE 4096
  190.  
  191. EXPORTS
  192.     WndProc @1
  193. ==============================================================================
  194.  
  195.  
  196.  
  197. ==============================================================================
  198. ==============================================================================
  199. Figure 5: FREEMEM Make-File
  200.  
  201. ==============================================================================
  202.  
  203. # Make file for FREEMEM -- assumes Microsoft C 4.0
  204. # ------------------------------------------------
  205.  
  206. freemem.obj : freemem.c
  207.     cl -c -d -Gsw -Os -W2 -Zdp freemem.c
  208.  
  209. freemem.exe : freemem.obj freemem.def
  210.     link4 freemem, /align:16, /map/line, slibw, freemem
  211.     mapsym freemem
  212. ==============================================================================
  213.