how can you get your battery life in c++

Hello there, my question is: Is there any possible why to have c++ tell you what your battery life is?

example output:

Computer: What would you like?

User: Battery Life.

Computer: Your current battery life is: 40%!

example input:

cout <<"What would you like?";

Q1(cin, getline);

if (Q1 == "Battery Life") {
//Here it would find what the battery life is

cout <<"Your current battery life is: " << //Here it would print the battery life

I want to know if this is even possible and if so how?
Of course it's possible, but it depends on your operating system.
On Windows:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa372693%28v=vs.85%29.aspx
I use winXP

Will it still work?
If have bothered to read what that page says, you will already have the answer.
@modoran

I posted before i clicked the link if there is a problem here though maybe we should settle it, the "gentlemen" like way of course.

and after i have read it i have to admit i am a little confused.
Last edited on
The Program runs but nothing happens this is my code:

1
2
3
4
5

int main()
{
      BOOL WINAPI GetSystemPowerStatus(__out  LPSYSTEM_POWER_STATUS lpSystemPowerStatus);
}
This should be in the Windows forum. You need to #include <Windows.h> for this...

Anyway, GetSystemPowerStatus() gets passed a LPSYSTEM_POWER_STATUS struct as an argument and returns true or false if it fails. You can follow the links and find:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa373232%28v=vs.85%29.aspx

which is what the LPSYSTEM_POWER_STATUS looks like.

so something like this:
1
2
3
4
5
6
LPSYSTEM_POWER_STATUS lpSysPwrStatus;
if (!GetSystemPowerStatus(lpSysPwrStatus)) return false;  // or return something meaningful
// you might need GetSystemPowerStatus(&lpSysPwrStatus)

//  Battery life is now
lpSysPwrStatus.BatteryLifePercent


My link has more things that the LPSYSTEM_POWER_STATUS holds.

Last edited on
That appears to be a function declaration. It is not an attempt to call the function.
Thx i will try and comply this.

i got an error msgbox that said this:

An unhandled exception of type 'System.AccessViolationException' occurred in hi.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

and asked if i wanted to break or continue

ps hi.exe is what i named it (i couldnt think of anything good) lol.
Last edited on
What should i do?

@lowestone
ps the link is broken
Last edited on
I was having the same trouble:

This works for me:
1
2
3
4
5
SYSTEM_POWER_STATUS status; // note not LPSYSTEM_POWER_STATUS
GetSystemPowerStatus(&status);

if (status.ACLineStatus == 1) errMsg("Online");
 //  errMsg() is a function I made that makes a quick message box with the phrase in there. 


~And indeed, my computer is plugged in.

MSDN makes it seem like you need to link to Kernel32.lib, but my little test runs fine without.
Last edited on
ok, if my computer is plugged in then i get the error msg (which i think is supposed to happen, right?)

and if it is unplugged then nothing happen (is that supposed to happen)

ps i am sorry if this is something retarded

i think i know what i did wrong am i supposed to add more things like:

if (status.ACLineStatus == 2) {

//Whatever it does

}

Refer to the article and the linked information about the SYSTEM_POWER_STATUS structure.
i cant my computer will not let me visit the msdn fourms at the moment for reasons i dont know. Even when i google it it just wont load.
Last edited on
Indeed, the first two lines there will get you all sorts of info about your power. It's up to you to figure out how to use it.

My example only showed me that my computer was plugged in (but most importantly, the code worked). If it wasn't, I would get zero. If something strange happened, I would get 255 (0xFF). To really be safe I should have done:
1
2
3
4
5
6
7
8
SYSTEM_POWER_STATUS status;
if (!GetSystemPowerStatus(&status))
{
  // Optionally figure out what happened via GetLastError()
  return false;
}

// then use 'status' to figure out what you want to know. 
Last edited on
Thank you so much I have one last question how do i get my exact battery percentage

nevermind i found out how. thank you everyone for trying to help!!!

and i got this to work properly here is the code i used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include "Windows.h"

using namespace std;

int main() {

	SYSTEM_POWER_STATUS status; // note not LPSYSTEM_POWER_STATUS
GetSystemPowerStatus(&status);

if (status.ACLineStatus == 1) {

	cout << "Charging";

}
else if (status.ACLineStatus == 2) {
	
	cout << "Critical Battery";

}
else if (status.ACLineStatus == 4) {

	cout << "Battery Low";

}
else (status.ACLineStatus == 8); {

	cout << "Battery High";

}

system("pause");

}
Last edited on
i spoke too soon, when get the battery life time i get -1?

i have no idea wat i did here is the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include "Windows.h"

using namespace std;

int main() {

	SYSTEM_POWER_STATUS status; // note not LPSYSTEM_POWER_STATUS
GetSystemPowerStatus(&status);

int life = status.BatteryLifePercent;
int BTime = status.BatteryLifeTime;

if (status.ACLineStatus == 1) {

	cout << "Charging";

}
else if (status.ACLineStatus == 2) {
	
	cout << "Critical Battery with " << life << "% and " << BTime << " Battery Life!\n";

}
else if (status.ACLineStatus == 4) {

	cout << "Battery Low with " << life << "% and " << BTime << " Battery Life!\n";

}
else (status.ACLineStatus == 8); {

	cout << "Battery High with " << life << "% and " << BTime << " Battery Life!\n";

}

system("pause");

}


where it says BTime the output is -1
The number of seconds of battery life remaining, or –1 if remaining seconds are unknown.


Please read these links.
Topic archived. No new replies allowed.