Creating System Environment Variables

MSDN:

This is sample code. Add error handling and adjust to your requirements as necessary.

#DEFINE HWND_BROADCAST 0xFFFF
#DEFINE WM_SETTINGCHANGE 0x001A
DECLARE INTEGER SendMessageTimeout IN WIN32API ;
    Long hWnd, Long Msg, Long wParam, String lParam, ;
    Long fuFlags, Long uTimeout, Long lpdwResult
DECLARE Long GetLastError IN kernel32
 
* To programmatically add or modify system environment variables, add them 
* to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key.
* WSH is used as an example. The registry key can be added by any other means available.
oShell = Createobject("wscript.shell")
lcEnvRegKey = "HKLM\System\CurrentControlSet\Control\Session Manager\Environment\"
 
* Environment variable name
lcEnvVarName = "Foo"
* Environment variable value
lcEnvVarVal = "FooValue"
oShell.RegWrite(lcEnvRegKey + lcEnvVarName, lcEnvVarVal, "REG_SZ")
 
* Broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment"
* This allows applications, such as the shell, to pick up updates
lnRetVal = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", 0, 1000, 0)
IF lnRetVal = 0
	* Error
	? GetLastError()
ENDIF 
Your rating: None Average: 1 (1 vote)