Jump to content

Shouldn't EnvUpdate() "export" changed Env to the OS' Env?


rudi
 Share

Recommended Posts

Hello,

I'd like to set some environment variables from an autoit script.

[helpfile]

EnvUpdate 
--------------------------------------------------------------------------------

Refreshes the OS environment.


EnvUpdate ( )

These settings shall be permanent for that PC:

EnvSet("ChoppitPath","C:\Choppit\")
RunWait (@ComSpec & " /k set | find /i ""Choppit""")
EnvUpdate()
; opening a "Start, CMD" cmd box or looking to the Env Settings in the "System Properties" shows
; that the OS' Environment was *NOT* updated?
MsgBox(0,"","done")

What do I miss?

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

...What do I miss?...

"EnvUpdate() has absolutely nothing to do with EnvSet/EnvGet."

http://www.autoitscript.com/forum/index.ph...ost&p=33358

"EnvUpdate() is irrelevant to causing environment variables to be updated unless writing the variables to the registry."

http://www.autoitscript.com/forum/index.ph...st&p=108910

Oddly enough, those were not the posts that I was looking for... but they may help you.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Hi, thanks for your replies.

I found a nice tool included in the XP SP2 Support Tools Pack: WindowsXP-KB838079-SupportTools-ENU.exe

With SETX.EXE it's easy to change environment variables to the OS Environment :D

SETX: This program is used to set values in the environment
of the machine or currently logged on user using one of three modes.

1) Command Line Mode: setx variable value [-m]
   Optional Switches:
    -m  Set value in the Machine environment. Default is User.

2) Registry Mode: setx variable -k hive\key\...\value
   Optional Switches:
    -m  Set value in the Machine environment. Default is User.

3) File Mode: setx variable -f file {-a x,y | -r x,y "string"} [-d d] [-x] [-m]
   Required Switches:
    -f  file         : Specify file name to use.
    -a  x,y       : Specify absolute coordinates and offset.
    -r  x,y "string" : Specify coordinates and offset relative to string.
   Optional Switches
    -d  ,:\ etc.  : Specify additional delimiters.
    -x          : Displays file coordinates. Switches -a -r -e ignored!!
    -m          : Set value in the Machine environment. Default is User.

For more information and example use: SETX -i

But EnvUpdate() seems not to work :)

Dim $UserEnv=" FooUser"
Dim $MachEnv=" FooMachine"
Dim $MyVal=" sunshine"
dim $setx ='"' &@ProgramFilesDir & '\Support Tools\setx.exe"'

RunWait($setx & $UserEnv & $MyVal ) 
RunWait($setx & $MachEnv & $MyVal & " -m")  
EnvUpdate()
MsgBox(0,"info","EnvUpdate() done...",2)
MsgBox(48,"Does NOT work","And now you can see, that the new opened CMD box doesn't reflect the updated Env.")
Run(@ComSpec & " /k set")
MsgBox(0,"Environment *IS* updated","Now you can check, that the Machine Env Var """ & $MachEnv & """"  & @LF & _
    "and the User Env Var """& $UserEnv & """ *ARE* set ...")
send("{LWINDOWN}{pause}{LWINUP}")

Why? :D

[hkcu\environment] and [HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment] both reflect the settings, EnvUpdate() should update these system wide, right? :D

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

This is my understanding of Env...()

EnvSet/EnvGet

The parent process inherits the environment at execution and any child process executed from the parent process inherits the same environment. You can use EnvSet() to alter the environment variables and create new environmental variables at a software level within the process and child processes of the parent process. You can use EnvGet() to retrieve the environmental variables inherited and by those set/modified by EnvSet(). EnvSet() does not do a permanent change to the environmental variables.

EnvSet() changes at the software level and the child process of @ComSpec inherits the change below.

EnvSet('ProgramFiles', 'C:\rudi')
RunWait(@ComSpec & ' /k set ProgramFiles')oÝ÷ Ù±'½J]j׿m.q©î±ä^jâµëh©¥êæjw§µÈZ¬¶§¾*èg§µ©oj¸nW¬{az· ËkÉ»­j|©àzÌ"Y趷­ë!¶ø«¢yÖ¥½ªâi¹^²íç.®·§¶«z{i®²ËajØ­x^®+^u«lµªí¡÷±yË­è­©òr¥vèqë,zDZyË­y×ë¢kaz«z{i®²Ãh®f¥&«zËZ®Ú¶J++z«¢­ç_¢¶§Êw°¥ªÞÚk¡Ç¬²Ú"«ÛayÈZ¬¶ayéïº'éíjÊkzø¨ºÉrvÞ{Ô¥Ö­y©e£)j·§¶èqë,zDZyË­yÖµêíë¬zÄõ)^¶§êâ¶Ø^z{â®æz{ZÈZ¬Â+a¢ëZ­ë-j»h~Ø^9$è±çõ)^Â䶧ʧyçv¥ªÞÚk¡Ç¬±ë-¢×¬µ§¶¬jëh×6If RegRead('HKCU\Environment', 'test') = '' And @error Then
    RegWrite('HKCU\Environment', 'test', 'REG_SZ', 'string')
    EnvUpdate()
    MsgBox(0x40000, Default, 'Start a new process now')
EndIfoÝ÷ ÚØ^^*.±ç­ã,º1¶§®éí+ºÚ"µÍ[ØZ]
ÛÛTÜXÈ  [È ÌÎNÈÚÈÙ]Ý    ÌÎNÊBYÑ[]J  ÌÎNÒÐÕIÌLÑ[ÛY[  ÌÎNË ÌÎNÝÝ   ÌÎNÊB[]J

Do not run both of these scripts from the same instance of Scite as Scite will be the parent process and both scripts will execute as child processes so the environment change may not be seen to change. Start the scripts through explorer will do.

:D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...