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

时间: 2024-10-27 10:31:34

VB.NET中实现关机/休眠/重启/注销的类的相关文章

Win7系统点击开始菜单中的关机按钮变成注销是怎么回事?

  Win7系统点击开始菜单中的关机按钮变成注销是怎么回事? 1.有可能是中了电脑病毒,对系统进行全盘查毒,清除电脑内木马病毒; 2.尝试使用命令进行关机,按Win+R打开运行,输入CMD,回车; 3.在cmd中输入shutdown/s,点击回车,看是否可以关机; 4.禁用组策略设置,在运行中输入gpedit.msc,点回车; 5.双击"用户配置"下的"管理模板";

windows 2003/2008中去掉关机或重启时的对话框提示

windows 2003 单击"开始→运行",在弹出的运行对话框中输入"gpedit.msc",打开组策略窗口,选中"计算机配置→管理模板→系统",双击"显示关机事件跟踪",在弹出的窗口中选择"已禁用",最后再单击"应用→确定"按钮即可. windows 2008 首先,单击"开始|运行",在"运行"对话框中输入"gpedit.msc&q

在Win8系统开始屏幕添加关机、重启按钮

  在Win8系统中,微软将关机.重启等选项从开始菜单转移到了全新的"超级按钮"中,但是血多朋友仍然希望能够在开始屏幕中直接使用关机.重启等功能.下面系小编就教教大家如何在Win8系统开始屏幕中添加关机.重启按钮. 1.首先,我们在传统桌面空白处点击鼠标右键,选择"新建-快捷方式" 2.在"位置栏"输入所需要的功能命令代码,系统之家小编以关机功能为例子,代码:shutdown.exe -s -t 00 -hybrid 3.接着给快捷方式起个名字,

Win8右键菜单中添加快速关机、重启、注销的方法

  希望在Win8系统里快速关机.重启.注销计算机的朋友有戏了.不用安装任何第三方的软件,即可轻松实现,再也不用去右下角点击关机了! 方法就是在注册表中添加命令,新建立记事本,把以下内容保存为.reg,双击导入即可. 以上是部分代码,由于论坛屏蔽了斜杠,只能放到附件里供大家下载了!上面显示的位置顺序可以自己调整或去除."

在win8右键菜单添加关机,重启,注销

在Windows 8右键菜单添加关机,重启,注销,休眠,睡眠,切换用户,锁住.呃呃呃想不到Windows 8的关机把大家给难住了 -- Windows 8右键菜单添加关机,重启,注销- 原理就是在注册表中添加命令,新建立记事本,把以下内容保存为.reg,双击导入即可. Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOTDesktopBackgroundShellPower Menu] "MUIVerb"="Power

C++编程实现Windows关机、重启、注销

要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_SHUTDOWN.EWX_REBOOT.EWX_LOGOFF等标志常量,分别表示关机.重启.注销.另外如果加上EWX_FORCE这个标志常量的话,该表明强制执行该操作.Windows在执行以上操作的时候会首先给每个正在运行中的程序发送一个WM_QUERYENDSESSION消息,告诉它们现在我要退出

Linux基本命令-注销、关机、重启

  一.注销,关机,重启 注销系统的logout命令 1,Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]#logout Red Hat Linuxrelease 9(Shike) Kernel 2.4.20.8 on an i686 Login: ← 回到登陆的画面 2,关机或重新启动的shutdown命令 Shutdown命令可以关闭所有程序,依照用户的需要,重新启动或关机. 参数说明如下: l

Windows7关机、重启、待机、休眠命令

  Windows7关机.重启.待机.休眠命令 Windows7下一些关机.重启.待机.休眠命令,通过Autohotkey可实现快速关机等操作. ;立即关机,无提示.延时. shutdown -p ;立即重启 shutdown -r -t 00 ;进入休眠状态,如有关闭休眠则进入待机状态 rundll32.exe powrProf.dll,SetSuspendState Windows7关闭休眠功能,混合睡眠将不可用: powercfg -h off 查看命令: powercfg -a

linux中reboot与shutdown重启/关机命令详解

我们在操作Linux vps/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用容易导致面板卡死.所以最好的方法就是通过SSH命令操作,老蒋这里分享2个命令,reboot与shutdown都可以用来作为重启VPS/关机重启操作. 第一.reboot 使用方法:直接在SSH ROOT权限用户下输入reboot命令就可以看到The system is going down for reboot NOW的提示,目