Thread: Writing current battery charge to a float value?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Writing current battery charge to a float value?

    I've found examples on the web for this- none of which are for C. Can anyone provide an example of a C program that scans a laptop's battery charge and writes that to a float value? I'm not at all sure how to go about doing this.

    Thanks in advance!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    40
    That's C++, but beggars can't be picky. Thanks! I'll see what I can do with this. B-)

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Flotonic View Post
    That's C++, but beggars can't be picky. Thanks! I'll see what I can do with this. B-)
    It is NOT C++... You can do that very easily in standard C....

    What it is is a simple windows API call... Like most of them, you provide the requisite struct, call the function and then extract the data you need...

    Here's what the GetSystemPowerStatusEx2 function returns...
    Code:
    typedef struct _SYSTEM_POWER_STATUS_EX2 {
      BYTE ACLineStatus;
      BYTE BatteryFlag;
      BYTE BatteryLifePercent;
      BYTE Reserved1;
      DWORD BatteryLifeTime;
      DWORD BatteryFullLifeTime;
      BYTE Reserved2;
      BYTE BackupBatteryFlag;
      BYTE BackupBatteryLifePercent;
      BYTE Reserved3;
      DWORD BackupBatteryLifeTime;
      DWORD BackupBatteryFullLifeTime;
      DWORD BatteryVoltage;
      DWORD BatteryCurrent;
      DWORD BatteryAverageCurrent;
      DWORD BatteryAverageInterval;
      DWORD BatterymAHourConsumed;
      DWORD BatteryTemperature;
      DWORD BackupBatteryVoltage;
      BYTE BatteryChemistry;
      //  Add any extra information after the BatteryChemistry member.
    } SYSTEM_POWER_STATUS_EX2, *PSYSTEM_POWER_STATUS_EX2, *LPSYSTEM_POWER_STATUS_EX2;
    In C.... #include <winbase.h> and link with coredll.lib....
    Code:
    float GetBatteryTime(void)
      { SYSTEM_POWER_STATUS_EX2  Stat;
         if (GetSystemPowerStatusEx2(&Stat,sizeof(Stat),TRUE))
           return (float) Stat.BatteryLifeTime;      
         else 
           return -1;  }  // error message;
    Really... it's not that hard.
    Last edited by CommonTater; 04-01-2011 at 11:01 AM.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    40
    Ahh. I got confused because most of the sources on Microsoft.com are in non-C languages. Again, thanks! You did a good job breaking it all down.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Flotonic View Post
    Ahh. I got confused because most of the sources on Microsoft.com are in non-C languages. Again, thanks! You did a good job breaking it all down.
    90% of the Windows API is written in C .... some of the newer stuff is C++...

  7. #7

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Codeplug View Post
    GetSystemPowerStatus Function (Windows)
    SYSTEM_POWER_STATUS Structure (Windows)

    Assuming you're not using Windows Embedded CE...

    gg
    Yep... that'll probably be better for laptops... Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Variables & updating Variables
    By DrC in forum C Programming
    Replies: 9
    Last Post: 01-30-2011, 02:46 AM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. help w/another program!!!
    By edshaft in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 11:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM