home *** CD-ROM | disk | FTP | other *** search
- /*
- FREERES.C -- Non WINIO application using MessageBox
-
- From Chapter 1 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
- */
-
- #include <windows.h>
-
- // from Petzold, Programming Windows, p. 441
- void OkMsgBox(char *szCaption, char *szFormat, ...)
- {
- char szBuffer[256] ;
- char *pArguments ;
-
- pArguments = (char *) &szFormat + sizeof szFormat ;
- wvsprintf(szBuffer, szFormat, pArguments) ; // changed from vsprintf
- MessageBox(NULL, szBuffer, szCaption, MB_OK) ;
- }
-
- #define GET_PROC(modname, funcname) \
- GetProcAddress(GetModuleHandle(modname), funcname)
-
- void heap_info(char *module, WORD *pfree, WORD *ptotal, WORD *ppercent)
- {
- static DWORD (FAR PASCAL *GetHeapSpaces)(WORD hModule) = 0;
- DWORD info;
-
- if (! GetHeapSpaces) // one-time initialization
- if (! (GetHeapSpaces = GET_PROC("KERNEL", "GETHEAPSPACES")))
- OkMsgBox("Error", "Can't find GetHeapSpaces\n");
-
- /* In ANSI C and C++, pfunc(x) is identical to (*pfunc)(x) */
- info = GetHeapSpaces(GetModuleHandle(module));
- *pfree = LOWORD(info);
- *ptotal = HIWORD(info);
- *ppercent = (WORD) ((((DWORD) *pfree) * 100L) / ((DWORD) *ptotal));
- }
-
- int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow)
- {
- WORD vers = GetVersion();
-
- if ((LOBYTE(vers) == 3) && (HIBYTE(vers) == 0)) // 3.0
- {
- WORD user_free, user_total, user_percent;
- WORD gdi_free, gdi_total, gdi_percent;
-
- heap_info("USER", &user_free, &user_total, &user_percent);
- heap_info("GDI", &gdi_free, &gdi_total, &gdi_percent);
-
- OkMsgBox("System Resources",
- "USER heap: %u bytes free out of %u (%u%% free)\n"
- "GDI heap: %u bytes free out of %u (%u%% free)\n"
- "Free system resources: %u%%\n",
- user_free, user_total, user_percent,
- gdi_free, gdi_total, gdi_percent,
- min(user_percent, gdi_percent));
- }
- else // 3.1+
- {
- /*
- This is actually unnecessary, because GetHeapSpaces()
- seems to work just fine in Windows 3.1 anyway.
- */
-
- WORD (FAR PASCAL *GetFreeSystemResources)(WORD id) =
- GET_PROC("USER", "GETFREESYSTEMRESOURCES");
- if (! GetFreeSystemResources)
- OkMsgBox("Error", "Can't find GetFreeSystemResources\n");
- else
- OkMsgBox("System Resources",
- "USER heap: %u%% free\n"
- "GDI heap: %u%% free\n"
- "Free system resources: %u%%\n",
- GetFreeSystemResources(2), // USER
- GetFreeSystemResources(1), // GDI
- GetFreeSystemResources(0)); // total
- }
- return 0;
- }
-