Results 1 to 15 of 15

Thread: Getting a battery level

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    24

    Getting a battery level

    I'm trying to write a visual basic 6 DLL (ActiveX) that can read the battery level on my laptop and return it (specifically, I'm using it as a COM object in mIRC).

    How might I get the level of my battery (Maximum charge and current charge)? I've looked through google, but I'm afraid I haven't been able to find anything helpful.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Getting a battery level

    Hi there,
    Take a look here:
    http://www.developerfusion.co.uk/show/130

    Good luck.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    24

    Re: Getting a battery level

    Thanks. I shoudl be able to use a Dim/Private/similar to create this control in a class module, right? There are no forms in this since it's a DLL >_>

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Getting a battery level

    I believe you should be able to.

    Good luck with your project.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    24

    Re: Getting a battery level

    Thanks. I am having some problems though...

    Option Explicit

    Public Function main()
    Dim Sys As SysInfoLib.SysInfo
    main = Sys.BatteryLifePercent <- this line creates the error below
    End Function

    Creates run-time error 91, "Object Variable or With Block not set." This is in a class module. I've tried adding "New" in there, but that just tells me it's an improper use of the new keyword. Is there anything you can see that I'm doing wrong?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Getting a battery level

    Try this
    Code:
    Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As _
    SYSTEM_POWER_STATUS) As Long
    
    Private Type SYSTEM_POWER_STATUS
            ACLineStatus As Byte
            BatteryFlag As Byte
            BatteryLifePercent As Byte
            Reserved1 As Byte
            BatteryLifeTime As Long
            BatteryFullLifeTime As Long
    End Type
    
    Private Sub Command1_Click()
    Dim MyStatus As SYSTEM_POWER_STATUS
    Dim MyLifeTime As Double
    GetSystemPowerStatus MyStatus
    MyLifeTime = MyStatus.BatteryLifeTime
    If MyLifeTime = -1 Then
        MsgBox "Using AC Power"
    Else
        MsgBox "I have " & MyLifeTime / 3600 & " hours remaining on battery."
    End If
    End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    24

    Re: Getting a battery level

    Actually I trie that first, and it doesn't return the correct info on my battery. Well, actually it doesnt' return any at all XD

    Anyways, I fixed it. I created a form, then in the class module I created an instance of the form, "Dim Sys As New Form1" and later used "Batt = Sys.Sysinfo1.BatteryLevelPercent" (Or whatever the name of that specific property was).

  8. #8
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Getting a battery level

    Very cool Hack. I love learning about APIs.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Getting a battery level

    Quote Originally Posted by nmadd
    Very cool Hack. I love learning about APIs.
    Have a look here. API Heaven

  10. #10
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Getting a battery level

    Quote Originally Posted by Hack
    Have a look here. API Heaven
    Oh yeah! Where's the drooling smiley?

  11. #11
    New Member
    Join Date
    Jul 2010
    Posts
    1

    Re: Getting a battery level

    Quote Originally Posted by Topkasa View Post
    Thanks. I am having some problems though...

    Option Explicit

    Public Function main()
    Dim Sys As SysInfoLib.SysInfo
    main = Sys.BatteryLifePercent <- this line creates the error below
    End Function

    Creates run-time error 91, "Object Variable or With Block not set." This is in a class module. I've tried adding "New" in there, but that just tells me it's an improper use of the new keyword. Is there anything you can see that I'm doing wrong?
    Hey man, you have to instantiate your object before using it, so replace:
    Dim Sys As SysInfoLIb.SysInfo by:
    Dim Sys As new SysInfoLib.SysInfo
    good luck

  12. #12
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Getting a battery level

    Just put an instance of the SysInfo control on a Form like any other control. You're fighting the obvious approach.

    This also gets you normal control event binding so you can detect changes without polling the value.

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Getting a battery level

    Am I the only one noticing that this thread is 3 years old?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Getting a battery level

    michelnacouzi fooled us all
    Actually it's me with another user account so you don't know your powerful foe.Trying to create an app so i can get the battery energy of all the laptops in the word so i can became the great X-Man Energlaptoper.
    Sorry 8 hours of straight computing is getting to my brain.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Getting a battery level

    haha yeah I think someone needs to go to bed
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width