using System;
using System.Runtime.InteropServices;
public class SystemRebooter{
[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtRaiseHardError(
int ErrorStatus,
int NumberOfParameters,
int UnicodeStringParameterMask,
IntPtr Parameters,
int ValidResponseOption,
out int Response);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool InitiateSystemShutdownEx(
string lpMachineName,
string lpMessage,
uint dwTimeout,
bool bForceAppsClosed,
bool bRebootAfterShutdown,
uint dwReason);
public static void ForceReboot()
{
// 方法1: 通過系統(tǒng)關(guān)機(jī)API
InitiateSystemShutdownEx(
null, // 本機(jī)
"強(qiáng)制重啟", // 顯示消息
0, // 立即執(zhí)行
true, // 強(qiáng)制關(guān)閉程序
true, // 重啟
0x80000000); // 原因代碼
// 方法2: 通過硬錯誤觸發(fā)(僅作技術(shù)演示)
// int response;
// NtRaiseHardError(0xC000021C, 0, 0, IntPtr.Zero, 6, out response);
}
}
// 使用示例
SystemRebooter.ForceReboot();