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); //---按键的一个连续动作 } } }
经本人测试,可用,都静态方法,直接调用就行。
时间: 2024-12-31 03:50:49