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).

This commit is contained in:
LoRd_MuldeR 2016-10-02 15:21:58 +02:00
parent 3c6a7b1745
commit 1f7d2131e8
4 changed files with 79 additions and 4 deletions

View File

@ -76,7 +76,7 @@ namespace MUtils
MUTILS_API const QString& temp_folder(void); MUTILS_API const QString& temp_folder(void);
//Process Utils //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 //Random
MUTILS_API void seed_rand(void); MUTILS_API void seed_rand(void);

View File

@ -190,6 +190,10 @@ namespace MUtils
MUTILS_API bool wow64fsredir_disable(void *oldValue); MUTILS_API bool wow64fsredir_disable(void *oldValue);
MUTILS_API bool wow64fsredir_revert (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 //Check if debugger is present
MUTILS_API void check_debugger(void); MUTILS_API void check_debugger(void);

View File

@ -384,7 +384,14 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
// PROCESS UTILS // 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 //Environment variable names
static const char *const s_envvar_names_temp[] = 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 //Setup PATH variable
const QString path = env.value("PATH", QString()).trimmed(); prependToPath(env, tempDir);
env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path)); if (!extraPath.isEmpty())
{
prependToPath(env, QDir::toNativeSeparators(extraPath));
}
//Setup QPorcess object //Setup QPorcess object
process.setWorkingDirectory(wokringDir); process.setWorkingDirectory(wokringDir);

View File

@ -1546,6 +1546,67 @@ bool MUtils::OS::wow64fsredir_revert(void *oldValue)
// DEBUGGER CHECK // 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)) #if (!(MUTILS_DEBUG))
static __forceinline bool is_debugger_present(void) static __forceinline bool is_debugger_present(void)
{ {