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

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

博客园 - 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);

        }

    }
}