// this function determines the predicted time that laptop battery will last.
// It may be used for local or remote host. When used for local host,
// all the parameters must be empty. For remote computer, parameters must be:
// TargetHost: name or IP address of the host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
function DetermineBatteryTime(TargetHost, UserName, Password: string): integer;
var
vConnection: TWmiConnection;
vQuery: TWmiQuery;
begin
// if battery not found then return -1;
Result := -1;
// setup: create and link the components together
vConnection := TWmiConnection.Create(nil);
vQuery := TWmiQuery.Create(nil);
vQuery.Connection := vConnection;
vQuery.WQL.Text := 'SELECT * FROM Win32_Battery';
try
// if remote host specified, setup the credentials
if (TargetHost <> '') then
begin
vConnection.MachineName := TargetHost;
vConnection.Credentials.UserName := UserName;
vConnection.Credentials.Password := Password;
end;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
vQuery.Active := true;
except
Exit;
end;
if not vQuery.EOF then
Result := vQuery.FieldByName('EstimatedRunTime').AsInteger;
finally;
vQuery.Free;
vConnection.Free;
end;
end;
|