首页  编辑  

获取硬盘大小信息

Tags: /超级猛料/Hardware.硬件相关/驱动器相关/硬盘/   Date Created:

使用DISK IO直接访问硬盘获取大小信息等

By Kingron,需要使用JEDI库中的一些定义,这里只是一个简单的演示调用而已。

const

 IOCTL_DISK_GET_PARTITION_INFO = $00074004;

type

 _PARTITION_INFORMATION = record

   StartingOffset: LARGE_INTEGER;

   PartitionLength: LARGE_INTEGER;

   HiddenSectors: DWORD;

   PartitionNumber: DWORD;

   PartitionType: BYTE;

   BootIndicator: ByteBool;

   RecognizedPartition: ByteBool;

   RewritePartition: ByteBool;

 end;

function GetDiskSize(const Disk: Integer): Int64;

{

  返回物理硬盘空间大小

}

var

 F : HFILE;

 Info : _PARTITION_INFORMATION;

 R : DWORD;

begin

 Result := 0;

 F := CreateFile(PChar(Format('\\.\PhysicalDrive%d', [Disk])),

                 GENERIC_READ,

                 FILE_SHARE_READ or FILE_SHARE_WRITE,

                 nil ,

                 OPEN_EXISTING,

                 FILE_FLAG_SEQUENTIAL_SCAN,

                 0);

 if F = INVALID_HANDLE_VALUE then Exit;

 if DeviceIoControl(F, IOCTL_DISK_GET_PARTITION_INFO, nil, 0, @Info, SizeOf(Info), R, nil) then

 begin

   Result := Int64(Info.PartitionLength);

 end;

 CloseHandle(F);

end;

function GetVolumeSize(const Disk: Char): Int64;

{

 返回逻辑磁盘大小

}

var

 F : HFILE;

 Info : _PARTITION_INFORMATION;

 R : DWORD;

begin

 Result := 0;

 F := CreateFile(PChar(Format('\\.\%c:', [Disk])),

                 GENERIC_READ,

                 FILE_SHARE_READ or FILE_SHARE_WRITE,

                 nil ,

                 OPEN_EXISTING,

                 FILE_FLAG_SEQUENTIAL_SCAN,

                 0);

 if F = INVALID_HANDLE_VALUE then Exit;

 if DeviceIoControl(F, IOCTL_DISK_GET_PARTITION_INFO, nil, 0, @Info, SizeOf(Info), R, nil) then

 begin

   Result := Int64(Info.PartitionLength);

 end;

 CloseHandle(F);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 Caption := BytesToString(GetHardDiskCacheSize(1));

 Button1.Caption := BytesToString(GetVolumeSize('e'));

end;