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
|
// drive sous la forme 'C:'
//uses WbemScripting_TLB, Variants, ActiveX, StrUtils;
// Constantes WMI
const
STR_LOCALHOST = '.';
STR_CIM2_ROOT = 'root\cimv2';
STR_CLASS = 'Win32_DISKDRIVE';
// Autres
const
STR_EMPTY = '';
function GetPhysicalDrive(Drive : String) : String;
var
Locator:ISWbemLocator;
Services:ISWbemServices;
SObject:ISWbemObject;
ObjSet:ISWbemObjectSet;
Enum:IEnumVariant;
Value:Cardinal;
partition : String;
function DeviceIDFromAssoc(ObjectSet : ISWbemObjectSet): String;
var SObject:ISWbemObject;
Enum:IEnumVariant;
TS:TStrings;
p,q : Integer;
begin
// Obtenir DeviceID de Antécédent
Enum := (ObjSet._NewEnum) as IEnumVariant;
// un objet trouvé ?
if (Enum.Next(1, TempObj, Value) = S_OK) then begin
SObject := IUnknown(TempObj) as ISWBemObject;
if (SObject <> nil) then begin
// Acceder aux propriétés
SObject.Properties_;
TS := TStringList.Create();
try
TS.Add(SObject.GetObjectText_(0));
p:=pos('DeviceID=\"',TS.Text)+11; // "sale" mais efficace
q:=pos('Dependent',TS.Text)-6; //
Result:=Copy(TS.Text,p,q-p);
finally
FreeAndNil(TS);
end;
end;
end;
end;
try
Locator := CoSWbemLocator.Create();
// Connection au service WMI
Services := Locator.ConnectServer(
STR_LOCALHOST, {local}
STR_CIM2_ROOT, {root}
STR_EMPTY, STR_EMPTY, {Utilisateur et Mot de passe -en local inutile-}
STR_EMPTY,STR_EMPTY, 0, nil);
// Acceder aux données
ObjSet := Services.ExecQuery('REFERENCES OF {Win32_LogicalDISK="'+Drive+'"} WHERE ResultClass=Win32_LogicalDiskToPartition ',
'WQL',wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
Partition:=DeviceIDFromAssoc(ObjSet);
// on passe de la partition au disque physique
ObjSet := Services.ExecQuery('REFERENCES OF {Win32_DiskPartition="'+Partition+'"} WHERE ResultClass=Win32_DiskDriveToDiskPartition',
'WQL',wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
result:=AnsiReplaceStr(DeviceIDFromAssoc(ObjSet),'\\','\');
except
result:='unknown';
end;
end; |
Partager