3 // Returns the
value(s
) of the environment variable
"VarName", which
is tokenized
4 // by
";" into an
array of strings
. This makes it easy query PATH
-like variables
5 // in addition
to normal variables
. If "AllUsers" is true, the common variables
6 // are searched
, else the user
-specific ones
.
7 function GetEnvStrings(VarName
:string;AllUsers
:Boolean):TArrayOfString
;
15 // See http
://www
.jrsoftware
.org
/isfaq
.php#env
16 if AllUsers
then begin
17 // We ignore errors here
. The resulting
array of strings will be empty
.
18 RegQueryStringValue(HKEY_LOCAL_MACHINE
,'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',VarName
,Path
);
20 // We ignore errors here
. The resulting
array of strings will be empty
.
21 RegQueryStringValue(HKEY_CURRENT_USER
,'Environment',VarName
,Path
);
24 // Fallback
for built
-in variables which are
not stored
in the Registry
.
25 if Length(Path
)=0 then begin
26 Path
:=ExpandConstant('{%'+VarName
+'}');
29 // Make sure we have at least one semicolon
.
32 // Split the directories
in PATH into an
array of strings
.
34 SetArrayLength(Result
,0);
38 SetArrayLength(Result
,i
+1);
40 Result
[i
]:=Copy(Path
,1,p
-1);
43 Path
:=Copy(Path
,p
+1,Length(Path
));
48 // Sets the contents
of the specified environment variable
for the current process
.
49 function SetEnvironmentVariable(lpName
,lpValue
:String):Boolean;
51 external 'SetEnvironmentVariableW@Kernel32.dll stdcall delayload';
53 external 'SetEnvironmentVariableA@Kernel32.dll stdcall delayload';
56 // Sets the environment variable
"VarName" to the concatenation
of "DirStrings"
57 // using
";" as the delimiter
. If "AllUsers" is true, a common variable
is set,
58 // else a user
-specific one
. If "DeleteIfEmpty" is true and "DirStrings" is
59 // empty
, "VarName" is deleted instead
of set if it exists
.
60 function SetEnvStrings(VarName
:string;AllUsers
,DeleteIfEmpty
:Boolean;DirStrings
:TArrayOfString
):Boolean;
65 // Merge all non
-empty directory strings into a PATH variable
.
67 for i
:=0 to GetArrayLength(DirStrings
)-1 do begin
68 if Length(DirStrings
[i
])>0 then begin
69 if Length(Path
)>0 then begin
70 Path
:=Path
+';'+DirStrings
[i
];
77 // See http
://www
.jrsoftware
.org
/isfaq
.php#env
78 if AllUsers
then begin
79 KeyName
:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
80 if DeleteIfEmpty
and (Length(Path
)=0) then begin
81 Result
:=(not RegValueExists(HKEY_LOCAL_MACHINE
,KeyName
,VarName
)) or
82 RegDeleteValue(HKEY_LOCAL_MACHINE
,KeyName
,VarName
);
84 Result
:=RegWriteStringValue(HKEY_LOCAL_MACHINE
,KeyName
,VarName
,Path
);
87 KeyName
:='Environment';
88 if DeleteIfEmpty
and (Length(Path
)=0) then begin
89 Result
:=(not RegValueExists(HKEY_CURRENT_USER
,KeyName
,VarName
)) or
90 RegDeleteValue(HKEY_CURRENT_USER
,KeyName
,VarName
);
92 Result
:=RegWriteStringValue(HKEY_CURRENT_USER
,KeyName
,VarName
,Path
);
96 // Also update the environment
of the current process
.
97 SetEnvironmentVariable(VarName
,Path
);