-
Notifications
You must be signed in to change notification settings - Fork 48
/
uDriveEjector.pas
1027 lines (855 loc) · 30.3 KB
/
uDriveEjector.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
******************************************************
USB Disk Ejector
Copyright (c) 2006 - 2015 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.
}
//TODO - rewrite with tlist instead of arrays
unit uDriveEjector;
interface
uses
Classes, Windows, forms, SysUtils, extctrls,
JwaWindows, jclsysinfo,
uProcessAndWindowUtils, uDiskEjectConst;
type
TRemovableDrive = packed record
DriveMountPoint: string;
VolumeLabel: string;
VendorId: string;
ProductID: string;
ProductRevision: string;
IsCardReader: boolean;
HasSiblings: boolean;
CardMediaPresent: boolean;
BusType: integer;
ParentDevInst: integer;
SiblingIndexes: array of integer;
end;
TDriveEjector = class
private
PollTimer: TTimer;
FOnCardMediaChanged: TNotifyEvent;
FPollTimerInterval: cardinal;
FPolling: boolean;
FBusy: boolean;
FOnDrivesChanged:TNotifyEvent;
function GetDrivesCount: integer;
function GetBusy: boolean;
function GetDrivesDevInstByDeviceNumber(DeviceNumber: Integer; DriveType: UINT; szDosDeviceName: PCHAR): DEVINST;
function EjectDevice(MountPoint: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false): boolean;
function EjectCard(MountPoint: string; var EjectErrorCode: integer): boolean;
function GetParentDriveDevInst(MountPoint: string; var ParentInstNum: integer): boolean;
function GetNoDevicesWithSameParentInst(ParentDevInst: integer): integer;
function GetNoDevicesWithSameProductId(ProductId: string): integer;
function CheckIfDriveHasMedia(MountPoint: string): boolean;
function GetCardPolling: boolean;
procedure SetCardPolling(Value: boolean);
function GetCardPollingInterval: cardinal;
procedure SetCardPollingInterval(value: cardinal);
procedure FindRemovableDrives;
procedure ScanDrive(GUIDVolumeName: String);
procedure CheckForCardReaders;
procedure CheckForSiblings;
procedure OnTimer (Sender:TObject);
procedure SetBusy(const Value: boolean);
procedure DeleteFromDrivesArray(const Index: Cardinal);
public
RemovableDrives: array of TRemovableDrive;
constructor Create;
destructor Destroy; override;
function RemoveDrive(MountPoint: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false; CardEject: boolean = false; CloseRunningApps: boolean = false; ForceRunningAppsClosure: boolean = false): boolean; overload;
procedure RescanAllDrives;
procedure ClearDriveList;
procedure SetDriveAsCardReader(Index: Integer; CardReader: boolean);
property DrivesCount: integer read GetDrivesCount;
property OnCardMediaChanged: TNotifyEvent read FOnCardMediaChanged write FOnCardMediaChanged;
property CardPollingInterval: cardinal read GetCardPollingInterval write SetCardPollingInterval;
property CardPolling: boolean read GetCardPolling write SetCardPolling;
property Busy: boolean read GetBusy write SetBusy;
property OnDrivesChanged: TNotifyEvent read FOnDrivesChanged write FOnDrivesChanged;
end;
TEventsThread = class(TThread)
private
fEjector: TDriveEjector;
protected
procedure Execute; override;
public
constructor Create(Ejector: TDriveEjector);
end;
implementation
var
fPrevWndProc: TFNWndProc = nil;
fChangeMessageCount: integer = 0;
CriticalSection: TCriticalSection;
EventsThread: TEventsThread;
{------------------------Hook events in dummy window--------------------------}
function UsbWndProc(hWnd: HWND; Msg: UINT; wParam, lParam: Longint): Longint; stdcall;
begin
Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);
if (Msg = WM_DEVICECHANGE) and
(((wParam = DBT_DEVICEARRIVAL) and
(PDevBroadcastHeader(lParam).dbcd_devicetype = DBT_DEVTYP_VOLUME)) or
(wParam = DBT_DEVICEREMOVECOMPLETE)) then
begin
EnterCriticalSection(CriticalSection);
inc(fChangeMessageCount);
LeaveCriticalSection(CriticalSection);
if EventsThread.Suspended then EventsThread.Resume;
end;
end;
{-----------------------------------------------------------------------------}
//Event thread
constructor TEventsThread.Create(Ejector: TDriveEjector);
begin
fEjector:=Ejector;
inherited create(false);
end;
procedure TEventsThread.Execute;
begin
while not terminated do
begin
if self.Terminated then break;
if (fChangeMessageCount > 0) and (fEjector.Busy = false) then
begin
sleep(500); //gives extra time for devices with multi volumes/partitions - sometimes theres only 1 message but it takes a moment for windows to mount both partitions
EnterCriticalSection(CriticalSection);
fChangeMessageCount:=0; //set it back to 0 because we're about to scan
LeaveCriticalSection(CriticalSection);
Synchronize( fEjector.RescanAllDrives); //Synchronise because changes will cause FOnDrivesChanged to be called which will cause main thread to update the form - but to be thread safe gui updating shouldnt be done from another thread - so synchronise makes it call it from main thead.
//messagebeep(0);
end
else
self.Suspend;
end;
end;
{-----------------------------------------------------------------------------}
constructor TDriveEjector.Create;
begin
LoadSetupApi;
LoadConfigManagerApi;
PollTimer:=TTimer.Create(nil);
fPolling:=false;
PollTimer.OnTimer:=OnTimer;
fPollTimerInterval:=5000;
PollTimer.Interval:=fPollTimerInterval;
//Setup dummy window to catch messages
if not Assigned(fPrevWndProc) then
begin
fPrevWndProc := TFNWndProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(@UsbWndProc));
end;
InitializeCriticalSection(CriticalSection);
fBusy := false;
//Create a thread to keep polling fChangeMessageCount
EventsThread:=TEventsThread.Create(self);
FindRemovableDrives;
// FindOpenHandlesTest( RemovableDrives[0].DriveMountPoint);
end;
destructor TDriveEjector.Destroy;
begin
EventsThread.Terminate;
if EventsThread.Suspended then EventsThread.Resume;
EventsThread.Free;
DeleteCriticalSection(CriticalSection);
PollTimer.free;
SetLength(RemovableDrives, 0);
UnloadConfigManagerApi;
UnloadSetupApi;
inherited;
end;
procedure TDriveEjector.DeleteFromDrivesArray(const Index: Cardinal);
var
ALength: Cardinal;
TailElements: Cardinal;
begin
ALength := Length(RemovableDrives);
Assert(ALength > 0);
Assert(Index < ALength);
Finalize(RemovableDrives[Index]);
TailElements := ALength - Index;
if TailElements > 0 then
Move(RemovableDrives[Index + 1], RemovableDrives[Index], SizeOf(TRemovableDrive) * TailElements);
Initialize(RemovableDrives[ALength - 1]);
SetLength(RemovableDrives, ALength - 1);
end;
procedure TDriveEjector.FindRemovableDrives;
var
FindRec: cardinal;
VolumeUniqueName: array[0..MAX_PATH] of Char;
i: integer;
begin
SetBusy(true);
SetLength(RemovableDrives, 0);
FindRec := FindFirstVolume(VolumeUniqueName, MAX_PATH);
while FindRec <> INVALID_HANDLE_VALUE do
begin
ScanDrive(VolumeUniqueName);
if not (FindNextVolume(FindRec, VolumeUniqueName, MAX_PATH)) then
break;
end;
FindVolumeClose(FindRec);
SetBusy(false);
//Finally check if any are card readers
CheckForCardReaders;
//Check if it has siblings (multiple partitions but 1 drive)
CheckForSiblings;
{--------------------------------------------------------------------------------------}
//HACK - delete card readers
{for i := DrivesCount - 1 downto 0 do
begin
if RemovableDrives[i].IsCardReader then
DeleteFromDrivesArray(i);
end;}
{--------------------------------------------------------------------------------------}
//NEW remove drives that have no mountpoint at all - they dont show up in the normal windows eject dialog and we cant eject them here - so dont show them.
for i := DrivesCount - 1 downto 0 do
begin
if RemovableDrives[i].DriveMountPoint = '' then
if RemovableDrives[i].IsCardReader = false then
DeleteFromDrivesArray(i);
end;
if assigned(FOnDrivesChanged) then
FOnDrivesChanged(nil);
end;
procedure TDriveEjector.ScanDrive(GUIDVolumeName: String);
type
PCharArray = ^TCharArray;
TCharArray = array[0..32767] of AnsiChar;
STORAGE_PROPERTY_QUERY = packed record
PropertyId: DWORD;
QueryType: DWORD;
AdditionalParameters: array[0..3] of Byte;
end;
STORAGE_DEVICE_DESCRIPTOR = packed record
Version: ULONG;
Size: ULONG;
DeviceType: Byte;
DeviceTypeModifier: Byte;
RemovableMedia: Boolean;
CommandQueueing: Boolean;
VendorIdOffset: ULONG;
ProductIdOffset: ULONG;
ProductRevisionOffset: ULONG;
SerialNumberOffset: ULONG;
STORAGE_BUS_TYPE: DWORD;
RawPropertiesLength: ULONG;
RawDeviceProperties: array[0..511] of Byte;
end;
const
IOCTL_STORAGE_QUERY_PROPERTY = $2D1400;
var
Returned, FFileHandle, MaxCompLen, FSFlags, ReturnLength: Cardinal;
DriveBuf, VolumeName: array[0..MAX_PATH] of Char;
Status: LongBool;
PropQuery: STORAGE_PROPERTY_QUERY;
DeviceDescriptor: STORAGE_DEVICE_DESCRIPTOR;
PCh: PAnsiChar;
Inst: integer;
DriveMountPoint: string;
begin
FFileHandle:=INVALID_HANDLE_VALUE;
try
FFileHandle := CreateFile(
PChar( ExcludeTrailingPathDelimiter(GUIDVolumeName) ),
0,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
0,
0
);
if FFileHandle = INVALID_HANDLE_VALUE then exit;
ZeroMemory(@PropQuery, SizeOf(PropQuery));
ZeroMemory(@DeviceDescriptor, SizeOf(DeviceDescriptor));
DeviceDescriptor.Size := SizeOf(DeviceDescriptor);
Status := DeviceIoControl(
FFileHandle,
IOCTL_STORAGE_QUERY_PROPERTY,
@PropQuery,
SizeOf(PropQuery),
@DeviceDescriptor,
DeviceDescriptor.Size,
@Returned,
nil
);
if not Status then exit;
if DeviceDescriptor.STORAGE_BUS_TYPE <= 0 then exit;
if (DeviceDescriptor.STORAGE_BUS_TYPE = 7) or
(DeviceDescriptor.STORAGE_BUS_TYPE = 4) then //7 is USB, 4 is firewire
SetLength(RemovableDrives, length(RemovableDrives) + 1) //enlarge the array for another item
else
exit;
//Error handling needed here - use SysErrorMessage to return string of error
Status:=GetVolumeInformation(PChar(GUIDVolumeName), VolumeName, MAX_PATH,
nil, MaxCompLen, FSFlags, nil, 0);
if not Status then VolumeName := ''; //exit; //getlasterror;
//outputdebugstring(pansichar(inttostr(getlasterror)));
Status:=GetVolumePathNamesForVolumeNameW(PChar(GUIDVolumeName), DriveBuf,
MAX_PATH, ReturnLength);
if not Status then exit; //getlasterror;
//outputdebugstring(pansichar(inttostr(getlasterror)));
//Drivebuf is length of drive string + 2 trailing #0's - can be more than one separated by null
//The list is an array of null-terminated strings terminated by an additional NULL character
//Eg g:\00
//Eg c:\my_usb_stick_mountpoint00
{if temp = 5 then //Drive letter
DriveMountPoint:=DriveBuf[0]
else} //Mount point
DriveMountPoint:=trim( copy(DriveBuf, 0, ReturnLength) );
//Drive Letter
RemovableDrives[high(RemovableDrives)].DriveMountPoint := DriveMountPoint;
//Volume Name
RemovableDrives[high(RemovableDrives)].VolumeLabel :=VolumeName;
//Vendor Id
if DeviceDescriptor.VendorIdOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.VendorIdOffset];
RemovableDrives[high(RemovableDrives)].VendorId := Trim(String(Pch));
end;
//Product Id
if DeviceDescriptor.ProductIdOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductIdOffset];
RemovableDrives[high(RemovableDrives)].ProductID := Trim(String(PCh));
end;
//Product Revision
if DeviceDescriptor.ProductRevisionOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductRevisionOffset];
RemovableDrives[high(RemovableDrives)].ProductRevision := Trim(String(PCh));
end;
//Is Card Reader //This is checked and changed later
RemovableDrives[high(RemovableDrives)].IsCardReader := false;
//Has siblings //This is checked and changed later
RemovableDrives[high(RemovableDrives)].HasSiblings := false;
//Does Card Reader have media in it?
if CheckIfDriveHasMedia(DriveMountPoint) then
RemovableDrives[high(RemovableDrives)].CardMediaPresent:=true
else
RemovableDrives[high(RemovableDrives)].CardMediaPresent:=false;
//Bus Type
RemovableDrives[high(RemovableDrives)].BusType:= DeviceDescriptor.STORAGE_BUS_TYPE;
//Parents Device Instance
if GetParentDriveDevInst(DriveMountPoint, Inst) then
RemovableDrives[high(RemovableDrives)].ParentDevInst:=Inst;
finally
if FFileHandle <> INVALID_HANDLE_VALUE then CloseHandle(FFileHandle);
end;
end;
function TDriveEjector.GetBusy: boolean;
begin
result := fBusy;
end;
function TDriveEjector.GetCardPolling: boolean;
begin
result:=fPolling;
end;
function TDriveEjector.GetCardPollingInterval: cardinal;
begin
result := FPollTimerInterval;
end;
procedure TDriveEjector.SetBusy(const Value: boolean);
begin
fBusy := Value;
end;
procedure TDriveEjector.SetCardPolling(Value: boolean);
begin
fPolling:=Value;
PollTimer.Enabled:=fPolling;
end;
procedure TDriveEjector.SetCardPollingInterval(value: cardinal);
begin
FPollTimerInterval := Value;
PollTimer.Interval:=fPollTimerInterval;
end;
procedure TDriveEjector.SetDriveAsCardReader(Index: Integer; CardReader: boolean);
begin
RemovableDrives[Index].IsCardReader := CardReader;
end;
function TDriveEjector.GetDrivesCount: integer;
begin
if fBusy then
while fBusy = true do
begin
end;
result:=Length(RemovableDrives);
end;
//This version returns an error code on failure
function TDriveEjector.RemoveDrive(MountPoint: string;
var EjectErrorCode: integer; ShowEjectMessage, CardEject, CloseRunningApps, ForceRunningAppsClosure: boolean): boolean;
var
DriveIndex, i: integer;
begin
result:=false;
EjectErrorCode:=REMOVE_ERROR_NONE;
DriveIndex:=-1;
//First find the MountPoint
if DrivesCount = 0 then
begin
EjectErrorCode:=REMOVE_ERROR_DRIVE_NOT_FOUND;
exit;
end;
for I := 0 to DrivesCount - 1 do
begin
if RemovableDrives[i].DriveMountPoint=MountPoint then
begin
DriveIndex:=i;
break;
end;
end;
if DriveIndex <> -1 then
begin
//First try and close explorer windows
EnumWindows(@EnumWindowsAndCloseFunc, LParam(MountPoint));
//Then close windows for other drives if its a partition
if RemovableDrives[DriveIndex].HasSiblings then
for I := low(RemovableDrives[DriveIndex].SiblingIndexes) to high(RemovableDrives[DriveIndex].SiblingIndexes) do
begin
EnumWindows(@EnumWindowsAndCloseFunc, LParam( RemovableDrives[RemovableDrives[DriveIndex].SiblingIndexes[i]].DriveMountPoint ));
end;
//Then try and close any programs that might be running from the drive
if CloseRunningApps then
begin
CloseAppsRunningFrom(MountPoint, ForceRunningAppsClosure);
//Then close for other drives if its a partition
if RemovableDrives[DriveIndex].HasSiblings then
for I := low(RemovableDrives[DriveIndex].SiblingIndexes) to high(RemovableDrives[DriveIndex].SiblingIndexes) do
begin
CloseAppsRunningFrom(RemovableDrives[RemovableDrives[DriveIndex].SiblingIndexes[i]].DriveMountPoint, ForceRunningAppsClosure);
end;
end;
//CHECK - stop card style eject if device isnt a card
if RemovableDrives[DriveIndex].IsCardReader = false then
CardEject:=false;
if CardEject then //keep the card reader device - eject the media
begin
if EjectCard(MountPoint, EjectErrorCode) then
begin
RemovableDrives[DriveIndex].CardMediaPresent:=false;
result:=true
end;
end
else
if EjectDevice(MountPoint, EjectErrorCode, ShowEjectMessage) then
begin
FindRemovableDrives;
result:=true;
end;
end
else
EjectErrorCode:=REMOVE_ERROR_DRIVE_NOT_FOUND;
end;
procedure TDriveEjector.RescanAllDrives;
begin
FindRemovableDrives;
end;
procedure TDriveEjector.ClearDriveList;
begin
SetLength(RemovableDrives, 0);
end;
procedure TDriveEjector.CheckForCardReaders;
var
i: integer;
begin
if DrivesCount = 0 then exit;
for i := 0 to DrivesCount - 1 do
begin
if GetNoDevicesWithSameParentInst(RemovableDrives[i].ParentDevInst) > 1 then
if GetNoDevicesWithSameProductID(RemovableDrives[i].ProductId) > 1 then //Hard drive partitions
RemovableDrives[i].IsCardReader:=False
else
RemovableDrives[i].IsCardReader:=True //Matching devices with parent inst but differing device names are likely to be card readers
end;
end;
procedure TDriveEjector.CheckForSiblings;
var
i, j: integer;
begin
{for I := 0 to DrivesCount - 1 do
begin
if GetNoDevicesWithSameParentInst(RemovableDrives[i].ParentDevInst) > 0 then
if GetNoDevicesWithSameProductID(RemovableDrives[i].ProductId) > 0 then //Hard drive partitions
RemovableDrives[i].HasSiblings := true
else
RemovableDrives[i].HasSiblings := false;
end;}
for I := 0 to DrivesCount - 1 do
for J := 0 to DrivesCount - 1 do
begin
if I = J then continue; //Same drive
if RemovableDrives[i].ParentDevInst = RemovableDrives[j].ParentDevInst then
if RemovableDrives[i].ProductId = RemovableDrives[j].ProductId then
begin
RemovableDrives[i].HasSiblings := true;
SetLength(RemovableDrives[i].SiblingIndexes, length(RemovableDrives[i].SiblingIndexes) + 1);
RemovableDrives[i].SiblingIndexes[High(RemovableDrives[i].SiblingIndexes)] := j;
end
else
RemovableDrives[i].HasSiblings := false;
end;
end;
function TDriveEjector.CheckIfDriveHasMedia(MountPoint: string): boolean;
var
Returned, DriveHandle: cardinal;
VolumeName: array[0..MAX_PATH-1] of Char;
begin
result:=false;
GetVolumeNameForVolumeMountPoint(pchar(MountPoint), VolumeName, MAX_PATH);
//GENERIC_READ or GENERIC_WRITE
DriveHandle := CreateFile(PChar(ExcludeTrailingPathDelimiter( VolumeName )),
FILE_READ_ATTRIBUTES,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
try
if DeviceIoControl(DriveHandle, IOCTL_STORAGE_CHECK_VERIFY2, nil, 0, nil, 0, @Returned, nil) then
result:=true; //Card is in reader
finally
CloseHandle(Drivehandle);
end;
end;
function TDriveEjector.EjectCard(MountPoint: string; var EjectErrorCode: integer): boolean;
var
Returned, DriveHandle: cardinal;
VolumeName: array[0..MAX_PATH-1] of Char;
begin
result:=false;
GetVolumeNameForVolumeMountPoint(pchar(MountPoint), VolumeName, MAX_PATH);
DriveHandle := CreateFile(PChar(ExcludeTrailingPathDelimiter( VolumeName )),
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
try
if DriveHandle = INVALID_HANDLE_VALUE then
begin
if GetLastError = 32 then
EjectErrorCode:=REMOVE_ERROR_DISK_IN_USE
else
EjectErrorCode:=REMOVE_ERROR_UNKNOWN_ERROR;
exit;
end;
if DeviceIoControl(DriveHandle, IOCTL_STORAGE_CHECK_VERIFY2, nil, 0, nil, 0, @Returned, nil) = false then
begin
EjectErrorCode:=REMOVE_ERROR_NO_CARD_MEDIA;
exit; //No card in reader
end;
result:=DeviceIoControl(Drivehandle, IOCTL_STORAGE_EJECT_MEDIA, nil, 0, nil, 0, @Returned, nil);
if result=false then
begin
if GetLastError = 32 then
EjectErrorCode:=REMOVE_ERROR_DISK_IN_USE
else
EjectErrorCode:=REMOVE_ERROR_UNKNOWN_ERROR;
end;
finally
CloseHandle(Drivehandle);
end;
end;
function TDriveEjector.EjectDevice(MountPoint: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false): boolean;
var
szRootPath, szDevicePath, szVolumeAccessPath: string;
dwBytesReturned: DWord;
DriveType: UINT;
hVolume: THandle;
SDN: STORAGE_DEVICE_NUMBER;
funcResult, tries, DeviceNumber: integer;
funcResultBool: boolean;
DeviceInst, DevInstParent: DEVINST;
szDosDeviceName, VetoNameW, VolumeName: array[0..MAX_PATH-1] of Char;
VetoType: PNP_VETO_TYPE;
begin
Result:=false;
GetVolumeNameForVolumeMountPoint(pchar(MountPoint), VolumeName, MAX_PATH);
szRootPath:=VolumeName;
szDevicePath:=ExcludeTrailingPathDelimiter( VolumeName );
szVolumeAccessPath:=ExcludeTrailingPathDelimiter( VolumeName );
szDevicePath:=Copy(szVolumeAccessPath, 5, length(szVolumeAccessPath) -4);
DeviceNumber:=-1;
hVolume:=INVALID_HANDLE_VALUE;
try
//Open the storage volume
hVolume:=CreateFile(PChar(szVolumeAccessPath), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if hVolume = INVALID_HANDLE_VALUE then
begin
if GetLastError = 32 then
EjectErrorCode:=REMOVE_ERROR_DISK_IN_USE
else
EjectErrorCode:=REMOVE_ERROR_UNKNOWN_ERROR;
exit;
end;
//Get the volume's device number
dwBytesReturned:=0;
funcResultBool:=DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, nil, 0, @SDN, SizeOf(SDN), @dwBytesReturned, nil);
if funcResultBool = true then
DeviceNumber:=SDN.DeviceNumber;
finally
CloseHandle(hVolume);
end;
if DeviceNumber = -1 then
begin
EjectErrorCode:=REMOVE_ERROR_WINAPI_ERROR;
exit;
end;
//Get the drive type
DriveType := GetDriveType(PChar(szRootPath));
szDosDeviceName[0]:=#0;
//Get the dos device name (like \deviceloppy0)
funcResult := QueryDosDevice(PChar(szDevicePath), szDosDeviceName, MAX_PATH);
if funcResult = 0 then
begin
EjectErrorCode:=REMOVE_ERROR_WINAPI_ERROR;
exit;
end;
//Get the device instance handle of the storage volume through a SetupDi enum and matching the device number
DeviceInst:= GetDrivesDevInstByDeviceNumber(DeviceNumber, DriveType, szDosDeviceName);
if ( DeviceInst = 0 ) then
begin
EjectErrorCode:=REMOVE_ERROR_WINAPI_ERROR;
exit;
end;
VetoType := PNP_VetoTypeUnknown;
VetoNameW[0] := #0;
//Get drives's parent - this is what gets ejected
DevInstParent := 0;
CM_Get_Parent(DevInstParent, DeviceInst, 0);
//Try and eject 3 times
for tries := 0 to 2 do
begin
VetoNameW[0] := #0;
if ShowEjectMessage then
funcResult := CM_Request_Device_EjectW(DevInstParent, nil, nil, 0, 0) //With messagebox (W2K, Vista) or balloon (XP)
else
funcResult := CM_Request_Device_EjectW(DevInstParent, @VetoType, VetoNameW, MAX_PATH, 0);
if (funcResult=CR_SUCCESS) and (VetoType=PNP_VetoTypeUnknown) then
begin
Result:=true;
break;
end;
Sleep(500); //Wait and then try again
end;
if result=false then
begin
if GetLastError = 32 then
EjectErrorCode:=REMOVE_ERROR_DISK_IN_USE
else
EjectErrorCode:=REMOVE_ERROR_UNKNOWN_ERROR;
end;
end;
function TDriveEjector.GetDrivesDevInstByDeviceNumber(DeviceNumber: Integer;
DriveType: UINT; szDosDeviceName: PCHAR): DEVINST;
var
IsFloppy, DoLoop: boolean;
myGUID: TGUID;
myhDevInfo: HDEVINFO;
dwIndex, dwSize, dwBytesReturned: DWORD;
//Buf: array[0..1024-1] of BYTE;
FunctionResult: boolean;
pspdidd: PSPDeviceInterfaceDetailData;
spdid: SP_DEVICE_INTERFACE_DATA;
spdd: SP_DEVINFO_DATA;
hDrive: THandle;
SDN: STORAGE_DEVICE_NUMBER;
begin
Result:=0;
IsFloppy:=true;
if StrPos(szDosDeviceName, '\Floppy') = nil then
IsFloppy:=false;
case (DriveType) of
DRIVE_REMOVABLE:
if ( IsFloppy ) then
begin
myguid := GUID_DEVINTERFACE_FLOPPY;
end
else
begin
myguid := GUID_DEVINTERFACE_DISK;
end;
DRIVE_FIXED:
myguid := GUID_DEVINTERFACE_DISK;
DRIVE_CDROM:
myguid := GUID_DEVINTERFACE_CDROM;
else
exit;
end;
//Get device interface info set handle for all devices attached to system
myhDevInfo := SetupDiGetClassDevs(@myguid, nil, 0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if (cardinal(myhDevInfo) = INVALID_HANDLE_VALUE) then
exit;
//Retrieve a context structure for a device interface of a device information set
dwIndex := 0;
//pspdidd :=PSP_DEVICE_INTERFACE_DETAIL_DATA(@Buf);
ZeroMemory(@spdd, SizeOf(spdd));
spdid.cbSize := SizeOf(spdid);
DoLoop:=True;
while (DoLoop=true) do
begin
FunctionResult := SetupDiEnumDeviceInterfaces(myhDevInfo, nil, myGUID, dwIndex, spdid);
if FunctionResult= false then
break;
dwSize := 0;
SetupDiGetDeviceInterfaceDetail(myhDevInfo, @spdid, nil, 0, dwSize, nil); //Check the buffer size
if ( dwSize <> 0) and (dwSize <= 1024) {SizeOf(Buf))} then
begin
GetMem(pspdidd, dwSize);
try
pspdidd.cbSize := SizeOf(pspdidd^); //SizeOf(TSPDeviceInterfaceDetailData)
ZeroMemory(@spdd, SizeOf(spdd));
spdd.cbSize := SizeOf(spdd);
FunctionResult := SetupDiGetDeviceInterfaceDetail(myhDevInfo, @spdid, pspdidd, dwSize, dwSize, @spdd);
if FunctionResult then
begin
//Open the disk or cdrom or floppy
hDrive:=INVALID_HANDLE_VALUE;
try
hDrive := CreateFile(pspdidd.DevicePath, 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if ( hDrive <> INVALID_HANDLE_VALUE ) then
begin
//Get its device number
dwBytesReturned := 0;
FunctionResult := DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, nil, 0, @sdn, SizeOf(sdn), @dwBytesReturned, nil);
if FunctionResult then
begin
if DeviceNumber = LongInt(sdn.DeviceNumber) then
begin //Match the device number with that of the current device
result:= spdd.DevInst;
break;
end;
end;
end;
finally
CloseHandle(hDrive);
end;
end;
finally
FreeMem(pspdidd);
end;
end;
dwIndex:= dwIndex + 1;
end;
SetupDiDestroyDeviceInfoList(myhDevInfo);
end;
function TDriveEjector.GetNoDevicesWithSameParentInst(
ParentDevInst: integer): integer;
var
i: integer;
begin
result:=-1; //will be inc'ed once when it goes through the one we're comparing to
for I := 0 to DrivesCount - 1 do
begin
if RemovableDrives[i].ParentDevInst = ParentDevInst then
inc(result);
end;
end;
function TDriveEjector.GetNoDevicesWithSameProductId(
ProductId: string): integer;
var
i: integer;
begin
result:=-1; //will be inc'ed once when it goes through the one we're comparing to
for I := 0 to DrivesCount - 1 do
begin
if RemovableDrives[i].ProductID = ProductID then
inc(result);
end;
end;
function TDriveEjector.GetParentDriveDevInst(MountPoint: string;
var ParentInstNum: integer): boolean;
var
szRootPath, szDevicePath, szVolumeAccessPath: string;
DeviceNumber: longint;
hVolume: THandle;
dwBytesReturned: DWord;
DriveType: UINT;
SDN: STORAGE_DEVICE_NUMBER;
FunctionResultInt: integer;
FunctionResultBool: boolean;
DeviceInst, DevInstParent: DEVINST;
szDosDeviceName, VolumeName: array[0..MAX_PATH-1] of Char;
begin
Result:=false;
GetVolumeNameForVolumeMountPoint(pchar(MountPoint), VolumeName, MAX_PATH);
szRootPath:=VolumeName;
szDevicePath:=ExcludeTrailingPathDelimiter( VolumeName );
szVolumeAccessPath:=ExcludeTrailingPathDelimiter( VolumeName );
szDevicePath:=Copy(szVolumeAccessPath, 5, length(szVolumeAccessPath) -4);
DeviceNumber:=-1;
hVolume:= INVALID_HANDLE_VALUE;
try
//Open the storage volume
hVolume:=CreateFile(PChar(szVolumeAccessPath), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if hVolume = INVALID_HANDLE_VALUE then
exit;
//Get the volume's device number
dwBytesReturned:=0;
FunctionResultBool:=DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, nil, 0, @SDN, SizeOf(SDN), @dwBytesReturned, nil);
if FunctionResultBool then
DeviceNumber:=SDN.DeviceNumber;
finally
CloseHandle(hVolume);
end;
if DeviceNumber = -1 then
exit;
//Get the drive type which is required to match the device numbers correctely
DriveType := GetDriveType(PChar(szRootPath));
szDosDeviceName[0]:=#0;
//Get the dos device name (like \deviceloppy0) to decide if it's a floppy or not
FunctionResultInt := QueryDosDevice(PChar(szDevicePath), szDosDeviceName, MAX_PATH);
if FunctionResultInt = 0 then
exit;
//Get the device instance handle of the storage volume by means of a SetupDi enum and matching the device number
DeviceInst:= GetDrivesDevInstByDeviceNumber(DeviceNumber, DriveType, szDosDeviceName);
if (DeviceInst = 0) then
exit;
//Get drives's parent
DevInstParent:=0;
CM_Get_Parent(DevInstParent, DeviceInst, 0);
if DevInstParent > 0 then
begin
ParentInstNum:=DevInstParent;
result:=true;
end;
end;
procedure TDriveEjector.OnTimer(Sender: TObject);
var
i: integer;
begin
//sysutils.Beep;
if GetDrivesCount = 0 then exit;
if fPolling = false then exit;
for I := 0 to GetDrivesCount - 1 do