I demonstrated the Power Management API of the Microsoft Platform SDK during the launch of Microsoft's Academic Projects Program here in Bangalore.
Microsoft Platform SDK is a colelction of powerful APIs which you can play around with to do a bit of system-level programming. You could use the Microsoft VC++ compiler to compile your programs.
The Power Management Demo I showed is pretty simple and straight-forward. The user is intimated through an alert when there is a change in power source. In a real-time scenario one could perform actions to change the execution state/processor consmption of the application when the computer is running on battery. This is done using one simple function:
BOOL GetSystemPowerStatus(
LPSYSTEM_POWER_STATUS lpSystemPowerStatus
);
to which you would have to pass the reference of an object of type SYSTEM_POWER_STATUS structure. The power source is indicated by the value of the structure variable ACLineStatus (0 - Battery, 1 - AC Power & 255 - Unknown).
In the main window message processing function (WndProc), you can invoke this function when the WM_POWERBROADCAST message is received.
case WM_POWERBROADCAST:
BOOL ret;
SYSTEM_POWER_STATUS sps;
ret = GetSystemPowerStatus(&sps);
if(sps.ACLineStatus)
MessageBox(hWnd,TEXT("You are on AC power"),TEXT("Power Management Demo"),0);
else
MessageBox(hWnd,TEXT("You are on battery power"),TEXT("Power Management Demo"),0);
By responding to the PBT_APMQUERYSUSPEND message, an application can put off a suspend request from the operating system. The application will need to respond with a BROADCAST_QUERY_DENY, if the application cannot allow the system operations to be suspended. The Power Management API has other useful features to support automatic wake-up using the System Wake-up Events.
[Download Source]
Read about using Microsoft Visual C++ 2005 Express with Platform SDK.