码迷,mamicode.com
首页 > Windows程序 > 详细

c# wince重启代码

时间:2014-12-24 11:15:11      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace restart
{
    class Class1
    {
        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, int lpBytesReturned);
        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern void SetCleanRebootFlag();
        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern void keybd_event(byte bVk, byte bScan, long dwFlags, long dwExtraInfo);
        //As Byte
        private const int KEYEVENTF_KEYUP = 2;
        private const int FILE_DEVICE_HAL = 0x101;
        private const int FILE_DEVICE_CONSOLE = 0x102;
        private const int FILE_DEVICE_PSL = 0x103;
        private const int METHOD_BUFFERED = 0;
        private const int METHOD_IN_DIRECT = 1;
        private const int METHOD_OUT_DIRECT = 2;
        private const int METHOD_NEITHER = 3;
        private const int FILE_ANY_ACCESS = 0;
        private const int FILE_READ_ACCESS = 0x1;
        private const int FILE_WRITE_ACCESS = 0x2;
        private static int CTL_CODE(int DeviceType, int func, int Method, int Access)
        {
            return (DeviceType << 16) | (Access << 14) | (func << 2) | Method;
        }
        //CTL_CODE
        public static void WarmBoot()
        {
            int bytesReturned = 0;
            int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
            KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
        }
        public static void ColdBoot()
        {
            int bytesReturned = 0;
            int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);

            SetCleanRebootFlag();
            //JJJ

            KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
        }
        //TestProc

        public static void HardReset()
        {
            int IOCTL_HAL_REBOOT = 0x101003c;
            int bytesReturned = 0;
            SetCleanRebootFlag();
            //  KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned) ‘一般
            //  KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 4, IntPtr.Zero, 1024, bytesReturned)      ‘ 热启动
            KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 4, IntPtr.Zero, 1024, bytesReturned);
            // 冷启动
        }
        //HardReset

        public static void hardHibernate()
        {
            int IOCTL_HAL_SHUTDOWN = 0x1012000;
            //休眠
            int bytesReturned = 0;
            byte VK_OFF = 0xdf;
            KernelIoControl(IOCTL_HAL_SHUTDOWN, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
            //---按键的一个连续动作
            keybd_event(VK_OFF, 0, 0, 0);
            keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0);
            //---按键的一个连续动作
        }
    }
}


经本人测试,可用,都静态方法,直接调用就行。

c# wince重启代码

标签:

原文地址:http://www.cnblogs.com/muzp/p/4181703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!