当前位置 : 主页 > 编程语言 > delphi >

delphi – 如何读取WMI参数“Data”和“InsertionStrings”?

来源:互联网 收集:自由互联 发布时间:2021-06-23
我尝试用WMI阅读 Windows EventLog – Win32_NTEventlogFile.我使用工具“WMI Delphi Code Creator”中的示例代码( link to WMI tutorial) procedure TEventLogsForm.GetWin32_NTLogEventInfo;const WbemUser =''; WbemPassword =''; Wbe
我尝试用WMI阅读 Windows EventLog – > Win32_NTEventlogFile.我使用工具“WMI Delphi Code Creator”中的示例代码( link to WMI tutorial)

procedure TEventLogsForm.GetWin32_NTLogEventInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NTLogEvent  Where Logfile="Application"','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    try
      // ????
      if not VarIsNull(FWbemObject.Data) then
        Showmessage(IntToStr(Integer(FWbemObject.Data)));// Array of Uint8
      if not VarIsNull(FWbemObject.InsertionStrings) then
        Showmessage(String(FWbemObject.InsertionStrings));

    except
      on E:Exception do
      begin
        MessageDlg(E.Message, mtError, [mbOK], 0);
      end;
    end;

    FWbemObject:=Unassigned;
  end;
end;

我尝试读取参数“FWbemObject.Data”和“FWbemObject.InsertionStrings”.
但我收到错误:类型的变体(数组变量)无法转换为类型(OleStr)

如何读取/显示此参数?

根据文件发现 here,
Data和InsertionString是Byte / String的数组;

下面我用它在for循环中迭代它们,我不知道是否有任何意义,但你可以用作例子,做你需要的任何事情:).

procedure GetWin32_NTLogEventInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
  Insertion     : array of String;
  Data          : array of Byte;
  I: integer;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NTLogEvent Where Logfile=''Application''','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    try

      if not VarIsNull(FWbemObject.Data) then
      begin
        Data := FWbemObject.Data;
        for I:= VarArrayLowBound(data,1) to VarArrayHighBound(data,1) do
          Showmessage(IntToStr(Data[I]));// Array of Uint8
      end;

      if not VarIsNull(FWbemObject.InsertionStrings) then
      begin
        Insertion := FWbemObject.InsertionStrings;            
        for I:= VarArrayLowBound(Insertion,1) to VarArrayHighBound(Insertion,1) do
          Showmessage(Insertion[I]);
      end;

    except
      on E:Exception do
      begin
        MessageDlg(E.Message, mtError, [mbOK], 0);
      end;
    end;

    FWbemObject:=Unassigned;
  end;
end;

这个link有几个例子,但它是在VB中写的

网友评论