views:

196

answers:

2

My HTC HD2 can't be rebooted from OS, just shut down. So I want to write a small program to do that.

Is it possible to programmatically reboot Windows Mobile 6.x device using C# (CF version makes no sense).

+1  A: 

I think this will help you: Hard Reset Windows Mobile Device..Still this method is not "clear c# code", because it uses Interop, but it works, so it can solve your problem.
For soft reset:

[DllImport("coredll.dll", SetLastError=true)]
private static extern bool KernelIoControl(int dwIoControlCode, byte[] inBuf, int inBufSize, byte[] outBuf, int outBufSize, ref int bytesReturned);

private const uint FILE_DEVICE_HAL = 0x00000101;
private const uint METHOD_BUFFERED = 0;
private const uint FILE_ANY_ACCESS = 0;

private static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
{
     return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}

public static void softReset()
{
     uint bytesReturned = 0;
     uint IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
     KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned);
}

(tho i haven't used this method myself..see here)

nihi_l_ist
I need to do soft reset, i.e. just reboot. Hard reset resets device to defaults
abatishchev
..Added code for soft reset too..
nihi_l_ist
+2  A: 

You should use the documented ExitWindowsEx API. IOCTL should only be used on platforms lacking the ExitWindowsEx function call (Pocket PC 2000, 2002, and 2003). See the MSDN doc for more information.

[DllImport("aygshell.dll", SetLastError=""true"")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ExitWindowsEx([MarshalAs(UnmanagedType.U4)]uint dwFlags, [MarshalAs(UnmanagedType.U4)]uint dwReserved);

enum ExitWindowsAction : uint
{
    EWX_LOGOFF = 0,
    EWX_SHUTDOWN = 1,
    EWX_REBOOT = 2,
    EWX_FORCE = 4,
    EWX_POWEROFF = 8
}

void rebootDevice()
{
    ExitWindowsEx(ExitWindowsAction.EWX_REBOOT, 0);
}
Trevor Balcom
Thanks a lot! btw, what do you think about `SetSystemPowerState` found here: http://www.krvarma.com/windows-mobile/how-to-soft-reset-windows-mobile-programmatically/
abatishchev
Trevor Balcom