forked from bgbennyboy/USB-Disk-Ejector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuDiskEjectUtils.pas
575 lines (496 loc) · 17.9 KB
/
uDiskEjectUtils.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
{
******************************************************
USB Disk Ejector
Copyright (c) 2006 - 2017 Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit uDiskEjectUtils;
interface
uses Classes, sysutils, windows, forms, jclsysinfo, jclfileutils, jclshell,
JCLRegistry, dialogs, ShellAPI, JwaWindows, JCLStrings,
uDiskEjectConst, uDriveEjector, uCardReaderManager, uDiskEjectOptions;
type
TTaskBarPos = (_TOP, _BOTTOM, _LEFT, _RIGHT, _NONE);
procedure CreateCleanupBatFileAndRun;
procedure StartInMobileMode(Parameters: string);
procedure RemoveReadOnlyFileAttribute(FileName: string);
procedure AddCustomCardReaders(CardReaders: TCardReaderManager; Ejector: TDriveEjector);
function GetTaskBarHeight: integer;
function GetTaskBarWidth: integer;
function GetTaskBarPos: TTaskBarPos;
function IsTaskbarAutoHideOn: Boolean;
function MatchNameToMountPoint(Name: string): string; overload;
function MatchNameToMountPoint(Name: string; Ejector: TDriveEjector): string; overload;
function MatchLabelToMountPoint(DiskLabel: string): string; overload;
function MatchLabelToMountPoint(DiskLabel: string; Ejector: TDriveEjector): string; overload;
function ConvertDriveLetterToMountpoint(DriveLetter: string): string;
function ConvertMountPointToDriveLetter(MountPoint: string): string;
function BalloonTipsEnabled: boolean;
function IsWindowsVistaorLater : Boolean;
function FindMountPoint(Directory: string): string;
function IsAppRunningFromThisLocation(MountPoint: string): boolean;
function GetCaseSensitiveMountPointName(MountPoint: string; Ejector: TDriveEjector): string;
function GetIfCardReader_FromName(Name: string; Ejector: TDriveEjector; var CardReaderResult: boolean): boolean;
function GetIfCardReader_FromMountPoint(MountPoint: string; Ejector: TDriveEjector; var CardReaderResult: boolean): boolean;
function IsDirectoryWriteable(const AName: string): Boolean;
implementation
{$WARN SYMBOL_PLATFORM OFF}
procedure RemoveReadOnlyFileAttribute(FileName: string);
var
Attributes: cardinal;
begin
if FileName = '' then exit;
Attributes:=FileGetAttr(FileName);
if Attributes = INVALID_FILE_ATTRIBUTES then exit;
if Attributes and faReadOnly = faReadOnly then
FileSetAttr(FileName, Attributes xor faReadOnly);
end;
procedure CreateCleanupBatFileAndRun;
var
BatFile: TStringList;
CmdLine: string;
begin
CmdLine:=IncludeTrailingPathDelimiter( Getwindowstempfolder) + 'USB_Eject_Cleanup.bat';
BatFile := TStringList.Create;
try
BatFile.Add(':redo');
BatFile.Add(Format('@del "%s"', [Application.ExeName]));
BatFile.Add('if EXIST "'+Application.ExeName+'" GOTO redo');
BatFile.Add(Format('@del "%s"', [IncludeTrailingPathDelimiter( Getwindowstempfolder) + str_Ini_FileName]));
BatFile.Add(Format('@del "%s"', [CmdLine]));
BatFile.SaveToFile(CmdLine);
finally
BatFile.Free;
end;
ShellExec(0, 'open', 'USB_Eject_Cleanup.bat', '', IncludeTrailingPathDelimiter( GetWindowsTempFolder), SW_HIDE);
end;
procedure StartInMobileMode(Parameters: string);
var
CopyResult : boolean;
begin
CopyResult := true;
//Copy the exe and the ini file to the temp folder and start the exe
if FileExists( Options.IniPath + str_Ini_FileName ) then
CopyResult := FileCopy(Options.IniPath + str_Ini_FileName, IncludeTrailingPathDelimiter(GetWindowsTempFolder) + str_Ini_FileName, true);
if CopyResult = true then
CopyResult := FileCopy(Application.ExeName, IncludeTrailingPathDelimiter(GetWindowsTempFolder) + extractfilename(application.ExeName), true);
if CopyResult = true then
begin
//Check if files are read only and if it is - change it so it can be deleted later
RemoveReadOnlyFileAttribute( IncludeTrailingPathDelimiter(GetWindowsTempFolder) + ExtractFileName(application.ExeName) );
RemoveReadOnlyFileAttribute( IncludeTrailingPathDelimiter(GetWindowsTempFolder) + str_Ini_FileName );
ShellExec(0, 'open', IncludeTrailingPathDelimiter(GetWindowsTempFolder) + ExtractFileName(application.ExeName), Parameters, IncludeTrailingPathDelimiter(GetWindowsTempFolder), SW_SHOWNORMAL);
end
else
begin
ShowMessage(str_Temp_Folder_Write_Error);
end;
end;
procedure AddCustomCardReaders(CardReaders: TCardReaderManager; Ejector: TDriveEjector);
var
i, j: integer;
begin
if CardReaders = nil then exit;
if CardReaders.CardReadersCount = 0 then exit;
for I := 0 to CardReaders.CardReadersCount - 1 do
begin
for J := 0 to Ejector.DrivesCount - 1 do
begin
if (Trim(Ejector.RemovableDrives[J].VendorId) = CardReaders.CardReaders[i].VendorID) and
(Trim(Ejector.RemovableDrives[J].ProductID) = CardReaders.CardReaders[i].ProductID) and
(Trim(Ejector.RemovableDrives[J].ProductRevision) = CardReaders.CardReaders[i].ProductRevision) then
begin
Ejector.SetDriveAsCardReader(J, true);
//break; //Removed break - some card readers have 2 identical drives so need both identifying as card reader - if we break here then only 1 gets identified as a card reader. Eg Kingston card reader MobileLite G4
end;
end;
end;
end;
function MatchNameToMountPoint(Name: string): string;
var
i, DrivesCount: integer;
Ejector: TDriveEjector;
begin
result:='';
Ejector:=TDriveEjector.Create;
try
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
if Name[1] = '*' then //wildcard - partial name match
begin
for I := 0 to DrivesCount -1 do
begin
if pos(Uppercase(copy(Name, 2, length(name) - 1 )), Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID)))) <> 0 then //found
begin
result:=Ejector.RemovableDrives[i].DriveMountPoint;
break;
end;
end;
end
else
for I := 0 to DrivesCount -1 do
begin
if Uppercase(Name) = Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID))) then
result:=Ejector.RemovableDrives[i].DriveMountPoint;
end;
finally
Ejector.free;
end;
end;
function MatchNameToMountPoint(Name: string; Ejector: TDriveEjector): string;
var
i, DrivesCount: integer;
begin
result:='';
if Ejector = nil then exit;
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
if Name[1] = '*' then //wildcard - partial name match
begin
for I := 0 to DrivesCount -1 do
begin
if pos(Uppercase(copy(Name, 2, length(name) - 1 )), Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID)))) <> 0 then //found
begin
result:=Ejector.RemovableDrives[i].DriveMountPoint;
break;
end;
end;
end
else
for I := 0 to DrivesCount -1 do
begin
if Uppercase(Name) = Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID))) then
result:=Ejector.RemovableDrives[i].DriveMountPoint;
end;
end;
function GetIfCardReader_FromName(Name: string; Ejector: TDriveEjector; var CardReaderResult: boolean): boolean;
var
i, DrivesCount: integer;
begin
result:=false;
if Ejector = nil then exit;
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
if Name[1] = '*' then //wildcard - partial name match
begin
for I := 0 to DrivesCount -1 do
begin
if pos(Uppercase(copy(Name, 2, length(name) - 1 )), Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID)))) <> 0 then //found
begin
CardReaderResult := Ejector.RemovableDrives[i].IsCardReader;
result := true;
break;
end;
end;
end
else
for I := 0 to DrivesCount -1 do
begin
if Uppercase(Name) = Trim(Uppercase(Ejector.RemovableDrives[i].VendorId) + ' ' + Trim(Uppercase(Ejector.RemovableDrives[i].ProductID))) then
CardReaderResult := Ejector.RemovableDrives[i].IsCardReader;
result := true;
end;
end;
function GetIfCardReader_FromMountPoint(MountPoint: string; Ejector: TDriveEjector; var CardReaderResult: boolean): boolean;
var
i, DrivesCount: integer;
begin
result:=false;
if Ejector = nil then exit;
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
for I := 0 to DrivesCount -1 do
begin
if MountPoint = Ejector.RemovableDrives[i].DriveMountPoint then
begin
CardReaderResult := Ejector.RemovableDrives[i].IsCardReader;
result := true;
end;
end;
end;
function MatchLabelToMountPoint(DiskLabel: string): string;
var
i, DrivesCount: integer;
Ejector: TDriveEjector;
begin
result:='';
Ejector:=TDriveEjector.Create;
try
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
if DiskLabel[1] = '*' then //wildcard - partial name match
begin
for I := 0 to DrivesCount -1 do
begin
if pos(Uppercase(copy(DiskLabel, 2, length(DiskLabel) - 1 )), Trim(Uppercase(Ejector.RemovableDrives[i].VolumeLabel))) <> 0 then //found
begin
result:=Ejector.RemovableDrives[i].DriveMountPoint;
break;
end;
end;
end
else
for I := 0 to DrivesCount -1 do
begin
if Uppercase(DiskLabel) = Trim(Uppercase(Ejector.RemovableDrives[i].VolumeLabel)) then
result:=Ejector.RemovableDrives[i].DriveMountPoint;
end;
finally
Ejector.free;
end;
end;
function MatchLabelToMountPoint(DiskLabel: string; Ejector: TDriveEjector): string;
var
i, DrivesCount: integer;
begin
result:='';
if Ejector = nil then exit;
DrivesCount:=Ejector.DrivesCount;
if DrivesCount = 0 then exit;
if DiskLabel[1] = '*' then //wildcard - partial name match
begin
for I := 0 to DrivesCount -1 do
begin
if pos(Uppercase(copy(DiskLabel, 2, length(DiskLabel) - 1 )), Trim(Uppercase(Ejector.RemovableDrives[i].VolumeLabel))) <> 0 then //found
begin
result:=Ejector.RemovableDrives[i].DriveMountPoint;
break;
end;
end;
end
else
for I := 0 to DrivesCount -1 do
begin
if Uppercase(DiskLabel) = Trim(Uppercase(Ejector.RemovableDrives[i].VolumeLabel)) then
result:=Ejector.RemovableDrives[i].DriveMountPoint;
end;
end;
function GetTaskBarHeight: integer;
var
hTB: HWND; //taskbar handle
TBRect: TRect; //taskbar rectangle
begin
hTB:= FindWindow('Shell_TrayWnd', '');
if hTB = 0 then
Result := 0
else
begin
GetWindowRect(hTB, TBRect);
Result := TBRect.Bottom - TBRect.Top;
end;
end;
function GetTaskBarWidth: integer;
var
hTB: HWND; //taskbar handle
TBRect: TRect; //taskbar rectangle
begin
hTB:= FindWindow('Shell_TrayWnd', '');
if hTB = 0 then
Result := 0
else
begin
GetWindowRect(hTB, TBRect);
Result := TBRect.right - TBRect.left;
end;
end;
function IsTaskbarAutoHideOn: Boolean;
var
ABData: TAppBarData;
begin
ABData.cbSize := SizeOf(ABData);
Result := (SHAppBarMessage(ABM_GETSTATE, ABData) and ABS_AUTOHIDE) > 0;
end;
function GetTaskBarPos: TTaskBarPos;
var
hTaskbar: HWND;
T: TRect;
scrW, scrH: integer;
begin
hTaskBar := FindWindow('Shell_TrayWnd', nil);
if hTaskbar <> 0 then
begin
GetWindowRect(hTaskBar, T);
ScrW := Screen.Width;
ScrH := Screen.Height;
if (T.Top > scrH div 2) and (T.Right >= scrW) then
Result := _BOTTOM
else
if (T.Top < scrH div 2) and (T.Bottom <= scrW div 2) then
Result := _TOP
else
if (T.Left < scrW div 2) and (T.Top <= 0) then
Result := _LEFT
else // the last "if" is not really needed
if T.Left >= ScrW div 2 then
Result := _RIGHT
else
Result := _NONE;
end
else
Result := _NONE;
end;
function ConvertDriveLetterToMountpoint(
DriveLetter: string): string;
var
UpperDrive: string;
begin
result := '';
if DriveLetter = '' then exit;
UpperDrive := Uppercase( DriveLetter[1] );
if not ( CharInSet(UpperDrive[1], ['A'..'Z']) ) then exit;
Result := UpperDrive + ':\';
end;
function ConvertMountPointToDriveLetter(
MountPoint: string): string;
begin
result := '';
if MountPoint = '' then exit;
Result := Uppercase( MountPoint[1] );
end;
function BalloonTipsEnabled: boolean;
begin
try
if RegReadDWord( HKEY_CURRENT_USER, 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced', 'EnableBalloonTips' ) = 0 then
result := false
else
result := true;
except on EJCLRegistryError do
result := true;
end;
end;
function IsWindowsVistaOrLater : Boolean;
const
Condition = VER_GREATER_EQUAL;
var
OSInfo : TOSVersionInfoEx;
ConditionMask : Int64;
begin
ZeroMemory(@OsInfo, sizeof(OSInfo));
OSInfo.dwOSVersionInfoSize := SizeOf(OSInfo);
OSInfo.dwMajorVersion := 6;
OSInfo.dwMinorVersion := 0;
OSInfo.wServicePackMajor := 0;
OSInfo.wServicePackMinor := 0;
ConditionMask := 0;
ConditionMask := VerSetConditionMask(ConditionMask, VER_MAJORVERSION, Condition);
ConditionMask := VerSetConditionMask(ConditionMask, VER_MINORVERSION, Condition);
ConditionMask := VerSetConditionMask(ConditionMask, VER_SERVICEPACKMAJOR, Condition);
ConditionMask := VerSetConditionMask(ConditionMask, VER_SERVICEPACKMINOR, Condition);
result := VerifyVersionInfo(OSInfo, VER_MAJORVERSION or VER_MINORVERSION or
VER_SERVICEPACKMAJOR or VER_SERVICEPACKMINOR,
ConditionMask);
end;
function FindMountPoint(Directory: string): string;
var
CurrPath: string;
Attributes: cardinal;
begin
result := '';
{See IsAppRunningFromThisLocation for full explanation.
Need to walk up the folders testing if each is a mountpoint as we go}
CurrPath := IncludeTrailingPathDelimiter( Directory );
while CurrPath <> '' do
begin
Attributes := GetFileAttributes( PChar(CurrPath) );
if ( (Attributes and FILE_ATTRIBUTE_REPARSE_POINT) <> 0 ) //its a mountpoint
or ( length( CurrPath ) = 2 ) then //or its a drive letter
begin
result := IncludeTrailingPathDelimiter( CurrPath );
break;
end;
if length(CurrPath) <= 2 then //at the root - escape
break;
//Otherwise get the parent folder
CurrPath := ExtractFilePath( ExcludeTrailingPathDelimiter( CurrPath ) );
end;
{Fix - always capitalise the drive letter. If started from command line with lower case drive then ExtractFilePath gives the drive as lower case
see http://quick.mixnmojo.com/usb-disk-eject-1-2-beta-4-last-beta-before-release-hopefully#comments for more info}
if result <> '' then
begin
result[1] := Uppercase(Result)[1];
//Add quotes in case path has spaces - these get stripped out later anyway
result := StrQuote(result, '"');
end;
end;
function IsAppRunningFromThisLocation(MountPoint: string): boolean;
var
CurrPath: string;
Attributes: cardinal;
CurrentVolumeName, SearchVolumeName: array[0..MAX_PATH] of Char;
begin
result := false;
{Cant just do ExtractFileDrive because the volume might be mounted in a folder
on C. Also have to be careful because a drive might have more than one mountpoint.
Could have a drive letter and be mounted in a folder. So we have to look at where
the app is running from and walk up the folders, testing as we go if a folder
is a mountpoint. If it is, then we find its volume identifier and see if thats
the same volume identifier as the mountpoint we're trying to eject.}
//Get the GUID volume name for the volume we're searching for
GetVolumeNameForVolumeMountPoint( PChar(MountPoint), SearchVolumeName, MAX_PATH);
//Need to walk up the folders testing if each is a mountpoint as we go
CurrPath := ExtractFilePath(Application.ExeName);
while CurrPath <> '' do
begin
Attributes := GetFileAttributes( PChar(CurrPath) );
if ( (Attributes and FILE_ATTRIBUTE_REPARSE_POINT) <> 0 ) //its a mountpoint
or ( length( CurrPath ) = 2 ) then //or its a drive letter
begin
//Get the GUID volume name for the mountpoint
GetVolumeNameForVolumeMountPoint( PChar(IncludeTrailingPathDelimiter(CurrPath)), CurrentVolumeName, MAX_PATH);
//ShowMessage( CurrentVolumeName) ;
//Showmessage( SearchVolumeName);
if string(CurrentVolumeName) = string(SearchVolumeName) then //the app is running off the same volume
begin
result := true;
break;
end;
end;
if length(CurrPath) <= 2 then //at the root
break;
//Otherwise get the parent folder
CurrPath := ExtractFilePath( ExcludeTrailingPathDelimiter( CurrPath ) );
end;
end;
{Looks up mountpoint in Ejector.Driveslist and returns the proper case sensitive mountpoint
If it doesnt exist - just return the original string - simpler to error out later rather than now}
function GetCaseSensitiveMountPointName(MountPoint: string; Ejector: TDriveEjector): string;
var
i: integer;
begin
result := MountPoint; //If it doesnt exist - just return the original string - simpler to error out later rather than now
if Ejector = nil then exit;
if Ejector.DrivesCount = 0 then exit;
for I := 0 to Ejector.DrivesCount - 1 do
begin
if AnsiCompareText(MountPoint, Ejector.RemovableDrives[i].DriveMountPoint) = 0 then
begin
result := Ejector.RemovableDrives[i].DriveMountPoint;
break;
end;
end;
end;
function IsDirectoryWriteable(const AName: string): Boolean;
var
TempFileName: array[0..MAX_PATH] of Char;
begin
{ attempt to create a temp file in the directory }
Result := GetTempFileName(PChar(AName), '$', 0, TempFileName) <> 0;
if Result then
{ clean up }
Result := DeleteFile(TempFileName);
end;
end.