读取光驱上的指定扇区
来源:互联网 发布:php csrf攻击 编辑:程序博客网 时间:2024/04/30 02:54
本例子是读取光驱上的指定扇区(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;
}
- 读取光驱上的指定扇区
- linux读取硬盘指定扇区
- 读取硬盘扇区的数据
- 读取软盘逻辑扇区的汇编过程
- 磁盘扇区数据的读取和写入
- 按扇区读取Windows Mobile的数据
- 按扇区读取Windows Mobile的数据
- Linux中如何读写硬盘上指定物理扇区
- Linux中如何读写硬盘上指定物理扇区
- 读取光驱driver信息 的方法
- 读取盘序列号(是物理序列号,不是硬盘扇区上的序列号)在win7下的确不能用了
- 锁定光驱在Win2000上的实现
- C++读取磁盘扇区
- win32读取raw扇区
- SQL Server 从硬盘上读取指定文件的方法
- 通过CreateFile来读取磁盘扇区的方法
- 读取硬盘的第一扇区数据(MBR+DPT+Signature)
- FindFirstStore函数 按扇区读取Windows Mobile的数据
- 大容量硬盘的读写操作
- 滤镜效果--鼠标提示
- 基于java的InputStream.read(byte[] b,int off,int len)算法学习
- 专访用自己姓氏命名编译器YC++的创始人
- Java libraries to read and write PDF files
- 读取光驱上的指定扇区
- 数据传递的郁闷
- cfs之lustre实验
- Socket通讯及底层TCP/IP连接原语
- 编写可靠的Windows CE代码
- Struts 返回前一页面时,图片Css样式无法加载的问题
- 不能访问某一网站
- rs.open sql,conn,1,1全接触
- X86,ARM的虚拟地址转换