Question HOW: Getting this code to work

metalm3

New member
Joined
Jul 21, 2010
Messages
4
Programming Experience
Beginner
Platform: SharpDeveloper

Okay, basically, I've just gotten this piece of code to get the Battery Level on a notebook.

VB.NET:
1.	Private Type SYSTEM_POWER_STATUS
2.	
3.	        ACLineStatus As Byte
4.	
5.	        BatteryFlag As Byte
6.	
7.	        BatteryLifePercent As Byte
8.	
9.	        Reserved1 As Byte
10.	
11.	        BatteryLifeTime As Long
12.	
13.	        BatteryFullLifeTime As Long
14.	
15.	End Type
16.	
17.	Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
18.	
19.	Private Sub Form_Paint()
20.	
21.	    'KPD-Team 2000
22.	
23.	    'URL: [url=http://www.allapi.net/]allapi.net[/url]
24.	
25.	    'E-Mail: [email]KPDTeam@Hotmail.com[/email]
26.	
27.	    Dim SPS As SYSTEM_POWER_STATUS
28.	
29.	    'get the battery powerstatus
30.	
31.	    GetSystemPowerStatus SPS
32.	
33.	    Me.AutoRedraw = True
34.	
35.	    'show some information
36.	
37.	    Select Case SPS.ACLineStatus
38.	
39.	        Case 0
40.	
41.	            Me.Print "AC power status: Offline"
42.	
43.	        Case 1
44.	
45.	            Me.Print "AC power status: OnLine"
46.	
47.	        Case 2
48.	
49.	            Me.Print "AC power status: Unknown"
50.	
51.	    End Select
52.	
53.	    Select Case SPS.BatteryFlag
54.	
55.	        Case 1
56.	
57.	            Me.Print "Battery charge status: High"
58.	
59.	        Case 2
60.	
61.	            Me.Print "Battery charge status: Low"
62.	
63.	        Case 4
64.	
65.	            Me.Print "Battery charge status: Critical"
66.	
67.	        Case 8
68.	
69.	            Me.Print "Battery charge status: Charging"
70.	
71.	        Case 128
72.	
73.	            Me.Print "Battery charge status: No system battery"
74.	
75.	        Case 255
76.	
77.	            Me.Print "Battery charge status: Unknown Status"
78.	
79.	    End Select
80.	
81.	End Sub

Problem is, I've no idea as to how to go ahead and utilise this piece of code to get the desired battery level. Maybe... do I need to perhaps insert a label to display the Print statuses?

I've also realised it's the norm for people to post codes like this when it comes to VB.NET. But how do the coder, who uses the code, get the code working? (Definitely not copy-pasting everything, yeah?)

So I guess a general answer is enough. :) Pls help.

Thanks. :)
 
Here's an example using Enums to get the status back so you don't have Select Case statements all over the place.

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sps As BatteryStatus.SYSTEM_POWER_STATUS = BatteryStatus.GetStatus

        MessageBox.Show(String.Concat("AC power status: ", [Enum].GetName(GetType(BatteryStatus.ACLineStatus), sps.ACLineStatus)))

        Select Case sps.ACLineStatus
            Case 0
                MessageBox.Show("AC power status: Offline")
            Case 1
                MessageBox.Show("AC power status: Online")
            Case 255
                MessageBox.Show("AC power status: Unknown")
        End Select

        MessageBox.Show(String.Concat("Battery charge status: ", [Enum].GetName(GetType(BatteryStatus.BatteryFlag), sps.BatteryFlag)))

        Select Case sps.BatteryFlag
            Case 1
                MessageBox.Show("Battery charge status: High")
            Case 2
                MessageBox.Show("Battery charge status: Low")
            Case 4
                MessageBox.Show("Battery charge status: Critical")
            Case 8
                MessageBox.Show("Battery charge status: Charging")
            Case 128
                MessageBox.Show("Battery charge status: No system battery")
            Case 255
                MessageBox.Show("Battery charge status: Unknown Status")
        End Select

        MessageBox.Show(String.Concat("Battery LifeTime ", sps.BatteryLifeTime))
    End Sub
End Class

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
 
Last edited:

Latest posts

Back
Top