Update tk to version 8.5.11
[msysgit.git] / share / WinGit / helpers.inc.iss
blob4118048108bb30cc3dee6e8046443ff28a828ad5
1 // Copies a NULL-terminated array of characters to a string.
2 function ArrayToString(Chars:array of Char):String;
3 var
4 Len,i:Longint;
5 begin
6 Len:=GetArrayLength(Chars);
7 SetLength(Result,Len);
9 i:=0;
10 while (i<Len) and (Chars[i]<>#0) do begin
11 Result[i+1]:=Chars[i];
12 Inc(i);
13 end;
15 SetLength(Result,i);
16 end;
18 // Copies a string to a NULL-terminated array of characters.
19 function StringToArray(Str:String):array of Char;
20 var
21 Len,i:Longint;
22 begin
23 Len:=Length(Str);
24 SetArrayLength(Result,Len+1);
26 i:=0;
27 while i<Len do begin
28 Result[i]:=Str[i+1];
29 Inc(i);
30 end;
32 Result[i]:=#0;
33 end;
35 // Returns the path to the common or user shell folder as specified in "Param".
36 function GetShellFolder(Param:string):string;
37 begin
38 if IsAdminLoggedOn then begin
39 Param:='{common'+Param+'}';
40 end else begin
41 Param:='{user'+Param+'}';
42 end;
43 Result:=ExpandConstant(Param);
44 end;
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;
51 var
52 Path:string;
53 i:Longint;
54 p:Integer;
55 begin
56 Path:='';
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);
62 end else begin
63 // We ignore errors here. The resulting array of strings will be empty.
64 RegQueryStringValue(HKEY_CURRENT_USER,'Environment',VarName,Path);
65 end;
67 // Fallback for built-in variables which are not stored in the Registry.
68 if Length(Path)=0 then begin
69 Path:=ExpandConstant('{%'+VarName+'}');
70 end;
72 // Make sure we have at least one semicolon.
73 Path:=Path+';';
75 // Split the directories in PATH into an array of strings.
76 i:=0;
77 SetArrayLength(Result,0);
79 p:=Pos(';',Path);
80 while p>0 do begin
81 SetArrayLength(Result,i+1);
82 if p>1 then begin
83 Result[i]:=Copy(Path,1,p-1);
84 i:=i+1;
85 end;
86 Path:=Copy(Path,p+1,Length(Path));
87 p:=Pos(';',Path);
88 end;
89 end;
91 // Sets the contents of the specified environment variable for the current process.
92 function SetEnvironmentVariable(lpName,lpValue:String):Boolean;
93 #ifdef UNICODE
94 external 'SetEnvironmentVariableW@Kernel32.dll stdcall delayload';
95 #else
96 external 'SetEnvironmentVariableA@Kernel32.dll stdcall delayload';
97 #endif
99 // Sets the environment variable "VarName" to the concatenation of "DirStrings"
100 // using ";" as the delimiter. If "AllUsers" is true, a common variable is set,
101 // else a user-specific one. If "DeleteIfEmpty" is true and "DirStrings" is
102 // empty, "VarName" is deleted instead of set if it exists.
103 function SetEnvStrings(VarName:string;AllUsers,DeleteIfEmpty:Boolean;DirStrings:TArrayOfString):Boolean;
105 Path,KeyName:string;
106 i:Longint;
107 begin
108 // Merge all non-empty directory strings into a PATH variable.
109 Path:='';
110 for i:=0 to GetArrayLength(DirStrings)-1 do begin
111 if Length(DirStrings[i])>0 then begin
112 if Length(Path)>0 then begin
113 Path:=Path+';'+DirStrings[i];
114 end else begin
115 Path:=DirStrings[i];
116 end;
117 end;
118 end;
120 // See http://www.jrsoftware.org/isfaq.php#env
121 if AllUsers then begin
122 KeyName:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
123 if DeleteIfEmpty and (Length(Path)=0) then begin
124 Result:=(not RegValueExists(HKEY_LOCAL_MACHINE,KeyName,VarName)) or
125 RegDeleteValue(HKEY_LOCAL_MACHINE,KeyName,VarName);
126 end else begin
127 Result:=RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
128 end;
129 end else begin
130 KeyName:='Environment';
131 if DeleteIfEmpty and (Length(Path)=0) then begin
132 Result:=(not RegValueExists(HKEY_CURRENT_USER,KeyName,VarName)) or
133 RegDeleteValue(HKEY_CURRENT_USER,KeyName,VarName);
134 end else begin
135 Result:=RegWriteStringValue(HKEY_CURRENT_USER,KeyName,VarName,Path);
136 end;
137 end;
139 // Also update the environment of the current process.
140 SetEnvironmentVariable(VarName,Path);
141 end;
143 // As IsComponentSelected() is not supported during uninstall, this work-around
144 // simply checks the Registry. This is unreliable if the user runs the installer
145 // twice, the first time selecting the component, the second deselecting it.
146 function IsComponentInstalled(Component:String):Boolean;
148 UninstallKey,UninstallValue:String;
149 Value:String;
150 begin
151 Result:=False;
153 UninstallKey:='SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#APP_NAME}_is1';
154 UninstallValue:='Inno Setup: Selected Components';
156 if RegQueryStringValue(HKEY_LOCAL_MACHINE,UninstallKey,UninstallValue,Value) then begin
157 Result:=(Pos(Component,Value)>0);
158 end;
159 end;