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;
}