www.pudn.com > ReadDriver.rar > Unit1.pas, change:2007-09-14,size:9686b


unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, RzButton, RzLabel, Mask, RzEdit, ExtCtrls, RzPanel; 
 
type 
  TForm1 = class(TForm) 
    RzPanel1: TRzPanel; 
    RzBitBtn1: TRzBitBtn; 
    RzBitBtn2: TRzBitBtn; 
    Driver_Edit: TRzEdit; 
    RzLabel1: TRzLabel; 
    File_Edit: TRzEdit; 
    RzLabel2: TRzLabel; 
    RzPanel2: TRzPanel; 
    Memo: TRzMemo; 
    ROpen_Btn: TRzBitBtn; 
    Open: TOpenDialog; 
    RzLabel3: TRzLabel; 
    WOpen_Btn: TRzBitBtn; 
    Driver2_Edit: TRzEdit; 
    ROpen2_Btn: TRzBitBtn; 
    RzBitBtn3: TRzBitBtn; 
    procedure RzBitBtn1Click(Sender: TObject); 
    procedure ROpen_BtnClick(Sender: TObject); 
    procedure WOpen_BtnClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure RzBitBtn2Click(Sender: TObject); 
    procedure ROpen2_BtnClick(Sender: TObject); 
    procedure RzBitBtn3Click(Sender: TObject); 
  private 
    { Private declarations } 
    Function iReadBuf(iDriver: String): String; 
    Function iWriteBuf(iDriver, iTxt: String): Boolean; 
    Function iUnite(iDriver1,iDriver2,iToDriver: String): Boolean; 
  public 
    { Public declarations } 
    iPath: String; 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  iPath := ExtractFilePath(application.ExeName);//得到系统路径 
  Driver_Edit.Text := iPath + 'nengbang.iso'; 
end; 
 
Function TForm1.iReadBuf(iDriver: String): String; 
var 
  Buf: array [0..511] of Byte;     //数据缓冲区 
  ShowText, TmpStr: string; 
  FileHandle: THandle; 
  ReadCount, i: Cardinal; 
  FileLength, Highsize: Dword; 
  j: Integer; 
  a, b: Integer; 
  iText : String; 
begin 
  //打开磁盘 
  FileHandle := CreateFile(pChar(iDriver), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, 
                           nil, OPEN_EXISTING, 0, 0); 
  if FileHandle = INVALID_HANDLE_VALUE then 
  begin 
    MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK); 
    ExitProcess(0); 
  end; 
  //读出引导扇区数据 
  iText := ''; 
  Filelength := GetFilesize(FileHandle,@Highsize); 
  a := Filelength div 512; 
  b := Filelength mod 512; 
//  For j := 0 to a - 1 do 
//  begin 
//    SetFilePointer(FileHandle,j * iBit,nil,fmOpenRead); 
    if ReadFile(FileHandle, Buf, 512, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(Buf) * 3); 
      //将数据转为字串 
      for i := Low(Buf) to High(Buf) do 
      begin 
        TmpStr := Format('%2.2x ', [Buf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
    iText := iText + ShowText; 
//  end; 
{  if b <> 0 then 
  begin 
    if ReadFile(FileHandle, Buf, b, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(Buf) * 3); 
      //将数据转为字串 
      for i := Low(Buf) to High(Buf) do 
      begin 
        TmpStr := Format('%2.2x ', [Buf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
    iText := iText + ShowText; 
  end;  } 
  Result := iText; 
  CloseHandle(FileHandle); 
end; 
 
Function TForm1.iUnite(iDriver1,iDriver2,iToDriver: String): Boolean; 
var 
  RBuf,WBuf: array [0..1023] of Byte;     //数据缓冲区 
  ShowText, TmpStr: string; 
  RFileHandle, WFileHandle: THandle; 
  ReadCount, WriteCount, i: Cardinal; 
  FileLength, Highsize: Dword; 
  j: Integer; 
  a, b: Integer; 
  wi,wj,wk: Integer; 
begin 
  //打开磁盘 
  RFileHandle := CreateFile(pChar(iDriver1), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, 
                           nil, OPEN_EXISTING, 0, 0); 
  WFileHandle := CreateFile(pChar(iToDriver), GENERIC_READ or GENERIC_WRITE, 
                           0, nil, OPEN_EXISTING, 0, 0); 
  if RFileHandle = INVALID_HANDLE_VALUE then 
  begin 
    MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK); 
    ExitProcess(0); 
  end; 
  //读出引导扇区数据 
  Filelength := GetFilesize(RFileHandle,@Highsize); 
  a := Filelength div 1024; 
  b := Filelength mod 1024; 
  For j := 0 to a - 1 do 
  begin 
    if ReadFile(RFileHandle, RBuf, 1024, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(RBuf) * 3); 
      //将数据转为字串 
      for i := Low(RBuf) to High(RBuf) do 
      begin 
        TmpStr := Format('%2.2x ', [RBuf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
 
    wk := 0; 
    wj := 0; 
    For wi := 1 to Length(ShowText) do 
    begin 
      if Copy(ShowText,wi,1) = ' ' then 
      begin 
        WBuf[wk] := StrtoInt('$' + Copy(ShowText,wj + 1,2)); 
        wj := wi; 
        wk := wk + 1; 
      end; 
    end; 
    WriteFile(WFileHandle, WBuf, 1024, WriteCount, nil); 
 
  end; 
  if b <> 0 then 
  begin 
    if ReadFile(RFileHandle, RBuf, b, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(RBuf) * 3); 
      //将数据转为字串 
      for i := Low(RBuf) to High(RBuf) do 
      begin 
        TmpStr := Format('%2.2x ', [RBuf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
    wk := 0; 
    wj := 0; 
    For wi := 1 to Length(ShowText) do 
    begin 
      if Copy(ShowText,wi,1) = ' ' then 
      begin 
        WBuf[wk] := StrtoInt('$' + Copy(ShowText,wj + 1,2)); 
        wj := wi; 
        wk := wk + 1; 
      end; 
    end; 
    WriteFile(WFileHandle, WBuf, 1024, WriteCount, nil); 
  end; 
 
  RFileHandle := CreateFile(pChar(iDriver2), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, 
                           nil, OPEN_EXISTING, 0, 0); 
  if RFileHandle = INVALID_HANDLE_VALUE then 
  begin 
    MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK); 
    ExitProcess(0); 
  end; 
  //读出引导扇区数据 
  Filelength := GetFilesize(RFileHandle,@Highsize); 
  a := Filelength div 1024; 
  b := Filelength mod 1024; 
  For j := 0 to a - 1 do 
  begin 
    if ReadFile(RFileHandle, RBuf, 1024, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(RBuf) * 3); 
      //将数据转为字串 
      for i := Low(RBuf) to High(RBuf) do 
      begin 
        TmpStr := Format('%2.2x ', [RBuf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
 
    wk := 0; 
    wj := 0; 
    For wi := 1 to Length(ShowText) do 
    begin 
      if Copy(ShowText,wi,1) = ' ' then 
      begin 
        WBuf[wk] := StrtoInt('$' + Copy(ShowText,wj + 1,2)); 
        wj := wi; 
        wk := wk + 1; 
      end; 
    end; 
    WriteFile(WFileHandle, WBuf, 1024, WriteCount, nil); 
 
  end; 
  if b <> 0 then 
  begin 
    if ReadFile(RFileHandle, RBuf, b, ReadCount, nil) then 
    begin 
      SetLength(ShowText, Length(RBuf) * 3); 
      //将数据转为字串 
      for i := Low(RBuf) to High(RBuf) do 
      begin 
        TmpStr := Format('%2.2x ', [RBuf[i]]); 
        CopyMemory(@ShowText[i*3+1], @TmpStr[1], 3); 
      end; 
    end; 
    wk := 0; 
    wj := 0; 
    For wi := 1 to Length(ShowText) do 
    begin 
      if Copy(ShowText,wi,1) = ' ' then 
      begin 
        WBuf[wk] := StrtoInt('$' + Copy(ShowText,wj + 1,2)); 
        wj := wi; 
        wk := wk + 1; 
      end; 
    end; 
    WriteFile(WFileHandle, WBuf, 1024, WriteCount, nil); 
  end; 
 
  Result := True; 
  CloseHandle(RFileHandle); 
  CloseHandle(WFileHandle); 
end; 
 
Function TForm1.iWriteBuf(iDriver, iTxt: String): Boolean; 
var 
  Buf: array [0..511] of Byte;     //数据缓冲区 
  FileHandle: THandle; 
  WriteCount: Cardinal; 
  i, j, k: Integer; 
begin 
  //以可读可写方式打开磁盘 
  FileHandle := CreateFile(pChar(iDriver), GENERIC_READ or GENERIC_WRITE, 
                           0, nil, OPEN_EXISTING, 0, 0); 
  if FileHandle = INVALID_HANDLE_VALUE then 
  begin 
    MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK); 
    ExitProcess(0); 
  end; 
  k := 0; 
  j := 0; 
  For i := 1 to Length(iTxt) do 
  begin 
    if Copy(iTxt,i,1) = ' ' then 
    begin 
      Buf[k] := StrtoInt('$' + Copy(iTxt,j + 1,2)); 
      j := i; 
      k := k + 1; 
    end; 
  end; 
  if WriteFile(FileHandle, Buf, 512, WriteCount, nil) then 
    Result := True 
  else 
    Result := False; 
  CloseHandle(FileHandle); 
end; 
 
procedure TForm1.ROpen_BtnClick(Sender: TObject); 
begin 
  if Open.Execute then 
    Driver_Edit.Text := Trim(Open.FileName); 
end; 
 
procedure TForm1.ROpen2_BtnClick(Sender: TObject); 
begin 
  if Open.Execute then 
    Driver2_Edit.Text := Trim(Open.FileName); 
end; 
 
procedure TForm1.WOpen_BtnClick(Sender: TObject); 
begin 
  if Open.Execute then 
    File_Edit.Text := Trim(Open.FileName); 
end; 
 
procedure TForm1.RzBitBtn1Click(Sender: TObject); 
var 
  iDriver: String; 
begin 
  Try 
    RzBitBtn1.Enabled := False; 
    RzBitBtn1.Caption := '正在读取...'; 
    RzBitBtn1.Repaint; 
    iDriver := Trim(Driver_Edit.Text); 
    Memo.Clear; 
    Memo.Text := iReadBuf(iDriver); 
  Finally 
    RzBitBtn1.Enabled := True; 
    RzBitBtn1.Caption := '读'; 
  end; 
end; 
 
procedure TForm1.RzBitBtn2Click(Sender: TObject); 
var 
  iDriver, iFile, iTxt: String; 
begin 
  iDriver := Trim(Driver_Edit.Text); 
  iFile := Trim(File_Edit.Text); 
  iTxt := iReadBuf(iFile); 
  if iWriteBuf(iDriver, iTxt) then 
  begin 
    Memo.Clear; 
    Memo.Text := iReadBuf(iDriver); 
  end; 
  Showmessage('OK'); 
end; 
 
procedure TForm1.RzBitBtn3Click(Sender: TObject); 
var 
  iDriver1,iDriver2,iToDriver: String; 
begin 
  Try 
    RzBitBtn3.Enabled := False; 
    RzBitBtn3.Repaint; 
    iDriver1 := Trim(Driver_Edit.Text); 
    iDriver2 := Trim(Driver2_Edit.Text); 
    iToDriver := Trim(File_Edit.Text); 
    if iUnite(iDriver1,iDriver2,iToDriver) then 
      Showmessage('OK'); 
  Finally 
    RzBitBtn3.Enabled := True; 
  end; 
end; 
 
end.