0

I need to write code in C that will indicate and notify me when the laptop battery power is low. I know that I can use the following:

BOOL WINAPI GetSystemPowerStatus(
    __out  LPSYSTEM_POWER_STATUS lpSystemPowerStatus
    );

But I want to send a function to the operating system that will notify me when power is low.

I want to have an "empty" loop in my code, and when battery is low, the code will send me a notification (printf or trace).

I think I should be using kernel functions, but I can't find them

jimmym715
  • 1,512
  • 1
  • 16
  • 25
user1386966
  • 3,302
  • 13
  • 43
  • 72
  • 1
    Since when does Windows allow kernel programming? – Shahbaz May 10 '12 at 11:51
  • 3
    @Shahbaz On Windows, "kernel programming" is basically "writing a driver". But what's probably confusing you is [the difference between the kernel and kernel mode](http://blogs.msdn.com/b/oldnewthing/archive/2012/04/17/10294294.aspx). Windows does export "kernel" functions that have nothing to do with running in kernel mode. – Cody Gray - on strike May 10 '12 at 11:54
  • 1
    Where have you gotten the idea that this needs to involve kernel *anything*? What's wrong with calling `GetSystemPowerStatus`? Do you mean that you effectively want the operating system to *call back* your code when the power level hits certain pre-defined points? – Cody Gray - on strike May 10 '12 at 11:56
  • yes - exactly.. I'm sorry for the confusing words, I'm kinda new in this still – user1386966 May 10 '12 at 11:57
  • 2
    It's quite common belief that the kernel consists of super-cool and omnipotent methods. And the only purpose of the "kernel" itself is to keep those methods out of mortals reach. – Agent_L May 10 '12 at 11:59
  • @user1386966 Whats wrong in using `WM_TIMER` to call `GetSystemPowerStatus` at regular intervals? – Agent_L May 10 '12 at 12:00
  • @Agent Constant polling is hardly ever the correct way to solve a problem. It would "work", for loose definitions of the term "work". But I wouldn't design an app around it. – Cody Gray - on strike May 10 '12 at 12:02
  • @CodyGray indeed, especially monitoring battery by draining it is counter-productive. But rarely outside world variables have OnThresholdReached hooks. – Agent_L May 10 '12 at 12:06
  • I don;t want to use timers, Callback function is better i think - but the API that @Cody Gray suggested is working with desktop apps only -and I need it to be able to work with the driver as well – user1386966 May 10 '12 at 12:38
  • 1
    @user1386966 Driver?? What driver? – Lundin May 10 '12 at 12:59

2 Answers2

4

I have never used these APIs, but what you are looking for seems to be WM_POWERBROADCAST.

There are various values for wParam that you could check upon receiving that message, such as PBT_APMBATTERYLOW. When you receive a WM_POWERBROADCAST message with the appropriate wParam value, call GetSystemPowerStatus() from there.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • thank you, but the problem that this API is working with desktop apps only. – user1386966 May 10 '12 at 12:36
  • 2
    @user1386966 Yes? Where in your question do you mention anything else? Do you have some silent, secret requirement that you aren't telling us about? – Lundin May 10 '12 at 13:07
1

In the kernel, there is a separate methodology for handling power status updates. See here for info.

If you are writing a driver that can be affected by power-state changes, you must be able to process the following types of information in your driver code:

etc.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140