Windows CDROM Eject

后端 未结 5 851
遇见更好的自我
遇见更好的自我 2020-11-30 08:20

Does anyone know a method to programmatically close the CD tray on Windows 2000 or higher? Open CD tray exists, but I can\'t seem to make it close especially under W2k.

5条回答
  •  忘掉有多难
    2020-11-30 08:51

    I kind of like to use DeviceIOControl as it gives me the possibility to eject any kind of removable drive (such as USB and flash-disks as well as CD trays). Da codez to properly eject a disk using DeviceIOControl is (just add proper error-handling):

    bool ejectDisk(TCHAR driveLetter)
    {
      TCHAR tmp[10];
      _stprintf(tmp, _T("\\\\.\\%c:"), driveLetter);
      HANDLE handle = CreateFile(tmp, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
      DWORD bytes = 0;
      DeviceIoControl(handle, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &bytes, 0);
      DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &bytes, 0);
      DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, &bytes, 0);
      CloseHandle(handle);
      return true;
    }
    

提交回复
热议问题