using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace 文件和系统操作
{
public class 注销和关闭计算机 : System.Windows.Forms.Form
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct TokPrivlLuid
{
public int Count;
public long Luid;
public int Attr;
}
// GetCurrentProcess函数返回当前进程的一个句柄
[DllImport("kernel32.dll",ExactSpelling=true)]
public static extern IntPtr GetCurrentProcess();
// OpenProcessToken 函数打开一个进程的访问代号
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool OpenProcessToken(IntPtr ProcessHandles, int DesiredAccess, ref IntPtr TokenHandle);
// LookupPrivilegeValue 函数获得本地唯一标识符(LUID),用于在特定系统中表示特定优先权
[DllImport("advapi32.dll",SetLastError=true)]
public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref long lpLuid);
// AdjustTokenPrivileges 函数使允许或者禁用指定访问记号的优先权
// 允许或者禁用优先权需要TOKEN_ADJUST_PRIVILEGES 访问权限
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges,
ref TokPrivlLuid NewState, int BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
// ExitWindowsEx 函数可以退出登陆、关机或者重新启动系统
[DllImport("user32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool ExitWindowsEx(int flg, int rea);
private System.Threading.Timer timer;
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
private const int EWX_LOGOFF = 0x00000000;//注销
private const int EWX_SHUTDOWN = 0x00000001;//关机
private const int EWX_REBOOT = 0x00000002;//重起
private const int EWX_FORCE = 0x00000004;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.ComponentModel.Container components = null;
public 注销和关闭计算机()
{
InitializeComponent();
this.textBox1.Text = (Environment.TickCount / (1000 * 60)).ToString() + "分钟";
timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, 0, 1000);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 23);
this.label1.TabIndex = 0;
this.label1.Text = "系统已运行时间";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 144);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 23);
this.button1.TabIndex = 2;
this.button1.Text = "关闭";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(128, 144);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 3;
this.button2.Text = "注销";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(216, 144);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 4;
this.button3.Text = "重起";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// 注销和关闭计算机
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 214);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "注销和关闭计算机";
this.Text = "注销和关闭计算机";
this.ResumeLayout(false);
}
#endregion
private static void RebootCommand(int flg)
{
bool ok;
TokPrivlLuid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
}
//获得系统已运行的时间
private void OnTimer(object state)
{
this.textBox1.Text = (Environment.TickCount / (1000 * 60)).ToString() + "分钟";
this.textBox1.Refresh();
}
private void button1_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_SHUTDOWN + EWX_FORCE);
}
private void button2_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_LOGOFF + EWX_FORCE);
}
private void button3_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_REBOOT + EWX_FORCE);
}
}
}