Reputation: 23
i have this code under a label of a batch. the code checks if a queued directory is in the system PATH environment variable, then edits the variable depending on the result.
i want to make sure the PATH variable is changed permanently in every version of windows, and since windows xp and below doesn't have the 'setx' command i ended up with a 'reg add' command for those operating systems.
everything works as it should with the code but the problem is that i want the changes to take effect immediately. in the versions of windows where the code edits the system PATH variable through 'reg add' this is not the case. apparently the registry changes has to be activated for 'cmd.exe' to take notice of the changes.
command index site ss64's description of the reg.exe commands state the following:
Activate
To activate registry changes in HKEY_CURRENT_USER without logging off:
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
but this only updates the CURRENT_USER hive and not he LOCAL_MACHINE hive.
i know i can change the code to work with the current users environment variables but that's a last case scenario. there is a lot more code to this as there could or could not be a PATH value when adding the directory from queue. the value could or could not contain data, and depending on that the value should or should not be deleted entirely.
what i'm asking:
related questions \ anwers:
How would you write a .bat or .cmd file to remove an element from the PATH?
Can a script.bat make changes to Windows PATH Environment Variable
How to persistently set a variable in Windows 7 from a batch file?
Window batch file - removing the directory from a file path
Add/Remove from Path using Batch?
call :confini;r;settings;installdir;instdir ; queue the value
for /f "delims=" %%i in ('echo "%path%" ^| find /c /i "%instdir%"') do (
set result=%%i 2>nul
)
for /f "tokens=4,5 delims=. " %%i in ('ver') do (
set version=%%i.%%j 2>nul
)
set reg1="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
set path0=%path%
for /l %%a in (1,1,1) do if "%path0:~-1%"==";" set "path0=%path0:~0,-1%"
set path1="%path0:;=" "%"
if /i %version% geq 6.0 (
set "excmd=setx path -m "!pathN!" >nul"
)
if /i %version% leq 5.2 (
set "excmd=reg add %reg1% /v PATH /d "!pathN!" /f >nul & "%~dp0envirupd.exe""
)
if %result%==0 (
if /i %version% geq 6.0 (
setx path -m "%instdir%;%path0%" >nul
)
if /i %version% leq 5.2 (
reg add %reg1% /v PATH /d "%instdir%;%path0%" /f >nul
%~dp0envirupd.exe
)
) else (
setlocal enabledelayedexpansion
for %%p in (%path1%) do (
if /i not "%%~p"=="%instdir%" (
if "!pathN!"=="" (
set pathN=%%~p
) else (
set pathN=!pathN!;%%~p
)
)
)
%excmd%
setlocal disabledelayedexpansion
)
Upvotes: 1
Views: 3032
Reputation: 23
#include "windows.h"
void main(void) {
SendMessageTimeout(
HWND_BROADCAST,
WM_SETTINGCHANGE,
(WPARAM) NULL,
(LPARAM) "Environment",
SMTO_NORMAL,
1000,
NULL
);
}
this will update / activate the environment changes in the registry. presented by MC ND
Upvotes: 1
Reputation: 26
The environment is program specific and created at program creation. SetX does not change anything for current programs.
So the computer boots and read the various environments (and when logging on).
Winlogon starts Explorer and gives it a copy of Winlogon's environment, Explorer starts notepad and gives it a copy of Explorer's environment.
Remember no program can access another program's memory. So you CANNOT change the environment of another running program.
So neither Winlogon, Explorer, or Notepad will be affected by changes to the environment till you restart.
CMD is a bit different as it gets the current settings itself when it starts and updates the copy it's given.
But you have to use both set and setx to change a current cmd window.
Also the function you are running is the SystemParametersInfo function update.
It updates the user profile with SystemParametersInfo information (not environment strings) then optionally broadcasts WM_SETTINGCHANGE which programs get. As it's isn't for environments programs won't know what to do with it.
Upvotes: 0