AutoHotkey Community

It is currently May 24th, 2012, 1:48 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: April 28th, 2010, 10:47 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
EnvFuncs

I made a quick, very small library (only 4 functions) to deal with permanently setting Environment Variables, rather than temporarily as commands like EnvSet does. Its not much, and wasn't hard to code, but I figured I'd share in case anyone is interested. I have tested in on XP, and would like to know if it works on Vista or 7, if anyone would be alright with testing it. BTW, it does stuff via the registry, to make changes permenant.

The functions are:

Code:
Env_Read()
Env_Write()
Env_Append()
Env_Delete()
Env_ReadAll()


I think they're all fairly simple, but there is more info, as well as the parameters, in the gen.chm file in the archive, generated with fincs's GenDocs.

Hope this is useful to someone.

Update: Updated to v1.1.

Changelog:

Code:
**v1.1**
- Changed function names to become Standard Library
- Added EnvUpdate to all functions which write/delete variables
- Added Env_ReadAll()

**v1.0**
- Initial Release



Download: http://www.autohotkey.net/~SifJar/EnvFunc%20v1.1.zip


Last edited by SifJar on May 1st, 2010, 8:56 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2010, 5:47 pm 
i know some would need this. I ask you to put an underscore after each env in the name of all functions. This make the lib available for standard library mechanism. Ill check this later. Thx for sharing.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2010, 10:26 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
Tuncay n-l-i mobile wrote:
i know some would need this. I ask you to put an underscore after each env in the name of all functions. This make the lib available for standard library mechanism. Ill check this later. Thx for sharing.


ok will do. i also have another function to add which will get all env variables in two arrays (one for names, one for values), but it needs testing. i should have both done and uploaded in about 24 hours


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2010, 11:44 pm 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 772
Location: Texas, USA
I have only performed preliminary tests but these functions don't work on Windows XP. I'm fairly sure they don't work on any OS. You are given the illusion that it works because the EnvGet function is able to get a value you just updated using the EnvWrite function but all these functions are doing is reading and writing to the registry. The real environment variables are not being updated. You can duplicate my result by using the EnvWrite function to create a new environment variable and using the standard AutoHotkey EnvGet command to collect the value of this variable. In addition, you should also be able to see the new environment variable when you perform the Set command in a DOS shell.

I think the registry location is correct (very limited research on msdn) but the doc indicates that you have to send the WM_SETTINGCHANGE message in order to get the environment variables permanently updated. I have not tested or verified this information.

And then again, I might be completely off base and if so, I apologize.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 30th, 2010, 6:30 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
jballi wrote:
I have only performed preliminary tests but these functions don't work on Windows XP. I'm fairly sure they don't work on any OS. You are given the illusion that it works because the EnvGet function is able to get a value you just updated using the EnvWrite function but all these functions are doing is reading and writing to the registry. The real environment variables are not being updated. You can duplicate my result by using the EnvWrite function to create a new environment variable and using the standard AutoHotkey EnvGet command to collect the value of this variable. In addition, you should also be able to see the new environment variable when you perform the Set command in a DOS shell.

I think the registry location is correct (very limited research on msdn) but the doc indicates that you have to send the WM_SETTINGCHANGE message in order to get the environment variables permanently updated. I have not tested or verified this information.

And then again, I might be completely off base and if so, I apologize.


Hmm, using my method seems to work for me in that I can add a folder to the Path registry entry, and then launch folders from there with AHK command Run or from Run box in windows...

How would I send that message? Would I just do that after changing the registry entry, and then all should work?

BTW, I tested on XP...

EDIT: Yeah, just did another test, DEFINITELY works for me on XP SP2. I can see the folder added to the Path by using the "set path" command in cmd.exe, and can open a file in the folder from Run box.

Using the following script:

Code:
#include env.ahk

EnvAppend("Path", ";C:\test")
Return


I got the following output in a command window:
Code:
C:\Documents and Settings\Inspiron 6000>set path
Path=c:\devkitPro\msys\bin;c:\Dev-Cpp\bin;c:\devkitpro\devkitpsp\bin;c:\Program
Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Common Files\Teleca S
hared;C:\shortcuts;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Prog
ram Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Rox
io Shared\11.0\DLLShared\;C:\Program Files\TortoiseSVN\bin;C:\Windows;C:\Windows
\system32;C:\Windows\system;C:\Scripts;C:\Program Files\QuickTime\QTSystem\;C:\P
ython26;C:\Documents and Settings\Inspiron 6000\My Documents\C++;C:\test\


Note ;C:\test\ at end of output.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 30th, 2010, 11:33 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
New version isn't quite ready yet, hopefully by tomorrow it'll be done.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 1st, 2010, 12:17 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 772
Location: Texas, USA
SifJar wrote:
How would I send that message? Would I just do that after changing the registry entry, and then all should work?

If by message, you mean WM_SETTINGCHANGE, here's the link:
http://tinyurl.com/2cjqfpt

SifJar wrote:
EDIT: Yeah, just did another test, DEFINITELY works for me on XP SP2. I can see the folder added to the Path by using the "set path" command in cmd.exe, and can open a file in the folder from Run box.

Using the following script:

Code:
#include env.ahk

EnvAppend("Path", ";C:\test")
Return


I repeated this test on my machine and I got everything except the additional ";C:\test".

Since there is a discrepancy, you might want to get a 3rd party to confirm the results. (Any volunteers?) Remember, you can't use the EnvGet function to confirm changes. Also, don't use the Path environment variable to confirm. Path is a special environment variable used by most programs and one of the programs/processes on you machine might be automatically updating the environment when the the Path is changed. Try something like SifJarTest.

Good luck!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 1st, 2010, 12:38 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
Am I correct in thinking that the built-in AHK command EnvUpdate will do this task? From my reading of the help page on it, it seems so. I will add it to the end of each function, and then hopefully it should work for you. I should have this in the new version I will release later.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 1st, 2010, 1:05 pm 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 772
Location: Texas, USA
SifJar wrote:
Am I correct in thinking that the built-in AHK command EnvUpdate will do this task? From my reading of the help page on it, it seems so. I will add it to the end of each function, and then hopefully it should work for you. I should have this in the new version I will release later.

I had never seen the EnvUpdate command before now but it looks like what you need. Give it a try/see.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 1st, 2010, 8:58 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
I added v1.1. This includes renamed functions (is that all I need to do to make this a Standard Library?), Env_ReadAll(), and EnvUpdate at the end of all functions which write/delete variables. Download link in the first post. I've also included all the examples from the help file as individual .AHK files in case anyone wants them.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2010, 3:41 pm 
Offline

Joined: March 28th, 2010, 9:02 pm
Posts: 39
Thanks for these! They work perfectly fine on my XP box. It would be cool to be able to remove a value from a variable, e.g. remove some path from the PATH var by searching for it.

I'll work on this later, if I get it working I'll post the code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 4th, 2010, 4:20 pm 
Offline

Joined: March 28th, 2010, 9:02 pm
Posts: 39
Ok, done. I just did a simple string-replace, it seems to work fine for manipulating the PATH variable. But it's a good idea to keep a backup of the environment variables (Rapid Environment Editor is pretty good for this).

Also, it doesn't keep track of the location of the string, so if you remove a value from the path and then want to add it again it gets appended to the end.

Here's the updated modules:

http://rapidshare.com/files/404906783/env_updated.zip (Can be downloaded 10 times max, where else can I host this?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2010, 9:57 pm 
Offline

Joined: February 13th, 2010, 3:52 pm
Posts: 175
AndrejM8 wrote:
Ok, done. I just did a simple string-replace, it seems to work fine for manipulating the PATH variable. But it's a good idea to keep a backup of the environment variables (Rapid Environment Editor is pretty good for this).

Also, it doesn't keep track of the location of the string, so if you remove a value from the path and then want to add it again it gets appended to the end.

Here's the updated modules:

http://rapidshare.com/files/404906783/env_updated.zip (Can be downloaded 10 times max, where else can I host this?)


Host on autohotkey.net, free hosting, pretty cool.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 19th, 2011, 5:43 pm 
Offline

Joined: May 17th, 2010, 6:16 pm
Posts: 29
SifJar and everyone... You must issue include this code after the reg changes if you expect any other apps to see the environment variable changes without a logout and log back in! I spent hours struggling to find the solution! A number of people (including here on AHK) had the right idea, to send the message WM_SETTINGSCHANGE but not had the right params! Without the "Environment" param below apps just seemed to ignore the message.

Code:
; This is what tells Explorer and all apps to update their reading of the environment settings from reg.
; As found: http://support.microsoft.com/kb/104011
SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE


Hope it saves someone else the headache I had!

^ Quinxy

_________________
Quinxy's AutoHotKey Scripts


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Cerberus, cmulcahy, hoppfrosch, MSN [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group