马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现关机、重启。注销的代码

  [复制链接]
查看6391 | 回复5 | 2016-5-30 09:23:55 | 显示全部楼层 |阅读模式
命名空间引用:
using System.Runtime.InteropServices;


此代码为转载,本人未经测试。

[C#] 纯文本查看 复制代码
[StructLayout(LayoutKind.Sequential, Pack = 1)]
 
        internal struct TokPriv1Luid
 
        {
 
            public int Count;
 
            public long Luid;
 
            public int Attr;
 
        }
 
        [DllImport("kernel32.dll", ExactSpelling = true)]
 
        internal static extern IntPtr GetCurrentProcess();
 
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
 
        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
 
        [DllImport("advapi32.dll", SetLastError = true)]
 
        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
 
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
 
        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
 
     ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
 
        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
 
        internal static extern bool ExitWindowsEx(int flg, int rea);
 
        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
 
        internal const int TOKEN_QUERY = 0x00000008;
 
        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
 
        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
 
        internal const int EWX_LOGOFF = 0x00000000;
 
        internal const int EWX_SHUTDOWN = 0x00000001;
 
        internal const int EWX_REBOOT = 0x00000002;
 
        internal const int EWX_FORCE = 0x00000004;
 
        internal const int EWX_POWEROFF = 0x00000008;
 
        internal const int EWX_FORCEIFHUNG = 0x00000010;
 
private static void DoExitWin(int flg)
 
        {
 
            bool ok;
 
            TokPriv1Luid tp;
 
            IntPtr hproc = GetCurrentProcess();
 
            IntPtr htok = IntPtr.Zero;
 
            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
 
            tp.Count = 1;
 
            tp.Luid = 0;
 
            tp.Attr = SE_PRIVILEGE_ENABLED;
 
            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
 
            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
 
            ok = ExitWindowsEx(flg, 0);
 
        }
 
        private void button2_Click(object sender, EventArgs e)
 
        {
 
            //此代码实现重启功能
 
            DoExitWin(EWX_REBOOT);
 
        }
 
        private void button3_Click(object sender, EventArgs e)
 
        {
 
             //此代码实现注销功能
 
            DoExitWin(EWX_LOGOFF);
 
        }
 
        private void button1_Click_1(object sender, EventArgs e)
 
        {
 
             //此代码实现关机功能
 
            DoExitWin(EWX_SHUTDOWN);
 
        }


C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2016-5-30 09:24:16 | 显示全部楼层
111
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
awarenessxie | 2016-5-30 12:53:25 | 显示全部楼层
电脑还是手机?
meng | 2016-5-30 14:16:01 | 显示全部楼层
已经测试了,可以实现,谢谢分享
ibcadmin | 2016-5-30 16:06:54 | 显示全部楼层

电脑
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2016-5-30 16:07:12 | 显示全部楼层
meng 发表于 2016-5-30 14:16
已经测试了,可以实现,谢谢分享

ok~~~
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则