"关机/休眠/重启/注销"的类

2016-01-29 13:16 7 1 收藏

"关机/休眠/重启/注销"的类,"关机/休眠/重启/注销"的类

【 tulaoshi.com - ASP.NET 】

Imports SystemImports System.TextImports System.DiagnosticsImports System.Runtime.InteropServicesPublic Class ExitWindwos Public Enum RestartOptions LogOff = 0 PowerOff = 8 Reboot = 2 ShutDown = 1 Suspend = -1 Hibernate = -2 'EWX_FORCE = 4 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 Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr Private Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer Private 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 Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer Private 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 Private 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 Private 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 Private 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 Private Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean) If Not CheckEntryPoint("powrprof.dll", "SetSuspendStat

来源:http://www.tulaoshi.com/n/20160129/1489540.html

延伸阅读
标签: Web开发
先看看两幅漫画,描述的是HTML,CSS两个语言的分工: HTML建造工的角色 CSS粉刷匠的角色 图片
标签: 育儿知识
见过如此可爱的入狱档案照么?这组名为《非常法匪》的作品,其实是一家专为儿童生产眼镜、太阳镜的公司的广告(图老师整理)创意,趣味十足,Very French Gangsters推出一系列的儿童眼镜,童真而又大牌的设计,配合孩子们搞怪的“入狱照”,萌翻不少人。 育儿相关推荐 ...
虽然关于Java字符串“==”和“equals”的问题是Java学习中的最基础的一个问题,但是仍然有不少刚刚学习Java的同学搞不清楚里面的原理,最近和朋友讨论到这个问题,所以写篇文章给大家分享一下我自己的理解。 首先让我们看一个例子: 代码如下:     public class TestStringEquals {       ...
标签: windows系统
详解Win8系统的"库" 库是可以从一个位置访问所有文档、音乐、图片和其他文件的集合。库其实类似于文件夹:你可以使用库来浏览文件和对文件进行排序。但与文件夹不同的是,库可以收集存储在多个位置中的文件,也就是说库实际上不存储项目,它只是一个文件的快捷展示窗口。 Win8的库 库可以在其他文件夹执行操作,并...
意外宝宝并非喜从天降 很多80后、90后将非计划 怀孕 美其名曰为喜从天降,视奉子成婚为水到渠成。然而事实并非想象那么完美,无数 新生儿 病例证明:这些来自于非计划怀孕的意外宝宝往往意味着胚胎命运多舛。 意外宝宝难保质量非计划怀孕导致孕前补充叶酸、接种疫苗等措施被耽误,胎儿质(图老师整理)量总体上比...

经验教程

601

收藏

15
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部