读书人

请各位帮帮忙啊!给100分!多谢了!只

发布时间: 2011-12-17 23:37:33 作者: rapoo

请各位帮帮忙啊!!!!给100分!谢谢了!只是解释一下这段代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace mywinlab
{
public partial class Form1 : Form
{
DateTime dateTime;
public int a = 1;
public Form1()
{
InitializeComponent();

}

[StructLayout(LayoutKind.Sequential, Pack = 1)]

internal struct TokPriv1Luid
{
public int Count;

public long Luid;

public int Attr;

}

[DllImport("kernel32.dll", ExactSpelling = true)]

internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError = true)]

internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

internal static extern bool ExitWindowsEx(int flg, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

internal const int TOKEN_QUERY = 0x00000008;

internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

internal const int EWX_LOGOFF = 0x00000000;

internal const int EWX_SHUTDOWN = 0x00000001;

internal const int EWX_REBOOT = 0x00000002;

internal const int EWX_FORCE = 0x00000004;

internal const int EWX_POWEROFF = 0x00000008;

internal const int EWX_FORCEIFHUNG = 0x00000010;



private void DoExitWin(int flg)
{

bool ok;

TokPriv1Luid 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 Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
dateTime = DateTime.Now;
string time = dateTime.ToString("HH:mm:ss:ff");
label1.Text = "现在时间:"+time;
}

private void button1_Click(object sender, EventArgs e)


{
timer2.Enabled = true;
button1.Enabled = false;
comboBox1.Enabled = false;
comboBox2.Enabled = false;
}

private void timer2_Tick(object sender, EventArgs e)
{
string time1 =dateTime.ToString("HH:mm");
if (comboBox1.Text + ":" + comboBox2.Text == time1)
{
DoExitWin(EWX_SHUTDOWN);//关机
DoExitWin(EWX_POWEROFF);//关机
//DoExitWin(EWX_FORCE);//注销
//DoExitWin(EWX_LOGOFF);//注销
//DoExitWin(EWX_REBOOT);//重新启动
//DoExitWin(EWX_FORCEIFHUNG);//注销


}
   
label4.Text = "将在 " + comboBox1.Text + ":" + comboBox2.Text + " 关闭系统!";

}

private void 重新设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.Enabled = true;
comboBox1.Enabled = true;
comboBox2.Enabled = true;
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (a == 1)
{
this.WindowState = FormWindowState.Normal;
a = 0;
}
else if (a == 0)
{
this.WindowState = FormWindowState.Minimized;
a = 1;
}
}

}
}

[解决办法]
相关api和常量说明

请查msdn或者 <windows api 大全>

这个东西都是现查的,没人会记那本比砖头还厚的api手册滴
[解决办法]
明明是20分,还说100分。忽悠人。。
[解决办法]
通过调用API函数ExitWindowEx来实现电脑操作
http://blog.csdn.net/zhan198271/archive/2008/02/14/2092764.aspx

他的值是16进制的

本人也刚入IT,最近也被那写函数迷住了.感觉CPU只认识2进制,为什么还麻烦给赋16进制的了

读书人网 >.NET

热点推荐