C + + realizes CD catching and turning WAV

Source: Internet
Author: User
Tags bool function prototype

Now introduce C + + to achieve the CD grab WAV,CD to grasp the track of several ways, now one of the introduction. We can obtain the device handle through the API function CreateFile, and then use the API function DeviceIoControl to realize the access information of the device. This will also be used to wave file structure WaveFormatEx, and then read the information written to the file to generate wave format files.

We need to use the header files are: ntddcdrm.h (NTDDK development package) Winioctl.h Mmreg.h

1. Search Optical Drive

We can use GetDriveType to determine the type of equipment, 5 is the CDROM type. The return type can be reviewed in MSDN, which is described in detail.

2. Open the equipment

Use the CreateFile to get the device handle, as shown in the following example:

HANDLE m_hDevice;
  CString FileName=”F:”;
  m_hDevice =CreateFile("\\\\.\\"+FileName,  // 文件名路径
  GENERIC_READ,  // 读写方式
  FILE_SHARE_READ | FILE_SHARE_WRITE,  // 共享方式
  NULL,  // 默认的安全描述符
  OPEN_EXISTING,  // 创建方式
  0,  // 不需设置文件属性
  NULL);  // 不需参照模板文件

3. Read CD parameters

With the device handle, we can use DeviceIoControl to obtain the relevant information.

DeviceIoControl function Prototype:

BOOL DeviceIoControl(
  HANDLE hDevice,  // 设备句柄
  DWORD dwIoControlCode,  // 控制码
  LPVOID lpInBuffer,  // 输入数据缓冲区指针
  DWORD nInBufferSize,  // 输入数据缓冲区长度
  LPVOID lpOutBuffer,  // 输出数据缓冲区指针
  DWORD nOutBufferSize,  // 输出数据缓冲区长度
  LPDWORD lpBytesReturned,  // 输出数据实际长度单元长度
  LPOVERLAPPED lpOverlapped           // 重叠操作结构指针
  );

4. Get track

Output CDROM_TOC structure using IOCTL_CDROM_READ_TOC control code

BOOL bResult;
 DWORD dwOutBytes;
 CDROM_TOC CdromTOC;  //曲目信息结构,详细请看MSDN
 bResult=DeviceIoControl(m_hDevice,
       IOCTL_CDROM_READ_TOC,NULL,0,
       &CdromTOC,
       sizeof(CdromTOC),
       &dwOutBytes, 
       (LPOVERLAPPED)NULL);

5. Get the starting point of the track

DWORD CCdToWavDlg::GetStartSector(int track)
{
     return (CdromTOC.TrackData[track-1].Address[1]*60*75   +
         CdromTOC.TrackData[track-1].Address[2]*75 +
         CdromTOC.TrackData[track-1].Address[3])-150;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.