-
Notifications
You must be signed in to change notification settings - Fork 7
/
RamRemove.pas
190 lines (174 loc) · 5.91 KB
/
RamRemove.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
unit RamRemove;
interface
Uses Definitions;
Function DetachRamDisk (Var existing:TRamDisk):Boolean;
implementation
uses SysUtils,Windows,Messages,RamSync;
function OpenVolume(ADrive: char): THandle;
var
VolumeName: string;
begin
VolumeName := Format('\\.\%s:', [ADrive]);
Result := CreateFile(PChar(VolumeName), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
end;
function PreventRemovalOfVolume(AVolumeHandle: THandle; APreventRemoval: boolean): boolean;
var
BytesReturned: Cardinal;
PreventMediaRemoval: BOOL;
begin
PreventMediaRemoval := APreventRemoval;
Result := DeviceIoControl(AVolumeHandle, IOCTL_STORAGE_MEDIA_REMOVAL,
@PreventMediaRemoval, SizeOf(PreventMediaRemoval), nil, 0, BytesReturned, nil);
end;
function AutoEjectVolume(AVolumeHandle: THandle): boolean;
var
BytesReturned: Cardinal;
begin
Result := DeviceIoControl(AVolumeHandle, IOCTL_STORAGE_EJECT_MEDIA, nil, 0, nil, 0, BytesReturned, nil);
end;
Procedure ImDiskNotifyRemovePending(DriveLetter:WideChar);
var
dwp: DWORD;
devBroadcastVol: TDevBroadcastVolume;
Begin
devBroadcastVol.dbch_size:=SizeOf(devBroadcastVol);
devBroadcastVol.dbch_devicetype:=DBT_DEVTYP_VOLUME;
devBroadcastVol.dbcv_unitmask:=1 shl (Ord(DriveLetter) - Ord('A'));
SendMessageTimeout(HWND_BROADCAST,
WM_DEVICECHANGE,
DBT_DEVICEQUERYREMOVE,
Integer(@devBroadcastVol),
SMTO_BLOCK or SMTO_ABORTIFHUNG,
4000,
dwp);
SendMessageTimeout(HWND_BROADCAST,
WM_DEVICECHANGE,
DBT_DEVICEREMOVEPENDING,
Integer(@devBroadcastVol),
SMTO_BLOCK or SMTO_ABORTIFHUNG,
4000,
dwp);
end;
Function ImScsiRemoveDeviceByNumber(hWnd, adapter: THandle; DeviceNumber: TDeviceNumber):Boolean;
Var
dw: DWORD;
remove_device: TScsiRemoveDevice;
begin
ImScsiInitializeSrbIoBlock(remove_device.SrbIoControl, sizeof(TScsiRemoveDevice), SMP_IMSCSI_REMOVE_DEVICE, 0);
remove_device.DeviceNumber := DeviceNumber;
if Not ImScsiDeviceIoControl(Adapter, SMP_IMSCSI_REMOVE_DEVICE, remove_device.SrbIoControl, sizeof(remove_device), 0, dw) then
begin
//ImScsiMsgBoxLastError(hWnd, L"Error removing virtual disk:");
Result:=FALSE;
End
else Result:=TRUE;
end;
Function DetachRamDisk(var existing:TRamDisk):Boolean;
var
device,adapter: THandle;
tmp: Integer;
dw: DWORD;
deviceNumber: TDeviceNumber;
portNumber: Byte;
forceDismount: Boolean;
Begin
Result:=False;
DebugLog('Begin DetachRamDisk');
If existing.letter = #0 Then
Begin
DebugLog('RamDisk has no drive letter attahed');
adapter := ImScsiOpenScsiAdapter(portNumber);
DebugLog(Format('SCSI adapter handle = %u',[adapter]));
if adapter = INVALID_HANDLE_VALUE then
begin
dw:=GetLastError;
if dw = ERROR_FILE_NOT_FOUND then DebugLog('Arsenal Driver not installed',EVENTLOG_ERROR_TYPE)
else DebugLog(SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
raise ERamDiskError.Create(RamNotInstalled);
end;
deviceNumber.LongNumber:=IMSCSI_ALL_DEVICES;
if not ImScsiRemoveDeviceByNumber(0, adapter, DeviceNumber) then
begin
dw:=GetLastError;
if dw = ERROR_FILE_NOT_FOUND then
begin
DebugLog('The SCSI device of the RAM-disk was not found',EVENTLOG_ERROR_TYPE);
Exit;
end
else
begin
DebugLog(SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
Exit;
end;
end;
DebugLog('RamDisk device has been destroyed');
Result:=True;
Exit;
end;
if existing.synchronize And (existing.persistentFolder<>'') then SaveRamDisk(existing);
forceDismount:=False;
DebugLog(Format('Trying to open volume %s',[existing.letter]));
device := OpenVolume(existing.letter);
if device = INVALID_HANDLE_VALUE then
begin
tmp:=GetLastError;
DebugLog(Format('Could not open the volume, error is "%s"',[SysErrorMessage(tmp)]),EVENTLOG_ERROR_TYPE);
case tmp of
ERROR_INVALID_PARAMETER:
// "This version of Windows only supports drive letters as mount points.\n"
// "Windows 2000 or higher is required to support subdirectory mount points.\n",
Exit;
ERROR_INVALID_FUNCTION:
// "Mount points are only supported on NTFS volumes.\n",
Exit;
ERROR_NOT_A_REPARSE_POINT,
ERROR_DIRECTORY,
ERROR_DIR_NOT_EMPTY:
// ImScsiOemPrintF(stderr, "Not a mount point: '%1!ws!'", MountPoint);
Exit;
else
raise Exception.Create(SysErrorMessage(tmp));
end;
End;
// Notify processes that this device is about to be removed.
DebugLog('Now notifying other processes that this device is about to be removed');
ImDiskNotifyRemovePending(WideChar(existing.letter));
DebugLog('Flushing OS file buffers');
FlushFileBuffers(device);
// Locking volume
try
DebugLog('Locking the volume');
if Not DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL) then
Begin
forceDismount := TRUE;
DebugLog('Could not lock the volume - so trying a forced unmount');
End;
// Unmounting filesystem
try
DebugLog('Trying to unmount the filesystem');
if DeviceIoControl(device, FSCTL_DISMOUNT_VOLUME, NIL, 0, NIL, 0, dw, NIL) then
begin
if forceDismount then
Begin
DeviceIoControl(device, FSCTL_LOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL);
DebugLog('Doing forced lock');
end;
// Set prevent removal to false and eject the volume
if PreventRemovalOfVolume(device, FALSE) then
Begin
AutoEjectVolume(device);
DebugLog('Ejected the volume');
End;
Result:=True;
end;
finally
DeviceIoControl(device, FSCTL_UNLOCK_VOLUME, NIL, 0, NIL, 0, dw, NIL);
DebugLog('Unlocked the volume');
End;
finally
CloseHandle(device);
end;
RestoreTempFolder(WideChar(existing.letter)); // MUST be before UpdateDismounted because it will clear the Letter
end;
end.