Retreiving Memory Status Using Visual Studio 2008

Hi to all, I am currently developing a simple program which analyses system hardware such as processor speed etc.I am having problems with retreiving system memory ie how large the ram is, i have a starting point but does not seem to work i always get errors, i have been told by others to define the double i.e. Double = temp; Temp = XXXXX, but do not understand what this means as i am a novice to C++ Please see source code below: -

MEMORYSTATUS memory;

memset(&memory,sizeof(MEMORYSTATUS),0);
memory.dwLength = sizeof(MEMORYSTATUS);

GlobalMemoryStatus(&memory);

double = memory.dwTotalPhys/(1024*1000);


Any help would be greatly appreciated!!
Well what you've done is not declare a variable properly.
When you use "double" it expects you to give it a variable name.
For example "totalram", ie "double totalram=..."
However, I think it makes more sense if you use "long" to store the RAM amount. So your new code would be something like:

1
2
3
4
5
6
7
8
9
10
MEMORYSTATUS memory;

memset(&memory,sizeof(MEMORYSTATUS),0);
memory.dwLength = sizeof(MEMORYSTATUS);

GlobalMemoryStatus(&memory);

long totalram=memory.dwTotalPhys;

printf("%d bytes of RAM total",totalram);


As you can see I have made it print simply, the total bytes of RAM. Divide the number by 1024 for the kilobytes of RAM, then divide by 1024 for the megabytes of RAM, again for the gigabytes (etc).
Thank you very much for your response. I have tried the above code and understand what you are implying but when I compile it there are still errors relating the following line of code:

memset(&memory,sizeof(MEMORYSTATUS),0);

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2365: 'memset' : redefinition; previous definition was 'function'

Any ideas?

What compiler are you using?
When I compile with GCC (the following code) it runs properly for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<windows.h>
#include<stdio.h>
int main()
{
MEMORYSTATUS memory;

memset(&memory,sizeof(MEMORYSTATUS),0);
memory.dwLength = sizeof(MEMORYSTATUS);

GlobalMemoryStatus(&memory);

long totalram=memory.dwTotalPhys;

printf("%d bytes of RAM total",totalram);
getchar();
return 0;
}
Thanks Chris, the code now compiles......I am trying to assign the contents of the totalram variable to a label via the use of a button (RAM) using the following code:

case RAM:
main();
SetDlgItemText(hdwnd,lblSpeed,totalram);
break;


I keep on getting the following error:

error C2664: 'SetDlgItemTextA' : cannot convert parameter 3 from 'long' to 'LPCSTR

Thanks for your help.

Right, the third parameter, in this case, "totalram" needs to be a null-terminated string (LPCTSTR) but it is a long.

BOOL SetDlgItemText(HWND hDlg, int nIDDlgItem, LPCTSTR lpString);

That is the definition of the subroutine you are using. In italics is the part you need to enter. Currently totalram is a long but it needs to be LPCTSTR so you will need to find a way of converting it.

There is this subroutine I have found:

BOOL SetDlgItemInt(HWND hDlg, int nIDDlgItem, UINT uValue, BOOL bSigned);

This time in italics is where (I presume) you would use the value totalram.

I think this should work (not got time to test it currently):

SetDlgItemInt(hdwnd,lblSpeed,(UINT)totalram,false);

Hope that helps.
Last edited on
Thanks Chris for your feedback it helped greatly!! Got it working.
No problems mate =)
Topic archived. No new replies allowed.