使用C#编程以编程方式重新启动Windows Mobile 6.x设备

5

我的HTC HD2无法从操作系统重启,只能关闭。因此我想编写一个小程序来解决这个问题。

是否可以使用C#编程以编程方式重新启动Windows Mobile 6.x设备?

3个回答

6

您应该使用文档化的 ExitWindowsEx API。只有在没有 ExitWindowsEx 函数调用的平台上(Pocket PC 2000、2002 和 2003)才应该使用 IOCTL。有关更多信息,请参见 MSDN 文档

[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);
}

非常感谢!顺便问一下,您对这里找到的 SetSystemPowerState 函数有什么看法:http://www.krvarma.com/windows-mobile/how-to-soft-reset-windows-mobile-programmatically/ - abatishchev
1
看起来SetSystemPowerState是ExitWindowsEx的有效替代品。相比ExitWindowsEx,SetSystemPowerState更加灵活,并且支持比ExitWindowsEx更早期的平台。如果你的目标是Windows CE或者混合使用Windows CE、Pocket PC和Windows Mobile设备,那么SetSystemPowerState似乎是你应该使用的函数。 - Trevor Balcom

5

SOFTRESET / HARDRESET

public class Reboot
{
    public const uint FILE_DEVICE_HAL = 0x00000101;
    public const uint METHOD_BUFFERED = 0;
    public const uint FILE_ANY_ACCESS = 0;

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

    [DllImport("Coredll.dll")]
    public extern static uint KernelIoControl
    (
        uint dwIoControlCode,
        IntPtr lpInBuf,
        uint nInBufSize,
        IntPtr lpOutBuf,
        uint nOutBufSize,
        ref uint lpBytesReturned
    );

    /// <summary>
    /// Causes the CE device to soft/warm reset
    /// </summary>
    public static uint SoftReset()
    {
        uint bytesReturned = 0;
        uint IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
        SetCleanRebootFlag();
        return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned);
    }

    [DllImport("coredll.dll")]
    public extern static uint SetSystemPowerState
    (
        String psState,
        Int32 StateFlags,
        Int32 Options
    );

    const int POWER_FORCE = 4096;
    const int POWER_STATE_RESET = 0x00800000;

    public static uint ColdReset()
    {
        SetCleanRebootFlag();
        return SetSystemPowerState(null, POWER_STATE_RESET, POWER_FORCE);
    }

    [DllImport("Coredll.dll")]
    public extern static int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, ref int lpBytesReturned);

    [DllImport("Coredll.dll")]
    public extern static void SetCleanRebootFlag();

    public static void HardReset()
    {
        int IOCTL_HAL_REBOOT = 0x101003C;
        int bytesReturned = 0;
        SetCleanRebootFlag();
        KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned);
    }


    [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);
    }

3

我认为这会对您有所帮助:重置Windows Mobile设备。虽然该方法使用Interop,不是“清晰的C#代码”,但它能解决您的问题。

如果需要软件重置:

[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);
}

虽然我自己没有使用过这种方法..请参考此处


我需要进行软重置,即重新启动。硬重置会将设备重置为默认设置。 - abatishchev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接