locked
Programatically Power Off Windows Phone RRS feed

  • Question

  • Hi guys, 

    I like to ask if how to programatically power off (shutdown) Windows Phone?

    Thanks in advance!

    Thursday, April 29, 2010 12:48 AM

Answers

  • OK I Got it working. here's my final code:

     

    private enum SetSystemPowerStateAction
    {
    	POWER_STATE_ON = 0x00010000,
    	POWER_STATE_OFF = 0x00020000,
    	POWER_STATE_SUSPEND = 0x00200000,
    	POWER_FORCE = 4096,
    	POWER_STATE_RESET = 0x00800000
    }
    
    [DllImport("coredll.dll", SetLastError = true)]
    static extern int SetSystemPowerState(string psState, int StateFlags, int Options);
    
    // to off
    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_OFF, (int)SetSystemPowerStateAction.POWER_FORCE);
    
    // to restart
    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_RESET, (int)SetSystemPowerStateAction.POWER_FORCE);

    power off still doesn't work.

    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_OFF, (int)SetSystemPowerStateAction.POWER_FORCE);
    this line, and am not sure why .. it reboots.

    • Marked as answer by Jayson Ragasa Thursday, April 29, 2010 7:57 AM
    Thursday, April 29, 2010 6:02 AM

All replies

  • Hi NullString,

    You could try calling the ExitWindowsEx API (documented on MSDN at
    http://msdn.microsoft.com/en-us/library/ms893047.aspx)

    Passing the EWX_REBOOT flag should Restart the device for you.

    Passing the EWX_POWEROFF flag should ShutDown the device for you.(This flag is not supported on a Windows Mobile-based Pocket PC)

    You would need to use Platform PInvoke functionality to access this from.

    C# :

    [DllImport("aygshell.dll")]
    private static extern bool ExitWindowsEx(int uFlags, int dwReserved);

    private const int EWX_REBOOT = 2;
    private const int EWX_POWEROFF = 8;


    VB :


    Private Const EWX_REBOOT As Integer = 2

    Private Const EWX_POWEROFF  As Integer = 8

    Private Declare Function ExitWindowsEx Lib "aygshell.dll" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Boolean

     

    I hope that helps.

    Regards : MrAlshahawy


    Please remember to mark the replies as answers if they help ,Don't forget to Vote the answer as Helpful if it helps
    Thursday, April 29, 2010 2:35 AM
  • Thanks Ashraf! Will try and report back later.
    Thursday, April 29, 2010 5:32 AM
  • Doesn't seem to work..

    EWX_REBOOT works but not EWX_POWEROFF, or EWX_SHUTDOWN = 1

    Thursday, April 29, 2010 5:49 AM
  • OK I Got it working. here's my final code:

     

    private enum SetSystemPowerStateAction
    {
    	POWER_STATE_ON = 0x00010000,
    	POWER_STATE_OFF = 0x00020000,
    	POWER_STATE_SUSPEND = 0x00200000,
    	POWER_FORCE = 4096,
    	POWER_STATE_RESET = 0x00800000
    }
    
    [DllImport("coredll.dll", SetLastError = true)]
    static extern int SetSystemPowerState(string psState, int StateFlags, int Options);
    
    // to off
    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_OFF, (int)SetSystemPowerStateAction.POWER_FORCE);
    
    // to restart
    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_RESET, (int)SetSystemPowerStateAction.POWER_FORCE);

    power off still doesn't work.

    SetSystemPowerState(null, (int)SetSystemPowerStateAction.POWER_STATE_OFF, (int)SetSystemPowerStateAction.POWER_FORCE);
    this line, and am not sure why .. it reboots.

    • Marked as answer by Jayson Ragasa Thursday, April 29, 2010 7:57 AM
    Thursday, April 29, 2010 6:02 AM
  • Hi Jayson ,

    I want to know wheather u r able to perform restart , power off for windows phone ,

    since I am facing some problem , and not sure how to start with ,

    please can u share a piece ofworking  code here for windows phone

    thanks in advance .

    or if possible reply to me on nidhi.singh.mnit@gmail.com 

    Friday, October 12, 2012 4:40 AM