sunny8107
sunny8107

Reputation: 409

Getting Free RAM in Windows Programmatically?

All, Need some help here. I am from a purely Java background and don't know much about C programming. Basically I need to write a C program (and create an executable) which would output the number of Free Bytes (RAM) on a Windows machine (XP/2008/7)

I have doing some research about it and found that there is a function called GlobalMemoryStatusEx and also a link on this: http://msdn.microsoft.com/en-us/library/aa366589(v=vs.85).aspx

I have installed cygwin with gcc and the above program doesn't even compile for me. I know I am doing something stupid here..

$ gcc hello.c -o hello.exe
hello.c:3:19: error: tchar.h: No such file or directory
hello.c: In function `main':
hello.c:7: error: `MEMORYSTATUSEX' undeclared (first use in this function)
hello.c:7: error: (Each undeclared identifier is reported only once
hello.c:7: error: for each function it appears in.)
hello.c:7: error: expected `;' before `statex'
hello.c:9: error: `statex' undeclared (first use in this function)

Any help is very much appreciated!

Upvotes: 0

Views: 973

Answers (6)

Ben Voigt
Ben Voigt

Reputation: 283684

To use cygwin gcc to compile programs using the Win32 API, you'll want the mingw-i686-headers package (which includes header files like tchar.h).

Then, you may also need to define some macros to enable newer APIs.

Upvotes: 1

NPE
NPE

Reputation: 500457

If you are unfamiliar with C, and all you want to do is write this little program for Windows, you might find it easier to get started with the free Visual C++ Express. Chances are that MSDN examples will work out of the box.

Edit: You mention Java, but it is not totally clear whether the sole purpose of this C app is to be invoked from Java. In case it is, I thought I'd mention something called JNative. It appears to be a pure Java library that contains, among other things, a wrapper for GlobalMemoryStatusEx. Note that I found JNative via a Google search and haven't used it myself.

Upvotes: 3

user124493
user124493

Reputation:

The answers presented are correct, but I don't feel that they explain the problem properly.

Unix and Unix-like operating systems strive to conform to a standard API called POSIX. Windpws does not conform to POSIX. Cygwin was written as way to provide POSIX compliance on Windows. Thus, when you compile on Cygwin, you are compiling on a POSIX API, not a Windows API. The function you found on MSDN does not exist in POSIX. Threefore, you get compile errors.

MinGW is gcc ported to Windows. It is aware of Windows API's with the proper includes. Visual Studio's nmake is the same in this regard (provides access to the Win API).

Upvotes: 2

Billy ONeal
Billy ONeal

Reputation: 106559

The function to which you refer is a Windows API function, which means you cannot call that function from cygwin, which is a POSIX emulation layer on top of Windows.

You have a few options at this point:

  1. You could download download and install the Windows SDK, which includes the Visual Studio C/C++ compiler(s), for free.
  2. You could download a copy of Visual C++ Express Edition for free.
  3. If you are a student, you could get a full copy of Visual Studio 2010 from MSDNAA (provided by your University) or from DreamSpark.
  4. You could purchase a full copy of Visual Studio.
  5. You could download and install MinGW, which is the GCC compilers ported to Windows.

Note that all of the Microsoft compilers support the entire Win32 API. MinGW is limited in its handling of Unicode on Windows, it does not support COM (so if you want to call COM APIs it's a pain), and it doesn't include all of the headers included with the Windows SDK.

GCC is a great compiler, its just not all that great in terms of its Windows support.

Upvotes: 2

Harper Shelby
Harper Shelby

Reputation: 16583

As larsmans said, cygwin is not a Windows native compiler. In addition to MinGW, you might also consider Visual C++ 2010 Express.

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363627

Cygwin by default emulates a Unix API on Windows, rather than presenting the Windows API.

You may have more success with the MinGW package, which is GCC for Windows + Windows API.

Upvotes: 3

Related Questions