1,要读取mbr数据,不能直接通过read之类的函数,不知道delphi可不可以直接读硬盘,如何读?
2,能不能随意指定要读取的硬盘扇区,怎么指定?唉,这问题有点变态,实在解决不了,只有在这里问高手了。分不够再加!~

解决方案 »

  1.   

    可以读,但应该不是用READ之类的函数
    可以写一段汇编程序来读。
      

  2.   

    我在debug里用int13读出来了,但在delphi里怎么调用int13呢?
      

  3.   

    windows NT/2000读写物理,逻辑磁盘扇区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;
      

  4.   

    函数确实一样但是
    CreateFile(drive, GENERIC_ALL, FILE_SHARE_READ or FILE_SHARE_WRITE,
           nil, OPEN_EXISTING, 0, 0);
    的参数drive不同
    drive = '\\.\C:'; //驱动盘
    drive = '\\.\PHYSICALDRIVE0'; //物理磁盘
      

  5.   

    http://vip.6to23.com/wenjinshan/ 8. Windows直接读写磁盘扇区的源代码,高技术含量!成功解决Windows下读写磁盘的问题,支持9x/NT/2000/XP,分逻辑磁盘扇区、物理磁盘扇区两种方式(见出版书籍的《Windows核心编程》一书)
      

  6.   

    TO:meiqingsong(阿飛)
        CreateFile()函数的参数是怎么设定的?我没有在delphi帮助上找到合适的说明啊。
        FileRead()读扇区的原理是什么?谢谢了~~
      

  7.   

    1.CreateFile()函数的参数是怎么设定的?
    --在delphi帮助上有,
      帮助 -〉windows sdk 中,查CreateFile,
      在  “找到主题“  的对话框中选择 CreateFile(win32 programmer's reference)
    2.windows NT/2000读写物理,逻辑磁盘扇区的关键不在于FileRead(),
    而在于用 CreateFile()获得物理,逻辑磁盘扇区的句柄