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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 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);
}
}
}