+ return (procInf) ? procInf->dwProcessId : 0;
+}
+
+quint32 MUtils::OS::thread_id(void)
+{
+ return GetCurrentThreadId();
+}
+
+quint32 MUtils::OS::thread_id(const QProcess *const proc)
+{
+ PROCESS_INFORMATION *procInf = proc->pid();
+ return (procInf) ? procInf->dwThreadId : 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// PROCESS SUSPEND/RESUME
+///////////////////////////////////////////////////////////////////////////////
+
+bool MUtils::OS::suspend_process(const QProcess *proc, const bool suspend)
+{
+ if(Q_PID pid = proc->pid())
+ {
+ if(suspend)
+ {
+ return (SuspendThread(pid->hThread) != ((DWORD) -1));
+ }
+ else
+ {
+ return (ResumeThread(pid->hThread) != ((DWORD) -1));
+ }
+ }
+ else
+ {
+ return false;
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// SYSTEM TIMER
+///////////////////////////////////////////////////////////////////////////////
+
+bool MUtils::OS::setup_timer_resolution(const quint32 &interval)
+{
+ return timeBeginPeriod(interval) == TIMERR_NOERROR;
+}
+
+bool MUtils::OS::reset_timer_resolution(const quint32 &interval)
+{
+ return timeEndPeriod(interval) == TIMERR_NOERROR;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// SET FILE TIME
+///////////////////////////////////////////////////////////////////////////////
+
+static QScopedPointer<QDateTime> s_epoch;
+static QReadWriteLock s_epochLock;
+
+static const QDateTime *get_epoch(void)
+{
+ QReadLocker rdLock(&s_epochLock);
+
+ if (s_epoch.isNull())
+ {
+ rdLock.unlock();
+ QWriteLocker wrLock(&s_epochLock);
+ if (s_epoch.isNull())
+ {
+ s_epoch.reset(new QDateTime(QDate(1601, 1, 1), QTime(0, 0, 0, 0), Qt::UTC));
+ }
+ wrLock.unlock();
+ rdLock.relock();
+ }
+
+ return s_epoch.data();
+}
+
+static FILETIME *qt_time_to_file_time(FILETIME *const fileTime, const QDateTime &dateTime)
+{
+ memset(fileTime, 0, sizeof(FILETIME));
+
+ if (const QDateTime *const epoch = get_epoch())
+ {
+ const qint64 msecs = epoch->msecsTo(dateTime);
+ if (msecs > 0)
+ {
+ const quint64 ticks = 10000U * quint64(msecs);
+ fileTime->dwHighDateTime = ((ticks >> 32) & 0xFFFFFFFF);
+ fileTime->dwLowDateTime = (ticks & 0xFFFFFFFF);
+ return fileTime;
+ }
+ }
+
+ return NULL;
+}
+
+static bool set_file_time(const HANDLE hFile, const QDateTime &created, const QDateTime &lastMod, const QDateTime &lastAcc)
+{
+ FILETIME ftCreated, ftLastMod, ftLastAcc;
+
+ FILETIME *const pCreated = created.isValid() ? qt_time_to_file_time(&ftCreated, created) : NULL;
+ FILETIME *const pLastMod = lastMod.isValid() ? qt_time_to_file_time(&ftLastMod, lastMod) : NULL;
+ FILETIME *const pLastAcc = lastAcc.isValid() ? qt_time_to_file_time(&ftLastAcc, lastAcc) : NULL;
+
+ if (pCreated || pLastMod || pLastAcc)
+ {
+ return (SetFileTime(hFile, pCreated, pLastAcc, pLastMod) != FALSE);
+ }
+
+ return false;
+}
+
+bool MUtils::OS::set_file_time(const QFile &file, const QDateTime &created, const QDateTime &lastMod, const QDateTime &lastAcc)
+{
+ const int fd = file.handle();
+ if (fd >= 0)
+ {
+ return set_file_time((HANDLE)_get_osfhandle(fd), created, lastMod, lastAcc);
+ }
+ return false;
+}
+
+bool MUtils::OS::set_file_time(const QString &path, const QDateTime &created, const QDateTime &lastMod, const QDateTime &lastAcc)
+{
+ const HANDLE hFile = CreateFileW(MUTILS_WCHR(path), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
+ bool okay = false;
+ if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
+ {
+ okay = set_file_time(hFile, created, lastMod, lastAcc);
+ CloseHandle(hFile);
+ }
+ return okay;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// CHECK KEY STATE
+///////////////////////////////////////////////////////////////////////////////
+
+bool MUtils::OS::check_key_state_esc(void)
+{
+ return (GetAsyncKeyState(VK_ESCAPE) & 0x0001) != 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// SHELL CHANGE NOTIFICATION
+///////////////////////////////////////////////////////////////////////////////
+
+void MUtils::OS::shell_change_notification(void)
+{
+ SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// WOW64 REDIRECTION
+///////////////////////////////////////////////////////////////////////////////
+
+typedef BOOL (_stdcall *Wow64DisableWow64FsRedirectionFun)(void *OldValue);
+typedef BOOL (_stdcall *Wow64RevertWow64FsRedirectionFun )(void *OldValue);
+
+bool MUtils::OS::wow64fsredir_disable(void *oldValue)
+{
+ const Wow64DisableWow64FsRedirectionFun wow64redir_disable = MUtils::Win32Utils::resolve<Wow64DisableWow64FsRedirectionFun>(QLatin1String("kernel32"), QLatin1String("Wow64DisableWow64FsRedirection"));
+ if(wow64redir_disable)
+ {
+ if (wow64redir_disable(oldValue))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool MUtils::OS::wow64fsredir_revert(void *oldValue)
+{
+ const Wow64RevertWow64FsRedirectionFun wow64redir_disable = MUtils::Win32Utils::resolve<Wow64RevertWow64FsRedirectionFun>(QLatin1String("kernel32"), QLatin1String("Wow64RevertWow64FsRedirection"));
+ if (wow64redir_disable)
+ {
+ if (wow64redir_disable(oldValue))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// DEBUGGER CHECK
+///////////////////////////////////////////////////////////////////////////////
+
+QString MUtils::OS::get_envvar(const QString &name)
+{
+ wchar_t *buffer = NULL;
+ size_t requiredSize = 0, buffSize = 0;
+ QString result;
+
+ forever
+ {
+ //Adjust the buffer size as required first!
+ if (buffSize < requiredSize)
+ {
+ if (buffer)
+ {
+ _freea(buffer);
+ }
+ if (!(buffer = (wchar_t*)_malloca(sizeof(wchar_t) * requiredSize)))
+ {
+ break; /*out of memory error!*/
+ }
+ buffSize = requiredSize;
+ }
+
+ //Try to fetch the environment variable now
+ const errno_t error = _wgetenv_s(&requiredSize, buffer, buffSize, MUTILS_WCHR(name));
+ if(!error)
+ {
+ if (requiredSize > 0)
+ {
+ result = MUTILS_QSTR(buffer);
+ }
+ break; /*done*/
+ }
+ else if (error != ERANGE)
+ {
+ break; /*somethging else went wrong!*/
+ }
+ }
+
+ if (buffer)
+ {
+ _freea(buffer);
+ buffSize = 0;
+ buffer = NULL;
+ }
+
+ return result;
+}
+
+bool MUtils::OS::set_envvar(const QString &name, const QString &value)
+{
+ if (!_wputenv_s(MUTILS_WCHR(name), MUTILS_WCHR(value)))
+ {
+ return true;
+ }
+ return false;