CreateFile

来源:互联网 发布:浮云网淘宝小号 编辑:程序博客网 时间:2024/04/28 00:34
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, FileCtrl;type  TForm1 = class(TForm)    Button1: TButton;    DriveComboBox1: TDriveComboBox;    memosd: TMemo;    procedure Button1Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);Var    driver          : pchar;    block_size      : Integer;    start_num , total_num , num_tmp : Integer;    hdevicehandle   : thandle;    i , j , k       : Integer;    C               : Char;    fbuf            : pchar;    hex_str , char_str , tmp_str : String;    tem_id          : String;    trans_no_tmp    : String;    num             : String;    bu_count , trans_count : Integer;Begin    driver := pchar('\\.\' + DriveComboBox1.Drive + ':');    block_size := 512;    start_num := 0;    total_num := 1;    hdevicehandle := CreateFile(driver , GENERIC_ALL , FILE_SHARE_READ Or FILE_SHARE_WRITE , Nil , OPEN_EXISTING , 0 , 0);    If (hdevicehandle <> INVALID_HANDLE_VALUE) Then        Begin            fbuf := allocmem(total_num * block_size);            FileSeek(hdevicehandle , start_num * block_size , 0);            If FileRead(hdevicehandle , fbuf[0] , total_num * block_size) <> total_num * block_size Then                Raise exception.Create('读磁盘错误!');            char_str := strpas(fbuf);            memosd.Lines.Add('字符【' + char_str + '】');            memosd.Lines.Add('读取包头结束!');            freemem(fbuf);            closehandle(hdevicehandle);        End;    tmp_str := copy(char_str , 1 , 10);    If trystrtoint(tmp_str , bu_count) = false Then        Begin            memosd.Lines.Add('补货记录总条数不是合法的整数!');            exit;        End;    tmp_str := '';    tmp_str := copy(char_str , 11 , 10);    If trystrtoint(tmp_str , trans_count) = false Then        Begin            memosd.Lines.Add('交易记录总条数不是合法的整数!');            exit;        End;    tem_id := copy(char_str , 21 , 8);   // del_bu_tmp(tem_id);    //write_bu_con_tmp(tem_id , inttostr(bu_count) , '');    start_num := 1;    While start_num <= bu_count Do        Begin            hdevicehandle := CreateFile(driver , GENERIC_ALL , FILE_SHARE_READ Or FILE_SHARE_WRITE , Nil , OPEN_EXISTING , 0 , 0);            If (hdevicehandle <> INVALID_HANDLE_VALUE) Then                Begin                    fbuf := allocmem(block_size);                    FileSeek(hdevicehandle , start_num * block_size , 0);                    If FileRead(hdevicehandle , fbuf[0] , block_size) <> block_size Then                        Raise exception.Create('读磁盘错误!');                    char_str := strpas(fbuf);                    memosd.Lines.Add('字符【' + char_str + '】');                    memosd.Lines.Add('读取补货记录' + inttostr(start_num) + '结束!');                    freemem(fbuf);                    closehandle(hdevicehandle);                End;       //     do_sd(char_str , tmp_str);            start_num := start_num + 1;        End;   // i := bi_bu_mixi_sd(tem_id);    If i = 2 Then        Begin   //         write_bu_from_tmp(tem_id);        End;    char_str := '';    tmp_str := '';    start_num := 301;    del_trans_tmp(tem_id);    write_trans_con_tmp(tem_id , inttostr(trans_count) , '');    While start_num <= (trans_count * 3 + 300) Do        Begin            hdevicehandle := CreateFile(driver , GENERIC_ALL , FILE_SHARE_READ Or FILE_SHARE_WRITE , Nil , OPEN_EXISTING , 0 , 0);            If (hdevicehandle <> INVALID_HANDLE_VALUE) Then                Begin                    fbuf := allocmem(3 * block_size);                    FileSeek(hdevicehandle , start_num * block_size , 0);                    If FileRead(hdevicehandle , fbuf[0] , 3 * block_size) <> 3 * block_size Then                        Raise exception.Create('读磁盘错误!');                    char_str := strpas(fbuf);                    memosd.Lines.Add('字符【' + char_str + '】');                    memosd.Lines.Add('读取交易记录' + inttostr(start_num) + '结束!');                    freemem(fbuf);                    closehandle(hdevicehandle);                End;            do_sd(char_str , tmp_str);            start_num := start_num + 3;        End;    i := bi_trans_mixi_sd(tem_id);    If i = 2 Then        Begin            write_trans_from_tmp(tem_id);        End;    frmdatamu.Message1.Text := '读取SD卡结束!';    frmdatamu.Message1.ShowModal;End;end.

原创粉丝点击