From 1f7d2131e8aed5784c49864eaff65a903e84075f Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 2 Oct 2016 15:21:58 +0200 Subject: [PATCH] Make it possible to set an extra PATH string for the new process, which (will be prepended to PATH environment variable (e.g for loading extra DLL's). --- include/MUtils/Global.h | 2 +- include/MUtils/OSSupport.h | 4 +++ src/Global.cpp | 16 ++++++++-- src/OSSupport_Win32.cpp | 61 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index 22fdd7a..e25957d 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -76,7 +76,7 @@ namespace MUtils MUTILS_API const QString& temp_folder(void); //Process Utils - MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true); + MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true, const QString &extraPath = QString()); //Random MUTILS_API void seed_rand(void); diff --git a/include/MUtils/OSSupport.h b/include/MUtils/OSSupport.h index 32cedd2..86df8ba 100644 --- a/include/MUtils/OSSupport.h +++ b/include/MUtils/OSSupport.h @@ -190,6 +190,10 @@ namespace MUtils MUTILS_API bool wow64fsredir_disable(void *oldValue); MUTILS_API bool wow64fsredir_revert (void *oldValue); + //Environment variables + MUTILS_API QString get_envvar(const QString &name); + MUTILS_API bool set_envvar(const QString &name, const QString &value); + //Check if debugger is present MUTILS_API void check_debugger(void); diff --git a/src/Global.cpp b/src/Global.cpp index 88f2c3f..26fd3bf 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -384,7 +384,14 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) // PROCESS UTILS /////////////////////////////////////////////////////////////////////////////// -void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir) +static void prependToPath(QProcessEnvironment &env, const QString &value) +{ + const QLatin1String PATH = QLatin1String("PATH"); + const QString path = env.value(PATH, QString()).trimmed(); + env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path)); +} + +void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QString &extraPath) { //Environment variable names static const char *const s_envvar_names_temp[] = @@ -419,8 +426,11 @@ void MUtils::init_process(QProcess &process, const QString &wokringDir, const bo } //Setup PATH variable - const QString path = env.value("PATH", QString()).trimmed(); - env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path)); + prependToPath(env, tempDir); + if (!extraPath.isEmpty()) + { + prependToPath(env, QDir::toNativeSeparators(extraPath)); + } //Setup QPorcess object process.setWorkingDirectory(wokringDir); diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index f4fc18e..5eebc4b 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -1546,6 +1546,67 @@ bool MUtils::OS::wow64fsredir_revert(void *oldValue) // 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; +} + +/////////////////////////////////////////////////////////////////////////////// +// DEBUGGER CHECK +/////////////////////////////////////////////////////////////////////////////// + #if (!(MUTILS_DEBUG)) static __forceinline bool is_debugger_present(void) {