如何使用 Delphi 7 检索 Windows 中所有磁盘的磁盘签名? [英] how to retrieve the disk signature of all the disks in Windows using Delphi 7?
问题描述
在 Windows >= XP 上使用 Delphi 7,如何从计算机检索每个磁盘的磁盘签名?最好不要使用 WMI 或 Diskpart.
Using Delphi 7, on a Windows >= XP, how can I retrieve the disk signature of every disk from the computer? Preferably without using WMI or Diskpart.
如果可能的话,也要快..
And if it's possible, to be fast too..
谢谢.
后期
Documentation: http://pcsupport.about.com/od/termsd/g/disk-signature.htm
MBR disks: http://diddy.boot-land.net/firadisk/files/signature.htm
GPT disks: http://thestarman.pcministry.com/asm/mbr/GPT.htm
How to get it with DiskPart (method found on Google when searching "disk signature"):
Diskpart >> list disk >> select disk [n] >>
detail disk >> Disk ID: 0E35445B for MBR disks
and GUID: 55FD03F2-6B11-49DF-8167-D30B94A4509D for GPT Disks
推荐答案
您可以使用 DeviceIoControl
和 IOCTL_DISK_GET_DRIVE_LAYOUT_EX
获取您需要的信息.
You can use DeviceIoControl
and IOCTL_DISK_GET_DRIVE_LAYOUT_EX
to obtain the information that you require.
program DiskSignatureGuid;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows;
type
TDriveLayoutInformationMbr = record
Signature: DWORD;
end;
TDriveLayoutInformationGpt = record
DiskId: TGuid;
StartingUsableOffset: Int64;
UsableLength: Int64;
MaxPartitionCount: DWORD;
end;
TPartitionInformationMbr = record
PartitionType: Byte;
BootIndicator: Boolean;
RecognizedPartition: Boolean;
HiddenSectors: DWORD;
end;
TPartitionInformationGpt = record
PartitionType: TGuid;
PartitionId: TGuid;
Attributes: Int64;
Name: array [0..35] of WideChar;
end;
TPartitionInformationEx = record
PartitionStyle: Integer;
StartingOffset: Int64;
PartitionLength: Int64;
PartitionNumber: DWORD;
RewritePartition: Boolean;
case Integer of
0: (Mbr: TPartitionInformationMbr);
1: (Gpt: TPartitionInformationGpt);
end;
TDriveLayoutInformationEx = record
PartitionStyle: DWORD;
PartitionCount: DWORD;
DriveLayoutInformation: record
case Integer of
0: (Mbr: TDriveLayoutInformationMbr);
1: (Gpt: TDriveLayoutInformationGpt);
end;
PartitionEntry: array [0..15] of TPartitionInformationGpt;
//hard-coded maximum of 16 partitions
end;
const
PARTITION_STYLE_MBR = 0;
PARTITION_STYLE_GPT = 1;
PARTITION_STYLE_RAW = 2;
const
IOCTL_DISK_GET_DRIVE_LAYOUT_EX = $00070050;
procedure Main;
const
// Max number of drives assuming primary/secondary, master/slave topology
MAX_IDE_DRIVES = 16;
var
i: Integer;
Drive: string;
hDevice: THandle;
DriveLayoutInfo: TDriveLayoutInformationEx;
BytesReturned: DWORD;
begin
for i := 0 to MAX_IDE_DRIVES - 1 do
begin
Drive := '\\.\PHYSICALDRIVE' + IntToStr(i);
hDevice := CreateFile(PChar(Drive), 0, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
if hDevice <> INVALID_HANDLE_VALUE then
begin
if DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, nil, 0,
@DriveLayoutInfo, SizeOf(DriveLayoutInfo), BytesReturned, nil) then
begin
case DriveLayoutInfo.PartitionStyle of
PARTITION_STYLE_MBR:
Writeln(Drive + ', MBR, ' +
IntToHex(DriveLayoutInfo.DriveLayoutInformation.Mbr.Signature, 8));
PARTITION_STYLE_GPT:
Writeln(Drive + ', GPT, ' +
GUIDToString(DriveLayoutInfo.DriveLayoutInformation.Gpt.DiskId));
PARTITION_STYLE_RAW:
Writeln(Drive + ', RAW');
end;
end;
CloseHandle(hDevice);
end;
end;
end;
begin
Main;
Readln;
end.
请注意,由于 0
已传递给 CreateFile
的 dwDesiredAccess
参数,因此不需要提升权限.在文档中对此进行了解释,尽管有些不透明一个>:
Note that since 0
is passed to the dwDesiredAccess
parameter of CreateFile
, elevated rights are not required. This is explained, albeit somewhat opaquely, in the documentation:
对磁盘或卷的直接访问受到限制......必须满足以下要求才能成功调用:
Direct access to the disk or to a volume is restricted ... The following requirements must be met for such a call to succeed:
- 呼叫者必须具有管理权限.
- dwCreationDisposition 参数必须具有 OPEN_EXISTING 标志.
- 打开卷或软盘时,dwShareMode 参数必须具有 FILE_SHARE_WRITE 标志.
注意 dwDesiredAccess 参数可以为零,允许应用程序在不访问设备的情况下查询设备属性.这对于应用程序确定软盘的大小很有用磁盘驱动器及其支持的格式,无需软盘例如,在驱动器中.也可用于阅读统计无需更高级别的数据读/写权限.
Note The dwDesiredAccess parameter can be zero, allowing the application to query device attributes without accessing a device. This is useful for an application to determine the size of a floppy disk drive and the formats it supports without requiring a floppy disk in a drive, for instance. It can also be used for reading statistics without requiring higher-level data read/write permission.
这篇关于如何使用 Delphi 7 检索 Windows 中所有磁盘的磁盘签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!