1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| unit routines;
interface
uses
System.SysUtils,
System.Variants,
System.Win.ComObj,
Winapi.ActiveX,
Vcl.ComCtrls;
type
TAnalyse = (AMOVIBLE, CDROM, AMOVIBLE_CDROM, TOUT);
TDevice = class
private
FID: String;
FPNPId: String;
procedure SetID(const Value: String);
procedure SetPNPId(const Value: String);
public
property ID: String read FID write SetID;
property PNPId: String read FPNPId write SetPNPId;
constructor Create(newid: String); overload;
constructor Create(newid, newpnpid: String); overload;
end;
procedure FillListe(const Liste: TTreeView;
const analyse: TAnalyse = TAnalyse.TOUT);
const
WbemUser = '';
WbemPassword = '';
WbemComputer = 'localhost';
wbemFlagForwardOnly = $00000020;
Giga = 1024;
Mo = 1024;
Ko = 1024;
implementation
resourcestring
StrFree = ' espace libre sur ';
function sizetostring(size: UInt64): String;
begin
if size = 0 then
exit(emptystr);
if size < $000000000400 then
exit(format('%d bytes', [size]));
if size < $000000100000 then
exit(format('%.1f Kb', [size / $000000000400]));
if size < $000040000000 then
exit(format('%.1f Mb', [size / $000000100000]));
if size < $010000000000 then
exit(format('%.2f Gb', [size / $000040000000]));
exit(format('%.2f Tb', [size / $010000000000]));
end;
procedure getletters(const Liste: TTreeView; const ParentNode: TTreeNode;
const ID: String);
var
FSWbemLocator: OLEVariant;
FWMIService: OLEVariant;
fWbemPartitions, fwbemDisk: OLEVariant;
PEnum, DEnum: IEnumvariant;
PValue, DValue: LongWord;
wsql: String;
s: String;
szfree,sz : Uint64;
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2',
WbemUser, WbemPassword);
wsql := 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID=' + QuotedStr(ID) +
'} WHERE AssocClass = Win32_DiskDriveToDiskPartition';
fWbemPartitions := FWMIService.ExecQuery(wsql, 'WQL', wbemFlagForwardOnly);
PEnum := IUnknown(fWbemPartitions._NewEnum) as IEnumvariant;
while PEnum.Next(1, fWbemPartitions, PValue) = 0 do
begin
wsql := 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID=' +
QuotedStr(String(fWbemPartitions.DeviceID)) +
'} WHERE AssocClass = Win32_LogicalDiskToPartition';
fwbemDisk := FWMIService.ExecQuery(wsql, 'WQL', wbemFlagForwardOnly);
DEnum := IUnknown(fwbemDisk._NewEnum) as IEnumvariant;
while DEnum.Next(1, fwbemDisk, PValue) = 0 do
begin
sz:=VariantToUInt64WithDefault(FWbemDisk.size, 0);
szFree:=VariantToUInt64WithDefault(FWbemDisk.FreeSpace, 0);
s := format('%s %s', [string(fwbemDisk.DeviceID),
string(fwbemDisk.VolumeName)]);
// s:=S+StringOfChar(#255,60-Length(s));
s := format('%s (%s%s%s)',[s,
sizetostring(szFree),
StrFree,
sizetostring(sz)]);
Liste.Items.AddChild(ParentNode, s);
end;
fwbemDisk := Unassigned;
end;
fWbemPartitions := Unassigned;
end;
procedure FillListe(const Liste: TTreeView;
const analyse: TAnalyse = TAnalyse.TOUT);
var
FSWbemLocator: OLEVariant;
FWMIService: OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject: OLEVariant;
oEnum: IEnumvariant;
iValue: LongWord;
wsql: String;
s: String;
sz: UInt64;
aListItem: TTreeNode;
aDevice: TDevice;
begin;
Liste.Items.BeginUpdate;
try
Liste.Items.Clear;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2',
WbemUser, WbemPassword);
wsql := 'Select Caption,Interfacetype,DeviceID,PNPDeviceID,MediaType,Size from Win32_DiskDrive';
if (analyse = TAnalyse.AMOVIBLE) OR (analyse = TAnalyse.AMOVIBLE_CDROM)
then
wsql := wsql + ' WHERE InterfaceType Like ''USB%''';
FWbemObjectSet := FWMIService.ExecQuery(wsql, 'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumvariant;
if analyse <> TAnalyse.CDROM then
begin
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
sz := VariantToUInt64WithDefault(FWbemObject.size, 0);
s := format('%s %s', [String(FWbemObject.Caption),
sizetostring(sz)]);
if VarIsNull(FWbemObject.PNPDeviceID) then
aDevice := TDevice.Create(String(FWbemObject.DeviceID))
else
aDevice := TDevice.Create(String(FWbemObject.DeviceID),
String(FWbemObject.PNPDeviceID));
aListItem := Liste.Items.AddObject(nil, s, aDevice);
aListItem.Imageindex := 0;
if sametext(String(FWbemObject.Interfacetype), 'usb') then
begin
aListItem.Imageindex := 2;
if String(FWbemObject.Mediatype).Contains('hard') then
aListItem.Imageindex := 1;
end;
getletters(Liste, aListItem, String(FWbemObject.DeviceID));
aListItem.Expanded := true;
end;
FWbemObject := Unassigned;
end;
if analyse <> TAnalyse.AMOVIBLE then
begin
FWbemObjectSet := FWMIService.ExecQuery
('Select Caption,Drive,DeviceID,PNPDeviceID,VolumeName,Size from Win32_CDROMDrive',
'WQL', wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumvariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
if VarisNull(FWbemObject.size)
then sz:=0
else sz := VariantToUInt64WithDefault(FWbemObject.size, 0);
s := format('%s %s', [String(FWbemObject.Caption),
sizetostring(sz)]);
aDevice := TDevice.Create(String(FWbemObject.DeviceID),
String(FWbemObject.PNPDeviceID));
aListItem := Liste.Items.AddObject(nil, s, aDevice);
aListItem.Imageindex := 3;
if VarisNull(FWbemObject.size)
then s:=String(FWbemObject.Drive)
else s := format('%s %s', [String(FWbemObject.Drive),
String(FWbemObject.VolumeName)]);
Liste.Items.AddChild(aListItem, s);
aListItem.Expanded := true;
end;
FWbemObject := Unassigned;
end;
finally
Liste.Items.EndUpdate;
end;
end;
{ TDevice }
constructor TDevice.Create(newid, newpnpid: String);
begin
FID := newid;
FPNPId := newpnpid;
end;
constructor TDevice.Create(newid: String);
begin
FID := newid;
FPNPId := emptystr;
end;
procedure TDevice.SetID(const Value: String);
begin
FID := Value;
end;
procedure TDevice.SetPNPId(const Value: String);
begin
FPNPId := Value;
end;
end. |