program test;
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj,
Variants;
procedure Main;
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
wmiDiskDrives : OLEVariant;
wmiDiskPartitions : OLEVariant;
wmiLogicalDisks : OLEVariant;
wmiDiskDrive : OLEVariant;
wmiDiskPartition : OLEVariant;
wmiLogicalDisk : OLEVariant;
oEnum : IEnumvariant;
oEnum2 : IEnumvariant;
oEnum3 : IEnumvariant;
iValue : LongWord;
DeviceID :
string;
begin;
FSWbemLocator := CreateOleObject('
WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('
localhost', '
root\CIMV2', '
', '
');
//Get the physical disk drive
wmiDiskDrives := FWMIService.ExecQuery('
SELECT Caption, DeviceID FROM Win32_DiskDrive','
WQL',wbemFlagForwardOnly);
oEnum := IUnknown(wmiDiskDrives._NewEnum)
as IEnumVariant;
while oEnum.Next(1, wmiDiskDrive, iValue) = 0
do
begin
//Use the disk drive device id to find associated partition
DeviceID:=StringReplace(
String(wmiDiskDrive.DeviceID),'
\','
\\',[rfReplaceAll]);
wmiDiskPartitions := FWMIService.ExecQuery('
ASSOCIATORS OF {Win32_DiskDrive.DeviceID="'+DeviceID+'
"} WHERE AssocClass = Win32_DiskDriveToDiskPartition','
WQL',wbemFlagForwardOnly);
oEnum2 := IUnknown(wmiDiskPartitions._NewEnum)
as IEnumVariant;
while oEnum2.Next(1, wmiDiskPartition, iValue) = 0
do
begin
wmiLogicalDisks := FWMIService.ExecQuery('
ASSOCIATORS OF {Win32_DiskPartition.DeviceID="'+
String(wmiDiskPartition.DeviceID)+'
"} WHERE AssocClass = Win32_LogicalDiskToPartition','
WQL',wbemFlagForwardOnly);
oEnum3 := IUnknown(wmiLogicalDisks._NewEnum)
as IEnumVariant;
while oEnum3.Next(1, wmiLogicalDisk, iValue) = 0
do
begin
Writeln(Format('
Drive letter associated with disk drive %s %s Partition %s is %s',[
String(wmiDiskDrive.Caption),
String(wmiDiskDrive.DeviceID),
String(wmiDiskPartition.DeviceID),
String(wmiLogicalDisk.DeviceID)]));
wmiLogicalDisk:=Unassigned;
end;
wmiDiskPartition:=Unassigned;
end;
wmiDiskDrive:=Unassigned;
Writeln;
end;
end;
begin
try
CoInitialize(
nil);
try
Main;
finally
CoUninitialize;
end;
except
on E:EOleException
do
Writeln(Format('
EOleException %s %x', [E.
Message,E.ErrorCode]));
on E:
Exception do
Writeln(E.Classname, '
:', E.
Message);
end;
Writeln('
Press Enter to exit');
Readln;
end.