📄 unit2.pas
字号:
unit Unit2;
interface
uses
Windows,Controls, Classes,Unit1 ,SysUtils;
type
mmcread = class(TThread)
private
{ Private declarations }
procedure updatemm ;
protected
procedure Execute; override;
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure mmcread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ mmcread }
var
sss:string;
procedure mmcread.Execute;
var
I: Integer;
j:integer;
hdiskhand2:THandle;
hfilehand:THandle;
errorcount:integer;//出现错误重试次数;连续三次则不读取跳过
pbuff:pchar;
// sss:string;
begin
{ Place thread code here }
QSectorCount := 1; //读写扇区数
QSectorStart := 0; //起始扇区数
// pbuff := allocmem(QSectorCount * QBytesPerSector); // 必须是新申请的内存或全局变量,不能是局部变量
Hdiskhand2 := CreateFile(Qdiskname,
GENERIC_ALL,
FILE_SHARE_READ ,
nil,
OPEN_EXISTING,
0,
0 );
if (Hdiskhand2 = INVALID_HANDLE_VALUE) then
begin
sss:= '创建设备错误:'+SysErrorMessage(GetLastError);
Synchronize(updatemm);
exit;
end;
sss:= '创建设备成功:'+SysErrorMessage(GetLastError);
Synchronize(updatemm);
hfilehand := FileOpen(filename1, fmOpenwrite);
FileSeek(hfilehand,0,0);
QSectorCount := sectonum; //读写扇区数
QSectorStart := 0; //起始扇区数
pbuff := allocmem(QSectorCount * QBytesPerSector);
for I := 0 to ((sector_file_end-sector_file_start+1)div sectonum) do // 读取指定扇区
begin
QSectorCount := sectonum; //读写扇区数
QSectorStart := I*sectonum +sector_file_start; //起始扇区数
for j := 0 to QSectorCount * QBytesPerSector-1 do // Iterate
begin
pbuff[j]:=char(0);
end; // for
if ((sector_file_end-sector_file_start+1) div sectonum )=i then
begin
if ((sector_file_end-sector_file_start+1) mod sectonum)=0 then
break;
QSectorCount := ((sector_file_end-sector_file_start+1) mod sectonum) ;
end;
if ((sector_file_end-sector_file_start+1) div sectonum )>i then
QSectorCount := sectonum ;
// pbuff := allocmem(QSectorCount * QBytesPerSector);
if ( FileSeek(Hdiskhand2, QSectorStart * QBytesPerSector, 0)=-1)
then
begin
sss:='文件指针定位错误'+SysErrorMessage(GetLastError); //起始扇区
Synchronize(updatemm);
exit;
end else //inttostr( QSectorStart ) QSectorCount
begin
sss:='起始扇区号:'+Format('%-.8d %-.8x', [QSectorStart,QSectorStart]) +' 扇区数:'+Format('%-8d', [QSectorCount])+'定位'+SysErrorMessage(GetLastError);
end;
if FileRead(Hdiskhand2, pbuff[0], QSectorCount * QBytesPerSector) <> QSectorCount * QBytesPerSector then//读扇区
begin
sss:= '文件读取错误 非整扇区'+SysErrorMessage(GetLastError);
Synchronize(updatemm);
raise Exception.Create('Read Error!');
exit;
end else
begin
sss:= sss +' 读取'+ SysErrorMessage(GetLastError);
Synchronize(updatemm);
end;
// 写入文件
if ( FileSeek(hfilehand, QSectorStart * QBytesPerSector, 0)=-1)
then
begin
sss:='文件指针定位错误'+SysErrorMessage(GetLastError); //起始扇区
Synchronize(updatemm);
exit;
end else //inttostr( QSectorStart ) QSectorCount
begin
sss:='起始扇区号:'+Format('%-.8d %-.8x', [QSectorStart,QSectorStart]) +' 扇区数:'+Format('%-8d', [QSectorCount])+'定位'+SysErrorMessage(GetLastError);
end;
if Filewrite(hfilehand, pbuff[0], QSectorCount * QBytesPerSector) <> QSectorCount * QBytesPerSector then//读扇区
begin
sss:= '文件写入错误 非整扇区'+SysErrorMessage(GetLastError);
Synchronize(updatemm);
raise Exception.Create('Read Error!');
exit;
end else
begin
sss:=sss+ ' 写入文件'+ SysErrorMessage(GetLastError);
Synchronize(updatemm);
end;
end; //end for
//输出状态信息
//保存数据
Closehandle(Hdiskhand2);
FreeMem(pbuff); // 使用完后需要释放内存
FileClose(hfilehand);
end;
procedure mmcread.updatemm;
begin
form1.Memo2.Lines.Add(sss) ;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -