读取光驱上的指定扇区

来源:互联网 发布: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;  
  }

原创粉丝点击