在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

C#封装windows系统化shutdown功能类

C# 清风 958℃ 0评论

    /// <summary>
    /// 封装了一些ShutDown中的功能
    /// </summary>
    public sealed class ShutDown
    {
        private ShutDown() { }
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        private struct tokpriv1luid
        {
            public int count;
            public long luid;
            public int attr;
        }
        [DllImport("kernel32.dll", ExactSpelling = true)]
        private static extern IntPtr GetCurrentProcess();
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref tokpriv1luid newst, int len, IntPtr prev, IntPtr relen);
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool ExitWindowsEx(int doflag, int rea);
        private const int se_privilege_enabled = 0x00000002;
        private const int token_query = 0x00000008;
        private const int token_adjust_privileges = 0x00000020;
        private const string se_shutdown_name = "seshutdownprivilege";
        private const int ewx_logoff = 0x00000000;
        private const int ewx_shutdown = 0x00000001;
        private const int ewx_reboot = 0x00000002;
        private const int ewx_force = 0x00000004;
        private const int ewx_poweroff = 0x00000008;
      
        internal const int ewx_forcehung = 0x00000010;
        private static void doexitwin(int doflag)
        {
            tokpriv1luid tp;
            IntPtr hproc = GetCurrentProcess();
            IntPtr htok = IntPtr.Zero;
            OpenProcessToken(hproc, token_adjust_privileges | token_query, ref htok);
            tp.count = 1;
            tp.luid = 0;
            tp.attr = se_privilege_enabled;
            LookupPrivilegeValue(null, se_shutdown_name, ref tp.luid);
            AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
            ExitWindowsEx(doflag, 0);
        }
        /// <summary>
        /// 重启计算机
        /// </summary>
        public static void Reboot()
        {
           
            doexitwin(ewx_force | ewx_reboot);
        }
        /// <summary>
        /// 关闭计算机
        /// </summary>
        public static void PowerOff()
        {
            doexitwin(ewx_force | ewx_poweroff);
        }
        /// <summary>
        /// 注销计算机
        /// </summary>
        public static void LogOff()
        {
            doexitwin(ewx_force | ewx_logoff);
        }
    }
  

 

 

 



转载请注明:清风亦平凡 » C#封装windows系统化shutdown功能类

喜欢 (0)or分享 (0)
支付宝扫码打赏 支付宝扫码打赏 微信打赏 微信打赏
头像
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址