应大家的要求,下面的是关机程序的核心代码:
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Public Enum RestartOptions
LogOff = 0
PowerOff = 8
Reboot = 2
ShutDown = 1
Suspend = -1
Hibernate = -2
End Enum
Protected Sub EnableToken(ByVal privilege As String)
If Not CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges") Then Return
Dim tokenHandle As IntPtr = IntPtr.Zero
Dim privilegeLUID = New LUID
Dim newPrivileges = New TOKEN_PRIVILEGES
Dim tokenPrivileges As TOKEN_PRIVILEGES
If (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
If (LookupPrivilegeValue("", privilege, privilegeLUID)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
tokenPrivileges.PrivilegeCount = 1
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
tokenPrivileges.Privileges.pLuid = privilegeLUID
Dim Size As Integer = 4
If (AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
End Sub
Private Sub logoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
If MessageBox.Show("你确定要注销吗?", "关闭计算机", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
Dim shutdown As New WindowsController
shutdown.ExitWindows(shutdown.RestartOptions.LogOff, 0)
Application.Exit()
End If
End Sub
Private Sub shutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If MessageBox.Show("你确定要关机吗?", "关闭计算机", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
shutdown.ExitWindows(shutdown.RestartOptions.PowerOff, True)
Application.Exit()
End If
只有定时只要设置一个timer就可以了
End Sub