delphi枚举wmi

2019-07-24 16:05:02 浏览数 (1)

代码语言:javascript复制
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,SiMath,SimEncrypt, StdCtrls,ActiveX,ComObj,JwaWbemCli;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MemoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure ShowProp(SProp: OleVariant);
  public
    { Public declarations }
  end;

const
  RPC_C_AUTHN_LEVEL_DEFAULT = 0;
  RPC_C_IMP_LEVEL_IMPERSONATE = 3;
  RPC_C_AUTHN_WINNT = 10;
  RPC_C_AUTHZ_NONE = 0;
  RPC_C_AUTHN_LEVEL_CALL = 3;
  EOAC_NONE = 0;

var
  Form1: TForm1;

implementation

function GetWMIProperty(WMIType, WMIProperty: string): string;
var
  Wmi, Objs, Obj: OleVariant;
  Enum: IEnumVariant;
  C: Cardinal;
  s:string;
begin
  Wmi:= CreateOleObject('WbemScripting.SWbemLocator');
  Objs := Wmi.ConnectServer('.','rootcimv2').ExecQuery('Select * from Win32_'  WMIType);
  Enum:=IEnumVariant(IUnknown(Objs._NewEnum));
  Enum.Reset;
  while (Enum.Next(1,Obj,C)=s_ok) do
  begin    //需要更多的信息请使用循环
    Obj:=Obj.Properties_.Item(WMIProperty,0).Value;
    if VarIsArray(Obj) then
      s:=Obj[0]
    else
      s:=Obj;
    result:=result ',' s;
  end;
end;


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const 
  strLocale = '';
  strUser = '';
  strPassword = '';
  strNetworkResource = 'rootcimv2';
  strAuthority = '';
  WQL = 'SELECT * FROM Win32_Volume'; // SELECT * FROM MSNdis_80211_ReceivedSignalStrength Where active=true
var
  FWbemLocator : IWbemLocator;
  FWbemServices : IWbemServices;
  FUnsecuredApartment : IUnsecuredApartment;
  ppEnum : IEnumWbemClassObject;
  apObjects : IWbemClassObject;
  puReturned : ULONG;
  pVal : OleVariant;
  pType : Integer;
  plFlavor : Integer;
  Succeed : HRESULT;
begin 
  // Set general COM security levels
  if Failed(CoInitializeSecurity(nil, -1, nil, nil, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, nil, EOAC_NONE, nil)) then
    Exit;
  // Obtain the initial locator to WMI
  if Succeeded(CoCreateInstance(CLSID_WbemLocator, nil, CLSCTX_INPROC_SERVER, IID_IWbemLocator, FWbemLocator)) then
  try // Connect to WMI through the IWbemLocator::ConnectServer method
    if Succeeded(FWbemLocator.ConnectServer(strNetworkResource, strUser, strPassword, strLocale, WBEM_FLAG_CONNECT_USE_MAX_WAIT, strAuthority, nil, FWbemServices)) then
    try // Set security levels on the proxy
      if Failed(CoSetProxyBlanket(FWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nil, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nil, EOAC_NONE)) then
        Exit;
      if Succeeded(CoCreateInstance(CLSID_UnsecuredApartment, nil, CLSCTX_LOCAL_SERVER, IID_IUnsecuredApartment, FUnsecuredApartment)) then
      try // Use the IWbemServices pointer to make requests of WMI
        Succeed := FWbemServices.ExecQuery('WQL', WQL, WBEM_FLAG_FORWARD_ONLY, nil, ppEnum);
        if Succeeded(Succeed) then
        begin
          memo.lines.add('Running WMI query...'); // Get the data from the query
          while (ppEnum.Next(WBEM_INFINITE, 1, apObjects, puReturned)=0) do
          begin
            apObjects.Get('Caption', 0, pVal, pType, plFlavor);
            memo.lines.add('"'   AnsiToUTF8(pVal)   '"');
            VarClear(pVal);
          end;
        end else
          memo.lines.add(Format('Error executing WQL sentence %x',[Succeed]));
      finally
        FUnsecuredApartment := nil;
      end;
    finally
      FWbemServices := nil;
    end;
  finally
    FWbemLocator := nil;
  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
const 
  WbemUser ='';
  WbemPassword ='';
  WbemComputer ='localhost';
  wbemFlagForwardOnly = $00000020;
var 
  FSWbemLocator : OLEVariant;
  FWMIService : OLEVariant;
  FWbemObjectSet: OLEVariant;
  classItems : OLEVariant;
  classItem : oleVariant;
  FWbemObject : Variant;
  classEnum : IEnumvariant;
  oEnum : IEnumvariant;
  sValue : string;
  c:Cardinal;
begin; 
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'rootCIMV2', WbemUser, WbemPassword);
  classItems := FWMIService.SubclassesOf();
  classEnum := IUnknown(classItems._NewEnum) As IEnumvariant;
  while classEnum.Next(1, classItem, c) = 0 do
  begin
    sValue := classItem.Path_.Class;
    memo.lines.add(Format('Class: %s', [sValue]));
    classItem := Unassigned;
  end;
  {FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_MemoryDevice','WQL',wbemFlagForwardOnly);
  oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, nil) = 0 do
  begin 
  sValue := FWbemObject.Properties_.Item('Caption').Value;
  memo.lines.add(Format('Caption %s', [sValue]));
  // String FWbemObject := Unassigned;
  end;}
end;

procedure TForm1.ShowProp(SProp: OleVariant);
var
  StrValue: string;
  Count: Cardinal;
begin
  StrValue := '';
  if VarIsNull(SProp.Get_Value) then
    StrValue := '<empty>'
  else
    case SProp.CIMType of
    Cim_Uint8, Cim_Sint8, Cim_Uint16, Cim_Sint16, Cim_Uint32, Cim_Sint32, Cim_Sint64:
      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
            StrValue := StrValue   ' '   IntToStr(SProp.Get_Value[Count]);
      end else
      StrValue := IntToStr(SProp.Get_Value);
      Cim_Real32, Cim_Real64: StrValue := FloatToStr(SProp.Get_Value);
      Cim_STRING: StrValue := SProp.Get_Value;
    else memo.lines.add('Unknown type');
    end;
  memo.lines.Add(StrValue);
end;

procedure TForm1.MemoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=ord('A')) and (ssctrl in Shift) then
  begin
    memo.SelectAll;
  end;
end;

end.

0 人点赞