// For the purposes of this sample, drive F: is the CD-ROM // drive. hCD = CreateFile ("\\\\.\\F:", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// If the CD-ROM drive was successfully opened, read sectors 16 // and 17 from it and write their contents out to a disk file. if (hCD != INVALID_HANDLE_VALUE) { DISK_GEOMETRY dgCDROM; PREVENT_MEDIA_REMOVAL pmrLockCDROM;
// Lock the compact disc in the CD-ROM drive to prevent accidental // removal while reading from it. pmrLockCDROM.PreventMediaRemoval = TRUE; //DeviceIoControl (hCD, IOCTL_CDROM_MEDIA_REMOVAL, DeviceIoControl (hCD, IOCTL_STORAGE_MEDIA_REMOVAL, &pmrLockCDROM, sizeof(pmrLockCDROM), NULL, 0, &dwNotUsed, NULL);
// Allocate buffer to hold sectors from compact disc. Note that // the buffer will be allocated on a sector boundary because the // allocation granularity is larger than the size of a sector on a // compact disk. lpSector = (unsigned char *)VirtualAlloc (NULL, dwSize, MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
// Move to 16th sector for something interesting to read. SetFilePointer (hCD, dgCDROM.BytesPerSector * 16, NULL, FILE_BEGIN);
// Read sectors from the compact disc and write them to a file. if (ReadFile (hCD, lpSector, dwSize, &dwNotUsed, NULL)) WriteFile (hFile, lpSector, dwSize, &dwNotUsed, NULL);
VirtualFree (lpSector, 0, MEM_RELEASE); }
// Unlock the disc in the CD-ROM drive. pmrLockCDROM.PreventMediaRemoval = FALSE; DeviceIoControl (hCD, IOCTL_STORAGE_MEDIA_REMOVAL, &pmrLockCDROM, sizeof(pmrLockCDROM), NULL, 0, &dwNotUsed, NULL);
HANDLE hDevice, // handle to device
DWORD dwIoControlCode, // operation
LPVOID lpInBuffer, // input data buffer
DWORD nInBufferSize, // size of input data buffer
LPVOID lpOutBuffer, // output data buffer
DWORD nOutBufferSize, // size of output data buffer
LPDWORD lpBytesReturned, // byte count
LPOVERLAPPED lpOverlapped // overlapped information
);下面的例子代码是在Window XP上调试通过的,希望对您有所帮助。本例子是读取光驱上的指定扇区(16,17两扇区)内容,如要读取软盘,在一开始CreateFile时候打开:"\\\\.\\A:"也可取得同样效果,但是如果是不可移动的drive如磁盘,则需要修改一些调用参数,代码中有显示):
步骤:
1 使用CreateFile来打开光驱drive
2 使用ReadFile或者ReadFileEx来读取扇区
3 使用CloseHandle来关闭光驱drive例子代码如下:
#include "stdafx.h"#include <windows.h>
#include <winioctl.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HANDLE hCD, hFile;
DWORD dwNotUsed;
// 存放读取出来的扇区内容的文件
hFile = CreateFile ("sector.txt",
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
// For the purposes of this sample, drive F: is the CD-ROM
// drive.
hCD = CreateFile ("\\\\.\\F:", GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
// If the CD-ROM drive was successfully opened, read sectors 16
// and 17 from it and write their contents out to a disk file.
if (hCD != INVALID_HANDLE_VALUE)
{
DISK_GEOMETRY dgCDROM;
PREVENT_MEDIA_REMOVAL pmrLockCDROM;
// Lock the compact disc in the CD-ROM drive to prevent accidental
// removal while reading from it.
pmrLockCDROM.PreventMediaRemoval = TRUE;
//DeviceIoControl (hCD, IOCTL_CDROM_MEDIA_REMOVAL,
DeviceIoControl (hCD, IOCTL_STORAGE_MEDIA_REMOVAL,
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
0, &dwNotUsed, NULL);
// Get sector size of compact disc
//if (DeviceIoControl (hCD, IOCTL_CDROM_GET_DRIVE_GEOMETRY,
if (DeviceIoControl (hCD, IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL, 0, &dgCDROM, sizeof(dgCDROM),
&dwNotUsed, NULL))
{
LPBYTE lpSector;
DWORD dwSize = 2 * dgCDROM.BytesPerSector; // 2 sectors
// Allocate buffer to hold sectors from compact disc. Note that
// the buffer will be allocated on a sector boundary because the
// allocation granularity is larger than the size of a sector on a
// compact disk.
lpSector = (unsigned char *)VirtualAlloc (NULL, dwSize,
MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
// Move to 16th sector for something interesting to read.
SetFilePointer (hCD, dgCDROM.BytesPerSector * 16,
NULL, FILE_BEGIN);
// Read sectors from the compact disc and write them to a file.
if (ReadFile (hCD, lpSector, dwSize, &dwNotUsed, NULL))
WriteFile (hFile, lpSector, dwSize, &dwNotUsed, NULL);
VirtualFree (lpSector, 0, MEM_RELEASE);
}
// Unlock the disc in the CD-ROM drive.
pmrLockCDROM.PreventMediaRemoval = FALSE;
DeviceIoControl (hCD, IOCTL_STORAGE_MEDIA_REMOVAL,
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
0, &dwNotUsed, NULL);
CloseHandle (hCD);
CloseHandle (hFile);
}
return 0;
}如果需要进一步的信息,您可以参看:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/hardware/devio_9quk.asp等有关信息。
- 微软全球技术中心 VC技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。