A question came up on our forums and so I investigated writing a wrapper for the GetSystemPowerState
API function. This allows you to retrieve the power state name, and also a bitmask
of flags - Is the backlight on, is the device password protected etc. This is the
result in VB.NET. We will add it to the wish list for the next version of the
library.
<DllImport("coredll.dll")>
_
PublicSharedFunction GetSystemPowerState(ByVal pBuffer As System.Text.StringBuilder, ByVal Length AsInteger, ByRef pFlags As PowerState) AsInteger
EndFunction
<Flags()> _
PublicEnum PowerState
[On] = &H10000 '//
on state
Off= &H20000 '
// no power, full off
Critical = &H40000 '//
critical off
Boot = &H80000 '
// boot state
Idle = &H100000 '
// idle state
Suspend = &H200000 '
// suspend state
Unattended = &H400000 '
// Unattended state.
Reset= &H800000 '
// reset state
UserIdle = &H1000000 '
// user idle state
BackLightOn = &H2000000 '
// device screen backlight on
Password = &H10000000 '
// This state is password protected.
EndEnum
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sb AsNew System.Text.StringBuilder(260)
Dim flags As PowerState = 0
Dim ret AsInteger= GetSystemPowerState(sb,
sb.Capacity, flags)
TextBox1.Text = sb.ToString()
TextBox2.Text = flags.ToString()
EndSub
The last method is just a very simple example of calling the function and displaying
the result.
Read the complete post at http://feeds.feedburner.com/~r/PeterFoot/~3/239987674/HowToGetSystemPowerStateNameAndFlags.aspx