function MagWmiGetPropStr (wmiProp: ISWbemProperty): string ;
var
I: integer ;
begin
result := '';
if VarIsNull(wmiProp.Get_Value) then
result := 'NULL'
else
begin
case wmiProp.CIMType of
wbemCimtypeSint8, wbemCimtypeUint8, wbemCimtypeSint16,
wbemCimtypeUint16, wbemCimtypeSint32, wbemCimtypeUint32,
wbemCimtypeSint64:
if VarIsArray(wmiProp.Get_Value) then
begin
for I := 0 to VarArrayHighBound (wmiProp.Get_Value, 1) do
begin
if I > 0 then result := result + '|' ;
result := result + IntToStr (wmiProp.Get_Value [i]) ;
end ;
end
else
result := IntToStr (wmiProp.Get_Value);
wbemCimtypeReal32, wbemCimtypeReal64:
result := FloatToStr (wmiProp.Get_Value);
wbemCimtypeBoolean:
if wmiProp.Get_Value then result := 'True' else result := 'False';
wbemCimtypeString, wbemCimtypeUint64:
if VarIsArray(wmiProp.Get_Value) then
begin
for I := 0 to VarArrayHighBound (wmiProp.Get_Value, 1) do
begin
if I > 0 then result := result + '|' ;
result := result + wmiProp.Get_Value [i] ;
end ;
end
else
result := wmiProp.Get_Value;
wbemCimtypeDatetime:
result := wmiProp.Get_Value;
wbemCimtypeReference:
begin
result := wmiProp.Get_Value ;
// Services.Get(result, 0, nil).GetObjectText_(0)); another query
end;
wbemCimtypeChar16:
result := '<16-bit character>';
wbemCimtypeObject:
result := '<CIM Object>';
end ;
end;
end ;
function MagWmiGetInfoEx (const Comp, NameSpace, User, Pass, Arg: string ;
var WmiResults: T2DimStrArray; var instances: integer; var errinfo: string): integer ;
var
wmiLocator: TSWbemLocator;
wmiServices: ISWbemServices;
wmiObjectSet: ISWbemObjectSet;
wmiObject: ISWbemObject;
propSet: ISWbemPropertySet;
wmiProp: ISWbemProperty;
propEnum, Enum: IEnumVariant;
ovVar1, ovVar2: OleVariant; // 5.2
lwValue: LongWord;
sValue: String;
inst, row: integer ;
dimmed: boolean ;
begin
result := 0 ;
errinfo := '' ;
Instances := 0 ;
SetLength (WmiResults, 0, 0) ;
dimmed := false ;
VarClear (ovVar1) ; // 5.2
VarClear (ovVar2) ; // 5.2
wmiLocator := TSWbemLocator.Create (Nil) ;
try
try
wmiServices := wmiLocator.ConnectServer (Comp, Namespace, User, Pass,
'', '', 0, nil) ;
if Pos ('SELECT', Arg) = 1 then
wmiObjectSet := wmiServices.ExecQuery (Arg, 'WQL', wbemFlagReturnImmediately, nil)
else
wmiObjectSet := wmiServices.InstancesOf (Arg, wbemFlagReturnImmediately or
wbemQueryFlagShallow, nil) ;
Instances := wmiObjectSet.Count ;
if Instances = 0 then exit ;
// Replicate VBScript's "for each" construct
Enum := (wmiObjectSet._NewEnum) as IEnumVariant;
inst := 0 ;
while (Enum.Next (1, ovVar1, lwValue) = S_OK) do // 5.2
begin
wmiObject := IUnknown(ovVar1) as SWBemObject; // 5.2
propSet := wmiObject.Properties_;
result := propSet.Count ;
if NOT dimmed then
begin
SetLength (WmiResults, Instances + 1, result + 1) ;
WmiResults [0, 0] := 'Instance' ;
dimmed := true ;
end ;
propEnum := (propSet._NewEnum) as IEnumVariant;
inc (inst) ;
row := 1 ;
WmiResults [inst, 0] := IntToStr (inst) ;
// Replicate VBScript's "for each" construct
while (propEnum.Next (1, ovVar2, lwValue) = S_OK) do // 5.2
begin
wmiProp := IUnknown(ovVar2) as SWBemProperty; // 5.2
sValue := MagWmiGetPropStr (wmiProp) ;
if inst = 1 then WmiResults [0, row] := wmiProp.Name ;
WmiResults [inst, row] := sValue ;
inc (row) ;
VarClear (ovVar2); // 5.2 whomp them mem leaks
end;
end;
VarClear (ovVar1); // 5.2 whomp them mem leaks
except
VarClear (ovVar1) ; // 5.2
VarClear (ovVar2) ; // 5.2
result := -1 ;
// errinfo := GetExceptMess (ExceptObject) ; // 5.2
end ;
finally
wmiLocator.Free;
end;
end;