burock

hi,

my development system is a notebook, powered by 32-bit Vista Home Premium OS.

i am trying to programmatically change the sleep button's behavior. I am pretty sure to use WriteGlobalPwrPolicy for this; but i am experiencing some difficulties.

First, let me start with variables:

GLOBAL_POWER_POLICY powPol;
// i want to save button's initial behavior. so, these two variables needed.
POWER_ACTION acSleep,
dcSleep;

Now, to the action.

...
if(!ReadGlobalPwrPolicy(&powPol))
return -1;
// save initial behavior
acSleep = powPol.user.SleepButtonAc.Action;
dcSleep = powPol.user.SleepButtonDc.Action;
// assign new behaviors: do nothing
powPol.user.SleepButtonAc.Action = PowerActionNone;
powPol.user.SleepButtonDc.Action = PowerActionNone;
// modifications are complete. Now it is time to write back
bool bRet = WriteGlobalPwrPolicy(&powPol);
DWORD dwErr = GetLastError();
...

Watch window shows : bRet = true and dwErr = 0.

When I execute this code, I do not see the behavior of the button is changed. Neither by practice nor from Power Options window.

Maybe I should provide some values for Flags and EventCode members; but i cannot make anything out of it.

Currently i am stuck on it and desperately in need of help.




Re: Notebook, Tablet PC, and UMPC Development WriteGlobalPwrPolicy problem

srberard

The Power Policy infrastructure has changed in Windows Vista. While the legacy APIs are still supported, what you are trying to do is best done through the new power APIs. See http://msdn2.microsoft.com/en-us/library/aa373163.aspx for more details.

Here is what you need to do in order to change the power button behavior programmatically:

  1. Get the active scheme using PowerGetActiveScheme (http://msdn2.microsoft.com/en-us/library/aa372731.aspx)
  2. Change power setting for the power button (GUID 7648efa3-dd9c-4e3e-b566-50f929386280) using PowerWriteACValueIndex (http://msdn2.microsoft.com/en-us/library/aa372765.aspx) or PowerWriteDCValueIndex (http://msdn2.microsoft.com/en-us/library/aa372769.aspx). You can change the setting independantly for on-battery and online.
  3. Activate the power scheme using PowerSetActiveScheme (http://msdn2.microsoft.com/en-us/library/aa372758.aspx)

You can repeat step #2 for each power setting you wish to change. Step #3 is critical as the change will not take effect until the scheme is activated.

This article details power mangement best practices for Applications on Windows Vista:

http://www.microsoft.com/whdc/system/pnppwr/powermgmt/PM_apps.mspx

Cheers,
Stephen [MSFT]





Re: Notebook, Tablet PC, and UMPC Development WriteGlobalPwrPolicy problem

burock

srberard wrote:

The Power Policy infrastructure has changed in Windows Vista. While the legacy APIs are still supported, what you are trying to do is best done through the new power APIs. See http://msdn2.microsoft.com/en-us/library/aa373163.aspx for more details.

Here is what you need to do in order to change the power button behavior programmatically:

  1. Get the active scheme using PowerGetActiveScheme (http://msdn2.microsoft.com/en-us/library/aa372731.aspx)
  2. Change power setting for the power button (GUID 7648efa3-dd9c-4e3e-b566-50f929386280) using PowerWriteACValueIndex (http://msdn2.microsoft.com/en-us/library/aa372765.aspx) or PowerWriteDCValueIndex (http://msdn2.microsoft.com/en-us/library/aa372769.aspx). You can change the setting independantly for on-battery and online.
  3. Activate the power scheme using PowerSetActiveScheme (http://msdn2.microsoft.com/en-us/library/aa372758.aspx)

You can repeat step #2 for each power setting you wish to change. Step #3 is critical as the change will not take effect until the scheme is activated.

This article details power mangement best practices for Applications on Windows Vista:

http://www.microsoft.com/whdc/system/pnppwr/powermgmt/PM_apps.mspx

Cheers,
Stephen [MSFT]



hello again,

i finally managed to try your recommendation and yes, it works just like you described. But the thing is, i do not want the system to be restarted when the power button behavior changes.

i had better give some details my purpose. I developed an anti-theft application. When it is enabled, it starts to beep loudly until correct password entered. Of course, to force the user to enter password, i employed many maskings. Disabled CTRL-ALT-DEL, hid mouse, etc... The last things to take of are those sleep and power button behaviors. My intention is, whenever the alarm mechanism is triggered, i want to set power and sleep buttons' behavior to "None" and after the correct password is entered, set them back to their original behavior.

As you can see, I do not have a luxury for a system reboot. On the other hand, if we can change those settings from Power Options menu in run-time, there must be a way to do this without rebooting.

Do you know another way