VB.NET中实现"关机休眠重启注销"的类

VB.NET中实现"关机休眠重启注销"的类,第1张

概述 Imports System Imports System.Text Imports System.Diagnostics Imports System.Runtime.InteropServices     Public Class WindowsController         Public Enum RestartOptions             LogOff = 0       imports System
imports System.Text
imports System.Diagnostics
imports System.Runtime.InteropServices

Public Class WindowsController
Public Enum Restartoptions
logoff = 0
PowerOff = 8
Reboot = 2
ShutDown = 1
Suspend = -1
Hibernate = -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 "advAPI32.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 Exit WindowsEx lib "user32" (ByVal uFlags As Integer,ByVal DWReserved As Integer) As Integer
Declare 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

Public Sub Exit Windows(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
Exit Windows(Convert.ToInt32(how),force)
End Select
End Sub

Protected Sub Exit Windows(ByVal how As Integer,ByVal force As Boolean)
Enabletoken("SeShutdownPrivilege")
If force Then how = how Or EWX_FORCE
If (Exit WindowsEx(how,0) = 0) Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
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,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,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,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 WindowsController()
shutdown.Exit Windows(shutdown.Restartoptions.logoff,False) 总结

以上是内存溢出为你收集整理的VB.NET中实现"关机/休眠/重启/注销"的类全部内容,希望文章能够帮你解决VB.NET中实现"关机/休眠/重启/注销"的类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1292210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存