-
Notifications
You must be signed in to change notification settings - Fork 63
/
DN.DelphiInstallation.pas
283 lines (255 loc) · 6.66 KB
/
DN.DelphiInstallation.pas
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
unit DN.DelphiInstallation;
interface
uses
DN.DelphiInstallation.Intf,
DN.Types,
Graphics;
type
TDNDelphInstallation = class(TInterfacedObject, IDNDelphiInstallation)
private
FName: string;
FShortName: string;
FRoot: string;
FDirectory: string;
FApplication: string;
FEdition: string;
FBDSVersion: string;
FBDSCommonDir: string;
FSupportedPlatforms: TDNCompilerPlatforms;
FIcon: TIcon;
procedure Load;
function GetIcon: TIcon;
function GetName: string;
function GetShortName: string;
function GetRoot: string;
function GetDirectory: string;
function GetApplication: string;
function GetEdition: string;
function GetBDSVersion: string;
function GetBDSCommonDir: string;
function ReadShortName(const AName: string): string;
function GetSupportedPlatforms: TDNCompilerPlatforms;
public
constructor Create(const ARoot: string);
destructor Destroy; override;
function IsRunning: Boolean;
property Name: string read GetName;
property ShortName: string read GetShortName;
property Edition: string read GetEdition;
property BDSVersion: string read GetBDSVersion;
property Icon: TIcon read GetIcon;
property Root: string read GetRoot;
property Directory: string read GetDirectory;
property Application: string read GetApplication;
property BDSCommonDir: string read GetBDSCommonDir;
property SupportedPlatforms: TDNCompilerPlatforms read GetSupportedPlatforms;
end;
implementation
uses
Classes,
Windows,
Registry,
SysUtils,
IOUtils,
ShellApi,
TLHelp32,
StrUtils;
{ TDNDelphInstallationInfo }
constructor TDNDelphInstallation.Create(const ARoot: string);
begin
inherited Create();
FRoot := ARoot;
Load();
end;
destructor TDNDelphInstallation.Destroy;
begin
if Assigned(FIcon) then
FIcon.Free();
inherited;
end;
function TDNDelphInstallation.GetApplication: string;
begin
Result := FApplication;
end;
function TDNDelphInstallation.GetBDSCommonDir: string;
var
LFile: TStringList;
LLine: string;
const
CPrefix = '@SET BDSCOMMONDIR=';
begin
if FBDSCommonDir = '' then
begin
LFile := TStringList.Create();
try
LFile.LoadFromFile(TPath.Combine(ExtractFilePath(Application), 'rsvars.bat'));
for LLine in LFile do
begin
if StartsText(CPrefix, LLine) then
begin
FBDSCommonDir := Copy(LLine, Length(CPrefix) + 1, Length(LLine));
Break;
end;
end;
if FBDSCommonDir = '' then
raise Exception.Create('Failed to read BDSCommonDir for BDS ' + BDSVersion);
finally
LFile.Free;
end;
end;
Result := FBDSCommonDir;
end;
function TDNDelphInstallation.GetBDSVersion: string;
begin
Result := FBDSVersion;
end;
function TDNDelphInstallation.GetDirectory: string;
begin
Result := FDirectory;
end;
function TDNDelphInstallation.GetEdition: string;
begin
Result := FEdition;
end;
function TDNDelphInstallation.GetIcon: TIcon;
begin
if not Assigned(FIcon) then
begin
FIcon := TIcon.Create();
FIcon.Handle := ExtractIcon(HInstance, PChar(FApplication), 0);
end;
Result := FIcon;
end;
function TDNDelphInstallation.GetName: string;
begin
Result := FName;
end;
function TDNDelphInstallation.GetShortName: string;
begin
Result := FShortName;
end;
function TDNDelphInstallation.GetSupportedPlatforms: TDNCompilerPlatforms;
const
CWin32 = 'DCC32.exe';
CWin64 = 'DCC64.exe';
COsx32 = 'DCCOSX.exe';
var
LBinDir: string;
begin
if FSupportedPlatforms = [] then
begin
LBinDir := ExtractFilePath(Application);
if TFile.Exists(TPath.Combine(LBinDir, CWin32)) then
Include(FSupportedPlatforms, cpWin32);
if TFile.Exists(TPath.Combine(LBinDir, CWin64)) then
Include(FSupportedPlatforms, cpWin64);
if TFile.Exists(TPath.Combine(LBinDir, COsx32)) then
Include(FSupportedPlatforms, cpOSX32);
if FSupportedPlatforms = [] then
raise ENotSupportedException.Create('Failed to detect supported platforms');
end;
Result := FSupportedPlatforms;
end;
function TDNDelphInstallation.ReadShortName(const AName: string): string;
var
i, LStart: Integer;
begin
LStart := 1;
for i := Length(AName) downto 1 do
begin
if AName[i] = ' ' then
Break;
LStart := i;
end;
Result := Copy(AName, LStart, Length(AName));
end;
function TDNDelphInstallation.GetRoot: string;
begin
Result := FRoot;
end;
function QueryFullProcessImageName(
hProcess: THandle;
dwFlags: DWord;
lpExeName: PChar;
out lpdwSize: DWord
): BOOL; stdcall external kernel32 name 'QueryFullProcessImageNameW';
function GetFullProcessImageName(APid: DWord): string;
var
LBuffer: PChar;
LSize: DWord;
LProcess: THandle;
begin
Result := '';
LProcess := OpenProcess(PROCESS_QUERY_INFORMATION, False, APid);
if LProcess <> INVALID_HANDLE_VALUE then
begin
try
LSize := MAX_PATH;
LBuffer := GetMemory(LSize * SizeOf(Char));
try
if QueryFullProcessImageName(LProcess, 0, LBuffer, LSize) then
begin
Result := LBuffer;
end;
finally
FreeMemory(LBuffer);
end;
finally
CloseHandle(LProcess);
end;
end;
end;
function TDNDelphInstallation.IsRunning: Boolean;
var
LSnapshot: THandle;
LProcess: TProcessEntry32;
LExe: string;
begin
LSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
try
LProcess.dwSize := SizeOf(LProcess);
if Process32First(LSnapshot, LProcess) then
begin
LExe := ExtractFileName(Application);
repeat
if SameText(LProcess.szExeFile, LExe)
and SameText(GetFullProcessImageName(LProcess.th32ProcessID), Application) then
Exit(true);
until not Process32Next(LSnapshot, LProcess);
end;
Result := False;
finally
CloseHandle(LSnapshot)
end;
end;
procedure TDNDelphInstallation.Load;
var
LRegistry: TRegistry;
const
CDelphiWin32 = 'Delphi.Win32';
begin
FBDSVersion := ExtractFileName(ExcludeTrailingPathDelimiter(FRoot));
LRegistry := TRegistry.Create();
try
LRegistry.Access := LRegistry.Access or KEY_WOW64_64KEY;
LRegistry.RootKey := HKEY_CURRENT_USER;
if LRegistry.OpenKey(FRoot, False) then
begin
FDirectory := LRegistry.ReadString('RootDir');
FApplication := LRegistry.ReadString('App');
FEdition := LRegistry.ReadString('Edition');
LRegistry.CloseKey();
if LRegistry.OpenKey(TPath.Combine(FRoot, 'Personalities'), False) then
begin
if LRegistry.ValueExists(CDelphiWin32) then
FName := LRegistry.ReadString(CDelphiWin32)
else
FName := LRegistry.ReadString('');
FShortName := ReadShortName(FName);
end;
end;
finally
LRegistry.Free();
end;
end;
end.