2024年1月24日发(作者:)
14 public long Luid;15 public int Attr;16 }17
18 [DllImport("", ExactSpelling = true)]19 internal static extern IntPtr GetCurrentProcess();20
21 [DllImport("", ExactSpelling = true, SetLastError = true)]22 internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);23
24 [DllImport("", SetLastError = true)]25 internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);26
27 [DllImport("", ExactSpelling = true, SetLastError = true)]28 internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);29
30 [DllImport("", ExactSpelling = true, SetLastError = true)]31 internal static extern bool ExitWindowsEx(int flg, int rea);32
33 internal const int SE_PRIVILEGE_ENABLED = 0x00000002;34 internal const int TOKEN_QUERY = 0x00000008;35 internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;36 internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";37 internal const int EWX_LOGOFF = 0x00000000;38 internal const int EWX_SHUTDOWN = 0x00000001;39 internal const int EWX_REBOOT = 0x00000002;40 internal const int EWX_FORCE = 0x00000004;41 internal const int EWX_POWEROFF = 0x00000008;42 internal const int EWX_FORCEIFHUNG = 0x00000010;43
44 private static void DoExitWin(int flg)45 {46 bool ok;47 TokPriv1Luid tp;48 IntPtr hproc = GetCurrentProcess();49 IntPtr htok = ;50 ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);51 = 1;52 = 0;53 = SE_PRIVILEGE_ENABLED;54 ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref );55 ok = AdjustTokenPrivileges(htok, false, ref tp, 0, , );56 ok = ExitWindowsEx(flg, 0);57 }58
59 public static void Reboot()60 {61 DoExitWin(EWX_FORCE | EWX_REBOOT); //重启62 }63
64 public static void PowerOff()65 {66 DoExitWin(EWX_FORCE | EWX_POWEROFF); //关机67 }68
69 public static void LogoOff()70 {71 DoExitWin(EWX_FORCE | EWX_LOGOFF); //注销72 }73
74 }75
76 }


发布评论