Fichier source en cours : FW - ComputerInfo.cpp






Voici le code source cpp :




	#include "Framework.h"

using namespace fw;

template<> ComputerInfo* Singleton<ComputerInfo>::ms_instance = nullptr;

ComputerInfo::ComputerInfo()
{
    ParseOSName();
    ParseCPU();
    ParseMemory();
	ParseHDSpace();
}

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;
}

std::string ComputerInfo::GetHDSpace()
{
	return m_sHDDAvailableMemory;
}
bool ComputerInfo::CompareWindowsVersionMinor(const DWORD dwMinorVersion)
{
	OSVERSIONINFOEX ver;
	DWORDLONG dwlConditionMask = 0;

	ZeroMemory(&ver, sizeof(OSVERSIONINFOEX));
	ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	ver.dwMinorVersion = dwMinorVersion;

	VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);

	return VerifyVersionInfo(&ver, VER_MINORVERSION, dwlConditionMask);
}

bool ComputerInfo::CompareWindowsVersionMajor(const DWORD dwMajorVersion)
{
	OSVERSIONINFOEX ver;
	DWORDLONG dwlConditionMask = 0;

	ZeroMemory(&ver, sizeof(OSVERSIONINFOEX));
	ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	ver.dwMajorVersion = dwMajorVersion;

	VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);

	return VerifyVersionInfo(&ver, VER_MAJORVERSION, dwlConditionMask);
}

void ComputerInfo::ParseOSName()
{
	std::string sOSName = "OS Inconnu";

	if (CompareWindowsVersionMajor(6))
	{
		if ((CompareWindowsVersionMinor(3)))
		{
			sOSName = "Windows 8.1";
		}
		else if ((CompareWindowsVersionMinor(2)))
		{
			sOSName = "Windows 8";
		}
		else if ((CompareWindowsVersionMinor(1)))
		{
			sOSName = "Windows 7";
		}
		else if ((CompareWindowsVersionMinor(0)))
		{
			sOSName = "Windows Vista";
		}
	}
	else if (CompareWindowsVersionMajor(5))
	{
		if (CompareWindowsVersionMinor(1))
		{
			sOSName = "Windows XP";
		}
	}

	m_sOsName = sOSName;
}

void ComputerInfo::ParseCPU()
{
    int CPUInfo[4] = {-1};
    unsigned nExIds;
	char CPUBrandString[0x40] = { 0 };

    __cpuid(CPUInfo, 0x80000000);
    nExIds = CPUInfo[0];

    for (uint32 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 = STRING_BUILDER->RemoveSpaceAfterString(CPUBrandString);

	/************************************/
	char Buffer[_MAX_PATH];
	DWORD BufSize = _MAX_PATH;
	DWORD dwMHz = _MAX_PATH;
	HKEY hKey;

	long lError = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
                        0,
                        KEY_READ,
                        &hKey);
    
    if(lError != ERROR_SUCCESS)
    {
           FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
                         NULL,
                         lError,
                         0,
                         Buffer,
                         _MAX_PATH,
                         0);
    }

    RegQueryValueExA(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);

    __int64 totalPhysicalMem = 0;
	__int64 availableMem = 0;

    totalPhysicalMem = status.ullTotalPhys / 1024;
    totalPhysicalMem = (totalPhysicalMem / 1024) + 1;

    availableMem = status.ullAvailPhys / 1024;
    availableMem = (availableMem / 1024) + 1;

    m_sTotalMemory = STRING_BUILDER->IntegerToString((int) totalPhysicalMem) + " MB";
    m_sAvailableMemory = STRING_BUILDER->IntegerToString((int) availableMem) + " MB";
}

void ComputerInfo::ParseHDSpace()
{
	int const drive = _getdrive();

	struct _diskfree_t diskfree;
	_getdiskfree(drive, &diskfree);

	unsigned __int64 const total = diskfree.sectors_per_cluster * diskfree.bytes_per_sector * diskfree.avail_clusters;

	m_sHDDAvailableMemory = STRING_BUILDER->IntegerToString((int)total / 1024 / 1024) + " GB";
}