Code:
/* returns TRUE for success and FALSE for failure */
BOOL GetPowerButtonAction(POWER_ACTION* acAction, POWER_ACTION* dcAction)
{
GLOBAL_POWER_POLICY gpolicy;
if(ReadGlobalPwrPolicy(&gpolicy) == 0)
return FALSE;
if(acAction)
*acAction = gpolicy.user.PowerButtonAc.Action;
if(dcAction)
*dcAction = gpolicy.user.PowerButtonDc.Action;
return TRUE;
}
/* returns TRUE for success and FALSE for failure */
/* Pass in -1 for an action to keep the currently set action */
BOOL SetPowerButtonAction(POWER_ACTION acAction, POWER_ACTION dcAction)
{
GLOBAL_POWER_POLICY gpolicy;
UINT scheme;
if(CanUserWritePwrScheme() == 0)
return FALSE;
if(ReadGlobalPwrPolicy(&gpolicy) == 0)
return FALSE;
if(GetActivePwrScheme(&scheme) == 0)
return FALSE;
if(acAction > -1)
gpolicy.user.PowerButtonAc.Action = acAction;
if(dcAction > -1)
gpolicy.user.PowerButtonDc.Action = dcAction;
if(SetActivePwrScheme(scheme, &gpolicy, NULL) == 0)
return FALSE;
return TRUE;
}
Code:
/* From winnt.h, include windows.h
*
* typedef enum {
* PowerActionNone = 0,
* PowerActionReserved,
* PowerActionSleep,
* PowerActionHibernate,
* PowerActionShutdown,
* PowerActionShutdownReset,
* PowerActionShutdownOff,
* PowerActionWarmEject
* } POWER_ACTION, *PPOWER_ACTION;
*/
if(SetPowerButtonAction(PowerActionSleep, PowerActionHibernate) == FALSE)
{
printf("Error setting button actions. Use GetLastError() for more info.\n");
}
For more info, see Power Management Functions (Windows)
Especially the part about the APIs used in this code,
Power Management Functions: Windows Server 2003 and Earlier
The following functions are supported in Windows Server 2008 and Windows Vista for compatibility with earlier versions of Windows. Applications written for Windows Server 2008 and Windows Vista should use the alternatives listed in this table.