📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hDeviceHandle: Thandle;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
const
BytesPerSector=512;
SectorCount=1;
SectorStart=0;
drive='\\.\C:';
var
str:string;
p:pchar;
i:Cardinal;
begin
hDeviceHandle := CreateFile(drive, GENERIC_ALL, //如果只是读扇区,可以用GENERIC_READ
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
begin
p:=allocmem(SectorCount*BytesPerSector);
FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);
if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
raise exception.create('Read错误');
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(hDevicehandle,SectorStart*BytesPerSector,0);
if FileWrite(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
raise exception.create('Write错误%d');
freemem(p,SectorCount*BytesPerSector);
closehandle(hDeviceHandle);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
BytesPerSector=512;
SectorCount=1;
SectorStart=0;
drive='\\.\PHYSICALDRIVE0';
var
str:string;
p:pchar;
i:Cardinal;
begin
hDeviceHandle := CreateFile(drive, GENERIC_ALL, //如果只是读扇区,可以用GENERIC_READ
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
begin
p:=allocmem(SectorCount*BytesPerSector);
FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);
if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
raise exception.create('Read错误');
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(hDevicehandle,SectorStart*BytesPerSector,0);
if FileWrite(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
raise exception.create('Write错误%d');
freemem(p,SectorCount*BytesPerSector);
closehandle(hDeviceHandle);
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -