Detect power status of laptop

  • Thread starter Thread starter Jacob Thastrup.com
  • Start date Start date
J

Jacob Thastrup.com

Hi

How do I detect whether my laptop is running on battery or using the
power outlet?

Thanks

Jacob Thastrup
 
look at the Win32_PortableBattery class under WMI.
check the Availability, BatteryStatus and Status properties - those might be
what you're looking for.

hope this helps..
Imran.
 
* "Jacob Thastrup.com said:
How do I detect whether my laptop is running on battery or using the
power outlet?

My FAQ:

Information about the system power status (battery status) can be obtained
using the 'GetSystemPowerStatus' platform function:

\\\
Imports System.IO
Imports System.Runtime.InteropServices
..
..
..
Private Declare Auto Function GetSystemPowerStatus Lib "kernel32.dll" ( _
ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS _
) As Boolean

<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEM_POWER_STATUS
Public ACLineStatus As ACLineStatus
Public BatteryFlag As BatteryFlag
Public BatteryLifePercent As Byte
Public Reserved1 As Byte
Public BatteryLifeTime As Int32
Public BatteryFullLifeTime As Int32
End Structure

Public Enum ACLineStatus As Byte
Offline = 0
Online = 1
UnknownStatus = 255
End Enum

<Flags()> _
Public Enum BatteryFlag As Byte
High = 1
Low = 2
Critical = 4
Charging = 8
NoSystemBattery = 128
UnknownStatus = 255
End Enum
..
..
..
Dim sps As SYSTEM_POWER_STATUS
If GetSystemPowerStatus(sps) Then
Dim sw As New StringWriter()
With sps
sw.WriteLine("ACLineStatus: " & .ACLineStatus.ToString())
sw.WriteLine("BatteryFlag: " & .BatteryFlag.ToString())
sw.WriteLine( _
"BatteryLifePercent: " & _
IIf( _
.BatteryLifePercent = 255, _
"Unknown", _
.BatteryLifePercent.ToString() & " percent" _
) _
)
sw.WriteLine( _
"BatteryLifeTime: " & _
IIf( _
.BatteryLifeTime = -1, _
"Unknown", _
.BatteryLifeTime.ToString() & " seconds" _
) _
)
sw.WriteLine( _
"BatteryFullLifeTime: " & _
IIf( _
.BatteryFullLifeTime = -1, _
"Unknown", _
.BatteryFullLifeTime.ToString() & " seconds" _
) _
)
End With
MsgBox(sw.ToString())
Else

' An error occured.
End If
///
 
Herfried,

I asssume this is no code directly written by you, IIF?

:-)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top