Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application copies files to DVD. I want to prevent DVD eject during this copy.

What I have tried:

Used DeviceIoControl( hVolume,IOCTL_STORAGE_EJECTION_CONTROL,&PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),NULL, 0,&dwBytesReturned, NULL);

The lock works fine until the first file is copied. After copying of first file i am able to eject the DVD. I want the lock to work until all files are copied

Is this a bug in DeviceIoControl() API? Any workarounds?
Posted
Updated 5-Mar-18 20:28pm
v2

1 solution

think you are missing one parameter to pass full stack of files so that writing control would no there are files to be written in the stack so it wont eject the CD as soon as first completes

however this may help to understand the function's parameters well

Call CreateFile with GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, and OPEN_EXISTING. The lpFileName parameter should be \.\X: (where X is the real drive letter). All other parameters can be zero.

Lock the volume by issuing the FSCTL_LOCK_VOLUME IOCTL via DeviceIoControl. If any other application or the system is using the volume, this IOCTL fails. Once this function returns successfully, the application is guaranteed that the volume is not used by anything else in the system.

Dismount the volume by issuing the FSCTL_DISMOUNT_VOLUME IOCTL. This causes the file system to remove all knowledge of the volume and to discard any internal information that it keeps regarding the volume.

Make sure the media can be removed by issuing the IOCTL_STORAGE_MEDIA_REMOVAL IOCTL. Set the PreventMediaRemoval member of the PREVENT_MEDIA_REMOVAL structure to FALSE before calling this IOCTL. This stops the device from preventing the removal of the media.

Eject the media with the IOCTL_STORAGE_EJECT_MEDIA IOCTL. If the device doesn't allow automatic ejection, then IOCTL_STORAGE_EJECT_MEDIA can be skipped and the user can be 
instructed to remove the media.

Close the volume handle obtained in the first step or issue the FSCTL_UNLOCK_VOLUME IOCTL. This allows the drive to be used by other processes.


hope your code syntax is like this -

C++
bResult = DeviceIoControl(hDevice,                       // device to be queried
                            IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
                            NULL, 0,                       // no input buffer
                            pdg, sizeof(*pdg),            // output buffer
                            &junk,                         // # bytes returned
                            (LPOVERLAPPED) NULL);          // synchronous I/O\
 
Share this answer
 
Comments
dilsdgr8 6-Mar-18 2:26am    
Thanks for the reply.
My implementation is same as mentioned. The drive is locked until the first file is copied. That is before file copying starts, drive is locked and cant be ejected.
Suppose i need to copy 3 files.
I locked the drive using IOCTL_STORAGE_MEDIA_REMOVAL IOCTL
Tried to eject disc.. Cant eject driove is locked.
Copy first file.. Tried to eject the disc,.. Disc is ejected
RDBurmon 6-Mar-18 3:18am    
Are you trying the drive eject from code or manually?
dilsdgr8 6-Mar-18 4:32am    
iam trying to eject manually.
My program copy files to disc.
I want to prevent media eject while the files are being copied.
But if I try to eject the disc after copying of first file, the lock is lost.

By code is as below:

1) Lock drive eject using DeviceIOControl() API IOCTL_STORAGE_EJECT_MEDIA IOCT
2) Copy first file
3) Copy second file.
..
4) Unlock drive using IOCTL_STORAGE_EJECT_MEDIA IOCT

But unfortunately iam able to eject DVD disc after copy of first file. I want to avoid this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900