Windows关机类

// 定义一个关机类,定义关机函数

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;

namespace  ZwLylDesktop
{
    
class ExitWindows
    
{
        
//声明关机类型
        public enum EixtWindowsType
        
{
            EWX_LOGOFF 
= 0x00000000,//中止进程,然后注销   
            EWX_SHUTDOWN = 0x00000001,//关掉系统电源
            EWX_REBOOT = 0x00000002,//重新引导系统   
            EWX_FORCE = 0x00000004,//强迫中止没有响应的进程   
            EWX_POWEROFF = 0x00000008,
            EWX_FORCEIFHUNG 
= 0x00000010,
        }


        [StructLayout(LayoutKind.Sequential, Pack 
= 1)]
        
internal struct TokPriv1Luid
        
{
            
public int Count;
            
public long Luid;
            
public int Attr;
        }

        
//调用Windows API
        [DllImport("advapi32.dll")]
        
private static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref   int TokenHandle);
        [DllImport(
"advapi32.dll")]
        
private static extern int LookupPrivilegeValue(string host, string name, ref   long pluid);
        [DllImport(
"advapi32.dll")]
        
private static extern bool AdjustTokenPrivileges(int htok, bool disall, ref   TokPriv1Luid newst, int len, int prev, int relen);
        [DllImport(
"user32.dll")]
        
private static extern int ExitWindowsEx(EixtWindowsType uFlags, int dwReserved);
        [DllImport(
"kernel32.dll", EntryPoint = "GetCurrentProcess")]
        
public static extern int GetCurrentProcess();
        [DllImport(
"kernel32.dll", EntryPoint = "GetLastError")]
        
public static extern int GetLastError();

 


        
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
        
private const int TOKEN_QUERY = 0x00000008;
        
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

        
public bool MyExitWindows(EixtWindowsType eixtWindowsType)
        
{


            TokPriv1Luid tp;

            
int htok = 0;

            
int CurrentProcessHandle = GetCurrentProcess();
            
// Get a token for this process.
            if (OpenProcessToken(CurrentProcessHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref   htok) == 0)
                
return false;

            tp.Count 
= 1;
            tp.Luid 
= 0;
            tp.Attr 
= SE_PRIVILEGE_ENABLED;
            
// Get the LUID for the shutdown privilege
            LookupPrivilegeValue(null"SeShutdownPrivilege"ref   tp.Luid);

 

            
// Get the shutdown privilege for this process. 

            AdjustTokenPrivileges(htok, 
falseref   tp, 000);
            
if (GetLastError() != 0)
                
return false;

            
// Shut down the system and force all applications to close. 

            
if (ExitWindowsEx(eixtWindowsType, 0== 0)
                
return false;

            
return true;
        }

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值