-
Notifications
You must be signed in to change notification settings - Fork 2
/
uDriveEjector.pas
523 lines (440 loc) · 14.1 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
unit uDriveEjector;
interface
uses
Classes, Windows, SysUtils, extctrls,
CfgMgr32, SetupApi, JwaWinBase, Cfg, JwaWinIoctl, jclsysinfo,
uProcessAndWindowUtils, uDiskEjectConst;
type
TRemovableDrive = packed record
DriveLetter: char;
VolumeName: string;
VendorId: string;
ProductID: string;
ProductRevision: string;
BusType: integer;
ParentDevInst: integer;
end;
TDriveEjector = class
private
function GetDrivesCount: integer;
function GetDrivesDevInstByDeviceNumber(DeviceNumber: Integer; DriveType: UINT; szDosDeviceName: PCHAR): DEVINST;
function GetParentDriveDevInst(DriveLetter: string; var ParentInstNum: integer): boolean;
//function EjectDevice(DriveLetter: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false): boolean;
procedure ScanDrive(DriveLetter: Char);
procedure DeleteArrayItem(const Index: Integer);
public
RemovableDrives: array of TRemovableDrive;
constructor Create;
destructor Destroy; override;
function RemoveDrive(DriveLetter: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false): boolean;
procedure FindRemovableDrives;
property DrivesCount: integer read GetDrivesCount;
end;
implementation
procedure TDriveEjector.DeleteArrayItem(const Index: Integer);
begin
if Index > High(RemovableDrives) then Exit;
if Index < Low(RemovableDrives) then Exit;
if Index = High(RemovableDrives) then
begin
SetLength(RemovableDrives, Length(RemovableDrives) - 1);
Exit;
end;
Finalize(RemovableDrives[Index]);
System.Move(RemovableDrives[Index +1], RemovableDrives[Index],(Length(RemovableDrives) - Index - 1) * SizeOf(string) + 1);
SetLength(RemovableDrives, Length(RemovableDrives) - 1);
end;
constructor TDriveEjector.Create;
begin
LoadSetupApi;
LoadConfigManagerApi;
end;
destructor TDriveEjector.Destroy;
begin
SetLength(RemovableDrives, 0);
UnloadConfigManagerApi;
UnloadSetupApi;
inherited;
end;
procedure TDriveEjector.FindRemovableDrives;
const
MAX_DRIVES = 26;
var
dwDriveMask: Cardinal;
DriveName: string;
i: integer;
begin
SetLength(RemovableDrives, 0);
dwDriveMask := GetLogicalDrives;
DriveName := 'A:\';
for i := 0 to MAX_DRIVES - 1 do
if (dwDriveMask and (1 shl I)) <> 0 then
begin
DriveName[1] := 'A';
Inc(DriveName[1], I);
if GetDriveType(PChar(DriveName)) > 1 then
ScanDrive(DriveName[1]);
end;
end;
procedure TDriveEjector.ScanDrive(DriveLetter: Char);
type
PCharArray = ^TCharArray;
TCharArray = array[0..32767] of Char;
STORAGE_PROPERTY_QUERY = packed record
PropertyId: Cardinal;
QueryType: Cardinal;
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: Cardinal;
RawPropertiesLength: ULONG;
RawDeviceProperties: array[0..511] of Byte;
end;
const
IOCTL_STORAGE_QUERY_PROPERTY = $2D1400;
var
Returned, FFileHandle: Cardinal;
PropQuery: STORAGE_PROPERTY_QUERY;
DeviceDescriptor: STORAGE_DEVICE_DESCRIPTOR;
PCh: PChar;
Inst: integer;
begin
FFileHandle:=INVALID_HANDLE_VALUE;
try
FFileHandle := CreateFile(
PChar('\\.\' + DriveLetter + ':'),
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);
if not DeviceIoControl(
FFileHandle,
IOCTL_STORAGE_QUERY_PROPERTY,
@PropQuery,
SizeOf(PropQuery),
@DeviceDescriptor,
DeviceDescriptor.Size,
@Returned,
nil
) then
Exit;
if not((DeviceDescriptor.STORAGE_BUS_TYPE = 7) or (DeviceDescriptor.STORAGE_BUS_TYPE = 4)) then
Exit;
SetLength(RemovableDrives, length(RemovableDrives) + 1);
//Drive Letter
RemovableDrives[high(RemovableDrives)].DriveLetter := DriveLetter;
//Volume Name
RemovableDrives[high(RemovableDrives)].VolumeName :=Trim(GetVolumeName(DriveLetter));
if RemovableDrives[high(RemovableDrives)].VolumeName = '' then
RemovableDrives[high(RemovableDrives)].VolumeName := 'Áåçûìÿííûé';
//Vendor Id
if DeviceDescriptor.VendorIdOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.VendorIdOffset];
RemovableDrives[high(RemovableDrives)].VendorId := Trim(PCh);
end;
//Product Id
if DeviceDescriptor.ProductIdOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductIdOffset];
RemovableDrives[high(RemovableDrives)].ProductID := Trim(PCh);
end;
//Product Revision
if DeviceDescriptor.ProductRevisionOffset <> 0 then
begin
PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductRevisionOffset];
RemovableDrives[high(RemovableDrives)].ProductRevision := Trim(PCh);
end;
//Bus Type
RemovableDrives[high(RemovableDrives)].BusType:= DeviceDescriptor.STORAGE_BUS_TYPE;
//Parents Device Instance
if GetParentDriveDevInst(DriveLetter, Inst) then
RemovableDrives[high(RemovableDrives)].ParentDevInst:=Inst;
finally
if FFileHandle <> INVALID_HANDLE_VALUE then CloseHandle(FFileHandle);
end;
end;
function TDriveEjector.GetDrivesCount: integer;
begin
result:=Length(RemovableDrives);
end;
function TDriveEjector.RemoveDrive(DriveLetter: string; var EjectErrorCode: integer; ShowEjectMessage: boolean): boolean;
var
DriveIndex: Integer;
i: Integer;
FuncResult: Integer;
VetoType: PNP_VETO_TYPE;
VetoNameW: array[0..MAX_PATH-1] of WCHAR;
DevInstParent: DEVINST;
begin
result := false;
EjectErrorCode := REMOVE_ERROR_NONE;
DriveIndex:=-1;
for i := 0 to DrivesCount - 1 do
begin
if RemovableDrives[i].DriveLetter = DriveLetter then
begin
DriveIndex := i;
break;
end;
end;
if DriveIndex <> -1 then
begin
DevInstParent := RemovableDrives[DriveIndex].ParentDevInst;
for i := 1 to 1 do
begin
VetoNameW[0] := #0;
VetoType := PNP_VetoTypeUnknown;
if ShowEjectMessage then
funcResult := CM_Request_Device_EjectW(DevInstParent, nil, nil, 0, 0)
else
funcResult := CM_Request_Device_EjectW(DevInstParent, @VetoType, VetoNameW, MAX_PATH, 0);
if (funcResult = CR_SUCCESS) and (VetoType = PNP_VetoTypeUnknown) then
begin
DeleteArrayItem(DriveIndex);
Result := true;
break;
end;
//Sleep(500);
end;
if result=false then
EjectErrorCode:=REMOVE_ERROR_DISK_IN_USE;
end
else
EjectErrorCode:=REMOVE_ERROR_DRIVE_NOT_FOUND;
end;
(*function TDriveEjector.EjectDevice(DriveLetter: string; var EjectErrorCode: integer; ShowEjectMessage: boolean = false): boolean;
var
szRootPath, szDevicePath, szVolumeAccessPath: string;
DeviceNumber: longint;
dwBytesReturned: Cardinal;
DriveType: UINT;
hVolume: THandle;
SDN: STORAGE_DEVICE_NUMBER;
funcResult, tries: integer;
funcResultBool: boolean;
DeviceInst, DevInstParent: DEVINST;
szDosDeviceName: array[0..MAX_PATH-1] of Char;
VetoType: PNP_VETO_TYPE;
VetoNameW: array[0..MAX_PATH-1] of WCHAR;
begin
Result:=false;
szRootPath:=DriveLetter + ':';
szDevicePath:=DriveLetter + ':';
szVolumeAccessPath:='\\.\' + DriveLetter + ':';
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) to decide if it's a floppy or not
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 by means of 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: Boolean;
myGUID: TGUID;
myhDevInfo: HDEVINFO;
dwIndex, dwSize, dwBytesReturned: Cardinal;
FunctionResult: boolean;
pspdidd: PSPDeviceInterfaceDetailData;
spdid: SP_DEVICE_INTERFACE_DATA;
spdd: SP_DEVINFO_DATA;
hDrive: THandle;
SDN: STORAGE_DEVICE_NUMBER;
begin
Result:=0;
if StrPos(szDosDeviceName, '\Floppy') = nil then
IsFloppy:=false
else
IsFloppy := true;
case (DriveType) of
DRIVE_REMOVABLE:
if IsFloppy then
myguid := GUID_DEVINTERFACE_FLOPPY
else
myguid := GUID_DEVINTERFACE_DISK;
DRIVE_FIXED:
myguid := GUID_DEVINTERFACE_DISK;
DRIVE_CDROM:
myguid := GUID_DEVINTERFACE_CDROM;
else
Exit;
end;
myhDevInfo := SetupDiGetClassDevs(@myguid, nil, 0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if (cardinal(myhDevInfo) = INVALID_HANDLE_VALUE) then
Exit;
dwIndex := 0;
ZeroMemory(@spdd, SizeOf(spdd));
spdid.cbSize := SizeOf(spdid);
while True do
begin
FunctionResult := SetupDiEnumDeviceInterfaces(myhDevInfo, nil, myGUID, dwIndex, spdid);
if FunctionResult= false then
break;
dwSize := 0;
SetupDiGetDeviceInterfaceDetail(myhDevInfo, @spdid, nil, 0, dwSize, nil);
if ( dwSize <> 0) and (dwSize <= 1024) then
begin
GetMem(pspdidd, dwSize);
try
pspdidd.cbSize := SizeOf(pspdidd^);
ZeroMemory(@spdd, SizeOf(spdd));
spdd.cbSize := SizeOf(spdd);
FunctionResult := SetupDiGetDeviceInterfaceDetail(myhDevInfo, @spdid, pspdidd, dwSize, dwSize, @spdd);
if FunctionResult then
begin
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
dwBytesReturned := 0;
FunctionResult := DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, nil, 0, @sdn, SizeOf(sdn), @dwBytesReturned, nil);
if FunctionResult and (DeviceNumber = LongInt(sdn.DeviceNumber)) then
begin
result:= spdd.DevInst;
break;
end;
end;
finally
CloseHandle(hDrive);
end;
end;
finally
FreeMem(pspdidd);
end;
end;
dwIndex:= dwIndex + 1;
end;
SetupDiDestroyDeviceInfoList(myhDevInfo);
end;
function TDriveEjector.GetParentDriveDevInst(DriveLetter: string; var ParentInstNum: integer): boolean;
var
szRootPath, szDevicePath, szVolumeAccessPath: string;
DeviceNumber: longint;
hVolume: THandle;
dwBytesReturned: Cardinal;
DriveType: UINT;
SDN: STORAGE_DEVICE_NUMBER;
FunctionResultInt: integer;
FunctionResultBool: boolean;
DeviceInst, DevInstParent: DEVINST;
szDosDeviceName: array[0..MAX_PATH-1] of Char;
begin
Result:=false;
szRootPath:=DriveLetter + ':';
szDevicePath:=DriveLetter + ':';
szVolumeAccessPath:='\\.\' + DriveLetter + ':';
DeviceNumber:=-1;
hVolume:= INVALID_HANDLE_VALUE;
try
hVolume:=CreateFile(PChar(szVolumeAccessPath), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if hVolume = INVALID_HANDLE_VALUE then
exit;
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;
DriveType := GetDriveType(PChar(szRootPath));
szDosDeviceName[0]:=#0;
FunctionResultInt := QueryDosDevice(PChar(szDevicePath), szDosDeviceName, MAX_PATH);
if FunctionResultInt = 0 then
exit;
DeviceInst:= GetDrivesDevInstByDeviceNumber(DeviceNumber, DriveType, szDosDeviceName);
if (DeviceInst = 0) then
exit;
DevInstParent:=0;
CM_Get_Parent(DevInstParent, DeviceInst, 0);
if DevInstParent > 0 then
begin
ParentInstNum:=DevInstParent;
result:=true;
end;
end;
end.