Creating System Environment Variables

MSDN:

$SAMPLECODE$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#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 


Comments