| 发表于:2007-09-12 11:38:031楼 得分:100 |
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); } } } | | |
|