Einzelnen Beitrag anzeigen

msickel

Registriert seit: 14. Mai 2005
108 Beiträge
 
Delphi 2005 Professional
 
#1

TSWbemLocator not found

  Alt 31. Jul 2010, 18:33
Hallo,
Ich habe einen Stück Code im Netz gefunden und versuche diesen zum laufen zu bringen.

Ich habe die Microsoft WMI Scripting V1.2 Library bereits importiert, bekomme aber immer noch den Fehler TSWbemLocator not found.

Welche TypeLibary braucht es noch, bzw. was muss ich noch tun um diesen Fehler zu vermeiden?

Hier noch der Code.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    StatusBar1: TStatusBar;
    CheckBox1: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses WbemScripting_TLB, activex, comobj;

type TIWrapper=class
     public
       prop:ISWbemProperty;
       constructor Create(p:ISWbemProperty);
     end;

constructor TIWrapper.Create(p: ISWbemProperty);
begin
  prop:=p;
end;

procedure clear(Adapters:TStrings);
var i:integer;
begin
  for i:=1 to Adapters.Count do
    Adapters.Objects[i-1]:=nil;
  Adapters.Clear;
end;

function getPropValue(sprop:ISWbemProperty):string;
var sValue:string;
    count:integer;
begin
  sValue := '';
  if VarIsNull(SProp.Get_Value) then
    sValue := '<empty>'
  else
  case SProp.CIMType of
    wbemCimtypeSint8, wbemCimtypeUint8, wbemCimtypeSint16, wbemCimtypeUint16,
    wbemCimtypeSint32, wbemCimtypeUint32, wbemCimtypeSint64:
      sValue := IntToStr(SProp.Get_Value);
    wbemCimtypeString, wbemCimtypeUint64:
      if VarIsArray(SProp.Get_Value) then
      begin
        if VarArrayHighBound(SProp.Get_Value, 1) > 0 then
          for Count := 1 to VarArrayHighBound(SProp.Get_Value, 1) do
            sValue := sValue + ' ' + SProp.Get_Value[Count];
      end
    else
      sValue := SProp.Get_Value;
    wbemCimtypeDatetime:sValue:=SProp.Get_Value//DateTimeToStr(SProp.Get_Value)
  else
      Exception.Create('Unknown type');
  end; {case}
  result:=sValue;
end;

procedure getAdapters(owner:TComponent; RemoteMachine,RemoteUser,RemotePassword:string; Adapters:TStrings);
var
  Locator: TSWbemLocator;
  SinkClasses: TSWbemSink;

  Services: ISWbemServices;
  ObjectSet: ISWbemObjectSet;
  SObject: ISWbemObject;
  propSet : ISWbemPropertySet;
  SProp: ISWbemProperty;
  Enum: IEnumVariant;
  tempObj: OleVariant;
  Value: Cardinal;

  sValue,
  className: String;
  strQuery: WideString;
begin
  className:='WIN32_NetworkAdapter';
  clear(adapters);
  Locator:=TSWbemLocator.Create(owner);
  SinkClasses:=TSWbemSink.Create(owner);

  try
    SinkClasses.Cancel;

    if RemoteMachine='then
      RemoteMachine:='.';// local machine
    Services := Locator.ConnectServer(RemoteMachine, 'root\CIMV2', RemoteUser, RemotePassword, '',
      '', 0, nil);

    ObjectSet := Services.InstancesOf(className, wbemFlagReturnImmediately or wbemQueryFlagShallow, nil);

    Enum := (ObjectSet._NewEnum) as IEnumVariant;
    while (Enum.Next(1, tempObj, Value) = S_OK) do
    begin
      SObject := IUnknown(tempObj) as SWBemObject;
      propSet := SObject.Properties_;
      SProp:=propSet.Item('NetConnectionID',0);// caption or systemname
      // now get the value of the property
      sValue:=getPropValue(SProp);
      if sValue<>'<empty>then
      begin
        sProp:=propSet.Item('NetConnectionStatus',0);
        Adapters.AddObject(sValue,TIWrapper.Create(SProp));
      end;
    end; {while Enum}

    strQuery := 'SELECT * FROM __InstanceCreationEvent within 5 WHERE TargetInstance' +
      ' ISA "'+className+'"';
    Services.ExecNotificationQueryAsync(SinkClasses.DefaultInterface, strQuery, 'WQL', 0, nil, nil);
      strQuery := 'SELECT * FROM __InstanceDeletionEvent within 5 WHERE TargetInstance' +
      ' ISA "'+className+'"';
    Services.ExecNotificationQueryAsync(SinkClasses.DefaultInterface, strQuery, 'WQL', 0, nil, nil);
  finally
  end; {try}
  Locator.Free;
  SinkClasses.Free;
  Services:=nil;// make sure the references are decreased
  ObjectSet:=nil;
  SObject:=nil;
  enum:=nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  statusbar1.simpletext:='querying WMI....';
  getAdapters(self, '', '', '', listbox1.items);
  statusbar1.simpletext:='done';
end;

{ TIWrapper }

procedure TForm1.FormDestroy(Sender: TObject);
begin
  clear(ListBox1.Items);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if ListBox1.ItemIndex=-1 then
    exit;
  with ListBox1.Items.Objects[ListBox1.ItemIndex] as TIWrapper do
    CheckBox1.checked:=getpropvalue(prop)='2';
end;

end.
ich weiss, das ich nichts weiss!
  Mit Zitat antworten Zitat