Jump to content

EnvSet and EnvUpdate problem


Dana
 Share

Recommended Posts

I'm trying to use environment variables to store a checkbox setting between program runs.  The use of EnvGet and EnvSet seems clear enough, but they don't seem to actually do anything.  Adding EnvUpdate() to the code seems to make the program permanently hang (in the full version) or take 25 seconds (in the test version below), but still, the environment variable isn't set so the next time I run it it comes up blank, and running SET from a command prompt doesn't show it as existing.  This happens whether I run the script from SciTE or as a compiled executable.  In the full program when I ran the compiled executable including EnvUpdate, the program hung, and after killing it I was unable to delete the .exe because Windows said it's still open in Windows Error Reporting, had to restart the computer.  Actually, in the full version, though I said it permanently hung it might have come back eventually, I didn't retry that as the autoit script is in turn called by another program and all of that takes a long time to set up.

 

$printopt = EnvGet("KC2ST_PRINTOPT")
MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @CRLF & '$printopt' & @CRLF & @CRLF & 'Return:' & @CRLF & $printopt) ;### Debug MSGBOX
$printopt = MsgBox(4097, "choose option", "ok to print, cancel to skip")
If ($printopt = 2) Then $printopt = 0
MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @CRLF & '$printopt' & @CRLF & @CRLF & 'Return:' & @CRLF & $printopt) ;### Debug MSGBOX
EnvSet("KC2ST_PRINTOPT", $printopt)
EnvUpdate()
MsgBox(4096, "result", EnvGet("KC2ST_PRINTOPT") & @crlf & "@error = " & @error)


 

Edited by Dana
Link to comment
Share on other sites

As the helpfile cleary states under EnvSet:

Quote

Once AutoIt closes, the variables will cease to exist.

So storing settings between runs won't work. I would suggest you use an .ini file or write to the registry for that.

 

Edited by RTFC
Link to comment
Share on other sites

Doh!  Missed that.  I misinterpreted the use of EnvUpdate as saving the local session copy of the environment to the windows master copy, but obviously I was wrong.  I was trying to avoid setting something (ini or registry) that persists after a reboot.

Running SET from a command prompt sets a variable used only for that session, but running SETX makes it persistent.  Have to see if that persists past a restart.

Link to comment
Share on other sites

OK, got it:

$printopt = EnvGet("KC2ST_PRINTOPT")
MsgBox(262144, 'Test', 'Current value of KC2ST_PRINTOPT is ' & $printopt)
$printopt = MsgBox(4097, "choose option", "ok to print, cancel to skip")
If ($printopt = 2) Then $printopt = 0
Run (@ComSpec & " /c " & 'SETX KC2ST_PRINTOPT ' & $printopt, "", @SW_HIDE)

This sets it for all future sessions, but not the current one.  That's OK, as during the current session I still have the updated value in the $printopt variable; I don't have any other external programs needing to access it.  If I needed the environment variable to be set for the current session, I could use SetEnv or the command line SET in addition to SETX, but I don't.

Interestingly (but unsurprisingly), if I run the script repeatedly from SciTE, it doesn't work as I guess it's the same session, but when compiled it works as it should. :)

It does stay persistent even after restarting the computer (since SETX puts it in the registry), but I'd rather have that than not remember it at all.  Given that, I guess there's no advantage to doing it this way over putting it in an ini file or other registry key, but I have it this way and it works, so I'm not going to worry about it.

Edited by Dana
Link to comment
Share on other sites

I spoke too soon.  What I posted worked in the test file, but not in the real one.  The problem is that unlike the test program, the real script is called from a parent program (my CAD software), so it inherits the parent's copy of the environment... which doesn't get the changes made with SETX unless the parent CAD software itself is restarted.  So I ended up doing it with RegRead and RegWrite after all, and now it really does work.  Some wheelspinning along the way, but I learned something in the process so all is good.

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