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 Disable Wakeevent 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, BYV Al Lpname as String, ByRef Lpluid as LUID) as Integer
Declare Function adjusttokenprivileges Lib "advapi32.dll" (ByVal tokenhandle as INTPTR, ByVal disableallprivileges as inte GER, ByRef newstate as Token_privileges, ByVal bufferlength as Integer, ByRef previousstate as token_privileges, ByRef Ret Urnlength 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 Lpsource as INTPTR, ByVal Dwmessageid As Integer, ByVal dwlanguageid As Integer, ByVal lpbuffer as StringBuilder, ByVal nsize As Integer, Byva L Arguments As Integer) As Integer
Public Sub exitwindows (ByVal 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
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 ())
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 + (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 isn't 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
For example, to implement logoff functionality:
Dim shutdown as New Windowscontroller ()
Shutdown. ExitWindows (shutdown. Restartoptions.logoff, False)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.