home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / CHAP5.ZIP / SYSTRES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.6 KB  |  75 lines

  1. /*
  2.     SYSTRES.C -- Shows System Resource usage
  3.  
  4.     From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.  
  7.     Build using: WINIOBC SYSTRES (for Borland C++ v3.00)
  8.                  WINIOMS SYSTRES (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h>
  12. #include "winio.h"
  13.  
  14. /* undocumented Windows call to use in 3.0 */
  15. extern DWORD FAR PASCAL GetHeapSpaces(WORD hModule);
  16.  
  17. /* Windows 3.1 function may not be in WINDOWS.H */
  18. WORD (FAR PASCAL *GetFreeSystemResources)(WORD wNum);
  19.  
  20. void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
  21. {
  22.     DWORD info = GetHeapSpaces(GetModuleHandle(module));
  23.     *pfree = LOWORD(info);
  24.     *ptotal = HIWORD(info);
  25.     *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
  26. }
  27.  
  28. main()
  29. {
  30.     WORD user_free, user_total, user_percent;
  31.     WORD gdi_free, gdi_total, gdi_percent;
  32.     WORD min_percent, diff;
  33.     WORD vers = GetVersion();
  34.  
  35.     winio_about("SYSTRES"
  36.         "\nShows System Resource usage"
  37.         "\n\nFrom Chapter 5 of"
  38.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  39.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  40.         );
  41.     
  42.     heap_info("USER", &user_free, &user_total, &user_percent);
  43.     heap_info("GDI",  &gdi_free,  &gdi_total, &gdi_percent);
  44.  
  45.     printf("Using GetHeapSpaces:\n");
  46.     printf("USER heap: %u bytes free out of %u (%u%% free)\n",
  47.         user_free, user_total, user_percent);
  48.     printf("GDI heap: %u bytes free out of %u (%u%% free)\n",
  49.         gdi_free, gdi_total, gdi_percent);
  50.     min_percent = min(user_percent, gdi_percent);
  51.     printf("Free system resources: %u%%\n", min_percent);
  52.     
  53.     if ((LOBYTE(vers) >= 3) && (HIBYTE(vers) >= 0x0a))  // 3.1+
  54.     {   // What's it doing in USER?!
  55.         GetFreeSystemResources = GetProcAddress(GetModuleHandle("USER"),
  56.             "GETFREESYSTEMRESOURCES");
  57.         puts("\nUsing GetFreeSystemResources:");
  58.         printf("USER heap: %u%% free\n", GetFreeSystemResources(2));
  59.         printf("GDI heap: %u%% free\n", GetFreeSystemResources(1));
  60.         printf("Free system resources: %u%%\n", GetFreeSystemResources(0));
  61.         
  62.         printf("\n");
  63.         if ((diff = GetFreeSystemResources(2) - user_percent) != 0)
  64.             printf("USER off by %u%%\n", abs(diff));
  65.         if ((diff = GetFreeSystemResources(1) - gdi_percent) != 0)
  66.             printf("GDI off by %u%%\n", abs(diff));
  67.         if ((diff = GetFreeSystemResources(0) - min_percent) != 0)
  68.             printf("FSR off by %u%%\n", abs(diff));
  69.         // no matter what, never find it off by more than 1%
  70.     }
  71.  
  72.     return 0;
  73. }
  74.  
  75.