admin管理员组文章数量:1532741
2024年3月1日发(作者:)
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 }
版权声明:本文标题:C#实现关机的两种方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1709245551a223052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论