Installer: Start included files with [Code] to get syntax highlighting
[msysgit.git] / share / WinGit / environment.inc.iss
blobdadf875e427e4a5179c5231ef0dcb9cc861415bb
1 [Code]
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;
8 var
9 Path:string;
10 i:Longint;
11 p:Integer;
12 begin
13 Path:='';
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);
19 end else begin
20 // We ignore errors here. The resulting array of strings will be empty.
21 RegQueryStringValue(HKEY_CURRENT_USER,'Environment',VarName,Path);
22 end;
24 // Fallback for built-in variables which are not stored in the Registry.
25 if Length(Path)=0 then begin
26 Path:=ExpandConstant('{%'+VarName+'}');
27 end;
29 // Make sure we have at least one semicolon.
30 Path:=Path+';';
32 // Split the directories in PATH into an array of strings.
33 i:=0;
34 SetArrayLength(Result,0);
36 p:=Pos(';',Path);
37 while p>0 do begin
38 SetArrayLength(Result,i+1);
39 if p>1 then begin
40 Result[i]:=Copy(Path,1,p-1);
41 i:=i+1;
42 end;
43 Path:=Copy(Path,p+1,Length(Path));
44 p:=Pos(';',Path);
45 end;
46 end;
48 // Sets the contents of the specified environment variable for the current process.
49 function SetEnvironmentVariable(lpName,lpValue:String):Boolean;
50 #ifdef UNICODE
51 external 'SetEnvironmentVariableW@Kernel32.dll stdcall delayload';
52 #else
53 external 'SetEnvironmentVariableA@Kernel32.dll stdcall delayload';
54 #endif
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;
61 var
62 Path,KeyName:string;
63 i:Longint;
64 begin
65 // Merge all non-empty directory strings into a PATH variable.
66 Path:='';
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];
71 end else begin
72 Path:=DirStrings[i];
73 end;
74 end;
75 end;
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);
83 end else begin
84 Result:=RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
85 end;
86 end else begin
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);
91 end else begin
92 Result:=RegWriteStringValue(HKEY_CURRENT_USER,KeyName,VarName,Path);
93 end;
94 end;
96 // Also update the environment of the current process.
97 SetEnvironmentVariable(VarName,Path);
98 end;