Hack Forums
[.NET] [VB.NET]Check Battery Status - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]Check Battery Status (/showthread.php?tid=397057)



[VB.NET]Check Battery Status - codepoet - May 1, 2010

originally posted on dotnet-snippets.com

Description:

With this little class you can check the battery status of your notebook.

Code
Public Class BatteryStatus
    Private Declare Auto Function GetSystemPowerStatus Lib "kernel32.dll" ( _
        ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS) _
    As Integer

    Public Structure SYSTEM_POWER_STATUS
        Public ACLineStatus As ACLineStatus
        Public BatteryFlag As BatteryFlag
        Public BatteryLifePercent As Byte
        Public Reserved1 As Byte
        Public BatteryLifeTime As Integer
        Public BatteryFullLifeTime As Integer
    End Structure

    Public Enum BatteryFlag As Byte
        High = 1
        Low = 2
        Critical = 4
        Charging = 8
        NoSystemBattery = 128
        Unknown = 255
    End Enum

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

    Public Shared Function GetStatus() As SYSTEM_POWER_STATUS
        Dim SPS As New SYSTEM_POWER_STATUS
        GetSystemPowerStatus(SPS)
        Return SPS
    End Function
End Class


If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3


* - Dark_Knight_ - May 2, 2010

The one way to take the result is
Code
MsgBox(BatteryStatus.GetStatus.BatteryLifePercent)



* - julian - May 2, 2010

The structure should have an attribute applied (for stability):

Code
<StructLayout(LayoutKind.Sequential)> _
Structre ...

BatteryFlags should have the Flags() attribute applied, as it is a flags enum:

Code
<Flags()> _
Enum BatteryFlags as Byte
...



* - Mario - May 2, 2010

Lol gunna add this to my RAT.. check victoms Laptop battrey status.. why.. i dont know. but it will be cool


* - Dragon Hawk - May 2, 2010

WTF 72 views and 3 replies lol

thanks for sharing


* - pr0t0typ3 - May 2, 2010

Nice work... Btw where do you get all these ideas of using the windows dlls and Api Calls for that good functions?


* - codepoet - May 2, 2010

im glad you all like it.


* - Icy Wind - May 2, 2010

How come if i google any of "your" snippets i find them on dotnet-snippets.com?


* - Jeff. - May 2, 2010

He's just coping or he's the author of them @ dotnet-snippets.com ?


* - codepoet - May 2, 2010

if you read the whole post you can tell that i posted it there and now here