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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Tor Project blog
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Latest news
Latest news
L
LINUX DO - 热门话题
罗磊的独立博客
T
Tenable Blog
The Hacker News
The Hacker News
美团技术团队
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
博客园 - 司徒正美
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
云风的 BLOG
云风的 BLOG
S
Secure Thoughts
Cloudbric
Cloudbric
S
Security @ Cisco Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
U
Unit 42
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Webroot Blog
Webroot Blog
爱范儿
爱范儿
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
P
Palo Alto Networks Blog
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
Project Zero
Project Zero

博客园 - sizzle

ruby函数回调的实现方法 软件单元测试之我见 非空判断 在COM中使用数组参数-SafeArray[转载] 指针赋值 - sizzle - 博客园 进程的深度隐藏 文件隐藏 Windows下网络数据报的监听和拦截技术 三键屏蔽 键盘知识 如何获取当前程序文件的路径 Net Remoting[转自Bruce Zhang's Blog] 解码 XML 和 DTD 【读书笔记】[Effective C++第3版][第27条]尽量不要使用类型转换 P2P之UDP穿透NAT的原理与实现 CommandBinding用法 窗体显示中Form.Show()和Form.ShowDialog()的区别 一段高深代码--未解 TEA加密算法的C/C++实现
使用C#注销/关闭计算机
sizzle · 2007-09-12 · via 博客园 - sizzle

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);
}
}
}