Ejecting Removable Disk


  1. Posts : 25
    Windows 10
       #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 Computer


  2. Posts : 31,470
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #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 Computers


  3. Posts : 25
    Windows 10
    Thread Starter
       #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 Computer


  4. Posts : 31,470
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #4

    rLsj said:
    ...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 Computers


  5. Posts : 25
    Windows 10
    Thread Starter
       #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 Computer


  6. Posts : 13,896
    Win10 Version 22H2 Pro and Home, Win11 Pro and Home
       #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 Computers


  7. Posts : 31,470
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #7

    Berton said:
    ...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 Computers


  8. Posts : 1,097
    Windows 10 Home x64 Version 1809 (OS Build 17763.437)
       #8

    Bree said:
    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 Computer


 

  Related Discussions
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 17:10.
Find Us




Windows 10 Forums