zoukankan      html  css  js  c++  java
  • VB.NET中实现"关机/休眠/重启/注销"的类

    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 StringAs IntPtr
            
    Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
            
    Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As StringAs IntPtr
            
    Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As IntegerByVal ForceCritical As IntegerByVal DisableWakeEvent As IntegerAs Integer
            
    Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As IntegerByRef TokenHandle As IntPtr) As Integer
            
    Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As StringByVal lpName As StringByRef lpLuid As LUID) As Integer
            
    Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As IntegerByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As IntegerByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As IntegerAs Integer
            
    Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As IntegerByVal dwReserved As IntegerAs Integer
            
    Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As IntegerByVal lpSource As IntPtr, ByVal dwMessageId As IntegerByVal dwLanguageId As IntegerByVal lpBuffer As StringBuilder, ByVal nSize As IntegerByVal Arguments As IntegerAs Integer

            
    Public 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

            
    Protected Sub ExitWindows(ByVal how As IntegerByVal force As Boolean)
                EnableToken(
    "SeShutdownPrivilege")
                
    If force Then how = how Or EWX_FORCE
                
    If (ExitWindowsEx(how, 0= 0Then 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, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
            
    End Sub

            
    Protected Sub SuspendSystem(ByVal hibernate As BooleanByVal 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, 10)), Convert.ToInt32(IIf(force, 10)), 0)
            
    End Sub

            
    Protected Function CheckEntryPoint(ByVal library As StringByVal method As StringAs 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 IntegerAs 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 WindowsController()
            shutdown.ExitWindows(shutdown.RestartOptions.LogOff, 
    False)

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    朴素贝叶斯方法(二分类)[机器学习实战]
    【HIHOCODER 1055】 刷油漆(树上背包)
    【HIHOCODER 1142】 三分·三分求极值
    【HIHOCODER 1420】 Bigint Multiplication
    【HIHOCODER 1403】后缀数组一·重复旋律(后缀数组)
    【HIHOCODER 1589】回文子串的数量(Manacher)
    【HIHOCODER 1599】逃离迷宫4
    【HIHOCODER 1182】欧拉路·三
    【HIHOCODER 1181】欧拉路·二
    【HIHOCODER 1176】 欧拉路·一
  • 原文地址:https://www.cnblogs.com/Athrun/p/670012.html
Copyright © 2011-2022 走看看