How to determine the hardware (CPU and RAM) on a machine?

前端 未结 13 1016
长发绾君心
长发绾君心 2020-12-31 03:29

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

13条回答
  •  执笔经年
    2020-12-31 03:50

    On Windows to determine CPU clock speed:

    double CPUSpeed()
    {
        wchar_t Buffer[_MAX_PATH];
        DWORD BufSize = _MAX_PATH;
        DWORD dwMHz = _MAX_PATH;
        HKEY hKey;
    
        // open the key where the proc speed is hidden:
        long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                    L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
                                    0,
                                    KEY_READ,
                                    &hKey);
        if(lError != ERROR_SUCCESS)
        {// if the key is not found, tell the user why:
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                            NULL,
                            lError,
                            0,
                            Buffer,
                            _MAX_PATH,
                            0);
            wprintf(Buffer);
            return 0;
        }
    
        // query the key:
        RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);
        return (double)dwMHz;
    }
    

提交回复
热议问题