Привет всем! Я новичок в delphi, по этому прошу сильно не ругать за данную тему. Решил написать программу поиска ext2 разделов на винте. Вот код, но почему он не работает непонятно.
Код:
unit BootSector;
interface
uses Windows;
type
TPartitionEntry = packed record
BootIndicator: BYTE;
StartHead: BYTE;
StSectCyl: BYTE;
StCyl: BYTE;
BootID: BYTE;
EndHead: BYTE;
EndSectCyl: BYTE;
EndCyl: BYTE;
StartPartition: DWORD;
PartitionSize: DWORD;
end;
TExt2Partitions = packed record
PartCount: BYTE;
StartPart: array [0..25] of Int64;
_end: Boolean;
end;
TPartitionTable = packed array [0..3] of TPartitionEntry;
function _SetFilePointer(hFile:Thandle; _Sectors: DWORD; dwMoveMethod: DWORD):Boolean;
function ReadMBRPartitionTable: TExt2Partitions;
procedure ReadChainOfPartTable(_ext2:TExt2Partitions; _sector: DWORD);
implementation
function _SetFilePointer(hFile:Thandle; _Sectors: DWORD; dwMoveMethod: DWORD):Boolean;
var
i:DWORD;
begin
Result:=True;
if hFile <> INVALID_HANDLE_VALUE then
begin
if _Sectors = 0 then
begin
Result:=False;
end
else
begin
case dwMoveMethod of
FILE_BEGIN: begin
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
for i:=0 to _Sectors-1 do
begin
SetFilePointer(hFile, 512, nil, FILE_CURRENT);
end;
end;
FILE_CURRENT: begin
for i:=0 to _Sectors-1 do
begin
SetFilePointer(hFile, 512, nil, FILE_CURRENT);
end;
end;
else
Result:=False;
end;
end;
end
else
begin
Result:=False;
end;
end;
function ReadMBRPartitionTable: TExt2Partitions;
var
hDisk:THandle;
Table:TPartitionTable;
tmp:Cardinal;
i:Integer;
begin
hDisk:=CreateFile(PChar('\\.\PHYSICALDRIVE0'), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if hDisk <> INVALID_HANDLE_VALUE then
begin
SetFilePointer(hDisk, $1BE, nil, FILE_BEGIN);
ReadFile(hDisk, Table, SizeOf(TPartitionTable), tmp, nil);
if tmp = SizeOf(TPartitionTable) then
begin
for i:=0 to 3 do
begin
case Table[i].BootID of
$83: begin
inc(Result.PartCount);
Result.StartPart[Result.PartCount-1]:=Table[i].StartPartition;
Result._end:= True;
end;
$05: begin
ReadChainOfPartTable(Result, Table[i].StartPartition);
end;
$0F: begin
ReadChainOfPartTable(Result, Table[i].StartPartition);
end;
else
Result._end:=False;
end;
end;
end
else
begin
Result._end:=false;
CloseHandle(hDisk);
end;
end
else
begin
Result._end:=false;
CloseHandle(hDisk);
end;
end;
procedure ReadChainOfPartTable(_ext2:TExt2Partitions; _sector: DWORD);
var
hDisk:THandle;
Table:TPartitionTable;
_adr:DWORD;
tmp:Cardinal;
i, j:Integer;
begin
hDisk:=CreateFile(PChar('\\.\PHYSICALDRIVE0'), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if hDisk <> INVALID_HANDLE_VALUE then
begin
_adr:=_sector;
SetFilePointer(hDisk, 0, nil, FILE_BEGIN);
for i:=0 to 25 do
begin
_SetFilePointer(hDisk, _adr, FILE_CURRENT);
SetFilepointer(hDisk, $1BE, nil, FILE_CURRENT);
ReadFile(hDisk, Table, SizeOf(TPartitionTable), tmp, nil);
if tmp = SizeOf(TPartitionTable) then
begin
for j:=0 to 3 do
begin
case Table[j].BootID of
$83: begin
inc(_ext2.PartCount);
_ext2.StartPart[_ext2.PartCount-1]:=Table[j].StartPartition;
_ext2._end:= True;
end;
$05: begin
_adr:=Table[j].StartPartition;
SetFilePointer(hDisk, -1*(($10 * j)+$1BE), nil, FILE_CURRENT);
end;
$0F: begin
_adr:=Table[j].StartPartition;
SetFilePointer(hDisk, -1*(($10 * j)+$1BE), nil, FILE_CURRENT);
end;
else
_ext2._end:=False;
end;
end;
end
else
begin
_ext2._end:=false;
CloseHandle(hDisk);
end;
end;
end
else
begin
_ext2._end:=false;
CloseHandle(hDisk);
end;
end;
end.
Запускать так:
Код:
procedure TForm1.Button1Click(Sender: TObject);
var
p:TExt2Partitions;
begin
P:=ReadMBRPartitionTable;
ShowMessage(Inttostr(p.PartCount));
end;