I have written a small piece of code to open/close a cd/dvd drive tray using the DeviceIoControl API and the proper IOCTL but I don\'t seem to be able to check the tray stat
Well, the documentation for this API states that it returns whether there is a disk in the drive and nothing about whether the tray is open or not. The specific requirements as to which error code is returns is not documented. I get 21 in both situations too.
I reviewed the documentation and could not find any documented way to do what you asked. To most applications, whether or not the tray is open is not that interesting.
I could not find a way to read the status of the drive tray. Instead I'm checking the time it takes to execute the eject command as explain there, if it take more than 100ms for the eject to return I'm assuming we actually did eject the media. If the eject command completes too fast am then assuming the tray is opened and needs to be closed. It seems to do the trick to implement an open/close toggle function. Time will tell :)