home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / CHAP1.ZIP / FREERES.C next >
Encoding:
C/C++ Source or Header  |  1992-06-07  |  2.8 KB  |  83 lines

  1. /*
  2.     FREERES.C -- Non WINIO application using MessageBox
  3.  
  4.     From Chapter 1 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6. */
  7.  
  8. #include <windows.h>
  9.  
  10. // from Petzold, Programming Windows, p. 441
  11. void OkMsgBox(char *szCaption, char *szFormat, ...)
  12. {
  13.     char szBuffer[256] ;
  14.     char *pArguments ;
  15.     
  16.     pArguments = (char *) &szFormat + sizeof szFormat ;
  17.     wvsprintf(szBuffer, szFormat, pArguments) ; // changed from vsprintf
  18.     MessageBox(NULL, szBuffer, szCaption, MB_OK) ;
  19. }
  20.  
  21. #define GET_PROC(modname, funcname) \
  22.     GetProcAddress(GetModuleHandle(modname), funcname)
  23.  
  24. void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
  25. {
  26.     static DWORD (FAR PASCAL *GetHeapSpaces)(WORD hModule) = 0;
  27.     DWORD info;
  28.     
  29.     if (! GetHeapSpaces)    // one-time initialization
  30.         if (! (GetHeapSpaces = GET_PROC("KERNEL", "GETHEAPSPACES")))
  31.             OkMsgBox("Error", "Can't find GetHeapSpaces\n");    
  32.  
  33.     /* In ANSI C and C++, pfunc(x) is identical to (*pfunc)(x) */
  34.     info = GetHeapSpaces(GetModuleHandle(module));
  35.     *pfree = LOWORD(info);
  36.     *ptotal = HIWORD(info);
  37.     *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
  38. }
  39.  
  40. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  41.     LPSTR lpszCmdLine, int nCmdShow)
  42. {
  43.     WORD vers = GetVersion();
  44.  
  45.     if ((LOBYTE(vers) == 3) && (HIBYTE(vers) == 0)) // 3.0
  46.     {
  47.         WORD user_free, user_total, user_percent;
  48.         WORD gdi_free, gdi_total, gdi_percent;
  49.  
  50.         heap_info("USER", &user_free, &user_total, &user_percent);
  51.         heap_info("GDI",  &gdi_free,  &gdi_total, &gdi_percent);
  52.  
  53.         OkMsgBox("System Resources",
  54.             "USER heap: %u bytes free out of %u (%u%% free)\n"
  55.             "GDI heap: %u bytes free out of %u (%u%% free)\n"
  56.             "Free system resources: %u%%\n",
  57.                 user_free, user_total, user_percent,
  58.                 gdi_free, gdi_total, gdi_percent,
  59.                 min(user_percent, gdi_percent));
  60.     }
  61.     else    // 3.1+
  62.     {
  63.         /*
  64.             This is actually unnecessary, because GetHeapSpaces()
  65.             seems to work just fine in Windows 3.1 anyway.
  66.         */
  67.  
  68.         WORD (FAR PASCAL *GetFreeSystemResources)(WORD id) =
  69.             GET_PROC("USER", "GETFREESYSTEMRESOURCES");
  70.         if (! GetFreeSystemResources)
  71.             OkMsgBox("Error", "Can't find GetFreeSystemResources\n");
  72.         else
  73.             OkMsgBox("System Resources",
  74.                 "USER heap: %u%% free\n"
  75.                 "GDI heap: %u%% free\n"
  76.                 "Free system resources: %u%%\n",
  77.                     GetFreeSystemResources(2),      // USER
  78.                     GetFreeSystemResources(1),      // GDI
  79.                     GetFreeSystemResources(0));     // total
  80.     }
  81.     return 0;
  82. }
  83.