如何获取 WbemScripting 查询返回的列的名称?

发布于 2024-10-19 17:07:41 字数 1446 浏览 6 评论 0原文

我有以下过程来运行 WMI 查询,并且效果非常好。

procedure TFormMain.GetWMIOSInfo(const RemoteMachine, Username, Password: string);
var
  FSWbemLocator: OLEVariant;
  FWMIService: OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject: OLEVariant;
  oEnum: IEnumvariant;
  iValue: LongWord;
begin;
  try
    FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
    FWMIService := FSWbemLocator.ConnectServer(RemoteMachine, 'root\CIMV2', Username, Password);
    FWbemObjectSet := FWMIService.ExecQuery(
        'select screenwidth, screenheight, status from Win32_DesktopMonitor','WQL', 0);
    try
      oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      while oEnum.Next(1, FWbemObject, iValue) = 0 do
        begin
          Listbox1.Items.Add(
            VarToStr(FWbemObject.availability) + ', ' + 
            VarToStr(FWbemObject.screenwidth)  + ', ' +
            VarToStr(FWbemObject.screenheight));

          FWbemObject := Unassigned;
        end;

    finally
      FWbemObjectSet := Unassigned;
    end;

  Except on E: Exception do
    Raise;
  end;
end;

我想更改查询以返回所有字段,例如 select * from Win32_DesktopMonitor。我的问题是我不知道如何确定 FWbemObject 中查询返回的列的名称。 IE。我想枚举 FWbemObject 中的列。

Listbox1.Items.Add(
  VarToStr(FWbemObject.<?>) + ', ' + 
  VarToStr(FWbemObject.<?>)  + ', ' +
  ....
  VarToStr(FWbemObject.<?>));

I have the following procedure to run a WMI query, and it works perfectly well.

procedure TFormMain.GetWMIOSInfo(const RemoteMachine, Username, Password: string);
var
  FSWbemLocator: OLEVariant;
  FWMIService: OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject: OLEVariant;
  oEnum: IEnumvariant;
  iValue: LongWord;
begin;
  try
    FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
    FWMIService := FSWbemLocator.ConnectServer(RemoteMachine, 'root\CIMV2', Username, Password);
    FWbemObjectSet := FWMIService.ExecQuery(
        'select screenwidth, screenheight, status from Win32_DesktopMonitor','WQL', 0);
    try
      oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      while oEnum.Next(1, FWbemObject, iValue) = 0 do
        begin
          Listbox1.Items.Add(
            VarToStr(FWbemObject.availability) + ', ' + 
            VarToStr(FWbemObject.screenwidth)  + ', ' +
            VarToStr(FWbemObject.screenheight));

          FWbemObject := Unassigned;
        end;

    finally
      FWbemObjectSet := Unassigned;
    end;

  Except on E: Exception do
    Raise;
  end;
end;

I would like to alter the query to return all the fields like select * from Win32_DesktopMonitor. My problem is that I do not know how to determine the names of the columns that are returned by the query in FWbemObject. ie. I'd like to enumerate the columns in the FWbemObject.

Listbox1.Items.Add(
  VarToStr(FWbemObject.<?>) + ', ' + 
  VarToStr(FWbemObject.<?>)  + ', ' +
  ....
  VarToStr(FWbemObject.<?>));

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

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

发布评论

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

评论(2

假情假意假温柔 2024-10-26 17:07:41

Pieter,您必须使用 SWbemObject.Properties_ 的属性SWbemObject 对象。

检查这个样本。

program GetWMI_Info;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;


procedure  GetWin32_DesktopMonitorInfo;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  FProperties   : OLEVariant;
  oEnumProp     : IEnumvariant;
  iValueProp    : LongWord;
  FPropObj      : OLEVariant;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DesktopMonitor','WQL',0);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin

    FProperties   := FWbemObject.Properties_;
    oEnumProp     := IUnknown(FProperties._NewEnum) as IEnumVariant;
    while oEnumProp.Next(1, FPropObj, iValueProp) = 0 do
    begin
       Writeln(FPropObj.Name);
       FPropObj:=Unassigned; //prevent memory leak
    end;

    FWbemObject:=Unassigned;//prevent memory leak
  end;
end;


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

Pieter you must use the SWbemObject.Properties_ Property of the SWbemObject Object.

check this sample.

program GetWMI_Info;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;


procedure  GetWin32_DesktopMonitorInfo;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  FProperties   : OLEVariant;
  oEnumProp     : IEnumvariant;
  iValueProp    : LongWord;
  FPropObj      : OLEVariant;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_DesktopMonitor','WQL',0);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin

    FProperties   := FWbemObject.Properties_;
    oEnumProp     := IUnknown(FProperties._NewEnum) as IEnumVariant;
    while oEnumProp.Next(1, FPropObj, iValueProp) = 0 do
    begin
       Writeln(FPropObj.Name);
       FPropObj:=Unassigned; //prevent memory leak
    end;

    FWbemObject:=Unassigned;//prevent memory leak
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_DesktopMonitorInfo;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    begin
        Writeln(E.Classname, ':', E.Message);
        Readln;
    end;
  end;
end.
踏雪无痕 2024-10-26 17:07:41

SWbemObject 接口公开 Properties_属性,它是一个集合(因此您可以通过与枚举 ExecQuery 返回的 SWebmObjectSet 接口相同的方式来枚举它)。此集合的项目是 SWbemProperty公开 NameValue 属性的接口。

The SWbemObject interface exposes the Properties_ property which is a collection (so you can enumerate it probably in the same way as you enumerate the SWebmObjectSet interface returned by ExecQuery). The items of this collection are SWbemProperty interfaces which expose Name and Value properties.

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