开发者

How to read system information in C++ on Windows and Linux?

I need to read system information like CPU/RAM/disks usage in C++. Maybe swap, network and process too but that's less important.

It has probably been done thousand of times before so I first tried to search for a library. Someone here suggested SIGAR, which seems to fit my needs but it has a GPL license and it is for inclusion in a proprietary product. So it'开发者_如何转开发s not an option here.

I feel like it's something not that easy to implement, as it'll need testing on several platforms. So a library would be welcome.

If you don't know of any library, could you point me in the right direction for both platforms?


On Windows, try GetDiskFreeSpaceEx and GlobalMemoryStatusEx.

Linux is a tad more complicated, due to the way it allows you to mount volumes. You can always system() out to "df", but that's horrid. Since Linux is open source, simply look at the source code to "df" to find out how it works! :)

If you don't have the time: for UNIX variants (including Linux), you can try libstatgrab. It's LGPL / proprietary friendly. You'll probably need to #ifdef some code specifically for Windows but, fortunately, the Windows calls are straightforward. Worst case: 200 lines. If you're feeling generous, you can contribute a patch for full-blown Windows support :)

Good luck!


Your best bet is to create something yourself.

On Windows, you would be looking at something like this: http://www.codeproject.com/KB/system/Using_WMI_in_Visual_C__.aspx and this: http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/


The short answer is it's not very difficult to roll your own implementation.

For a more complete answer take a look at the following topic on QT forum. It's from 2006, but I think it addresses your problem:

http://lists.trolltech.com/qt-interest/2006-05/thread00922-0.html

UPDATE:

You could try:

#if defined(WINDOWS)
  // either macro format
  #define CPU_INFO (<your cpu macro>)
  // or function format
  void fs_info()
  ...
#elif defined(LINXU)
  ...
#elif defined(MAC)
  ...
#endif

and then use those macros/functions in your code.

I'm sure there's a way to create a C++ Template-based solution that would be cleaner then the C mess above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜