Sorry for my english...
I'm using these 2 functions to flush system data on a volume and to lock it:
hVolume := CreateFile(PAnsiChar('\\.\' + VolumesInfo[i].Letter + ':'),
GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
function LockVolume(const hVolume: THandle): Boolean;
FSCTL_LOCK_VOLUME = (9 shl 16) or (0 shl 14) or (6 shl 2) or 0;
Result := False;
for i := 1 to 20 do
Result := DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, nil, 0,
nil, 0, dwBytesReturned, nil);
if Result then
I have a couple of questions:
I read that using FlushFileBuffers does not guarantee an 100% flush of the system data. Is there a better way to flush it?
I've seen in other examples of code that I found that Sleep(500) is used after DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, nil, 0, nil, 0, dwBytesReturned, nil). Is it better this way and, if so, why...?
Is there a way to ensure that during those 10 sec of waiting, only system data is flushed and other applications will not try to write (and not to read if it's possible) on the volume...? Removing FILE_SHARE_WRITE or maybe even the FILE_SHARE_WRITE flags from the CreateFile will help...?
I'm only trying to ensure that it will lock it in as many situations as possible and as fast as possible.