-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScanThread.pas
302 lines (268 loc) · 7.64 KB
/
ScanThread.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
unit ScanThread;
interface
uses
Windows, SysUtils, Classes, ComCtrls, Forms, StdCtrls,
PopupWindow, Graphics, ShellAPI, Controls, Registry;
type
TRGB = record
b, g, r:byte;
end;
ARGB = array [0..1] of TRGB;
PARGB = ^ARGB;
TChars = set of char;
ScanFlash = class(TThread)
private
ResultWindow:TPopupWnd;
detected, removed: integer;
LastName, LastSize, LastDecr: string;
VolumeChar: char;
procedure AddToList;
procedure PrepareVisualComponents;
procedure UpdateVisualComponents;
function IsExecutableFile(const FileName:string):boolean;
function FormatDiskSize (const Value: TLargeInteger): string; overload;
function FormatDiskSize (const FileName: string): string; overload;
procedure ParseString(const Delimeter: TChars; StrToParse: string; List: TStrings);
procedure RemoveAutorun;
procedure FixRegistryMountPoints;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean; pVolumeChar: char);
end;
implementation
uses InformWnd;
{procedure ScanFlash.ScanDirectory(Path: string);
var
SearchRec: TSearchRec;
begin
if FindFirst(Path + '\*', faAnyFile, SearchRec) = 0 then
begin
repeat
if (SearchRec.Name = '..') or (SearchRec.Name = '.') then
continue;
inc(detected);
if (SearchRec.Attr and faDirectory) = faDirectory then
ScanDirectory(Path + '\' + SearchRec.Name)
else
;
Synchronize(UpdateVisualComponents);
until Self.Terminated or (FindNext(SearchRec) <> 0);
FindClose(SearchRec);
end;
end;}
procedure ScanFlash.Execute;
begin
Synchronize(PrepareVisualComponents);
RemoveAutorun;
FixRegistryMountPoints;
ResultWindow.ParentThread := nil;
if (not ResultWindow.Active) and ResultWindow.AdditionalInfoLabel.Enabled then
ResultWindow.CloseTimer.Enabled := true;
end;
procedure ScanFlash.UpdateVisualComponents;
begin
with ResultWindow do
begin
DetectedLabel.Caption := IntToStr(detected);
RemovedLabel.Caption := IntToStr(removed);
end;
end;
constructor ScanFlash.Create(CreateSuspended: boolean; pVolumeChar: char);
begin
VolumeChar := pVolumeChar;
inherited Create(CreateSuspended);
end;
procedure ScanFlash.PrepareVisualComponents;
var
VolumeName, FileSystemName: array [0..MAX_PATH-1] of Char;
VolumeSerialNo, MaxComponentLength, FileSystemFlags: LongWord;
TotalBytes, FreeBytes, TotalFree: TLargeInteger ;
begin
ResultWindow := TPopupWnd.Create(nil);
with ResultWindow, AdditionInfoForm do
begin
GetVolumeInformation(PChar(VolumeChar + ':\'),
VolumeName,
MAX_PATH,
@VolumeSerialNo,
MaxComponentLength,
FileSystemFlags,
FileSystemName,
MAX_PATH);
if VolumeName = '' then
VolumeName := 'Áåçûìÿííûé';
LabelName.Caption := VolumeName + ' (' + VolumeChar + ':)';
DetectedLabel.Caption := IntToStr(detected);
RemovedLabel.Caption := IntToStr(removed);
GetDiskFreeSpaceEx(PChar(VolumeChar + ':\'), FreeBytes, TotalBytes, @TotalFree);
DriveNameLabel.Caption := VolumeName + ' (' + VolumeChar + ':)';
DriveSizeLabel.Caption := FormatDiskSize(TotalBytes);
FreeSizeLabel.Caption := FormatDiskSize(FreeBytes);
UsedSizeLabel.Caption := FormatDiskSize(TotalBytes - FreeBytes);
FileSystemLabel.Caption := FileSystemName;
SerialNumberLabel.Caption := IntToHex(VolumeSerialNo,8);
end;
ResultWindow.ParentThread := Self;
ResultWindow.Show;
Application.BringToFront;
end;
function ScanFlash.IsExecutableFile(const FileName: string): boolean;
var
ext:string;
begin
result:=false;
ext:=ExtractFileExt(FileName);
ext:=LowerCase(ext);
if (ext = '.exe') or (ext = '.com') or (ext = '.bat') or (ext = '.scr') or
(ext = '.msc') or (ext = '.key') or (ext = '.dll') or (ext = '.vbs') or
(ext = '.cmd') or (ext = '.vbe') or (ext = '.js' ) or (ext = '.jse') or
(ext = '.wsf') or (ext = '.wsh') then
result:=true;
end;
function ScanFlash.FormatDiskSize(const Value: TLargeInteger): string;
const
SizeUnits: array[1..5] of string = (' Bytes', ' KBytes', ' MBytes', ' GBytes', 'TBytes');
var
SizeUnit: Integer;
Temp: TLargeInteger;
Size: Integer;
begin
SizeUnit := 1;
if Value < 1024 then
Result := IntToStr(Value)
else begin
Temp := Value;
while (Temp >= 1000*1024) and (SizeUnit <= 5) do begin
Temp := Temp shr 10;
Inc(SizeUnit);
end;
Inc(SizeUnit);
Size := (Temp shr 10);
Temp := Temp - (Size shl 10);
if Temp > 1000 then
Temp := 999;
if Size > 100 then
Result := IntToStr(Size)
else if Size > 10 then
Result := Format('%d%s%.1d', [Size, DecimalSeparator, Temp div 100])
else
Result := Format('%d%s%.2d', [Size, DecimalSeparator,
Temp div 10])
end;
Result := Result + SizeUnits[SizeUnit];
end;
procedure ScanFlash.AddToList;
begin
with ResultWindow.AdditionInfoForm.ProcessingFileList.Items.Add do
begin
Caption := LastName;
SubItems.Add(LastSize);
SubItems.Add(LastDecr);
end;
end;
procedure ScanFlash.RemoveAutorun;
var
AutoRun: TStrings;
TempString: string;
i: integer;
begin
if FileExists(VolumeChar + ':\autorun.inf') then
begin
AutoRun := TStringList.Create;
AutoRun.LoadFromFile(VolumeChar + ':\autorun.inf');
for i := AutoRun.Count - 1 downto 0 do
begin
TempString := Trim(AutoRun[i]);
if ((TempString = '') or (TempString[1] = ';')) or (pos('=', TempString) = 0) then
AutoRun.Delete(i)
else
begin
Delete(TempString, 1, pos('=', AutoRun[i]));
AutoRun[i] := Trim(TempString);
end;
end;
AutoRun.Delimiter := ' ';
AutoRun.QuoteChar := ' ';
TempString := AutoRun.DelimitedText;
AutoRun.Clear;
AutoRun.Add('autorun.inf');
ParseString([' ', ':', ','], TempString, AutoRun);
{for i := AutoRun.Count - 1 downto 1 do
if not IsExecutableFile(AutoRun[i]) then
AutoRun.Delete(i);}
for i := 0 to AutoRun.Count - 1 do
begin
LastName := VolumeChar + ':\' + AutoRun[i];
if not FileExists(LastName) then
Continue;
inc(detected);
LastSize := FormatDiskSize(LastName);
if SetFileAttributes(PChar(LastName), FILE_ATTRIBUTE_NORMAL) and DeleteFile(PChar(LastName)) then
begin
LastDecr := 'Ôàéë óñïåøíî óäàëåí';
inc(removed);
end
else
LastDecr := 'Íå óäàëîñü óäàëèòü ôàéë';
UpdateVisualComponents;
Synchronize(AddToList);
end;
end;
end;
procedure ScanFlash.ParseString(const Delimeter: TChars;
StrToParse: string; List: TStrings);
var
PrevCharIsDelimeter: boolean;
i: integer;
CurrentItem: integer;
begin
CurrentItem := List.Add('');
while (StrToParse <> '') and (StrToParse[1] in Delimeter) do
Delete(StrToParse, 1, 1);
PrevCharIsDelimeter := false;
for i := 1 to Length(StrToParse) do
begin
if not (StrToParse[i] in Delimeter) then
begin
List[CurrentItem] := List[CurrentItem] + StrToParse[i];
PrevCharIsDelimeter := false;
end
else
begin
if not PrevCharIsDelimeter then
begin
CurrentItem := List.Add('');
PrevCharIsDelimeter := true;
end
else
continue;
end;
end;
if List[CurrentItem] = '' then
List.Delete(CurrentItem);
end;
function ScanFlash.FormatDiskSize(const FileName: string): string;
var
SearchRec: TSearchRec;
Size: TLargeInteger;
begin
if FindFirst(FileName, faAnyFile, SearchRec) = 0 then
begin
Size := SearchRec.Size;
FindClose(SearchRec);
end
else
Size := 0;
Result := FormatDiskSize(Size);
end;
procedure ScanFlash.FixRegistryMountPoints;
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
Reg.DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2');
Reg.Free;
end;
end.