WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Environment

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Setting Environment variables in Windows NT

Information in this article applies to NT and newer.

User environment variables editing the following Registry key using the RegSetExpSz function

 
   HKEY_CURRENT_USER \
	 Environment

System environment variables editing the following Registry key using the RegSetExpSz function

 
   HKEY_LOCAL_MACHINE \
	       SYSTEM \
    CurrentControlSet \
	      Control \
      Session Manager \
	  Environment

Note, however, that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.

To effect these changes without having to log off, broadcast a WM_WININICHANGE message to all windows in the system, so that any interested applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update. The WININICHG is for telling running applications it changed. Newly run applications will pick up the environment automatically. This is done as follows:

Current versions

IntControl(59, -1, "Environment", 0, 0)

Older Versions

WININICHG=26
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:WININICHG, long:0, lpstr:"Environment") 


Here is a complete code sample that set the System Path:

#DefineFunction EnvUpdate ()
   ; Similar effect as logging off and then on again.
   ; For example, changes to the %path% environment might not take effect
   ; until you call EnvUpdate (or you logoff/reboot).
   IntControl(59,-1,"Environment",0,0)
   Return 1
#EndFunction

; Initialize variables
keyhandle = @REGMACHINE
envkeyname = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
envvalue = '[PATH]'
newdata = 'c:\TEMP'

; Read current the System Path
currentenvpath = RegQueryExpSz( keyhandle, envkeyname:envvalue  )
Pause('Current System PATH', currentenvpath)

; Update the System Path
newpath = currentenvpath:';':newdata ; IMPORTANT APPEND NEW DATA TO COMPLETE PATH
RegSetExpSz( keyhandle, envkeyname:envvalue,newpath )

; Read updated the System Path
currentenvpath = RegQueryExpSz( keyhandle, envkeyname:envvalue  )
Pause('Updated System PATH', currentenvpath)

; Update System Environment to reflect changes withourt reboot
Test = EnvUpdate ()
If Test
   Message ("Result","Environment Refresh Successful")
Else
   Message ("Result","Environment Refresh Unsuccessful")
EndIf
Exit


Article ID:   W12921
Filename:   NT Environment variables.txt
File Created: 2011:06:15:11:05:08
Last Updated: 2011:06:15:11:05:08