如何在Delphi中使用ManagementObjectSearcher?

发布于 2024-09-18 08:15:44 字数 241 浏览 7 评论 0原文

我在互联网上找到了,但我不知道如何在delphi中使用 ManagementObjectSearcher 。我的主要问题是我必须在“使用”中添加哪个文件。

我找到了一个代码但是无法让它在我的系统中运行。

I found on internet but I didn't got how can I use ManagementObjectSearcher in delphi. My main question which file I have to add in 'uses'.

I found one code but can't make it run in my system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-09-25 08:15:44

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

1) 从组件->导入组件导入Microsoft WMIScripting 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 组件库

< img src="https://i.sstatic.net/Vzyq1.jpg" alt="alt text">

您可以在 SO 中找到有关 WMI 和 Delphi

Himadri, the code wich you refer uses the ManagementObjectSearcher in Delphi Prism, the ManagementObjectSearcher is a .Net class for retrieve information about the WMI. you can access the WMI from delphi using 3 alternatives.

1) importing the Microsoft WMIScripting Library from Component->Import Component and then select 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) use a COM late binding

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) using a WMI library, like the GLibWMI VCL Component Library

alt text

you can found several samples in S.O about the WMI and Delphi

木格 2024-09-25 08:15:44

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

请检查此示例以从 ManagementObjectSearcher 访问code>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.

Another option is use the Delphi 2007 .Net personality (if you have the RadStudio 2007) which is based on .NET Framework 2.0

check this sample to access the ManagementObjectSearcher from an Delphi 2007 .Net console application.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文