首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 程式碼關閉/重啟/登出 Windows XP

     編輯:關於C語言

原理:調整進程權杖權限後呼叫 ExitWindowsEx

需開啟 SE_SHUTDOWN_NAME 權限,再呼叫 ExitWindowsEx 配合旗標實現不同操作。

完整程式碼


using System;
using System.Runtime.InteropServices;

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

    [DllImport("kernel32.dll", ExactSpelling = true)]
    static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    static extern bool ExitWindowsEx(int flg, int rea);

    const int SE_PRIVILEGE_ENABLED = 0x00000002;
    const int TOKEN_QUERY = 0x00000008;
    const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

    const int EWX_LOGOFF = 0x00;
    const int EWX_SHUTDOWN = 0x01;
    const int EWX_REBOOT = 0x02;
    const int EWX_FORCE = 0x04;
    const int EWX_POWEROFF = 0x08;
    const int EWX_FORCEIFHUNG = 0x10;

    static void DoExitWin(int flg)
    {
        TokPriv1Luid tp = new TokPriv1Luid { Count = 1, Attr = SE_PRIVILEGE_ENABLED };
        IntPtr hproc = GetCurrentProcess(), htok = IntPtr.Zero;
        OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
        LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
        AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
        ExitWindowsEx(flg, 0);
    }

    public static void Main()
    {
        Console.WriteLine("正在關機……");
        // 修改旗標:EWX_SHUTDOWN / EWX_REBOOT / EWX_LOGOFF / EWX_POWEROFF 等
        DoExitWin(EWX_SHUTDOWN | EWX_FORCE);
    }
}

旗標說明

  • EWX_SHUTDOWN (0x01):關機
  • EWX_REBOOT (0x02):重啟
  • EWX_LOGOFF (0x00):登出
  • EWX_POWEROFF (0x08):切斷電源
  • EWX_FORCE (0x04):強制關閉應用程式
  • 可組合使用,如 EWX_SHUTDOWN | EWX_FORCE

XP 下可在命令列執行 SHUTDOWN /? 查看完整參數說明。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved