Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Akku ID auslesen (https://www.delphipraxis.net/118426-akku-id-auslesen.html)

hathor 11. Aug 2008 18:12

Re: Akku ID auslesen
 
Liste der Anhänge anzeigen (Anzahl: 1)
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.

Strahlex 12. Aug 2008 08:15

Re: Akku ID auslesen
 
Danke hathor,
aber leider scheint die WMI mit fpc nicht zu funktionieren (keine WbemScripting_TLB). :(

hathor 12. Aug 2008 11:09

Re: Akku ID auslesen
 
Zitat:

Zitat von Strahlex
Danke hathor,
aber leider scheint die WMI mit fpc nicht zu funktionieren (keine WbemScripting_TLB). :(

BAHNHOF ?

Hat man Dir verboten, die Suchfunktion zu nutzen?
Oder zu kopieren?


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:21 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz