Home:ALL Converter>How to identify the type of disc in a CD-ROM drive using WinAPI?

How to identify the type of disc in a CD-ROM drive using WinAPI?

Ask Time:2014-04-11T19:45:25         Author:Daniel Kamil Kozar

Json Formatter

I'm writing an application that's going to work with audio CDs and mixed CDs. I would like to have a method of determining whether there is an audio or mixed-type (with at least one audio track) disc currently in the drive that the application uses.

So far, I was able to identify that the drive is a CD-ROM by GetDriveType. However, it turns out that identifying the media that is actually inside the drive is not that easy. This is what I've got so far :

int drive_has_audio_disc(const char *root_path)
{
  char volume_name[MAX_PATH+1];
  BOOL winapi_rv;
  DWORD fs_flags;
  int rv;

  winapi_rv = GetVolumeInformation(root_path, volume_name, sizeof(volume_name),
    NULL, NULL, &fs_flags, NULL, 0);
  if(winapi_rv != 0)
  {
    rv = (strcmp(volume_name, "Audio CD") == 0 &&
      (fs_flags & FILE_READ_ONLY_VOLUME));
  }
  else
  {
    rv = (GetLastError() == ERROR_INVALID_PARAMETER) ? 0 : -1;
  }
  return rv;
}

However, it relies on the fact that Windows assigns the name "Audio CD" to all discs that are recognized as audio. This doesn't feel right, and is going to fail miserably on mixed-mode CDs, since their name in Windows is determined by the volume name of the data track. Also, the else block is here because I've noticed that GetVolumeInformation returns an error with GetLastError equal to ERROR_INVALID_PARAMETER when there is no disc in the drive at all.

Ideally, I'm looking for something like the CDROM_DISC_STATUS ioctl present on Linux. It returns CDS_NO_INFO, CDS_AUDIO, CDS_MIXED, or some other values, depending on the contents of the disc.

Is there any other way of handling this? And what about mixed-mode discs?

Author:Daniel Kamil Kozar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23011697/how-to-identify-the-type-of-disc-in-a-cd-rom-drive-using-winapi
Igor Skochinsky :

You can use the CD-ROM I/O Control Codes, in particular the IOCTL_CDROM_READ_TOC. The structure it returns looks like this:\n\nstruct TRACK_DATA {\n UCHAR Reserved;\n UCHAR Control :4;\n UCHAR Adr :4;\n UCHAR TrackNumber;\n UCHAR Reserved1;\n UCHAR Address[4];\n} \n\nstruct CDROM_TOC {\n UCHAR Length[2];\n UCHAR FirstTrack;\n UCHAR LastTrack;\n TRACK_DATA TrackData[MAXIMUM_NUMBER_TRACKS];\n};\n\n\nYou can find an example of how to retrieve it on Larry Osterman's blog.\nFrom this you should be able to determine the exact disc type. If not, check out other IOCTLs, I'm sure there should be one that gives you the necessary info.",
2014-04-11T13:36:55
yy