如何确定机器上的硬件(CPU和RAM)? [英] How to determine the hardware (CPU and RAM) on a machine?

查看:103
本文介绍了如何确定机器上的硬件(CPU和RAM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个跨平台的分析套件,并且想在每次运行的报告中添加有关计算机的CPU(架构/时钟速度/核心)和RAM(总计)的信息。当前我需要以Windows和Unix为目标,所以我需要一些方法可以从两个平台上获取此信息,有任何线索吗?

I'm working on a cross platform profiling suite, and would like to add information about the machine's CPU (architecture/clock speed/cores) and RAM(total) to the report of each run. Currently I need to target Windows and Unix, so I need methods to obtain this information from both platforms, any clues?

编辑:很好的答案,现在我有了CPU体系结构,CPU内核数和总内存,但是我仍然缺乏CPU的时钟速度,对此有什么想法吗?

Thanks for the great answers, Now I got CPU architecture, CPU number of cores and total Memory, but I'm still lacking a clockspeed for the CPU any ideas for that one?

推荐答案

这里是在Windows计算机上获取所需信息的一种方法。我从实际项目中复制并粘贴了一些小修改,所以请随时清理它以使它更有意义。

Here is one method for getting the information you want on a Windows machine. I copied and pasted it from an actual project with some minor modifications, so feel free to clean it up to make more sense.

        int CPUInfo[4] = {-1};
        unsigned   nExIds, i =  0;
        char CPUBrandString[0x40];
        // Get the information associated with each extended ID.
        __cpuid(CPUInfo, 0x80000000);
        nExIds = CPUInfo[0];
        for (i=0x80000000; i<=nExIds; ++i)
        {
            __cpuid(CPUInfo, i);
            // Interpret CPU brand string
            if  (i == 0x80000002)
                memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
            else if  (i == 0x80000003)
                memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
            else if  (i == 0x80000004)
                memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
        }
        //string includes manufacturer, model and clockspeed
        cout << "CPU Type: " << CPUBrandString << endl;


        SYSTEM_INFO sysInfo;
        GetSystemInfo(&sysInfo);
        cout << "Number of Cores: " << sysInfo.dwNumberOfProcessors << endl;

        MEMORYSTATUSEX statex;
        statex.dwLength = sizeof (statex);
        GlobalMemoryStatusEx(&statex);
        cout << "Total System Memory: " << (statex.ullTotalPhys/1024)/1024 << "MB" << endl;

有关更多信息,请参见 GetSystemInfo GlobalMemoryStatusEx __ cpuid 。尽管我没有提供它,但是您也可以通过GetSystemInfo函数确定操作系统是32位还是64位。

For more information, see GetSystemInfo, GlobalMemoryStatusEx and __cpuid. Although I didn't include it, you can also determine if the OS is 32 or 64 bit via the GetSystemInfo function.

这篇关于如何确定机器上的硬件(CPU和RAM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆