From a00be68a1204ef69373335442dd8c986e332cc69 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 11 Mar 2013 11:48:58 +0200 Subject: [PATCH] Adding function to add to local environement path of calling process --- editenv.cpp | 36 ++++++++++++++++++++++++++++++++++++ editenv.hpp | 4 ++++ editenv.rc | 8 ++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/editenv.cpp b/editenv.cpp index 9d5571d..23c3724 100755 --- a/editenv.cpp +++ b/editenv.cpp @@ -22,6 +22,9 @@ //////////////////////////////////////////////////////////////////////////////// #include +#define NOMINMAX +#include +#undef NOMINMAX #include "editenv.hpp" @@ -100,6 +103,39 @@ void pathAdd (env_scope scope, char const *path) } } +void pathAddImmediate (env_scope scope, char const *path) +{ + size_t const sizeMax = std::numeric_limits::max(); + + size_t length = std::string(path).length(); + size_t pos; + + char currPath[8096] = {0}; + GetEnvironmentVariableA("PATH", currPath, 8096); + + std::string value = currPath; + pos = value.find(path, 0); + while (sizeMax != pos) { + if (((0 == pos) || + (';' == value[pos - 1])) && + ((pos + length == value.length()) || + (';' == value[pos + length]))) { + // Found the path in the "Path" environment variable already. + return; + } + pos = value.find(path, pos + 1); + } + + if (0 != value.length()) { + // Nothing is in the Path environment variable yet. + value.append(";"); + } + value.append(path); + + // add to local process + SetEnvironmentVariableA("PATH", value.c_str()); +} + // Removes all matching instances of the specified path from the Path // environment variable. unsigned int pathRemove (env_scope scope, char const *path) diff --git a/editenv.hpp b/editenv.hpp index e593acc..4c32f54 100755 --- a/editenv.hpp +++ b/editenv.hpp @@ -109,6 +109,10 @@ EDITENV_API char const * envValue (editenv::env_scope scope, char const *name); // Return Value: Nothing. EDITENV_API void pathAdd (editenv::env_scope, char const *path); +// Almost the same as pathAdd, but only adds to the local path variable for +// the calling process. +EDITENV_API void pathAddImmediate (editenv::env_scope, char const *path); + // Removes all matching instances of the specified path from the Path // environment variable. // diff --git a/editenv.rc b/editenv.rc index a8a7300..0cc82f0 100755 --- a/editenv.rc +++ b/editenv.rc @@ -53,8 +53,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 0,9,1,0 - PRODUCTVERSION 0,9,1,0 + FILEVERSION 0,9,2,0 + PRODUCTVERSION 0,9,2,0 FILEFLAGSMASK 0x17L #ifdef _DEBUG FILEFLAGS 0x1L @@ -70,12 +70,12 @@ BEGIN BLOCK "040904b0" BEGIN VALUE "FileDescription", "Environment Variable Editor DLL" - VALUE "FileVersion", "0, 9, 1, 0" + VALUE "FileVersion", "0, 9, 2, 0" VALUE "InternalName", "editenv" VALUE "LegalCopyright", "Copyright (C) 2009" VALUE "OriginalFilename", "editenv.dll" VALUE "ProductName", "editenv" - VALUE "ProductVersion", "0, 9, 1, 0" + VALUE "ProductVersion", "0, 9, 2, 0" END END BLOCK "VarFileInfo"