uses
WbemScripting_TLB,
ActiveX;
function QueryWMI(
const QueryClass, QueryProperty:
String; AStrings: TStrings;
AIsArray: Boolean = False): Boolean;
var
Locator: ISWbemLocator;
Services: ISWbemServices;
ObjectSet: ISWbemObjectSet;
WMIObject: ISWbemObject;
WMIProperty: ISWbemProperty;
Enum: IEnumVariant;
ObjValue: Cardinal;
TempObj: OleVariant;
i: Integer;
begin
Result := FALSE;
try
Locator := CoSWbemLocator.Create;
try
Services := Locator.ConnectServer('
', '
root\cimv2', '
', '
', '
','
', 0,
NIL);
if Assigned(Services)
then begin
Services.Security_.ImpersonationLevel := 3;
ObjectSet := Services.ExecQuery('
SELECT * FROM '+QueryClass,
'
WQL',
wbemFlagForwardOnly
or wbemFlagReturnWhenComplete,
NIL);
Enum := (ObjectSet._NewEnum)
as IEnumVariant;
while (Enum.Next(1, TempObj, ObjValue) = S_OK)
do begin
try
WMIObject := IUnknown(TempObj)
as ISWBemObject;
except
WMIObject :=
NIL;
end;
TempObj := Unassigned;
// Interface in TempObj freigeben
if Assigned(WMIObject)
then begin
WMIProperty := WMIObject.Properties_.Item(QueryProperty, 0);
if (
NOT VarIsNull(WMIProperty.Get_Value))
then
if AIsArray
then
for i := VarArrayLowBound(WMIProperty.Get_Value, 1)
to VarArrayHighBound(WMIProperty.Get_Value, 1)
do
AStrings.Add(Trim(WMIProperty.Get_Value[i]))
else
AStrings.Add(Trim(WMIProperty.Get_Value))
end;
end;
Result := TRUE;
end;
finally
Services :=
NIL;
Locator :=
NIL;
end;
except // Exceptions fangen (Wenn WMI nicht verfügbar/installiert ist, gibt´s eine!)
Result := FALSE;
end;
end;
procedure TForm185.Button1Click(Sender: TObject);
begin
QueryWMI('
Win32_NetworkAdapterConfiguration', '
DefaultIPGateway', Memo1.Lines,
True);
end;