Type of "shutdownsleeprestartlogout" in VB.NET

xiaoxiao2021-03-06  81

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 ​​"LoadLibFilename As String) AS INTPTR

Declare Function Freelibrary LIB "Kernel32" (Byval Hlibmodule As INTPTR) AS INTEGER

Declare function getProcaddress lib "kernel32" (Byval LPPROCNAME AS STRING) AS INTPTR

Declare function setsuspendState lib "PoWrprof" (Byval Forcecritical AS Integer, Byval DisablewakeEvent As Integer) AS Integer

Declare function openprocessToken lib "advapi32.dll" (byval processhandle as integer, byref tokenhandle as intptr) AS integer

Declare function lookupprivilegevalue lib "advapi32.dll" alias "lookupprivilerage, 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 IntegerDeclare 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, ByVal Arguments As Integer) As Integer

Public Sub EXITWINDOWS (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 Force As Boolean)

EnableToken ("SESHUTDOWNPRIVILEGE")

If force dam = how or ewx_force

IF (exitwindowsex (how, 0) = 0) THROW new privilegeexception (formaterror (Marshal.getlastwin32error ()))

End Sub

Protected sub enabletoken (Byval Privilege As String)

IF Not CheckenPoint ("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.privilegect = 1

Tokenprivileges.privileges.attributes = SE_PRIVILE_ENABED

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") 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 Privilegeexceptioninherits Exception

Public Sub New ()

Mybase.new ()

End Sub

Public Sub New (Byval Message As String)

Mybase.new (message)

End Sub

END CLASS

For example, implementation of the logout function:

DIM Shutdown As New WindowsController ()

Shutdown.exitwindows (shutdown.restartOptions.logoff, false)

转载请注明原文地址:https://www.9cbs.com/read-108501.html

New Post(0)