home *** CD-ROM | disk | FTP | other *** search
- /*
- SYSTRES.C -- Shows System Resource usage
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC SYSTRES (for Borland C++ v3.00)
- WINIOMS SYSTRES (for Microsoft C/SDK)
- */
-
- #include <windows.h>
- #include "winio.h"
-
- /* undocumented Windows call to use in 3.0 */
- extern DWORD FAR PASCAL GetHeapSpaces(WORD hModule);
-
- /* Windows 3.1 function may not be in WINDOWS.H */
- WORD (FAR PASCAL *GetFreeSystemResources)(WORD wNum);
-
- void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
- {
- DWORD info = GetHeapSpaces(GetModuleHandle(module));
- *pfree = LOWORD(info);
- *ptotal = HIWORD(info);
- *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
- }
-
- main()
- {
- WORD user_free, user_total, user_percent;
- WORD gdi_free, gdi_total, gdi_percent;
- WORD min_percent, diff;
- WORD vers = GetVersion();
-
- winio_about("SYSTRES"
- "\nShows System Resource usage"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- heap_info("USER", &user_free, &user_total, &user_percent);
- heap_info("GDI", &gdi_free, &gdi_total, &gdi_percent);
-
- printf("Using GetHeapSpaces:\n");
- printf("USER heap: %u bytes free out of %u (%u%% free)\n",
- user_free, user_total, user_percent);
- printf("GDI heap: %u bytes free out of %u (%u%% free)\n",
- gdi_free, gdi_total, gdi_percent);
- min_percent = min(user_percent, gdi_percent);
- printf("Free system resources: %u%%\n", min_percent);
-
- if ((LOBYTE(vers) >= 3) && (HIBYTE(vers) >= 0x0a)) // 3.1+
- { // What's it doing in USER?!
- GetFreeSystemResources = GetProcAddress(GetModuleHandle("USER"),
- "GETFREESYSTEMRESOURCES");
- puts("\nUsing GetFreeSystemResources:");
- printf("USER heap: %u%% free\n", GetFreeSystemResources(2));
- printf("GDI heap: %u%% free\n", GetFreeSystemResources(1));
- printf("Free system resources: %u%%\n", GetFreeSystemResources(0));
-
- printf("\n");
- if ((diff = GetFreeSystemResources(2) - user_percent) != 0)
- printf("USER off by %u%%\n", abs(diff));
- if ((diff = GetFreeSystemResources(1) - gdi_percent) != 0)
- printf("GDI off by %u%%\n", abs(diff));
- if ((diff = GetFreeSystemResources(0) - min_percent) != 0)
- printf("FSR off by %u%%\n", abs(diff));
- // no matter what, never find it off by more than 1%
- }
-
- return 0;
- }
-
-