Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,621 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,952 people online right now. Registration is fast and FREE... Join Now!





Battery life snippet

Prints out the remaining battery-life, and how much time there is remaining, if it can be determined.

Submitted By: dance_monkey()
Actions:
Rating:
Views: 3,701

Language: C++

Last Modified: July 3, 2008

Snippet


  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. #define UNKNOWN         0xFFFFFFFF
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.         SYSTEM_POWER_STATUS status;
  10.  
  11.         GetSystemPowerStatus( &status );
  12.         int life = status.BatteryLifePercent;
  13.         int secs = status.BatteryLifeTime;
  14.  
  15.         cout << life << "%   ->   ";
  16.         switch (status.BatteryFlag) {
  17.                case 1: cout << "High";
  18.                     break;
  19.                case 2: cout << "Low";
  20.                     break;
  21.                case 4: cout << "Critical";
  22.                     break;
  23.                case 8: cout << "Charging";
  24.                     break;
  25.                case 128: cout << "No system battery";
  26.                     break;
  27.                case 256: cout << "Unknown status";
  28.                     break;
  29.         }
  30.  
  31.         if (secs == UNKNOWN) {
  32.                 cout << endl << "Amount of time remaining is unkown";
  33.         }
  34.         else cout << endl << secs << " seconds remaining";
  35.        
  36.         Sleep( 2000 );
  37. }

Copy & Paste


Comments


Rudif 2008-05-25 10:42:02

Warning : the snippet should be corrected, from if (secs = UNKNOWN) {... to if (secs == UNKNOWN) {...

Codegamer 2008-07-03 03:14:51

Thank you for this!!

dance_monkey() 2008-07-03 12:10:31

XD, thanks Rudif, I missed that.

JoaoPatrx 2008-07-28 15:07:34

Hi! please help me out, i'm not being able to compile this with cegcc so I can use it on my ppc... Here's what I get: /tmp/ccx3UC6r.o:battery.cpp:(.text+0xcc): undefined reference to `GetSystemPowerStatus(_SYSTEM_POWER_STATUS*)' collect2: ld returned 1 exit status

JoaoPatrx 2008-07-28 15:26:07

Sorry, actually this is what i get without changing any of the presented code: battery.cpp: In function 'int main()': battery.cpp:17: error: 'SYSTEM_POWER_STATUS' was not declared in this scope battery.cpp:17: error: expected `;' before 'sps' battery.cpp:18: error: 'sps' was not declared in this scope battery.cpp:18: error: 'GetSystemPowerStatus' was not declared in this scope Please help!!!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month