Dana Posted January 23, 2018 Share Posted January 23, 2018 (edited) 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 January 23, 2018 by Dana Link to comment Share on other sites More sharing options...
RTFC Posted January 23, 2018 Share Posted January 23, 2018 (edited) 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 January 23, 2018 by RTFC My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Dana Posted January 23, 2018 Author Share Posted January 23, 2018 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 More sharing options...
Dana Posted January 23, 2018 Author Share Posted January 23, 2018 (edited) 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 January 23, 2018 by Dana Link to comment Share on other sites More sharing options...
jdelaney Posted January 24, 2018 Share Posted January 24, 2018 Environment variables ARE registry modification. I'd use an INI. IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Dana Posted January 25, 2018 Author Share Posted January 25, 2018 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now