注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Win XP中震荡波后应采取的措施
Win 2000防毒从安装系统时开始
Win XP SP2拖放IE窗口可能引发黑
Linux系统下的病毒发展及其分类
用WinRAR解析木马病毒的捆绑原理
识破用WinRAR捆绑的木马
走近 WSH(Windows Scripting Hos
利用Windows控制台删除无法删除的
安全小知识:Rundll.exe是病毒吗?
用Windows自带工具打造“免检”木
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Delphi >> 文章正文
windows NT/2000读写物理,逻辑磁盘扇区
作者:meiqingsong  来源:csdn  点击:  更新:2006-12-19
简介:
procedure  TForm1.Button1Click(Sender:  TObject);  
var  
   hDriveHandle:  THandle;  
procedure  TForm1.Button1Click(Sender:  TObject);  
const  
   BytesPerSector  =  512;  
   SectorCount  =  1;    //读写扇区数  
   SectorStart  =  0;    //起始扇区数  
   drive  =  '\\.\C:';  //驱动盘  
var  
   str:  String;  
   p:  PChar;  
   i:  Cardinal;  
begin  
   hDriveHandle  :=  CreateFile(drive,  GENERIC_ALL,  FILE_SHARE_READ  or  FILE_SHARE_WRITE,  
             nil,  OPEN_EXISTING,  0,  0);  
   if  (hDriveHandle  <>  INVALID_HANDLE_VALUE)  then  
   begin  
       p  :=  allocmem(SectorCount  *  ByteSPerSector);//p  必须是新申请的内存或全局变量,不能是局部变量  
                                                                                               //定义为局部变量是不能读写磁盘的  
       FileSeek(hDriveHandle,  SectorStart  *  BytesPerSector,  0);  //起始扇区  
       if  FileRead(hDriveHandle,  p[0],  SectorCount  *  BytesperSector)<>  //读扇区  
           SectorCount  *  BytesperSector  then  
           raise  Exception.Create('Read  Error!');  
       str  :='';  
       for  i  :=  0  to  512  -  1  do  
       begin  
           str  :=  str  +  Format('%.2x',  [integer(p[i])]);  
           if  i  mod  16  =  15  then  
               str  :=  str  +  #13;  
       end;  
       ShowMessage(str);  
 
       FileSeek(hDriveHandle,  SectorStart  *  BytesPerSector,  0);  //起始扇区  
       if  FileWrite(hDriveHandle,  p[0],  SectorCount  *  BytesperSector)<>  //写扇区  
           SectorCount  *  BytesperSector  then  
           raise  Exception.Create('Write  Error!');  
 
       FreeMem(p,  SectorCount  *  BytesperSector);  
       Closehandle(hDriveHandle);  
   end;  
end;  
 
procedure  TForm1.Button2Click(Sender:  TObject);  
const  
   BytesPerSector  =  512;  
   SectorCount  =  1;    //读写扇区数  
   SectorStart  =  0;    //起始扇区数  
   drive  =  '\\.\PHYSICALDRIVE0';  //物理磁盘  
var  
   str:  String;  
   p:  PChar;  
   i:  Cardinal;  
begin  
   hDriveHandle  :=  CreateFile(drive,  GENERIC_ALL,  FILE_SHARE_READ  or  FILE_SHARE_WRITE,  
             nil,  OPEN_EXISTING,  0,  0);  
   if  (hDriveHandle  <>  INVALID_HANDLE_VALUE)  then  
   begin  
       p  :=  allocmem(SectorCount  *  ByteSPerSector);//p  必须是新申请的内存或全局变量,不能是局部变量  
                                                                                               //定义为局部变量是不能读写磁盘的  
       FileSeek(hDriveHandle,  SectorStart  *  BytesPerSector,  0);  //起始扇区  
       if  FileRead(hDriveHandle,  p[0],  SectorCount  *  BytesperSector)<>  //读扇区  
           SectorCount  *  BytesperSector  then  
           raise  Exception.Create('Read  Error!');  
       str  :='';  
       for  i  :=  0  to  512  -  1  do  
       begin  
           str  :=  str  +  Format('%.2x',  [integer(p[i])]);  
           if  i  mod  16  =  15  then  
               str  :=  str  +  #13;  
       end;  
       ShowMessage(str);  
 
       FileSeek(hDriveHandle,  SectorStart  *  BytesPerSector,  0);  //起始扇区  
       if  FileWrite(hDriveHandle,  p[0],  SectorCount  *  BytesperSector)<>  //写扇区  
           SectorCount  *  BytesperSector  then  
           raise  Exception.Create('Write  Error!');  
 
       FreeMem(p,  SectorCount  *  BytesperSector);  
       Closehandle(hDriveHandle);  
   end;  
end;






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务