Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2006, 10:04 AM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Very simple memory status program in C++ (Windows)

Well you can just open the taks manager (taskmgr.exe / ctrl+alt+del), but this migth be useful in certain circumstances. (Or maybe you can't find it on google as fast.) I basically changed the printf's from MSDN to cout.
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalmemorystatusex.asp

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
	const int MB = 1048576; // A megabyte is 1,048,576 bytes
	MEMORYSTATUSEX statex;

	statex.dwLength = sizeof (statex);
	GlobalMemoryStatusEx (&statex);

	cout << statex.dwMemoryLoad << " percent of memory is in use." << endl;
	cout << endl;
	cout << "There are " << statex.ullTotalPhys/MB << " total megabytes of physical memory." << endl;
	cout << "There are " << statex.ullAvailPhys/MB << " free megabytes of physical memory." << endl;
	cout << endl;
	cout << "There are " << statex.ullTotalPageFile/MB << " total megabytes of paging file." << endl;
	cout << "There are " << statex.ullAvailPageFile/MB << " free megabytes of paging file." << endl;
	cout << endl;
	cout << "There are " << statex.ullTotalVirtual/MB << " total megabytes of virtual memory." << endl;
	cout << "There are " << statex.ullAvailVirtual/MB << " free megabytes of virtual memory." << endl;
	cout << endl;
	cout << "There are " << statex.ullAvailExtendedVirtual/MB << " free megabytes of virtual memory." << endl;

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 12:04 PM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
What did you compile this in? I tried to compile it in Dev-Cpp, but it had errors. However, MSDN's C version of the program compiled without complaining. Still pretty cool, as I can see what the program is doing by looking at the code.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Apr 20th, 2006, 2:03 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Prm753
What did you compile this in? I tried to compile it in Dev-Cpp, but it had errors. However, MSDN's C version of the program compiled without complaining. Still pretty cool, as I can see what the program is doing by looking at the code.
I have VS 2005 (Professional), I don't have Dev-C++. Maybe the hyperlink causes errors. Try removing it. Otherwise tell me what it says. Yeah the program is super-simple.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 3:30 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Prm753
What did you compile this in? I tried to compile it in Dev-Cpp, but it had errors. However, MSDN's C version of the program compiled without complaining. Still pretty cool, as I can see what the program is doing by looking at the code.


If you took a second to look in 'winbase.h' that ships with Dev-C++ you would see:
typedef struct _MEMORYSTATUS {
	DWORD dwLength;
	DWORD dwMemoryLoad;
	DWORD dwTotalPhys;
	DWORD dwAvailPhys;
	DWORD dwTotalPageFile;
	DWORD dwAvailPageFile;
	DWORD dwTotalVirtual;
	DWORD dwAvailVirtual;
} MEMORYSTATUS,*LPMEMORYSTATUS;
#if (_WIN32_WINNT >= 0x0500)
typedef struct _MEMORYSTATUSEX {
	DWORD dwLength;
	DWORD dwMemoryLoad;
	DWORDLONG ullTotalPhys;
	DWORDLONG ullAvailPhys;
	DWORDLONG ullTotalPageFile;
	DWORDLONG ullAvailPageFile;
	DWORDLONG ullTotalVirtual;
	DWORDLONG ullAvailVirtual;
	DWORDLONG ullAvailExtendedVirtual;
} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;
#endif

So, the following will fix the problem:
#define _WIN32_WINNT 0x0500

Seriously, learn to dig a little. It'll save countless hours of fumbling around.
Cache is offline   Reply With Quote
Old Apr 20th, 2006, 5:01 PM   #5
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Yea, I should have looked at the header file.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC