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 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
library pro001;
{ Remarque importante concernant la gestion de mémoire de DLL : ShareMem doit
être la première unité de la clause USES de votre bibliothèque ET de votre projet
(sélectionnez Projet-Voir source) si votre DLL exporte des procédures ou des
fonctions qui passent des chaînes en tant que paramètres ou résultats de fonction.
Cela s'applique à toutes les chaînes passées de et vers votre DLL --même celles
qui sont imbriquées dans des enregistrements et classes. ShareMem est l'unité
d'interface pour le gestionnaire de mémoire partagée BORLNDMM.DLL, qui doit
être déployé avec vos DLL. Pour éviter d'utiliser BORLNDMM.DLL, passez les
informations de chaînes avec des paramètres PChar ou ShortString. }
uses
SysUtils,inifiles,strutils, dialogs,
Classes,
pro001u in 'pro001u.pas';
type TMedia = record
DriveType : integer;
MediaType : Integer;
MediaTypeString : String;
Interfacetype : String;
Caption : String;
FreeSpace : Cardinal;
end;
IEnumVariant = interface(IUnknown)
['{00020404-0000-0000-C000-000000000046}']
function Next(celt: LongWord; var rgvar : OleVariant;
out pceltFetched: LongWord): HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Clone(out Enum: IEnumVariant): HResult; stdcall;
end;
// Constantes WMI
const
STR_LOCALHOST = '.';
STR_CIM2_ROOT = 'root\cimv2';
STR_CLASS = 'Win32_DISKDRIVE';
// Autres
const
STR_EMPTY = '';
// WMI ------------------------------------------------------------------------
// ces fonctions sont faites dans le but de pouvoir traiter toutes les informations WMI
// pas seulement ,comme c'est mon but ici , obtenir des informations disque
function WMIGet(SQL : String) : WideString;
var
Locator:ISWbemLocator;
Services:ISWbemServices;
ObjSet:ISWbemObjectSet;
Value:Cardinal;
SObject:ISWbemObject;
TempObj : OleVariant;
Enum :IEnumVariant;
begin
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(SQL,'WQL',
wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
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_;
result:=SObject.GetObjectText_(0);
end;
end;
except
Result:='';
end;
end;
function WMICollection(Chaine : WideString;out THS :THashedStringList): Integer;
var d,f : word;
start : Boolean;
Ws : String;
begin
Chaine:=AnsiReplaceStr(Chaine,';','');
Chaine:=AnsiReplaceStr(Chaine,' = ','=');
Chaine:=AnsiReplaceStr(Chaine,#9,'');
Ws:='';
for f := Length(Chaine) downto 0 do
if chaine[f]='}' then break;
Start:=False;
for d:= 0 to f-1 do
begin
if Start then ws:=ws+Chaine[d]
else Start:=Chaine[d]='{';
end;
THS.Text:=ws;
Result:=THS.Count;
end;
function WMISubCollection(Chaine : String;out TS : TStrings):Integer;
var d,f : word;
start : Boolean;
Ws : String;
begin
ws:='';
for f := Length(Chaine) downto 0 do
if chaine[f]='}' then break;
start:=False;
for d:= 0 to f-1 do
begin
if Start then ws:=ws+Chaine[d]
else Start:=Chaine[d]='{';
end;
TS.CommaText:=ws;
Result:=TS.Count;
end;
function WMIPropriete(WMIGot : WideString ;Name : String) : String;
var TS : THashedStringList;
begin
TS:=THashedStringList.Create;
try
WMICollection(WMIGot,TS);
Result:=AnsiReplaceStr(TS.ValueFromIndex[TS.IndexOfName(Name)],'"','');
finally
FreeAndNil(TS);
end;
end;
function WMIProprieteInt(WMIGot: WideString; Name : String): LongInt;
var TS : THashedStringList;
R : String;
begin
TS:=THashedStringList.Create;
try
WMICollection(WMIGot,TS);
R:=AnsiReplaceStr(Trim(TS.ValueFromIndex[TS.IndexOfName(Name)]),'"','');
try Result:=StrToInt(R); except Result:=-1; end;
finally
FreeAndNil(TS);
end;
end;
function GetPhysicalDrive(Drive : String) : String;
var
Locator:ISWbemLocator;
Services:ISWbemServices;
ObjSet:ISWbemObjectSet;
Value:Cardinal;
partition : String;
function DeviceIDFromAssoc(ObjectSet : ISWbemObjectSet): String;
var SObject:ISWbemObject;
TempObj : OleVariant;
Enum :IEnumVariant;
TS:TStringList;
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.Text:=AnsiReplaceStr(WMIPropriete(SObject.GetObjectText_(0),'Antecedent'),'"','');
Result:=Copy(TS.ValueFromIndex[0],2,Length(TS.ValueFromIndex[0])-2);
finally
FreeAndNil(TS);
end;
end;
end;
end;
begin
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;
function nserie(i:byte):shortstring;
var
j:string;
t:shortstring;
k,l:word;
begin
t:='';
j:=WMIGet('SELECT * FROM Win32_DiskDrive');
k:=pos('Caption = "',j)+11;
for l:=k to k+100 do
begin
if (j[l]=';')or(j[l]='"') then
break;
t:=t+j[l];
end;
nserie:=t;
end;
exports
nserie name 'nserie';
begin
end. |
Partager