0

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!

JeremyP
  • 84,577
  • 15
  • 123
  • 161
sunny8107
  • 389
  • 2
  • 4
  • 12
  • 1
    Even if you can get hold of this statistic, you should be warned that it is largely useless. – David Heffernan Feb 17 '11 at 16:49
  • update: I installed MiniGW, that got rid of the tchar.h problem but rest of the errors are still there. Using the exact same program as mentioned in this link: http://msdn.microsoft.com/en-us/library/aa366589(v=vs.85).aspx – sunny8107 Feb 17 '11 at 18:48
  • @David: It's not useless, it just doesn't mean what most people think it does. – Ben Voigt Feb 17 '11 at 19:32
  • @sunny stop fighting with GCC and use Visual Studio. It's actually quite good these days. It even has a very fine debugger. If you are starting out with C or C++ on Windows then you'll get up and running quicker this way. – David Heffernan Feb 17 '11 at 19:34
  • @Sunny: Did you `#include `? If you did, then GCC simply does not support that Windows API function, so you'll either need to declare it yourself, or switch compilers to one that actually supports the Windows API. – Billy ONeal Feb 17 '11 at 20:18
  • @David, I took your advice and installed Visual Studio. Wrote and executed the program on my XP machine - works good. But, I ftp'd the same binary to Windows 2008 machine for test and fails with error ** "The application has failed to start because MSVCR100D.dll was not found. Re-installing the application may fix this problem" ** – sunny8107 Feb 17 '11 at 20:19
  • @sunny That's because the C runtime is missing on that machine. Build with static linking to the runtime (or install the runtime everywhere you want it to run) and problem solved. – David Heffernan Feb 17 '11 at 20:22
  • @David, the second option won't work since this binary should work OOTB with the software we install without any pre-requisite at all for the customer. I don't know how to do static linking in Visual Studio -- all I do is Right click project name and choose build. I'll poke around. Thanks! – sunny8107 Feb 17 '11 at 20:27
  • @Billy, yes I did #include earlier in MiniGW but was failing after that when accessing windows API function. Trying Visual Studio now. – sunny8107 Feb 17 '11 at 20:35
  • Guys, good and bad news. Good news is finally got it working and resolved the library issue. This link helped: [http://stackoverflow.com/questions/37398/how-do-i-make-a-fully-statically-linked-exe-with-visual-studio-express-2005/37402#37402] Bad News is that memory output using GlobalMemoryStatusEx works fine in XP but the output doesn't match what shows up using Task Manager in Windows 2008...sigh..i'll discuss that in a new thread as this has become big. Thanks to everyone who took the time in helping out! – sunny8107 Feb 17 '11 at 21:37
  • @sunny The whole issue of interpreting memory usage statistics is fraught with confusion! – David Heffernan Feb 17 '11 at 22:06
  • Yes, I found this link which was helpful http://support.microsoft.com/kb/935268 Basically, the output of GlobalMemoryStatusEx is right when compared with MsInfo32.exe, Task Manager shows something else on Windows 2008. On Windows XP it matched correctly with Task Manager. – sunny8107 Feb 17 '11 at 23:47

6 Answers6

5

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

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
  • 1
    Note that if you are a student and at a University, you can get a full copy of Visual Studio from MSDNAA, and if you're a student where that is not available, you can use [DreamSpark](http://bit.ly/dYpBTO). – Billy ONeal Feb 17 '11 at 16:47
3

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Thanks, I have downloaded MiniGW. It got rid of the tchar.h header issue but other errors are still there :-( – sunny8107 Feb 17 '11 at 18:47
3

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

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.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
2

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).

San Jacinto
  • 8,774
  • 5
  • 43
  • 58
1

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks, I just downloaded MiniGW and took one step further. Not sure why my program is still not compiling. – sunny8107 Feb 17 '11 at 19:01