VB.NET实现计算机关机/重启/休眠/注销
2006-11-09 12:36
537 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Imports System
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Imports System.Text
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Imports System.Diagnostics
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Imports System.Runtime.InteropServices
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
Public Class WindowsControllerClass WindowsController
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Enum RestartOptionsEnum RestartOptions
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LogOff = 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PowerOff = 8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Reboot = 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShutDown = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Suspend = -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hibernate = -2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Enum
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Structure LUIDStructure LUID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim LowPart As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim HighPart As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Structure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Structure LUID_AND_ATTRIBUTESStructure LUID_AND_ATTRIBUTES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim pLuid As LUID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim Attributes As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Structure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Structure TOKEN_PRIVILEGESStructure TOKEN_PRIVILEGES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim PrivilegeCount As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim Privileges As LUID_AND_ATTRIBUTES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Structure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const TOKEN_QUERY = &H8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const SE_PRIVILEGE_ENABLED = &H2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const EWX_FORCE = 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function LoadLibrary()Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function FreeLibrary()Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function GetProcAddress()Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function SetSuspendState()Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function OpenProcessToken()Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function LookupPrivilegeValue()Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function AdjustTokenPrivileges()Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function ExitWindowsEx()Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Declare Function FormatMessage()Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Sub ExitWindows()Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Select Case how
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case RestartOptions.Suspend
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SuspendSystem(False, force)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case RestartOptions.Hibernate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SuspendSystem(True, force)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ExitWindows(Convert.ToInt32(how), force)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Select
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Protected Sub ExitWindows()Sub ExitWindows(ByVal how As Integer, ByVal force As Boolean)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EnableToken("SeShutdownPrivilege")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If force Then how = how Or EWX_FORCE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If (ExitWindowsEx(how, 0) = 0) Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Protected Sub EnableToken()Sub EnableToken(ByVal privilege As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Not CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges") Then Return
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim tokenHandle As IntPtr = IntPtr.Zero
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim privilegeLUID = New LUID()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim newPrivileges = New TOKEN_PRIVILEGES()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim tokenPrivileges As TOKEN_PRIVILEGES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If (LookupPrivilegeValue("", privilege, privilegeLUID)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tokenPrivileges.PrivilegeCount = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tokenPrivileges.Privileges.pLuid = privilegeLUID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim Size As Integer = 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If (AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Protected Sub SuspendSystem()Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Not CheckEntryPoint("powrprof.dll", "SetSuspendState") Then Throw New PlatformNotSupportedException("The SetSuspendState method is not supported on this system!")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetSuspendState(Convert.ToInt32(IIf(hibernate, 1, 0)), Convert.ToInt32(IIf(force, 1, 0)), 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Protected Function CheckEntryPoint()Function CheckEntryPoint(ByVal library As String, ByVal method As String) As Boolean
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim libPtr As IntPtr = LoadLibrary(library)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Not libPtr.Equals(IntPtr.Zero) Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Not GetProcAddress(libPtr, method).Equals(IntPtr.Zero) Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FreeLibrary(libPtr)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FreeLibrary(libPtr)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Protected Function FormatError()Function FormatError(ByVal number As Integer) As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim Buffer = New StringBuilder(255)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, Buffer, Buffer.Capacity, 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Return Buffer.ToString()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
End Class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
Public Class PrivilegeExceptionClass PrivilegeException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Inherits Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Sub New()Sub New()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyBase.New()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Sub New()Sub New(ByVal message As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyBase.New(message)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
End Class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
例如实现注销功能:
Dim shutdown As New WindowsController()
shutdown.ExitWindows(shutdown.RestartOptions.LogOff, False)
相关文章推荐
- VB.NET中实现"关机/休眠/重启/注销"的类
- VB.NET中实现"关机/休眠/重启/注销"的类
- VB.NET中实现"关机/休眠/重启/注销"的类
- VB.NET利用Windows API 实现关机、注销、重启等操作
- VC++简单实现关机、重启计算机实例代码
- ASP.NET 实现重启系统或关机
- VB.NET实现关机和重新启动
- VB.NET中实现"关机/休眠/重启/注销
- VB.NET关闭窗体时最小化(附带解决.NET程序导致XP无法关机重启的问题)
- 直接写IO端口0x64完成计算机重启操作实现暴力关机
- vb.net实现Windows关机渐淡效果
- VB实现关机,重启
- 用VB.NET实现设置计算机IP地址
- VB.NET实现关机和重新启动
- VB.NET实现关机和重新启动
- [VB.NET]实现关机和重新启动
- VB.NET实现关机和重新启动
- python实现远程通过网络邮件控制计算机重启或关机
- VC++关机、重启计算机代码
- VB.NET C#实现基于UDP的免服务器局域网多客户端点对点通讯