Thema: Delphi Akku ID auslesen

Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#11

Re: Akku ID auslesen

  Alt 11. Aug 2008, 18:12
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Activex, WbemScripting_TLB, StdCtrls;


type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure WMIbattery(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetWMIstring (wmiHost, wmiClass, wmiProperty : string):string;
var // These are all needed for the WMI querying process
  Locator: ISWbemLocator;
  Services: ISWbemServices;
  SObject: ISWbemObject;
  ObjSet: ISWbemObjectSet;
  SProp: ISWbemProperty;
  Enum: IEnumVariant;
  Value: Cardinal;
  TempObj: OleVariant;
  SN: string;
begin
  try
  Locator := CoSWbemLocator.Create;
   Services := Locator.ConnectServer(wmiHost, 'root\cimv2', '', '', '','', 0, nil);
  ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
    wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
  Enum := (ObjSet._NewEnum) as IEnumVariant;
  while (Enum.Next(1, TempObj, Value) = S_OK) do
  begin
    SObject := IUnknown(tempObj) as ISWBemObject;
    SProp := SObject.Properties_.Item(wmiProperty, 0);
    if VarIsNull(SProp.Get_Value) then
      result := ''
    else
    begin
      SN := SProp.Get_Value;
      result := SN;
    end;
  end;
  except // Trap any exceptions (Not having WMI installed will cause one!)
   on exception do
    result := '';
   end;
end;

function GetWMIstring2 (wmiHost, wmiClass, wmiProperty : string):string;
var // These are all needed for the WMI querying process
  Locator: ISWbemLocator;
  Services: ISWbemServices;
  SObject: ISWbemObject;
  ObjSet: ISWbemObjectSet;
  SProp: ISWbemProperty;
  Enum: IEnumVariant;
  Value: Cardinal;
  TempObj: OleVariant;
  SN: string;
begin
  try
  Locator := CoSWbemLocator.Create;
   Services := Locator.ConnectServer(wmiHost, 'root\wmi', '', '', '','', 0, nil);
  ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
    wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
  Enum := (ObjSet._NewEnum) as IEnumVariant;
  while (Enum.Next(1, TempObj, Value) = S_OK) do
  begin
    SObject := IUnknown(tempObj) as ISWBemObject;
    SProp := SObject.Properties_.Item(wmiProperty, 0);
    if VarIsNull(SProp.Get_Value) then
      result := ''
    else
    begin
      SN := SProp.Get_Value;
      result := SN;
    end;
  end;
  except
   on exception do
    result := '';
   end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
WMIbattery(Self);
end;

procedure TForm1.WMIbattery(Sender: TObject);
var tmpstr, FullChargedCapacity, RemainingCapacity, addstr : string;
    tmp,Difference : integer;
Begin
Memo1.Clear;
  tmpstr := getWMIstring('','Win32_Battery','DesignVoltage');
  Memo1.lines.add(' DesignVoltage : '+ tmpstr + ' mVolt ');

  tmpstr := getWMIstring2('','BatteryStatus','Voltage');
  Memo1.lines.add(' Voltage : '+tmpstr + ' mVolt ');

  tmpstr := getWMIstring('','Win32_Battery','EstimatedRunTime');
  if StrToInt(tmpstr)<1000 then
  Memo1.lines.add(' EstimatedRunTime : '+tmpstr + ' Minutes ');

  tmpstr := getWMIstring('','Win32_Battery','BatteryStatus');
  tmp:= StrToInt(tmpstr);
  case tmp of
  1: addstr:='Other';
  2: addstr:='Unknown';
  3: addstr:='Fully Charged';
  4: addstr:='Low';
  5: addstr:='Critical';
  6: addstr:='Charging';
  7: addstr:='Charging and High';
  8: addstr:='Charging and Low';
  9: addstr:='Charging and Critical';
  10: addstr:='Undefined';
  11: addstr:='Partially Charged';
  end;
  Memo1.lines.add(' BatteryStatus : '+ addstr);

  tmpstr := getWMIstring2('','BatteryFullChargedCapacity','FullChargedCapacity');
  Memo1.lines.add(' FullChargedCapacity : '+tmpstr + ' mWatt/h ');
  FullChargedCapacity:=tmpstr;

  tmpstr := getWMIstring2('','BatteryStatus','RemainingCapacity');
  Memo1.lines.add(' RemainingCapacity : '+tmpstr + ' mWatt/h ');
  RemainingCapacity:=tmpstr;

  Difference:=StrToInt(FullChargedCapacity)- StrToInt(RemainingCapacity);
  Memo1.lines.add(' Difference : '+IntToStr(Difference) + ' mWatt ');

  tmpstr := getWMIstring2('','BatteryStatus','Chargerate');
  Memo1.lines.add(' Chargerate : '+tmpstr + ' mWatt/h ');

  tmpstr := getWMIstring2('','BatteryStatus','DisChargerate');
  Memo1.lines.add(' DisChargerate : '+tmpstr + ' mWatt/h ');

  tmpstr := getWMIstring('','Win32_Battery','DeviceID');
  Memo1.lines.add(' DeviceID : '+tmpstr);
end;

end.
Miniaturansicht angehängter Grafiken
batterystatus_877.jpg  
  Mit Zitat antworten Zitat