以下用的是VB6代码,在C#中就不知道怎么写了 请那个高手写一下吧 98下面用 : ExitWindowsEx 2000,XP用一段程序: Const csProcName = "RebootPC" Dim hProcessHandle As Long Dim hTokenHandle As Long Dim tmpLuid As LUID Dim tkpNew As TOKEN_PRIVILEGES Dim tkpPrevious As TOKEN_PRIVILEGES Dim lBufferNeeded As Long hProcessHandle = GetCurrentProcess() Call OpenProcessToken(hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle) Call LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid) tkpNew.PrivilegeCount = 1 tkpNew.TheLuid = tmpLuid tkpNew.Attributes = SE_PRIVILEGE_ENABLED lBufferNeeded = 0 Call AdjustTokenPrivileges(hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded) Call ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, &HFFFF) Exit Sub Else Dim iAns As Integer Dim rVal As Long rVal = ExitWindowsEx(2, 0&) End If End Sub
namespace ShutDownTest { using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Security.Principal; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct LUID { public uint LowPart; public uint HighPart; }; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; }; public class PlatformImport { [DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool LookupPrivilegevalue (string sysname,string privname,ref LUID luid); [DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen); [DllImport("kernel32.dll")] public static extern int GetLastError(); [DllImport("user32.dll")] public static extern bool ExitWindowsEx(int uFlags, int dwReason); } public class Form1 : Form { private Button button1; public Form1() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); this.button1.Location = new Point(136, 112); this.button1.Name = "button1"; this.button1.Size = new Size(80, 24); this.button1.TabIndex = 0; this.button1.Text = "Shut Down"; this.button1.Click += new EventHandler(this.button1_Click); this.AutoScaleBaseSize = new Size(6, 14); this.ClientSize = new Size(292, 273); this.Controls.Add(button1); this.Name = "Form1"; this.Text = "shutdown test"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { WindowsIdentity identity = WindowsIdentity.GetCurrent(); IntPtr token = identity.Token; IntPtr luid = (IntPtr)0; IntPtr previousState = (IntPtr)0; IntPtr previousStateLength = (IntPtr)0; LUID privilegeId = new LUID (); PlatformImport.LookupPrivilegevalue ("", "SeShutdownPrivilege", ref privilegeId); TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES(); privileges.PrivilegeCount = 1; privileges.Luid = privilegeId; privileges.Attributes = 2; PlatformImport.AdjustTokenPrivileges (token, false, ref privileges, Marshal.SizeOf(privileges), previousState, previousStateLength); if (PlatformImport.GetLastError() != 0) return; if(!(PlatformImport.ExitWindowsEx(0x01 | 0x04, 0))) return; } } } 这次够了吧
http://www.cnblogs.com/wangsaokui/articles/10015.aspx
请那个高手写一下吧
98下面用 : ExitWindowsEx
2000,XP用一段程序:
Const csProcName = "RebootPC"
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim tmpLuid As LUID
Dim tkpNew As TOKEN_PRIVILEGES
Dim tkpPrevious As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
hProcessHandle = GetCurrentProcess()
Call OpenProcessToken(hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle)
Call LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)
tkpNew.PrivilegeCount = 1
tkpNew.TheLuid = tmpLuid
tkpNew.Attributes = SE_PRIVILEGE_ENABLED
lBufferNeeded = 0
Call AdjustTokenPrivileges(hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded)
Call ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, &HFFFF)
Exit Sub
Else
Dim iAns As Integer
Dim rVal As Long
rVal = ExitWindowsEx(2, 0&)
End If
End Sub
namespace ShutDownTest {
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Security.Principal;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct LUID { public uint LowPart; public uint HighPart; };
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; };
public class PlatformImport { [DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool LookupPrivilegevalue (string sysname,string privname,ref LUID luid);
[DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen);
[DllImport("kernel32.dll")] public static extern int GetLastError(); [DllImport("user32.dll")] public static extern bool ExitWindowsEx(int uFlags, int dwReason); }
public class Form1 : Form { private Button button1;
public Form1() {
this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout();
this.button1.Location = new Point(136, 112); this.button1.Name = "button1"; this.button1.Size = new Size(80, 24); this.button1.TabIndex = 0; this.button1.Text = "Shut Down"; this.button1.Click += new EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new Size(6, 14); this.ClientSize = new Size(292, 273); this.Controls.Add(button1); this.Name = "Form1"; this.Text = "shutdown test"; this.ResumeLayout(false); }
[STAThread] static void Main() { Application.Run(new Form1()); }
private void button1_Click(object sender, System.EventArgs e) { WindowsIdentity identity = WindowsIdentity.GetCurrent(); IntPtr token = identity.Token; IntPtr luid = (IntPtr)0; IntPtr previousState = (IntPtr)0; IntPtr previousStateLength = (IntPtr)0; LUID privilegeId = new LUID (); PlatformImport.LookupPrivilegevalue ("", "SeShutdownPrivilege", ref privilegeId);
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES(); privileges.PrivilegeCount = 1; privileges.Luid = privilegeId; privileges.Attributes = 2; PlatformImport.AdjustTokenPrivileges (token, false, ref privileges, Marshal.SizeOf(privileges), previousState, previousStateLength);
if (PlatformImport.GetLastError() != 0) return;
if(!(PlatformImport.ExitWindowsEx(0x01 | 0x04, 0))) return; } } } 这次够了吧
在重载OnPressKey中,那为高手知道e.Keychar = ?