Intro :
Cette classe va vous permettre de récupérer les informations de votre système d’exploitation, du nom de votre processeur et de sa vitesse, du nombre de mémoire que vous disposez…
Explications :
|
1 2 3 4 5 |
std::string getOsName(); std::string getCpuName(); std::string getCpuSpeed(); std::string getTotalMemory(); std::string getAvailableMemory(); |
Dans le fichier ComputerInfo.h :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#ifndef COMPUTER_INFO_H #define COMPUTER_INFO_H class ComputerInfo { public: ComputerInfo(); ~ComputerInfo(); void parseOSName(); void parseCPU(); void parseMemory(); void parseHDSpace(); std::string getOsName(); std::string getCpuName(); std::string getCpuSpeed(); std::string getTotalMemory(); std::string getAvailableMemory(); private: std::string m_sOsName; std::string m_sCpuName; std::string m_sCpuSpeed; std::string m_sTotalMemory; std::string m_sAvailableMemory; }; #endif |
Dans votre fichier ComputerInfo.cpp :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
#include <direct.h> #include "ComputerInfo.h" ComputerInfo::ComputerInfo() { parseOSName(); parseCPU(); parseMemory(); } ComputerInfo::~ComputerInfo() { } std::string ComputerInfo::getOsName() { return m_sOsName; } std::string ComputerInfo::getCpuName() { return m_sCpuName; } std::string ComputerInfo::getCpuSpeed() { return m_sCpuSpeed; } std::string ComputerInfo::getTotalMemory() { return m_sTotalMemory; } std::string ComputerInfo::getAvailableMemory() { return m_sAvailableMemory; } void ComputerInfo::parseOSName() { OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); std::string sOSName; switch(osvi.dwMajorVersion) { case 6: { if (osvi.dwMinorVersion == 3) sOSName = "Windows 8.1"; else if (osvi.dwMinorVersion == 2) sOSName = "Windows 8"; else if (osvi.dwMinorVersion == 1) sOSName = "Windows 7"; else if (osvi.dwMinorVersion == 0) sOSName = "Windows Vista"; } break; case 5: { if (osvi.dwMinorVersion == 1) sOSName = "Windows XP"; } break; default: sOSName = "OS Inconnu"; } m_sOsName = sOSName; } void ComputerInfo::parseCPU() { int CPUInfo[4] = {-1}; unsigned nExIds, i = 0; char CPUBrandString[0x40]; __cpuid(CPUInfo, 0x80000000); nExIds = CPUInfo[0]; for (i=0x80000000; i<=nExIds; ++i) { __cpuid(CPUInfo, i); 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)); } m_sCpuName = CPUBrandString; /************************************/ char Buffer[_MAX_PATH]; DWORD BufSize = _MAX_PATH; DWORD dwMHz = _MAX_PATH; HKEY hKey; long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey); if(lError != ERROR_SUCCESS) { FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, lError, 0, Buffer, _MAX_PATH, 0); } RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize); char string[512] = { 0 }; sprintf(string, "%i", dwMHz); m_sCpuSpeed = std::string(string) + " MHz"; } void ComputerInfo::parseMemory() { MEMORYSTATUSEX status; status.dwLength = sizeof(status); GlobalMemoryStatusEx(&status); int totalPhysicalMem = 0; int availableMem = 0; totalPhysicalMem = (int) status.ullTotalPhys / 1024; totalPhysicalMem = (totalPhysicalMem / 1024) + 1; availableMem = (int) status.ullAvailPhys / 1024; availableMem = (availableMem / 1024) + 1; m_sTotalMemory = MiscManager::intToString(totalPhysicalMem) + " MB"; m_sAvailableMemory = MiscManager::intToString(availableMem) + " MB"; } |
Résumé :
Cette classe permet d’accéder aux informations de votre système d’exploitation Windows.


