1. 程式人生 > >C# API強制關機、重啟以及註銷計算機

C# API強制關機、重啟以及註銷計算機

ntp exit name 版本 啟動 用戶 event ... code

在Windows系統中有2種方式進行關機、重啟以及註銷計算機操作:

1、使用shutdown()命令;2、使用系統API;

以下是使用系統API進行操作的實例。

程序實例界面:

技術分享圖片

程序實例代碼:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9
using System.Runtime.InteropServices; 10 11 12 namespace ShutDownSys 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 public class ShutDownSys 22 { 23 //C#關機代碼
24 //這個結構體將會傳遞給API。使用StructLayout 25 //(...特性,確保其中成員是按順序排列的,C#編譯器不會對其進行調整) 26 [StructLayout(LayoutKind.Sequential, Pack = 1)] 27 internal struct TokPriv1Luid 28 { 29 public int Count; public long Luid; public int Attr; 30 }
31 32 //以下使用DLLImport特性導入了所需的Windows API。 33 //導入這些方法必須是static extern的,並且沒有方法體。 34 //調用這些方法就相當於調用Windows API。 35 [DllImport("kernel32.dll", ExactSpelling = true)] 36 internal static extern IntPtr GetCurrentProcess(); 37 38 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 39 internal static extern bool OpenProcessToken(IntPtr h,int acc,ref IntPtr phtok); 40 41 [DllImport("advapi32.dll", SetLastError = true)] 42 internal static extern bool LookupPrivilegeValueA 43 (string host, string name, ref long pluid); 44 45 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 46 internal static extern bool 47 AdjustTokenPrivileges(IntPtr htok,bool disall,ref TokPriv1Luid newst,int len,IntPtr prev,IntPtr relen); 48 49 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] 50 internal static extern bool ExitWindowsEx(int flg,int rea); 51 52 //C#關機代碼 53 //以下定義了在調用WinAPI時需要的常數。 54 //這些常數通常可以從Platform SDK的包含文件(頭文件)中找到。 55 public const int SE_PRIVILEGE_ENABLED = 0x00000002; 56 public const int TOKEN_QUERY = 0x00000008; 57 public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 58 public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; 59 public const int EWX_LOGOFF = 0x00000000; 60 public const int EWX_SHUTDOWN = 0x00000001; 61 public const int EWX_REBOOT = 0x00000002; 62 public const int EWX_FORCE = 0x00000004; 63 public const int EWX_POWEROFF = 0x00000008; 64 public const int EWX_FORCEIFHUNG = 0x00000010; 65 // 通過調用WinAPI實現關機,主要代碼再最後一行ExitWindowsEx //這調用了同名的WinAPI,正好是關機用的。 66 67 68 public static void DoExitWin(int flg) 69 { 70 bool ok; 71 TokPriv1Luid tp; 72 IntPtr hproc = GetCurrentProcess(); 73 IntPtr htok = IntPtr.Zero; 74 ok = OpenProcessToken(hproc,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,ref htok); 75 tp.Count = 1; 76 tp.Luid = 0; 77 tp.Attr = SE_PRIVILEGE_ENABLED; 78 ok = LookupPrivilegeValueA(null,SE_SHUTDOWN_NAME,ref tp.Luid); 79 ok = AdjustTokenPrivileges(htok,false,ref tp,0,IntPtr.Zero,IntPtr.Zero); 80 ok = ExitWindowsEx(flg,0); 81 } 82 } 83 84 //調用 85 public void Reboot() 86 { 87 ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_REBOOT); 88 } 89 90 public void ShutDown() 91 { 92 ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_POWEROFF); 93 } 94 95 public void LogOff() 96 { 97 ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_LOGOFF); 98 } 99 100 //關機 101 private void button1_Click(object sender, EventArgs e) 102 { 103 ShutDown(); 104 } 105 106 //重啟 107 private void button2_Click(object sender, EventArgs e) 108 { 109 Reboot(); 110 } 111 112 //註銷 113 private void button3_Click(object sender, EventArgs e) 114 { 115 LogOff(); 116 } 117 } 118 }

相關:

1、用System.Runtime.InteropServices服務的DllImport方法引入非托管代碼程序集,例如調用系統API,C語言寫的方法等等。在這種情況下,聲明必須為static
extern 主要用於聲明在外部實現的方法,同時,還可以定義外部程序集別名,使得可以從單個程序集中引用同一組件的不同版本。

2、

ExitwindowsEx函數的原型:

bool ExitwindowsEx(UINT uFlags,DWORD dwReserved);

函數功能:
該函數註銷當前用戶,關閉系統;或者關閉並重新啟動系統。此函數發送WM_QUERYENDSESSION消息給應用程序來確定它們是否能被終止。
參數:
uFlags;指定關機類型。此參數必須包括下列值之一:EWX_LOGOFF,EWX_POWEROFF,EWX_REBOOT,EWX_SHUTDOWN。還包括EWX_FORCE,EWX_FORCEIFHUNG兩個可選值。

EWX_LOGOFF:關閉所有調用函數ExitWindowsEx的進程的安全環境裏運行的進程,然後註銷用戶。
EWX_REBOOT:關閉系統並重新啟動系統。
EWX_SHUTDOWN:關閉系統使之能完全關閉電源,所有文件緩沖區都被清洗到磁盤,所有的運行的進程都停止。

C# API強制關機、重啟以及註銷計算機