Added get_file_version() function + workaround to make get_real_os_version() give the correct result on Windows 10+.

This commit is contained in:
LoRd_MuldeR 2015-06-21 16:37:03 +02:00
parent 2722678293
commit b1c2ecee3f
4 changed files with 74 additions and 3 deletions

View File

@ -164,7 +164,7 @@
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\..\Prerequisites\Qt4\MSVC-2013\Debug\lib;$(SolutionDir)\..\Prerequisites\VisualLeakDetector\lib\Win32</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(SolutionDir)\..\Prerequisites\Qt4\MSVC-2013\Debug\lib;$(SolutionDir)\..\Prerequisites\VisualLeakDetector\lib\Win32</AdditionalLibraryDirectories>
<AdditionalDependencies>QtCored4.lib;QtGuid4.lib;Winmm.lib;Psapi.lib;Sensapi.lib;PowrProf.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>QtCored4.lib;QtGuid4.lib;Winmm.lib;Psapi.lib;Sensapi.lib;PowrProf.lib;Shlwapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -193,7 +193,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)\..\Prerequisites\Qt4\MSVC-2013\Shared\lib;$(SolutionDir)\..\Prerequisites\VisualLeakDetector\lib\Win32</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(SolutionDir)\..\Prerequisites\Qt4\MSVC-2013\Shared\lib;$(SolutionDir)\..\Prerequisites\VisualLeakDetector\lib\Win32</AdditionalLibraryDirectories>
<AdditionalDependencies>QtCore4.lib;QtGui4.lib;Winmm.lib;Psapi.lib;Sensapi.lib;PowrProf.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>QtCore4.lib;QtGui4.lib;Winmm.lib;Psapi.lib;Sensapi.lib;PowrProf.lib;Shlwapi.lib;Version.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>false</GenerateDebugInformation> <GenerateDebugInformation>false</GenerateDebugInformation>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>

View File

@ -108,6 +108,9 @@ namespace MUtils
typedef bool (*progress_callback_t)(const double &progress, void *const userData); typedef bool (*progress_callback_t)(const double &progress, void *const userData);
MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true, const progress_callback_t callback = NULL, void *const userData = NULL); MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true, const progress_callback_t callback = NULL, void *const userData = NULL);
//Get file version
MUTILS_API bool get_file_version(const QString fileName, quint16 *const major = NULL, quint16 *const minor = NULL, quint16 *const patch = NULL, quint16 *const build = NULL);
//Get the OS version //Get the OS version
MUTILS_API const Version::os_version_t &os_version(void); MUTILS_API const Version::os_version_t &os_version(void);
MUTILS_API const char *os_friendly_name(const MUtils::OS::Version::os_version_t &os_version); MUTILS_API const char *os_friendly_name(const MUtils::OS::Version::os_version_t &os_version);

View File

@ -31,4 +31,4 @@
#define VER_MUTILS_MAJOR 1 #define VER_MUTILS_MAJOR 1
#define VER_MUTILS_MINOR_HI 0 #define VER_MUTILS_MINOR_HI 0
#define VER_MUTILS_MINOR_LO 3 #define VER_MUTILS_MINOR_LO 4

View File

@ -176,6 +176,59 @@ MUTILS_API bool MUtils::OS::copy_file(const QString &sourcePath, const QString &
return (result != FALSE); return (result != FALSE);
} }
///////////////////////////////////////////////////////////////////////////////
// GET FILE VERSION
///////////////////////////////////////////////////////////////////////////////
static bool get_file_version_helper(const QString fileName, PVOID buffer, const size_t &size, quint16 *const major, quint16 *const minor, quint16 *const patch, quint16 *const build)
{
if(!GetFileVersionInfo(MUTILS_WCHR(fileName), 0, size, buffer))
{
qWarning("GetFileVersionInfo() has failed, file version cannot be determined!");
return false;
}
VS_FIXEDFILEINFO *verInfo;
UINT verInfoLen;
if(!VerQueryValue(buffer, L"\\", (LPVOID*)(&verInfo), &verInfoLen))
{
qWarning("VerQueryValue() has failed, file version cannot be determined!");
return false;
}
if(major) *major = quint16((verInfo->dwFileVersionMS >> 16) & 0x0000FFFF);
if(minor) *minor = quint16((verInfo->dwFileVersionMS) & 0x0000FFFF);
if(patch) *patch = quint16((verInfo->dwFileVersionLS >> 16) & 0x0000FFFF);
if(build) *build = quint16((verInfo->dwFileVersionLS) & 0x0000FFFF);
return true;
}
bool MUtils::OS::get_file_version(const QString fileName, quint16 *const major, quint16 *const minor, quint16 *const patch, quint16 *const build)
{
if(major) *major = 0U; if(minor) *minor = 0U;
if(patch) *patch = 0U; if(build) *build = 0U;
const DWORD size = GetFileVersionInfoSize(MUTILS_WCHR(fileName), NULL);
if(size < 1)
{
qWarning("GetFileVersionInfoSize() has failed, file version cannot be determined!");
return false;
}
PVOID buffer = _malloca(size);
if(!buffer)
{
qWarning("Memory allocation has failed!");
return false;
}
const bool success = get_file_version_helper(fileName, buffer, size, major, minor, patch, build);
_freea(buffer);
return success;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// OS VERSION DETECTION // OS VERSION DETECTION
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -322,6 +375,21 @@ static bool get_real_os_version(unsigned int *major, unsigned int *minor, bool *
break; break;
} }
//Workaround for the mess that is sometimes referred to as "Windows 10"
if(((*major) > 6) || (((*major) == 6) && ((*minor) >= 2)))
{
quint16 kernel32_major, kernel32_minor;
if(MUtils::OS::get_file_version(QLatin1String("kernel32"), &kernel32_major, &kernel32_minor))
{
if((kernel32_major > (*major)) || ((kernel32_major == (*major)) && (kernel32_minor > (*minor))))
{
*major = kernel32_major;
*minor = kernel32_minor;
*pbOverride = true;
}
}
}
return true; return true;
} }