Function TWMIClass.Query(
Const Query: WideString): Boolean;
Var WMIObjectSet: ISWbemObjectSet;
WMIProp: ISWbemProperty;
Enum, PropEnum: IEnumVariant;
WMIObject: OleVariant;
Instance, Prop: Integer;
W: LongWord;
Begin
Result := True;
ClearResults;
Try
If CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, '
SELECT', 6, PWideChar(
Query), 6) <> 2
Then
WMIObjectSet := _WMIServices.InstancesOf(
Query, wbemFlagReturnImmediately
or wbemQueryFlagShallow,
nil)
Else WMIObjectSet := _WMIServices.ExecQuery(
Query, '
WQL', wbemFlagReturnImmediately,
nil);
SetLength(_WMIResults.Instance, WMIObjectSet.Count);
If _WMIResults.Instance =
nil Then Exit;
Enum := (WMIObjectSet._NewEnum)
as IEnumVariant;
Instance := 0;
While Enum.Next(1, WMIObject, W) = S_OK
do Begin
PropEnum := ISWbemPropertySet((IUnknown(WMIObject)
as ISWbemObject).Properties_)._NewEnum
as IEnumVariant;
Prop := 0;
If Instance > 0
Then SetLength(_WMIResults.Instance[Instance].PropValue, Length(_WMIResults.PropName));
While PropEnum.Next(1, WMIObject, W) = S_OK
do Begin
WMIProp := IUnknown(WMIObject)
as ISWbemProperty;
If Instance = 0
Then Begin
SetLength(_WMIResults.PropName, Prop + 1);
SetLength(_WMIResults.Instance[Instance].PropValue, Prop + 1);
_WMIResults.PropName[Prop] := WMIProp.
Name;
End;
_WMIResults.Instance[Instance].PropValue[Prop] := WMIProp;
Inc(Prop);
End;
Inc(Instance);
End;
Except
Result := False;
If ExceptObject
is Exception Then _LastError :=
Exception(ExceptObject).
Message
Else _LastError := '
Unknown Error: ' + ExceptObject.ClassName;
End;
End;