如何在 Windows 和 Linux 上用 C++ 读取系统信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2496581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read system information in C++ on Windows and Linux?
提问by f4.
I need to read system information like CPU/RAM/disks usage in C++. Maybe swap, network and process too but that's less important.
我需要在 C++ 中读取系统信息,如 CPU/RAM/磁盘使用情况。也许交换、网络和进程也是如此,但这并不重要。
It has probably been done thousand of times before so I first tried to search for a library. Someone heresuggested SIGAR, which seems to fit my needs but it has a GPLlicense and it is for inclusion in a proprietary product. So it's not an option here.
之前可能已经完成了数千次,所以我首先尝试搜索图书馆。这里有人建议使用SIGAR,它似乎符合我的需求,但它具有GPL许可证,并且包含在专有产品中。所以这里不是一个选项。
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?
如果您不知道任何图书馆,您能否为我指出两个平台的正确方向?
回答by pestilence669
On Windows, try GetDiskFreeSpaceExand GlobalMemoryStatusEx.
在 Windows 上,尝试GetDiskFreeSpaceEx和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! :)
Linux 稍微复杂一些,因为它允许您挂载卷。你总是system()
可以“df”,但这太可怕了。由于 Linux 是开源的,只需查看“df”的源代码即可了解它是如何工作的!:)
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 :)
如果您没有时间:对于 UNIX 变体(包括 Linux),您可以尝试libstatgrab。它是 LGPL / 专有友好的。您可能需要#ifdef
一些专门用于 Windows 的代码,但幸运的是,Windows 调用很简单。最坏情况:200 行。如果你感觉很慷慨,你可以为全面的 Windows 支持贡献一个补丁:)
Good luck!
祝你好运!
回答by Misha M
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:
有关更完整的答案,请查看 QT 论坛上的以下主题。它是从 2006 年开始的,但我认为它解决了您的问题:
http://lists.trolltech.com/qt-interest/2006-05/thread00922-0.html
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.
我确信有一种方法可以创建一个基于 C++ 模板的解决方案,它会比上面的 C 混乱更清晰。
回答by Nathan Osman
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__.aspxand this: http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/
在 Windows 上,你会看到这样的东西:http: //www.codeproject.com/KB/system/Using_WMI_in_Visual_C__.aspx和这个:http: //www.philosophicalgeek.com/2009/01/03/determine- cpu-usage-of-current-process-c-and-c/