CreateFile

unit Unit1;

interface

uses
  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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值