1 // Copies a NULL
-terminated
array of characters
to a
string.
2 function ArrayToString(Chars
:array of Char):String;
6 Len
:=GetArrayLength(Chars
);
10 while (i
<Len
) and (Chars
[i
]<>#
0) do begin
11 Result
[i
+1]:=Chars
[i
];
18 // Copies a
string to a NULL
-terminated
array of characters
.
19 function StringToArray(Str
:String):array of Char;
24 SetArrayLength(Result
,Len
+1);
35 // Returns the path
to the common
or user shell folder
as specified
in "Param".
36 function GetShellFolder(Param
:string):string;
38 if IsAdminLoggedOn
then begin
39 Param
:='{common'+Param
+'}';
41 Param
:='{user'+Param
+'}';
43 Result
:=ExpandConstant(Param
);
46 // Returns the
value(s
) of the environment variable
"VarName", which
is tokenized
47 // by
";" into an
array of strings
. This makes it easy query PATH
-like variables
48 // in addition
to normal variables
. If "AllUsers" is true, the common variables
49 // are searched
, else the user
-specific ones
.
50 function GetEnvStrings(VarName
:string;AllUsers
:Boolean):TArrayOfString
;
58 // See http
://www
.jrsoftware
.org
/isfaq
.php#env
59 if AllUsers
then begin
60 // We ignore errors here
. The resulting
array of strings will be empty
.
61 RegQueryStringValue(HKEY_LOCAL_MACHINE
,'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',VarName
,Path
);
63 // We ignore errors here
. The resulting
array of strings will be empty
.
64 RegQueryStringValue(HKEY_CURRENT_USER
,'Environment',VarName
,Path
);
67 // Make sure we have at least one semicolon
.
70 // Split the directories
in PATH into an
array of strings
.
72 SetArrayLength(Result
,0);
76 SetArrayLength(Result
,i
+1);
78 Result
[i
]:=Copy(Path
,1,p
-1);
81 Path
:=Copy(Path
,p
+1,Length(Path
));
86 // Sets the environment variable
"VarName" to the concatenation
of "DirStrings"
87 // using
";" as the delimiter
. If "AllUsers" is true, a common variable
is set,
88 // else a user
-specific one
. If "DeleteIfEmpty" is true and "DirStrings" is
89 // empty
, "VarName" is deleted instead
of set if it exists
.
90 function SetEnvStrings(VarName
:string;AllUsers
,DeleteIfEmpty
:Boolean;DirStrings
:TArrayOfString
):Boolean;
95 // Merge all non
-empty directory strings into a PATH variable
.
97 for i
:=0 to GetArrayLength(DirStrings
)-1 do begin
98 if Length(DirStrings
[i
])>0 then begin
99 if Length(Path
)>0 then begin
100 Path
:=Path
+';'+DirStrings
[i
];
107 // See http
://www
.jrsoftware
.org
/isfaq
.php#env
108 if AllUsers
then begin
109 KeyName
:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
110 if DeleteIfEmpty
and (Length(Path
)=0) then begin
111 Result
:=(not RegValueExists(HKEY_LOCAL_MACHINE
,KeyName
,VarName
)) or
112 RegDeleteValue(HKEY_LOCAL_MACHINE
,KeyName
,VarName
);
114 Result
:=RegWriteStringValue(HKEY_LOCAL_MACHINE
,KeyName
,VarName
,Path
);
117 KeyName
:='Environment';
118 if DeleteIfEmpty
and (Length(Path
)=0) then begin
119 Result
:=(not RegValueExists(HKEY_CURRENT_USER
,KeyName
,VarName
)) or
120 RegDeleteValue(HKEY_CURRENT_USER
,KeyName
,VarName
);
122 Result
:=RegWriteStringValue(HKEY_CURRENT_USER
,KeyName
,VarName
,Path
);