Change Power Button Action in Windows XP

This is a discussion on Change Power Button Action in Windows XP within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm looking for a way, either by programming in C++ or C, or even writing a script of some ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    Change Power Button Action in Windows XP

    Hi, I'm looking for a way, either by programming in C++ or C, or even writing a script of some sort, like a batch file, to change advanced settings of power options in Windows XP. I know you can use Powercfg.exe to set the basic stuff, but it doesn't encompass this particular option, that is, changing the action of the power button. Any ideas? I appreciate any insight I can get on this... I have been Googling this for two days now!
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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.
    Last edited by _Mike; 03-20-2010 at 04:58 AM.

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Thank you very much Mike, that's exactly what I needed!
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Hi again. I am using this code in a skeleton Win32 app, and it works, but only if I'm using it to set the power button to sleep or shut down. Using PowerActionNone has no effect (I'm expecting it to set the power button to "Do Nothing"), and I don't how to set it to "Ask me what to do." Is there a more flexible option than this one? I can show you the code if anyone is interested... By the way, I'm using this in Windows XP Service Pack 3.

    EDIT: I found the solution: to set the Do Nothing and Ask me what to do options, you first set the PowerActionNone and then you also have to set the EventCode to POWER_FORCE_TRIGGER_RESET or POWER_USER_NOTIFY_BUTTON respectively. Thanks for the help, and I hope this is helpful to other people.
    Last edited by linucksrox; 04-21-2010 at 10:22 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Differences between Windows XP and Windows Vista programming
    By nathan3011 in forum Windows Programming
    Replies: 3
    Last Post: 01-15-2009, 10:05 AM
  2. simulate keyboard events on windows xp
    By xximranxx in forum Windows Programming
    Replies: 4
    Last Post: 04-26-2007, 06:43 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. hbrBackground (WNDCLASSEX) problems in Windows XP
    By codec in forum Windows Programming
    Replies: 4
    Last Post: 05-17-2004, 09:30 AM
  5. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 03:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21