惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - brake

dataview 查询 - brake - 博客园 名言名心事 一个男人写给老婆的年终总结 一个系统工程,并不是说要每个部件能力都最强、系统最好、性能最优才是最好,关键是要协调,各个部件协调匹配才是最好 成功并没有模式,每个人走的路都不一样 让学习成为一种习惯,当学习成为一种习惯时,你就会欲罢不能.想不学都不行! 解析HTML文件 - 运用SgmlReader类来解析HTML文件 asp.net實現異步調用總結 資料表欄位自动增量属性去除 Illustrator工具也能匯出XAML Code 如何讀取http地址頁面內容,http地址有可能是夸網站 - brake - 博客园 从对技术“新”、“奇”、“特”的一味追捧,到对稳定系统、成熟产品的理性认识,以及多次探讨“业务”与“技术”的关系之后,没有最好的技术,只有恰当的技术! WCF議程 WCF开发指南(自编) SQL 查询关键字用法祥解 圖片從右向左出現 ASP导出Excel数据的四种方法 C#编写Windows服务的基本过程 C# 用SQLDMO.dll 备份和恢复数据库
C# 實現關機,重啟電腦
brake · 2009-06-27 · via 博客园 - brake

Code
using System;
using System.Runtime.InteropServices;
using System.IO;

namespace AutoExitWindows
{
    
/// <summary>
    
/// ExitWindows 的摘要说明。
    
/// 关机、注销windows.
    
/// </summary>
    
/// 
    
public class ExitWindows
    {
        
public const int EWX_LOGOFF = 0//退出(注销)
        
public const int EWX_SHUTDOWN = 1//'关机
        public const int EWX_REBOOT = 2; //
'重启动
        
public const int EWX_FORCE = 4;// '强制关机,即不通知现在活动应用程序让其先自我关闭

        public const int TOKEN_ADJUST_PRIVILEGES = 0x20;
        public const int TOKEN_QUERY = 0x8;
        public const int SE_PRIVILEGE_ENABLED = 0x2;
        public const int ANYSIZE_ARRAY = 1;

        struct LUID
        {
            int lowpart;
            int highpart;

            LUID(int low, int high)
            {
                lowpart = low;
                highpart = high;
            }
        }

        struct LUID_AND_ATTRIBUTES
        {
            LUID pLuid;
            int Attributes;
            public LUID_AND_ATTRIBUTES(LUID luid, int attributes)
            {
                pLuid = luid;
                Attributes = attributes;
            }
        }

        struct TOKEN_PRIVILEGES
        {
            int PrivilegeCount;
            LUID_AND_ATTRIBUTES Privileges;
            public TOKEN_PRIVILEGES(int privilegecount, LUID_AND_ATTRIBUTES privileges)
            {
                PrivilegeCount = privilegecount;
                Privileges = privileges;
            }
        }

        [DllImport("user32", SetLastError = true)]
        static extern int ExitWindowsEx(int uFlags,int dwReserved);

        [DllImport("kernel32", SetLastError = true)]
        static extern int GetCurrentProcess();

        [DllImport("advapi32", SetLastError = true)]
        static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);

        [DllImport("advapi32", SetLastError = true)]
        static extern int AdjustTokenPrivileges(int TokenHandle, int DisableAllPrivileges, TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, int ReturnLength);

        [DllImport("advapi32", SetLastError = true)]
        static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);

        public ExitWindows()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public void CloseWindows(int exittype)
        {
            AdjustTokenPrivilegesForNT();
            ExitWindowsEx(exittype, 0);
        }

        //这个函数就是用于NT关机中使用的
        public static void AdjustTokenPrivilegesForNT()
        {
            int hdlProcessHandle = 0;
            int hdlTokenHandle = 0;
            LUID tmpLuid = new LUID();
            TOKEN_PRIVILEGES tkp;
            TOKEN_PRIVILEGES tkpNewButIgnored = new TOKEN_PRIVILEGES();
            int lBufferNeeded = 0;

            hdlProcessHandle = GetCurrentProcess();
            OpenProcessToken(hdlProcessHandle, TOKEN_ADJUST_PRIVILEGES, ref hdlTokenHandle);

            LookupPrivilegeValue("", "SeShutdownPrivilege", ref tmpLuid);
            LUID_AND_ATTRIBUTES luidtemp = new LUID_AND_ATTRIBUTES(tmpLuid, SE_PRIVILEGE_ENABLED);
            tkp = new TOKEN_PRIVILEGES(1, luidtemp);

            AdjustTokenPrivileges(hdlTokenHandle, 0, tkp, 100, ref tkpNewButIgnored, lBufferNeeded);

        }

    }
}