VB.NET中實現"關機/休眠/重啟/登出"的類 (轉)
Imports System
Imports System.Text
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Public Class Controller
Public Enum RestartOptions
LogOff = 0
PowerOff = 8
Reboot = 2
ShutDown = 1
Suspend = -1
= -2
End Enum
Public Structure LUID
Dim LowPart As Integer
Dim HighPart As Integer
End Structure
Public Structure LUID_AND_ATTRIBUTES
Dim pLuid As LUID
Dim Attributes As Integer
End Structure
Public Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim Privileges As LUID_AND_ATTRIBUTES
End Structure
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const EWX_FORCE = 4
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
Declare Function OpenProcessToken Lib "adv32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
Declare 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
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Integer, ByVal lp As IntPtr, ByVal dwMessageId As Integer, ByVal dguageId As Integer, ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
Public Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
Case how
Case RestartOptions.Suspend
SuspendSystem(False, force)
Case RestartOptions.Hibernate
SuspendSystem(True, force)
Case Else
ExitWindows(Convert.ToInt32(how), force)
End Select
End Sub
Protected Sub ExitWindows(ByVal how As Integer, ByVal force As Boolean)
EnableToken("SeShutdownPrivilege")
If force Then how = how Or EWX_FORCE
If (ExitWindowsEx(how, 0) = 0) Then Throw New PrivilegeException(FormatError(Marshal.GetLastError()))
End Sub
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
Protected Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean)
If Not CheckEntryPoint("powrprof.dll", "SetSuspendState") Then Throw New PlatformNotSupportedException("The SetSuspendState method is not supported on this system!")
SetSuspendState(Convert.ToInt32(IIf(hibernate, 1, 0)), Convert.ToInt32(IIf(force, 1, 0)), 0)
End Sub
Protected Function CheckEntryPoint(ByVal library As String, ByVal method As String) As Boolean
Dim libPtr As IntPtr = LoadLibrary(library)
If Not libPtr.Equals(IntPtr.Zero) Then
If Not GetProcAddress(libPtr, method).Equals(IntPtr.Zero) Then
FreeLibrary(libPtr)
Return True
End If
FreeLibrary(libPtr)
End If
Return False
End Function
Protected Function FormatError(ByVal number As Integer) As String
Dim Buffer = New StringBuilder(255)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, Buffer, Buffer.Capacity, 0)
Return Buffer.ToString()
End Function
End Class
Public Class PrivilegeException
Inherits Exception
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
例如實現登出功能:
Dim shutdown As New Windowntroller()
shutdown.ExitWindows(shutdown.RestartOptions.LogOff, False)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993499/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Ubuntu關機、重啟、登出命令Ubuntu
- Android關機重啟實現Android
- win10關機按鍵只有關機重啟睡眠沒有登出怎麼辦Win10
- Android使用程式碼實現關機/重啟Android
- Linux 關機重啟流程分析(轉)Linux
- PHP 實現平滑關閉/重啟PHP
- 關閉 Ubuntu 中的關機/重啟確認的小技巧Ubuntu
- android 系統重啟與關機:java 程式碼實現AndroidJava
- Windows XP 系統中實現1秒關機(轉)Windows
- Linux的關機與重啟命令Linux
- centos關機與重啟命令CentOS
- Linux重啟和關機Linux
- VB.NET中關於DataGrid顏色的自定義。 (轉)
- VB.NET中物件的克隆 (轉)物件
- 關於容器安全機制的登入/登出
- VB.Net中文教程(4) 類別繼承(Inheritance)關係 (轉)繼承
- VM中實現手機震動功能的類
- win7休眠的開啟與關閉方法Win7
- 精通ASP.NET(基於VB.NET)( 二)VB.NET類 (轉)ASP.NET
- Vista/win7中關機、睡眠和休眠的區別Win7
- Linux關機和重啟命令Linux
- redhat shutdown關機及重啟示例Redhat
- Solaris關機重啟命令小結
- windows10如何關閉休眠_win10怎麼關閉待機休眠WindowsWin10
- 5分鐘瞭解使用WGCLOUD實現伺服器主機遠端關機、重啟的操作GCCloud伺服器
- hibernate 啟動和輔助類實現資源的重複使用
- vb.net 類庫中如何使用webserviceWeb
- 類轉json的基類實現JSON
- 【VB.Net機房重構】儲存過程的使用儲存過程
- Linux關機與重啟的命令詳解Linux
- win10為什麼關機後自動重啟 win10關機後自動重啟的方法Win10
- Go 如何實現熱重啟Go
- Android 關機、重啟、recovery流程分析Android
- Centos6.7關機和重啟命令CentOS
- Mac 設定定時關機、重啟、睡眠Mac
- Linux關機重啟命令詳解Linux
- centos關機與重啟命令詳解CentOS
- VB.net中HOOK的應用(二) (轉)Hook