///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
-// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
#include <Shellapi.h>
#include <PowrProf.h>
#include <Mmsystem.h>
+#include <WinIoCtl.h>
#pragma warning(push)
#pragma warning(disable:4091) //for MSVC2015
#include <ShlObj.h>
#include <QDir>
#include <QWidget>
#include <QProcess>
+#include <QSet>
//Main thread ID
static const DWORD g_main_thread_id = GetCurrentThreadId();
bool os_version_t::operator<= (const os_version_t &rhs) const { return (type == rhs.type) && ((versionMajor < rhs.versionMajor) || ((versionMajor == rhs.versionMajor) && (versionMinor <= rhs.versionMinor))); }
//Known Windows NT versions
- const os_version_t WINDOWS_WIN2K = { OS_WINDOWS, 5, 0, 2195 }; // 2000
- const os_version_t WINDOWS_WINXP = { OS_WINDOWS, 5, 1, 2600 }; // XP
- const os_version_t WINDOWS_XPX64 = { OS_WINDOWS, 5, 2, 3790 }; // XP_x64
- const os_version_t WINDOWS_VISTA = { OS_WINDOWS, 6, 0, 6000 }; // Vista
- const os_version_t WINDOWS_WIN70 = { OS_WINDOWS, 6, 1, 7600 }; // 7
- const os_version_t WINDOWS_WIN80 = { OS_WINDOWS, 6, 2, 9200 }; // 8
- const os_version_t WINDOWS_WIN81 = { OS_WINDOWS, 6, 3, 9600 }; // 8.1
- const os_version_t WINDOWS_WN100 = { OS_WINDOWS, 10, 0, 10240 }; // 10
+ const os_version_t WINDOWS_WIN2K = { OS_WINDOWS, 5, 0, 2195, 0 }; // 2000
+ const os_version_t WINDOWS_WINXP = { OS_WINDOWS, 5, 1, 2600, 0 }; // XP
+ const os_version_t WINDOWS_XPX64 = { OS_WINDOWS, 5, 2, 3790, 0 }; // XP_x64
+ const os_version_t WINDOWS_VISTA = { OS_WINDOWS, 6, 0, 6000, 0 }; // Vista
+ const os_version_t WINDOWS_WIN70 = { OS_WINDOWS, 6, 1, 7600, 0 }; // 7
+ const os_version_t WINDOWS_WIN80 = { OS_WINDOWS, 6, 2, 9200, 0 }; // 8
+ const os_version_t WINDOWS_WIN81 = { OS_WINDOWS, 6, 3, 9600, 0 }; // 8.1
+ const os_version_t WINDOWS_WN100 = { OS_WINDOWS, 10, 0, 10240, 0 }; // 10
//Unknown OS
- const os_version_t UNKNOWN_OPSYS = { OS_UNKNOWN, 0, 0, 0 }; // N/A
+ const os_version_t UNKNOWN_OPSYS = { OS_UNKNOWN, 0, 0, 0, 0 }; // N/A
}
}
}
return (ret != FALSE);
}
-static bool get_real_os_version(unsigned int *major, unsigned int *minor, unsigned int *build, bool *pbOverride)
+static bool verify_os_spack(const WORD spack)
{
- static const DWORD MAX_VERSION = 0xFFFF;
+ OSVERSIONINFOEXW osvi;
+ DWORDLONG dwlConditionMask = 0;
+ initialize_os_version(&osvi);
+
+ //Initialize the OSVERSIONINFOEX structure
+ osvi.wServicePackMajor = spack;
+ osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;
+
+ //Initialize the condition mask
+ VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
+ VER_SET_CONDITION(dwlConditionMask, VER_PLATFORMID, VER_EQUAL);
+
+ // Perform the test
+ const BOOL ret = rtl_verify_version(&osvi, VER_SERVICEPACKMAJOR | VER_PLATFORMID, dwlConditionMask);
+
+ //Error checking
+ if (!ret)
+ {
+ if (GetLastError() != ERROR_OLD_WIN_VERSION)
+ {
+ qWarning("VerifyVersionInfo() system call has failed!");
+ }
+ }
+
+ return (ret != FALSE);
+}
+
+static bool get_real_os_version(unsigned int *const major, unsigned int *const minor, unsigned int *const build, unsigned int *const spack, bool *const pbOverride)
+{
+ static const DWORD MAX_VERSION = MAXWORD;
static const DWORD MAX_BUILDNO = MAXINT;
+ static const DWORD MAX_SRVCPCK = MAXWORD;
- *major = *minor = *build = 0;
+ *major = *minor = *build = *spack = 0U;
*pbOverride = false;
//Initialize local variables
*major = osvi.dwMajorVersion;
*minor = osvi.dwMinorVersion;
*build = osvi.dwBuildNumber;
+ *spack = osvi.wServicePackMajor;
}
else
{
}
}
+ //Service Pack
+ if (verify_os_spack(SAFE_ADD((*spack), 1, MAX_SRVCPCK)))
+ {
+ DWORD stepSize = initialize_step_size(MAX_SRVCPCK);
+ for (DWORD nextSPackNo = SAFE_ADD((*spack), stepSize, MAX_SRVCPCK); (*spack) < MAX_SRVCPCK; nextSPackNo = SAFE_ADD((*spack), stepSize, MAX_SRVCPCK))
+ {
+ if (verify_os_spack(nextSPackNo))
+ {
+ *build = nextSPackNo;
+ *pbOverride = true;
+ continue;
+ }
+ if (stepSize > 1)
+ {
+ stepSize = stepSize / 2;
+ continue;
+ }
+ break;
+ }
+ }
+
return true;
}
}
//Detect OS version
- unsigned int major, minor, build; bool overrideFlg;
- if(get_real_os_version(&major, &minor, &build, &overrideFlg))
+ unsigned int major, minor, build, spack; bool overrideFlg;
+ if(get_real_os_version(&major, &minor, &build, &spack, &overrideFlg))
{
g_os_version_info.type = Version::OS_WINDOWS;
g_os_version_info.versionMajor = major;
g_os_version_info.versionMinor = minor;
g_os_version_info.versionBuild = build;
+ g_os_version_info.versionSPack = spack;
g_os_version_info.overrideFlag = overrideFlg;
}
else
{
- qWarning("Failed to determin the operating system version!");
+ qWarning("Failed to determine the operating system version!");
}
//Completed
}
///////////////////////////////////////////////////////////////////////////////
+// DRIVE TYPE
+///////////////////////////////////////////////////////////////////////////////
+
+static wchar_t get_drive_letter(const QString &path)
+{
+ QString nativePath = QDir::toNativeSeparators(path);
+ while (nativePath.startsWith("\\\\?\\") || nativePath.startsWith("\\\\.\\"))
+ {
+ nativePath = QDir::toNativeSeparators(nativePath.mid(4));
+ }
+ if ((path.length() > 1) && (path[1] == QLatin1Char(':')))
+ {
+ const wchar_t letter = static_cast<wchar_t>(path[0].unicode());
+ if (((letter >= 'A') && (letter <= 'Z')) || ((letter >= 'a') && (letter <= 'z')))
+ {
+ return towupper(letter);
+ }
+ }
+ return L'\0'; /*invalid path spec*/
+}
+
+static QSet<DWORD> get_physical_drive_ids(const wchar_t drive_letter)
+{
+ QSet<DWORD> physical_drives;
+ wchar_t driveName[8];
+ _snwprintf_s(driveName, 8, _TRUNCATE, L"\\\\.\\%c:", drive_letter);
+ const HANDLE hDrive = CreateFileW(driveName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+ if (hDrive && (hDrive != INVALID_HANDLE_VALUE))
+ {
+ const size_t BUFF_SIZE = sizeof(VOLUME_DISK_EXTENTS) + (32U * sizeof(DISK_EXTENT));
+ VOLUME_DISK_EXTENTS *const diskExtents = (VOLUME_DISK_EXTENTS*)_malloca(BUFF_SIZE);
+ DWORD dwSize;
+ if (DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, (LPVOID)diskExtents, (DWORD)BUFF_SIZE, (LPDWORD)&dwSize, NULL))
+ {
+ for (DWORD index = 0U; index < diskExtents->NumberOfDiskExtents; ++index)
+ {
+ physical_drives.insert(diskExtents->Extents[index].DiskNumber);
+ }
+ }
+ _freea(diskExtents);
+ CloseHandle(hDrive);
+ }
+ return physical_drives;
+}
+
+static bool incurs_seek_penalty(const DWORD device_id)
+{
+ wchar_t driveName[24];
+ _snwprintf_s(driveName, 24, _TRUNCATE, L"\\\\?\\PhysicalDrive%u", device_id);
+ const HANDLE hDevice = CreateFileW(driveName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+ bool seeking_penalty = true;
+ if (hDevice && (hDevice != INVALID_HANDLE_VALUE))
+ {
+ STORAGE_PROPERTY_QUERY spq;
+ DEVICE_SEEK_PENALTY_DESCRIPTOR dspd;
+ memset(&spq, 0, sizeof(STORAGE_PROPERTY_QUERY));
+ spq.PropertyId = (STORAGE_PROPERTY_ID)StorageDeviceSeekPenaltyProperty;
+ spq.QueryType = PropertyStandardQuery;
+ DWORD dwSize;
+ if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, (LPVOID)&spq, (DWORD)sizeof(spq), (LPVOID)&dspd, (DWORD)sizeof(dspd), (LPDWORD)&dwSize, NULL))
+ {
+ seeking_penalty = dspd.IncursSeekPenalty;
+ }
+ CloseHandle(hDevice);
+ }
+ return seeking_penalty;
+}
+
+static bool is_fast_seeking_drive(const wchar_t drive_letter)
+{
+ bool fast_seeking = false;
+ const QSet<DWORD> physical_drive_ids = get_physical_drive_ids(drive_letter);
+ if (!physical_drive_ids.empty())
+ {
+ fast_seeking = true;
+ for (QSet<DWORD>::const_iterator iter = physical_drive_ids.constBegin(); iter != physical_drive_ids.constEnd(); ++iter)
+ {
+ fast_seeking = fast_seeking && (!incurs_seek_penalty(*iter));
+ }
+ }
+ return fast_seeking;
+}
+
+MUtils::OS::drive_type_t MUtils::OS::get_drive_type(const QString &path, bool *fast_seeking)
+{
+ drive_type_t driveType = DRIVE_TYPE_ERR;
+ const wchar_t driveLetter = get_drive_letter(path);
+ if (driveLetter)
+ {
+ wchar_t driveName[8];
+ _snwprintf_s(driveName, 8, _TRUNCATE, L"\\\\.\\%c:\\", driveLetter);
+ switch (GetDriveTypeW(driveName))
+ {
+ case DRIVE_REMOVABLE: driveType = DRIVE_TYPE_FDD; break;
+ case DRIVE_FIXED: driveType = DRIVE_TYPE_HDD; break;
+ case DRIVE_REMOTE: driveType = DRIVE_TYPE_NET; break;
+ case DRIVE_CDROM: driveType = DRIVE_TYPE_OPT; break;
+ case DRIVE_RAMDISK: driveType = DRIVE_TYPE_RAM; break;
+ }
+ }
+ if (fast_seeking)
+ {
+ if (driveType == DRIVE_TYPE_HDD)
+ {
+ *fast_seeking = is_fast_seeking_drive(driveLetter);
+ }
+ else
+ {
+ *fast_seeking = (driveType == DRIVE_TYPE_RAM);
+ }
+ }
+ return driveType;
+}
+
+///////////////////////////////////////////////////////////////////////////////
// SHELL OPEN
///////////////////////////////////////////////////////////////////////////////
bool MUtils::OS::shell_open(const QWidget *parent, const QString &url, const QString ¶meters, const QString &directory, const bool explore)
{
- return ((int) ShellExecuteW((parent ? parent->winId() : NULL), (explore ? L"explore" : L"open"), MUTILS_WCHR(url), ((!parameters.isEmpty()) ? MUTILS_WCHR(parameters) : NULL), ((!directory.isEmpty()) ? MUTILS_WCHR(directory) : NULL), SW_SHOW)) > 32;
+ return ((int) ShellExecuteW((parent ? reinterpret_cast<HWND>(parent->winId()) : NULL), (explore ? L"explore" : L"open"), MUTILS_WCHR(url), ((!parameters.isEmpty()) ? MUTILS_WCHR(parameters) : NULL), ((!directory.isEmpty()) ? MUTILS_WCHR(directory) : NULL), SW_SHOW)) > 32;
}
bool MUtils::OS::shell_open(const QWidget *parent, const QString &url, const bool explore)
///////////////////////////////////////////////////////////////////////////////
static MUtils::Internal::CriticalSection g_fatal_exit_lock;
-static volatile bool g_fatal_exit_flag = true;
+static QAtomicInt g_fatal_exit_flag;
static DWORD WINAPI fatal_exit_helper(LPVOID lpParameter)
{
{
g_fatal_exit_lock.enter();
- if(!g_fatal_exit_flag)
+ if(!g_fatal_exit_flag.testAndSetOrdered(0, 1))
{
return; /*prevent recursive invocation*/
}
- g_fatal_exit_flag = false;
-
if(g_main_thread_id != GetCurrentThreadId())
{
if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id))
}
}
- if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID) errorMessage, 0, NULL))
+ if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID)errorMessage, 0, NULL))
{
WaitForSingleObject(hThread, INFINITE);
}
+ else
+ {
+ fatal_exit_helper((LPVOID)errorMessage);
+ }
for(;;)
{