VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
当前位置:
主页 > 编程开发 > VBnet >
  • vb.net教程之VB.NET中实现"关机/休眠/重启/注销"

  • 2017-06-06 19:00 来源:未知
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 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 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 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

        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 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

        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

例如实现注销功能:
        Dim shutdown As New WindowsController()
        shutdown.ExitWindows(shutdown.RestartOptions.LogOff, False)

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。

相关教程
  • 互联网资产管理须持牌经营 2018-12-09
  • 新闻中心给记者留下美好记忆 2018-12-09
  • 险企偿付能力2017年“成绩单”:3家不达标 7家被关注 2018-12-07
  • 【学习时刻】清华大学周绍杰:坚定不移地贯彻落实新发展理念 2018-12-07
  • 建立市场经济,原本的计划经济哪里去?坚持集体的南街村没有到户就没有建立市场经济,是不是还在搞计划经济?看其实现公有资本在其所有者们进行分配应是计划经济。 2018-10-30
  • 市场经济就是一种唯利是图的经济,是一种波动、走极端的经济,新时期的发展方式与结果就是如 此。那么指望市场经济来解决需要与不平衡的矛盾,对症下药了吗? 2018-10-30
  • 比利时30大胜巴拿马 卢卡库梅开二度 强队总算没有都凉凉 2018-10-18
  • 新科欧洲状元 五盾军团能否延续法兰西之夏荣耀 2018-10-18
  • 河北一保时捷车主疑遭枪击 警方悬赏9万元通缉 2018-09-26
  • 【十九大·理论新视野】动漫:“社会主义现代化强国”如何建成 2018-09-26
  • 林海起诉海底捞侵权 对方回应:已停止播放其音乐作品 2018-06-23
  • 新疆伊犁:薰衣草香飘万里 “紫色经济”成产业 2018-06-23
  • 《与书行》第一集先导片丨李小白:阅读让时尚圈回归平静 2018-06-23
  • 太原:幸福是奋斗出来的--山西频道--人民网 2018-06-23
  • 294| 19| 753| 564| 688| 648| 763| 835| 101| 288| 403| 469| 100| 554| 694|