这个问题也就是关于修改磁盘数据的问题!!代码如下:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, MPHexEditor, MPHexEditorEx;type
  TForm1 = class(TForm)
    hexform: TMPHexEditorEx;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  hDeviceHandle: Thandle;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
  str,str1:string;
  p:pchar;
  i:Cardinal;
  j:integer;  BytesPerSector,SectorCount,SectorStart:Cardinal;
  drive:pchar;
  begin
  BytesPerSector:=512;
  SectorCount:=1;
  SectorStart:=4;
  drive:=pchar('\\.\C:');
//读扇区hDeviceHandle := CreateFile(drive, GENERIC_ALL, 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('Read错误');    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(hDevicehandle,SectorStart*BytesPerSector,0);
    if FileWrite(hDevicehandle,buffer[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
    raise exception.create('Write错误%d');    freemem(p,SectorCount*BytesPerSector);
    closehandle(hDeviceHandle);
  end;
end;end.现在的问题是把扇区的信息读出来,如何修改扇区的信息,然后再把修改的信息写入到磁盘中!!
例如:读出的扇区信息是(也就是显示扇区信息):
AD 12 32 15 1F 2D 34 3F 00 00 00 00 00 00 00 00
...............................................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
现在要把信息的最后两个字节改为:11 22,也就是:
AD 12 32 15 1F 2D 34 3F 00 00 00 00 00 00 00 00
...............................................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 22
然后再写入到磁盘中,如何做??
谢谢各位大侠了!!

解决方案 »

  1.   

    //非原创
    //Windows NT2000下读写物理逻辑扇区&
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      hDeviceHandle: Thandle;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    const
      BytesPerSector=512;
      SectorCount=1;
      SectorStart=0;
      drive='\\.\C:';
    var
      str:string;
      p:pchar;
      i:Cardinal;
    begin
      hDeviceHandle := CreateFile(drive, GENERIC_ALL,  //如果只是读扇区,可以用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('Read错误');    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(hDevicehandle,SectorStart*BytesPerSector,0);
        if FileWrite(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
           raise exception.create('Write错误%d');    freemem(p,SectorCount*BytesPerSector);
        closehandle(hDeviceHandle);
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    const
      BytesPerSector=512;
      SectorCount=1;
      SectorStart=0;
      drive='\\.\PHYSICALDRIVE0';
    var
      str:string;
      p:pchar;
      i:Cardinal;
    begin
      hDeviceHandle := CreateFile(drive, GENERIC_ALL,  //如果只是读扇区,可以用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('Read错误');    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(hDevicehandle,SectorStart*BytesPerSector,0);
        if FileWrite(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
           raise exception.create('Write错误%d');    freemem(p,SectorCount*BytesPerSector);
        closehandle(hDeviceHandle);
      end;
    end;end.
      

  2.   

    //非原创
    //Windows 9x下读写逻辑扇区的方法#
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
     TDiskIO=packed Record
        dwStartSector:longint;
        wSectors     :smallint;
        lpBuffer     :pchar;
     end;
     P32Regs = ^T32Regs; //32位寄存器结构
      T32Regs = record
        EBX: Longint;
        EDX: Longint;
        ECX: Longint;
        EAX: Longint;
        EDI: Longint;
        ESI: Longint;
        Flags: Longint;
      end;
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end; const
        FILE_FLAG_DELETE_ON_CLOSE=$04000000;
        VWIN32_DIOC_DOS_IOCTL = 1; { MS-DOS Int 21h 44xxh functions call }
        VWIN32_DIOC_DOS_INT25 = 2; { MS-DOS Int 25h function call }
        VWIN32_DIOC_DOS_INT26 = 3; { MS-DOS Int 26h function call }
        VWIN32_DIOC_DOS_INT13 = 4; { MS-DOS Int 13h functions call }
        VWIN32_DIOC_DOS_DRIVEINFO = 6; {MS-DOS Int 21h function 730X}
    var
      Form1: TForm1;
      buffer:TDiskio;
      hDeviceHandle:THandle;
      reg:T32Regs;implementation{$R *.DFM}
    function LockDisk(VMM32Handle:cardinal;disk:byte;LockOrNot:boolean):boolean;
    var
      R: T32Regs;
      cb: DWord;
    begin
      if (VMM32Handle=INVALID_HANDLE_VALUE)then
      begin
         result:=false;
         exit;
      end;
      fillchar(r, sizeof(r), 0);
      if LockOrNot=true then
      begin
         R.ECX := $084b;
         R.EBX := $100+disk; //bh:0-3级  0,1,$80,$81...
         R.EDX := 1;    //1允许写,0允许格式化
      end
      else begin
         R.ECX := $086b;
         R.EBX := disk;   //0,1,$80,$81...
      end;
      R.EAX := $440D;
      DeviceiOControl(VMM32Handle, VWIN32_DIOC_DOS_IOCTL, @R, SizeOf(R), @R, SizeOf(R), cb, nil);
      Result := (R.Flags and 1 = 0);
    end;function LockDrive(VMM32Handle:cardinal;drive:byte;LockOrNot:boolean):boolean;
    var
      R: T32Regs;
      cb: DWord;
    begin
      if (VMM32Handle=INVALID_HANDLE_VALUE)then
      begin
         result:=false;
         exit;
      end;
      fillchar(r, sizeof(r), 0);
      if LockOrNot=true then
      begin
         R.ECX := $084a;
         R.EBX := $100+drive; //bh:0-4级  0当前盘,1:A,2:B,3:C
         R.EDX := 1;    //1允许写,0允许格式化
      end
      else begin
         R.ECX := $086A;
         R.EBX := drive;   //0当前盘,1:A,2:B,3:C
      end;
      R.EAX := $440D;
      DeviceiOControl(VMM32Handle, VWIN32_DIOC_DOS_IOCTL, @R, SizeOf(R), @R, SizeOf(R), cb, nil);
      Result := (R.Flags and 1 = 0); //and (R.EAX and $FFFF = 0);
    end;procedure TForm1.Button1Click(Sender: TObject);
    const
       drive=3;  //c盘
    var
      cb:DWORD;
      str:string;
      i:integer;
      boot:array[0..512-1]of byte;
    begin
        hDeviceHandle:=CreateFile('\\.\VWIN32',0,0, nil,0, FILE_FLAG_DELETE_ON_CLOSE, 0);
        if(hDeviceHandle<>INVALID_HANDLE_VALUE) then
         begin
           buffer.dwStartSector:=0;
           buffer.wSectors:=1;
           buffer.lpBuffer:=@boot;
           reg.EAX:=$7305;
           reg.EBX:=Integer(@buffer);
           reg.ECX:=-1;
           reg.EDX:=drive;//1-A 2-b 3-c
           reg.ESI:=0;//读
           reg.Flags:=0;
           DeviceIoControl(hDeviceHandle,VWIN32_DIOC_DOS_DRIVEINFO,@reg,sizeof(reg),@reg,sizeof(reg),cb,nil);
           if ((reg.Flags and 1)=1) then
              raise exception.createfmt('错误代码:%.2x',[reg.EAX and $FFFF]);
        end;
        str:='';
        for i:=0 to buffer.wSectors*512-1 do
        begin
          str:=str+format('%.2x',[integer(boot[i])]);
          if i mod 16=15 then str:=str+#13;
        end;
        showmessage(str);
        LockDrive(hDeviceHandle,drive,true);
        if(hDeviceHandle<>INVALID_HANDLE_VALUE) then
         begin
           buffer.dwStartSector:=0;
           buffer.wSectors:=1;
           buffer.lpBuffer:=@boot;
           reg.EAX:=$7305;
           reg.EBX:=Integer(@buffer);
           reg.ECX:=-1;
           reg.EDX:=drive;//1-A 2-b 3-c
           reg.ESI:=1;//写
           reg.Flags:=0;
           DeviceIoControl(hDeviceHandle,VWIN32_DIOC_DOS_DRIVEINFO,@reg,sizeof(reg),@reg,sizeof(reg),cb,nil);
           if ((reg.Flags and 1)=1) then
              raise exception.createfmt('错误代码:%.2x',[reg.EAX and $FFFF]);
        end;
        LockDrive(hDeviceHandle,drive,false);
    end;end.