我是
靠谱客的博主
成就大侠,最近开发中收集的这篇文章主要介绍
VB.NET中实现"关机/休眠/重启/注销"的类 ,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Imports
System
Imports
System.Text
Imports
System.Diagnostics
Imports
System.Runtime.InteropServices
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101038681954.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101032110532.gif)
Public
Class WindowsController
Class WindowsController
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Enum RestartOptionsEnum RestartOptions
LogOff = 0
PowerOff = 8
Reboot = 2
ShutDown = 1
Suspend = -1
Hibernate = -2
End Enum
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Structure LUIDStructure LUID
Dim LowPart As Integer
Dim HighPart As Integer
End Structure
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Structure LUID_AND_ATTRIBUTESStructure LUID_AND_ATTRIBUTES
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
Dim pLuid As LUID
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
Dim Attributes As Integer
End Structure
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Structure TOKEN_PRIVILEGESStructure TOKEN_PRIVILEGES
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
Dim PrivilegeCount As Integer
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
Dim Privileges As LUID_AND_ATTRIBUTES
End Structure
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function LoadLibrary()Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function FreeLibrary()Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function GetProcAddress()Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function SetSuspendState()Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function OpenProcessToken()Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Declare Function ExitWindowsEx()Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Sub ExitWindows()Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
Select 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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Protected Sub ExitWindows()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.GetLastWin32Error()))
End Sub
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Protected Sub EnableToken()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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Protected Sub SuspendSystem()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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Protected Function CheckEntryPoint()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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Protected Function FormatError()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
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101038681954.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101032110532.gif)
Public
Class PrivilegeException
Class PrivilegeException
Inherits Exception
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Sub New()Sub New()
MyBase.New()
End Sub
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101044632447.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101043127548.gif)
Public Sub New()Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101038681954.gif)
例如实现注销功能:
Dim
shutdown
As
New
WindowsController()
shutdown.ExitWindows(shutdown.RestartOptions.LogOff,
False
)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101038681954.gif)
![](https://file2.kaopuke.com:8081/files_image/2023062421/202306242101038681954.gif)
转自: http://blog.csdn.net/modou/archive/2003/04/25/14901.aspx
最后
以上就是成就大侠为你收集整理的VB.NET中实现"关机/休眠/重启/注销"的类 的全部内容,希望文章能够帮你解决VB.NET中实现"关机/休眠/重启/注销"的类 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复