我用VC编写代码,用DeviceIoControl函数,能实现对硬盘的直接读写吗?如何做呢?
解决方案 »
- 虚拟机的ip问题
- tcp/ip通讯c/s模式其中一个客户端如何通过服务端发给指定的客户端?
- 打包:csocket+mysql
- 帮忙看一下
- 用VC遇到问题,请问大家碰到过没
- 【新人广告】本年度CSDN最佳新人Service Unavailable err先生 (删号也要说
- 关于清空CListBox的问题
- 如何去掉CPropertySheet中的那个“帮助”按钮?
- 很急,也很简单的打印问题
- 求助!遍历一个文件夹下的txt文件,并可以随机的读取一篇txt的内容出来。赋值给一个字符串保存,
- 程序呼叫,並且通過我的程序,去響應另外一個被呼叫程序的介面事件...可以實現麼..如何解決?
- 我想在文件属性对话框中增加自己的属性页,该怎么做,我是不吝啬点数的
步骤:
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;
}