
EjectMedia V3.2.4 - ejects a media from a drive
Freeware by Uwe Sieber - www.uwe-sieber.de


Win32 version works Window 2000, XP and higher, including x64 editions.
x64 version works under Windows x64 editions only.


Before the ejection EjectMedia tries to dismount the volume. This
succeeds only if there are no open handles on the volume which can
be something banal like an open Word document, a mounted TrueCrypt
container or some kind of monitoring tool like a virus scanner.
With parameter -t EjectMedia scans for TrueCrypt volumes hosted
by the drive to eject and tries to release it. Since V2.7 this
works with VeraCrypt too.

Started with -f or when pressing the F key while in the try
loop the dismount is forced which makes the open handles
invalid.



The 'eject' request is passed to 'Fixed' drives too. It depends
on the drive's hardware if it is supported or not.
It usually works with USB flash drives turned into 'fixed' drives.

Some drives report a successful ejection but do nothing in fact.

CD/DVD drives and some USB flash drives can bring back an ejected
media by means of a 'load media' request:
http://www.uwe-sieber.de/drivetools_e.html#loadmedia


To prepare an external drive for 'safe removal' use my tool
RemoveDrive:
http://www.uwe-sieber.de/drivetools_e.html#removedrive


Usage:
EjectMedia DriveSpec: [-l] [-d] [-f] [-o] [-s] [-h] [-a] [-na] [-w:nnnn]

DriveSpec: is the drive to remove, e.g. U: or a NTFS mountpoint like "C:\Mount\USB Drive1"
           or a volume name like \\?\Volume{8108336c-7299-11de-ac31-000102b35cc3}
           or . for the current drive
           or \ for the drive of the EjectMedia.exe
-d       dismount only (no eject)
-f       force dismount when lock failed
-o       take volume offline
-f       force eject
-s       self delete EjectDrive.exe when finished
-t       release TrueCrypt volumes hosted by the drive to remove
-h       show open handles
-L       loop until success
-na      no about info
-w:nnnn  wait nnnn milliseconds before close

Parameters are case-insensitive.

Under XP by default admin rights are required to eject a media. Since SP2
this can be changed by a policy:
http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/regentry/96271.mspx
This reg file includes the required setting:
http://www.uwe-sieber.de/files/allocatedasd_2.reg



Returns Errorlevels:

0 - successfully ejected a media
1 - media not ejected/dismouted/taken offline
2 - parameters are invalid
3 - no media present
4 - EjectMedia.exe located on the drive to remove -> temporary copy created and executed
5 - EjectMedia.exe located on the drive to remove -> temporary copy failed


About correct eject

Ejection is safe only when the media's file system is dismounted. It is safe too (at least
for the media's file system) when the media is write protected.
The correct order is
- lock the volume which means that no file is open
- dismount the file system
- eject
- unlock the volume

BTW: The Windows Explorer does it wrong by not dismounting the file system and unlocking before
ejecting:
- lock the volume which means that no file is open
- unlock the volume
- eject



About open handles

EjectMedia does no use a driver for gaterhing handle information. Therefore
when hitting the handle of a waiting pipe under XP the function for getting
the handle name does not return, the thread freezes. Since V3.0 EjectMedia
uses the Win32 call GetFileType to filter out pipes before calling NtQueryObject.
GetFileType freezes too when hitting a pipe but when the EjectMedia process
ends Windows sucessfully kills it in contrast to NtQueryObject.
Since Vista GetFileType doesn't freeze at all, so no more problem since V3.0.

EjectMedia finds handles of processes of the same or lower trust level. So if
it is called restricted then it finds handles of restricted processes only.
Since V3.1 it gets handles of system processes too if started with admin privileges.




Homepage:
http://www.uwe-sieber.de/drivetools_e.html#ejectmedia



Licence


EjectMedia is Freeware.

Allowed:
- usage in any environment, including commercial
- include in software products, including commercial
- include on CD/DVD of computer magazines
- making available for download by means of package managers

Not allowed:
- changing any of the files
- offer for download by means of a 'downloader' software




Uwe Sieber
March 2025

