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

发布于 2024-09-13 14:46:25 字数 103 浏览 6 评论 0原文

我的 HTC HD2 无法从操作系统重新启动,只能关机。所以我想写一个小程序来做到这一点。

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

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2024-09-20 14:46:25

您应该使用已记录的 ExitWindowsEx API。 IOCTL 只能用于缺少 ExitWindowsEx 函数调用的平台(Pocket PC 2000、2002 和 2003)。有关详细信息,请参阅 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);
}

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);
}
挽手叙旧 2024-09-20 14:46:25

软重置/硬重置

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

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);
    }
梦回梦里 2024-09-20 14:46:25

我认为这会对您有所帮助:硬重置 Windows Mobile 设备 ..这个方法仍然不是“清晰的C#代码”,因为它使用Interop,但它有效,所以它可以解决你的问题。

对于重置:(

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

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文