How to flush a storage volume's File Cache, lock and dismount it, and eject the media

Source: Internet
Author: User
Introduction

Ejecting a media is a well known task for CD/DVD drives. here we get a physical ejection. but also zip/Jaz drives and card readers (drives with a removable media) Support ejection and since they are not read only, it is required to dismount their File System
Before ejection to prevent loss of data.

Background

Ejecting a media is already med byDeviceIoControlCall
IOCTL_STORAGE_EJECT_MEDIA. But it is quite reckless, It ejects even when there are open files on the drive! Therefore it's better to flush, lock, and dismount before doing it.

The sample

This sample is a simplified version of my command-line tool ejectmedia.

It expects the drive letter to eject as a parameter. For non CD/DVD drives, the first step is to flush the File Cache by means of
FlushFileBuffersAPI call. It needs a handle with write access. So, for flushing a whole storage volume, we have to open it with write access, which will fail
ERROR_ACCESS_DENIEDFor Restricted Users:

// "\\.\X:" -> to open the volumechar szVolumeAccessPath[] = "\\\\.\\X:";szVolumeAccessPath[4] = DriveLetter;HANDLE hVolWrite = CreateFile(szVolumeAccessPath,                              GENERIC_READ | GENERIC_WRITE,                              FILE_SHARE_READ | FILE_SHARE_WRITE,                              NULL, OPEN_EXISTING, 0, NULL);if ( hVolWrite != INVALID_HANDLE_VALUE ) {  FlushFileBuffers(hVolWrite);  CloseHandle(hVolWrite);}

Then we try to lock and dismount. here read access is good enough which even restricted users can get. A lock succeeds only if there are no open handles to files on this volume. dismount succeeds even when lock fails and there are open handles. after dismount,
All open file handles on the volume are still open but invalid, any try to use it leads
ERROR_INVALID_HANDLE. Therefore it is an option to dismount even when the lock fails, that is the meaning of'Force'Boolean value.

bool Force = false;HANDLE hVolRead = CreateFile(szVolumeAccessPath,                  GENERIC_READ,                  FILE_SHARE_READ | FILE_SHARE_WRITE,                  NULL, OPEN_EXISTING, 0, NULL);if ( hVolRead != INVALID_HANDLE_VALUE ) {  int Locked = DeviceIoControl(hVolRead, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwRet, NULL);  if ( Locked || Force ) {    res = DeviceIoControl(hVolRead, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwRet, NULL);  }}

Finally we perform the ejection:

res = DeviceIoControl(hVolRead, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwRet, NULL);

Since we are friendly, we release the lock if we get it:

if ( Locked ) {  DeviceIoControl(hVolRead, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwRet, NULL);}

BTW: the opposite of ejection is loading a media. This really works on CD/DVD drives only:

res = DeviceIoControl(hVolRead, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwRet, NULL);
Discussionwhy is there no check for drivetypes drive_cdrom and drive_removable?

Windows does not care about the drive type here. the eject request is passed down to the drive's driver which passes it to the hardware. finally it is the hardware which gives the answer. there are USB hard drives which support the eject. I 've seen safe
Removal failing but ejection succeeding.

What exactly is dismounting?

Dismounting means to detach the file system from the storage volume. this guarantees that the file system is not held in an inconsistent state which is important before mounting it by another driver, e.g ., while the system is hibernated and you boot from
A bootable CD.

Isn' t it the same as removing the drive letter (mountvol X:/d )?

No, absolutely not! The/D does not stand for dismount, it stands for deleting the mount point. And that is all it does. No flushing, no dismounting, no handles are closed or invalidated.

How do we remount a volume?

If the lock is released, then just try to open a file on the volume. Windows automatically mounts the volume then.

Why does locking take a while sometimes?

It seems that locking also flushes the File Cache and this may take a little time. Furthermore, applications can register for custom notifications (RegisterDeviceNotification) And then
DBT_CUSTOMEVENTComesGUID_IO_VOLUME_LOCK.FSCTL_LOCK_VOLUMEWill not return before all applications registered for custom specifications of the volume in question have processed the message.

The demo project

The demo project is made with vs6. it requires some headers from a Microsoft platform SDK. No special libs are required.

History
  • 26 Sept 2011-first release.
License

This article, along with any associated source code and files, is licensed under the Code project open license (cpol)

About the author

Uwe_sieber

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.