ring3级下直接读写硬盘

C代码

#include  < windows.h >
#include 
< stdio.h >

#pragma  comment(lib, "user32.lib");
#pragma  comment(lib, "kernel32.lib");

const  UINT uSectorSize   =   512  ;
const  UINT uBegSector   =   0  ;
const  UINT uSectorNum   =   1  ;

const  UINT uReadSize   =  uSectorSize  *  uSectorNum ;

BYTE bBuffer[uReadSize] 
=   {0}  ;
const   char  pDiskPath[]  =   " //./PHYSICALDRIVE0 "  ;

void  ShowByteInform ( PBYTE pBuf, UINT uSize )
{
  
for ( UINT i = 1 ; i <= uSize; i++ )
  
{
    printf ( 
" %02X", pBuf[i-1] ) ;
    
if ( i % 16 == 0 )
      printf ( 
" " ) ;
    
else if ( i % 8 == 0 )
      printf ( 
" " ) ;
  }

}


int  main()
{
  HANDLE hDisk ;
  __try 
{
    
    hDisk 
= CreateFile ( pDiskPath, GENERIC_READ, 
      FILE_SHARE_READ
|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 00 ) ;
    
if ( hDisk == INVALID_HANDLE_VALUE )
    
{
      MessageBox ( 
0"磁盘打开错误!"00 ) ;
      
return 0;
    }

    
    SetFilePointer ( hDisk, uBegSector 
* uSectorSize, 0, FILE_BEGIN ) ;
    DWORD dwReadByte ;
    ReadFile ( hDisk, (LPVOID)bBuffer, uReadSize, 
&dwReadByte, NULL ) ;
    
if ( dwReadByte == 0 )
    
{
      MessageBox ( 
0"磁盘读取错误!"00 ) ;
      
return 0;
    }

    
    ShowByteInform ( bBuffer, uReadSize ) ;
  }

  __finally 
{
    CloseHandle ( hDisk ) ;
  }

  
return 0;
}

 

Delphi代码

 

program Project1;

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

{$R *.RES}

const
  BytesPerSector 
= 512 ;
  SectorCount 
= 1 ;
  SectorStart 
= 0 ;
  drive 
= ' /.PHYSICALDRIVE0 ' ;

var
  str :
string ;
  p :pchar;
  i :Cardinal;
  hDeviceHandle :Thandle;

begin                        
  hDeviceHandle :
=  CreateFile(drive, GENERIC_READ,
    FILE_SHARE_READ OR FILE_SHARE_WRITE, nil, OPEN_EXISTING,
0 0 );
  
if  (hDeviceHandle  <>  INVALID_HANDLE_VALUE) then
  begin
    p:
= allocmem(SectorCount * BytesPerSector);

    FileSeek(hDevicehandle,SectorStart
* BytesPerSector, 0 );
    
if  FileRead(hDevicehandle,p[ 0 ],SectorCount * BytesPerSector) <> SectorCount * BytesPerSector then
       raise exception.create(
' 读取错误 ' );

    str:
= '' ;
    
for  i: = 0  to  512 - 1   do
    begin
      
if  i mod  16 = 0  then
      str:
= str + format( ' 0x%.8x  ' ,[i]);
      str:
= str + format( '   %.2x ' ,[integer(p[i])]);
      
if  i mod  16 = 15  then
      str:
= str + # 13 # 10 ;
    end;
    ShowMessage( str);

    freemem(p,SectorCount
* BytesPerSector);
    closehandle(hDeviceHandle);
  end;
end.
{END}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值