Recent

Author Topic: UtilWMI issue - not returning all values.  (Read 498 times)

teco

  • New Member
  • *
  • Posts: 31
UtilWMI issue - not returning all values.
« on: October 07, 2024, 04:17:15 pm »
Hi,
I am using Utilwmi from Jurassic Pork and have a little issue with it. The following code does not return all values.

Lazarus Version I am using: 2.2.6

Any Idea why I can not read the values for the Battery Capacity, etc.

Parameters are from the Microsoft Webpage:
https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Windows, contnrs, utilwmi, ComObj, Variants, ActiveX;
  3. ....
  4.  
  5.   procedure TForm1.Start1Click(Sender: TObject);
  6. var
  7.   tmpint1 : int64;   //not currently used. Reserved for catching multiple batteries
  8.   f, j : integer;
  9.   retVal : string;
  10.   WMIResult : TFPObjectList;
  11. begin
  12.   ausgabe1.Clear;     //TMemo
  13.  
  14. // This responds with OK  
  15.     WMIResult := GetWMIInfo('Win32_Battery', ['Status']);
  16.     if (WMIResult.Count > 0) then
  17.     begin j := 0;
  18.       retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  19.       ausgabe1.append(j.ToString + ' C Battery : ' + retVal);
  20.     end;
  21.  
  22. // This responds with <Null>
  23.     WMIResult := GetWMIInfo('Win32_Battery', ['DesignCapacity']);
  24.     if (WMIResult.Count > 0) then
  25.     begin j := 0;
  26.       retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  27.       ausgabe1.append(j.ToString + ' C DC : ' + retVal);
  28.     end;
  29.  
  30. // This responds with <Null>
  31.      WMIResult := GetWMIInfo('Win32_Battery', ['FullChargeCapacity']);
  32.     if (WMIResult.Count > 0) then
  33.     begin j := 0;
  34.       retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  35.       ausgabe1.append(j.ToString + ' C FCC : ' + retVal);
  36.     end;
  37.  
  38. // This responds with <Null>
  39.      WMIResult := GetWMIInfo('CIM_Battery', ['TimeOnBattery']);
  40.     if (WMIResult.Count > 0) then
  41.     begin j := 0;
  42.       retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  43.       ausgabe1.append(j.ToString + ' C TOB : ' + retVal);
  44.     end;
  45.  
  46. // This responds with <Null>
  47.     WMIResult := GetWMIInfo('CIM_Battery', ['EstimatedChargeRemaining']);
  48.     if (WMIResult.Count > 0) then
  49.     begin j := 0;
  50.       retVal := TStringList(WMIResult[f]).ValueFromIndex[j];
  51.       ausgabe1.append(j.ToString + ' C ECR : ' + retVal);
  52.     end;
  53.  
  54.  
  55.  
  56.    WMIResult.Free;
  57.   //readln;
  58.  
  59. end;          
  60.  
  61.  
  62.  

jamie

  • Hero Member
  • *****
  • Posts: 6735
The only true wisdom is knowing you know nothing

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1252
Re: UtilWMI issue - not returning all values.
« Reply #2 on: October 08, 2024, 01:01:00 pm »
Hello,
use wmi explorer to see what are the WMI classes and properties availables for your windows O.S
Friendly, J.P

« Last Edit: October 08, 2024, 01:10:33 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

teco

  • New Member
  • *
  • Posts: 31
Re: UtilWMI issue - not returning all values.
« Reply #3 on: October 08, 2024, 02:33:14 pm »
Hi,
Thank you for your help.
You are right. Designed Capacity and some other values are not included in the CIMV2 Namespace, but in WMI Namespace.

Can you extend your Utilwmi.pas to select additional the namespace. It is currently fixed to CIMV2.

Thank you.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1252
Re: UtilWMI issue - not returning all values.
« Reply #4 on: October 09, 2024, 07:58:44 am »
Hello,
try the version in attachment
Code: Pascal  [Select][+][-]
  1. function  GetWMIInfo(const WMIClass: string; const WMIPropertyNames: Array of String;
  2.                      const Condition: string = '';
  3.                      const WMIroot: string = 'root\CIMV2'): TFPObjectList;

Example of use :
 
Code: Pascal  [Select][+][-]
  1. WMIResult := GetWMIInfo('BatteryFullChargedCapacity',['FullChargedCapacity'],'','root\WMI');

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

teco

  • New Member
  • *
  • Posts: 31
Re: UtilWMI issue - not returning all values.
« Reply #5 on: October 09, 2024, 09:27:08 am »
Works perfectly.

You can get now information from all Namespaces...


Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: UtilWMI issue - not returning all values.
« Reply #6 on: October 09, 2024, 10:36:53 am »
Just battery level is easy:
Code: Pascal  [Select][+][-]
  1. program BatteryLevel;
  2. {$apptype console}{$mode objfpc}
  3. uses
  4.   SysUtils, Windows;
  5.  
  6. var
  7.   PowerStatus: TSystemPowerStatus;
  8.   BatteryLifePercent: Byte;
  9.  
  10. begin
  11.   if GetSystemPowerStatus(PowerStatus) then
  12.   begin
  13.     BatteryLifePercent := PowerStatus.BatteryLifePercent;
  14.     if BatteryLifePercent <> 255 then
  15.       WriteLn('Battery level: ', BatteryLifePercent, '%')
  16.     else
  17.       WriteLn('Battery level: Unknown');
  18.   end
  19.   else
  20.     WriteLn('Unable to retrieve battery status');
  21.     Readln;
  22. end.
Code is NOT by me.

On linux you can open the file '/sys/class/power_supply/BAT0/capacity'
if it is available.
Code: Pascal  [Select][+][-]
  1. program BatteryLevelLinux;
  2. {$mode objfpc}{$I-}
  3. uses
  4.   SysUtils;
  5.  
  6. function ReadBatteryLevel: Integer;
  7. var
  8.   BatteryFile: Text;
  9.   Line: string;
  10.   BatteryLevel: Integer;
  11. begin
  12.   Assign(BatteryFile, '/sys/class/power_supply/BAT0/capacity');
  13.   Reset(BatteryFile);
  14.   if IOresult <> 0 then
  15.   begin
  16.     writeln('Battery not present?');
  17.     halt(1);
  18.   end;
  19.   ReadLn(BatteryFile, Line);
  20.   BatteryLevel := StrToInt(Line);
  21.   Result := BatteryLevel;
  22.   Close(BatteryFile);
  23. end;
  24.  
  25. begin
  26.     WriteLn('Battery level: ', ReadBatteryLevel, '%');
  27. end.
That code is by me, but does not work on all systems.....

« Last Edit: October 09, 2024, 11:59:42 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018