Windows 10: Ejecting Removable Disk

  1.    15 Mar 2018 #1

    Ejecting Removable Disk


    I'm writing a C program for Windows that deals specifically with lettered optical drives. From the dearth of information, I gather no easy solution exists for programmatically ejecting such a disk. But I'll ask. Does anyone know an API under C that can be used to eject an optical disk without disturbing the user, such as a disk mounted on drive e:?

    Thank you for your attention.

    --rlsj
      My ComputerSystem Spec


  2. Posts : 6,392
    10 Home x64 (1709) (10 Pro on 2nd pc)
       15 Mar 2018 #2

    Does this help?

    Win32 applications that run on Windows NT can programmatically eject media from drives that have hardware support for media removal. However, they must do so correctly to avoid corrupting the media. This article explains how to eject media correctly on Windows NT.
    https://support.microsoft.com/en-us/...000-windows-xp
      My ComputersSystem Spec

  3.    15 Mar 2018 #3

    Thank you very much!

    My program deals solely with read-only optical disks -- no flashdrive, so I believe that a drastic shortening of your example will work. Here is the resulting code. It compiles cleanly but I'm not yet far along enough to test it. Will report later.

    --rLsj

    Code:
    #include <windows.h>
    #include <winioctl.h>
    #include <tchar.h>
    #include <stdio.h>
    
    
    /* Eject optical disk */
    void eject(drn)
    char *drn;            /* drive ID letter (& :) */
    {
    
        HANDLE hvol;
        DWORD byr;
    
    /* entry */
        hvol = CreateFile(drn,GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
        if (hvol == INVALID_HANDLE_VALUE) {
            printf("Manually eject %s\n",drn);  return;
        }
        DeviceIoControl(hvol,IOCTL_STORAGE_EJECT_MEDIA,0,0,0,0,&byr,0);
        CloseHandle(hvol);
        return;
    }
      My ComputerSystem Spec


  4. Posts : 6,392
    10 Home x64 (1709) (10 Pro on 2nd pc)
       15 Mar 2018 #4

    rLsj said: View Post
    ...I believe that a drastic shortening of your example will work.
    Glad to hear that, but I can't take credit for the example (that goes to Microsoft) just for knowing where to look for one
      My ComputersSystem Spec

  5.    4 Weeks Ago #5

    Well, it didn't work. Amended the code to show the system reason. When "drn" contains, "e:", the actual optical drive ID, the system message is "Access denied." With anything else it reports can't find the file or the path. To "\\e:" it reported couldn't find the network drive. "e:" worked with an earlier call to GetVolumeInformation, so I conclude that must be the expected name format.

    "Access denied" smacks of administrative privilege. So I ran the program under Task Manager, giving it administrative privilege. Same result: "Access denied."

    Guess when running this program I'll have to keep a File Manager window open to click "eject." Sigh.

    Code:
    #include <windows.h>
    #include <winioctl.h>
    #include <tchar.h>
    #include <stdio.h>
    
    /* Eject optical disk */
    void eject(drn)
    char *drn;            /* drive ID letter (& :) */
    {
    
        HANDLE hvol;
        int dwfl;
        DWORD byr;
        char msbuf[256];
    
    /* entry */
        hvol = CreateFile(drn,GENERIC_READ,
            0,0,OPEN_EXISTING,0,0);
        if (hvol == INVALID_HANDLE_VALUE) {
            byr = GetLastError();
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,byr,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),msbuf,256,0);
            puts(msbuf);
            printf("Manually eject %s\n",drn);
            return;
        }
        DeviceIoControl(hvol,IOCTL_STORAGE_EJECT_MEDIA,0,0,0,0,&byr,0);
        CloseHandle(hvol);
        return;
    }
      My ComputerSystem Spec


  6. Posts : 4,475
    Win10 Home and Pro, Win10 Insider Preview, Win7 Home, Linux Mint
       4 Weeks Ago #6

    What kind of computer, Desktop or Notebook? Some Desktop keyboards or third-party keyboards have an Eject key for ODDs as do a number of Notebooks, especially those marked for multi-media use. If playing audio CDs in Windows Media Player there is an Eject choice on one of the Menu items [Ctrl+J], other such programs may have it. Don't find it on VLC.
      My ComputerSystem Spec


  7. Posts : 6,392
    10 Home x64 (1709) (10 Pro on 2nd pc)
       4 Weeks Ago #7

    Berton said: View Post
    ...in Windows Media Player there is an Eject choice on one of the Menu items... Don't find it on VLC.
    There's an eject button in VLC's 'Media > Open disc...' - use Ctrl+D for that.

    @rLsj, if you like, you could find out how theirs works by searching through VLC's source code, available here.
    https://www.videolan.org/vlc/download-sources.html

    Being Open Source and covered by the GPL you're free to use their code.
      My ComputersSystem Spec


  • Posts : 577
    Windows 10 Home x64 Version 1709 (OS Build 16299.371)
       4 Weeks Ago #8

    Bree said: View Post
    Glad to hear that, but I can't take credit for the example (that goes to Microsoft) just for knowing where to look for one
    What sets you apart is: You know where to look and what to look for!
      My ComputersSystem Spec


  •  

    Related Threads
    Windows 10 is showing one of my drives as non-removable/fixed. I need it to show as a removable drive. I don't mind making registry changes or whatever needs to be done. I've found information on going the other way (have removable drive show...
    I'm setting up a Toshiba Encore 2 Tablet for a family member. I've already updated it to run 32-bit Windows 10 Pro 1607. It came with a very small amount of internal EMCC storage (24 GB available, NTFS formatted), but fortunately it also includes a...
    So, as soon as I boot my computer, any CD on the drive will work normally. If I eject and try to put any other CD or the same CD, it stops working. It only works one time, until I reboot. Tried all the fixes, going to the registry and deleting and...
    There is no drive assigned. I wonder what it refers to? I've checked the registry but can't figure it out. How would I remove it? 63548
    Issues with ejecting USB in General Support
    Is it just me or is there a known bug involving ejecting USB drives? Typically in 7 or 8 if I plug in my USB then transfer a file and close the window, I can easily eject it with no problems. With Windows 10 it seems like it holds the USB hostage...
    Our Sites
    Site Links
    About Us
    Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 04:03.
    Find Us