Functions to eject/retract or lock/unlock the tray in a CD or DVD drive, or set a drive's volume label.


Changes Drive's volume label to be NewLabel.

DriveSetLabel Drive , NewLabel

The drive letter followed by a colon and an optional backslash (might also work on UNC paths and mapped drives).


The new label to set. If omitted, the drive will have no label.

For example: DriveSetLabel("C:", "Seagate200").

To retrieve the current label, follow this example: Label := DriveGetLabel("C:").


Prevents a drive's eject feature from working.

DriveLock Drive

For example: DriveLock("D:").

Most drives cannot be "locked open". However, locking the drive while it is open will probably result in it becoming locked the moment it is closed.

This function has no effect on drives that do not support locking (such as most read-only drives).

To unlock a drive, call DriveUnlock. If a drive is locked by a script and that script exits, the drive will stay locked until another script or program unlocks it, or the system is restarted.

If the specified drive does not exist or does not support the locking feature, ErrorLevel is set to 1. Otherwise, it is set to 0.


Reverses DriveLock.

DriveUnlock Drive

For example: DriveUnlock("D:")

This function needs to be called multiple times if the drive was locked multiple times (at least for some drives). For example, if DriveLock("D:") was called three times, DriveUnlock("D:") might need to be called three times to unlock it. Because of this and the fact that there is no way to determine whether a drive is currently locked, it is often useful to keep track of its lock-state in a variable.


Ejects or retracts the tray of a CD or DVD drive.

DriveEject Drive, Retract := false

The drive letter followed by a colon and an optional backslash. If omitted, the default CD/DVD drive will be used.


Specify true to retract/close the tray. Specify false or omit this parameter to eject the tray.

DriveEject waits for the ejection or retraction to complete before allowing the script to continue. If the tray is already in the correct state (open or closed), ErrorLevel is set to 0 (i.e. "no error").

DriveEject will probably not work on a network drive or non-CD/DVD drive. If it fails in such cases or for any other reason, ErrorLevel is set to 1. To eject other types of media or devices, see the DllCall example at the bottom of this page.

It may be possible to detect the previous tray state by measuring the time the function takes to complete. For example, the following hotkey toggles the tray to the opposite state (open or closed):

; If the function completed quickly, the tray was probably already ejected.
; In that case, retract it:
if A_TimeSinceThisHotkey < 1000  ; Adjust this time if needed.
    DriveEject(, true)

To determine the media status of a CD or DVD drive (playing, stopped, open, etc.), see DriveGetStatusCD.


ErrorLevel is set to 1 if there was a problem or 0 otherwise.

These functions return 1 on success and 0 on failure.


The following is an alternate ejection method that also works on types of media/devices other than CD/DVD:

; Update the first line below to match the desired drive letter (you can ignore all the other lines below).
Driveletter := "I:"  ; Set this to the drive letter you wish to eject.

hVolume := DllCall("CreateFile"
    , Str, "\\.\" . Driveletter
    , UInt, 0x80000000 | 0x40000000  ; GENERIC_READ | GENERIC_WRITE
    , UInt, 0x1 | 0x2  ; FILE_SHARE_READ | FILE_SHARE_WRITE
    , UInt, 0
    , UInt, 0x3  ; OPEN_EXISTING
    , UInt, 0, UInt, 0)
if hVolume <> -1
        , UInt, hVolume
        , UInt, 0x2D4808   ; IOCTL_STORAGE_EJECT_MEDIA
        , UInt, 0, UInt, 0, UInt, 0, UInt, 0
        , UIntP, dwBytesReturned  ; Unused.
        , UInt, 0)
    DllCall("CloseHandle", UInt, hVolume)