How to determine if a CD is in CD-ROM drive
I was wondering if there is a Windows API that can tell me if there is a empty CD is inside a CD-ROM/DVD-Rom drive.
I've already looked at GetVolumeInformation(), but that only brings back information on the actual CD-ROM drive, not the media that is inside it, if there's even a CD in it or not.
All I'开发者_Go百科m trying to do is see if there's a cd in the drive or not and return a boolean value.
Thanks for any help
Call GetFileAttributes()
on the root directory. GetFileAttributes()
is quite optimized as it's commonly used as a check whether a file or directory exists.
Another method is to call GetDiskFreeSpace
, which (despite its name) also returns the total disk size.
The Shell also has some useful functions. SHGetDriveMedia
will tell you what's loaded, but you should first call GetDriveType()
.
The API you're looking for is most probably IMAPI.
I think this article here could be of help for you http://msdn.microsoft.com/en-us/magazine/cc163992.aspx
You can use DeviceIoControl API passing IOCTL_STORAGE_CHECK_VERIFY as the dwIoControlCode. This will check if a particular disc drive is ready i. e. has a disc in it and, of course, the tray is closed. Check the DeviceIoControl MSDN documentation for more info on this function.
// FROM VISTA to ....
BOOL CDYesNo(WCHAR letter) {// like F
DWORD pdwMediaContent=0;
BOOL ret =1;
WCHAR catw[3]; // create wchar string
catw[0]=letter;
catw[1]=L':';
catw[2]=NULL; `enter code here`
HRESULT rcd=SHGetDriveMedia( catw, &pdwMediaContent);
//rcd normally is S_OK, maybe tested.
if(pdwMediaContent ==0) ret=0;
return ret; // 0=empty
}
精彩评论