如何在Delphi中使用ManagementObjectSearcher?

时间:2010-09-09 05:30:15

标签: delphi wmi delphi-2007

我在互联网上找到但我没有得到如何在delphi中使用ManagementObjectSearcher。我的主要问题是我必须在'使用'中添加哪个文件。

我发现one code但无法让它在我的系统中运行。

2 个答案:

答案 0 :(得分:5)

Himadri,您引用的代码使用ManagementObjectSearcher中的Delphi PrismManagementObjectSearcher是.Net类,用于检索有关WMI的信息。您可以使用3种替代方法从delphi访问WMI。

1)从Component-> Import Component导入Microsoft WMIScripting Library,然后选择Import type library。

program WMI_Test;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Variants,
  SysUtils,
  WbemScripting_TLB in '..\..\..\Documents\RAD Studio\5.0\Imports\WbemScripting_TLB.pas';//


Procedure AccessWMI;
var
  WMIServices : ISWbemServices;
  Root        : ISWbemObjectSet;
  Item        : Variant;
begin
  WMIServices := CoSWbemLocator.Create.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);
  Root  := WMIServices.ExecQuery('Select Caption FROM Win32_OperatingSystem','WQL', 0, nil);
  Item := Root.ItemIndex(0);
  Writeln(VarToStr(Item.Caption));
end;


begin
  try
    CoInitialize(nil);
    AccessWMI;
    Readln;
    CoUninitialize;
  except
    on E:Exception do
    Begin
        CoUninitialize;
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.

2)使用COM后期绑定

program WMI_Test;

{$APPTYPE CONSOLE}

uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;


Procedure AccessWMI;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
  Writeln(VarToStr(colItem.Caption));
end;


begin
 try
    CoInitialize(nil);
    try
      AccessWMI;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.

3)使用WMI库,如GLibWMI VCL Component Library

alt text

你可以在S.O中找到关于WMI and Delphi

的几个样本

答案 1 :(得分:1)

另一种选择是使用基于.NET Framework 2.0的Delphi 2007 .Net个性(如果您有RadStudio 2007)

检查此示例以从ManagementObjectSearcher控制台应用程序访问Delphi 2007 .Net

program WmiTest;
{$APPTYPE CONSOLE}
uses
  System.Management,
  SysUtils;
var
Searcher   : ManagementObjectSearcher ;
Collection : ManagementObjectCollection;
iter       : ManagementObjectCollection.ManagementObjectEnumerator;
WmiObject  : ManagementObject;
begin
  try
    Searcher  :=ManagementObjectSearcher.Create('SELECT Caption FROM Win32_OperatingSystem');
    Collection:=Searcher.Get();
    iter:=Collection.GetEnumerator;
    while(iter.MoveNext()) do
    begin
      WmiObject:=ManagementObject(iter.Current);
      Writeln(WmiObject['Caption']);
    end;
    Readln;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.