分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Imports System

Imports System.Text

Imports System.Diagnostics

Imports System.Runtime.InteropServices

Public 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", "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

Private 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

Private 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

Public Sub LogOff() '注销

ExitWindows(RestartOptions.Reboot, False)

End Sub

Public Sub PowerOff() '关闭电源

ExitWindows(RestartOptions.PowerOff, False)

End Sub

Public Sub Reboot() '重启计算机

ExitWindows(RestartOptions.Reboot, False)

End Sub

Public Sub ShutDown() '关闭系统

ExitWindows(RestartOptions.ShutDown, False)

End Sub

Public Sub Suspend() '待机

ExitWindows(RestartOptions.Suspend, False)

End Sub

Public Sub Hibernate() '休眠

ExitWindows(RestartOptions.Hibernate, False)

End Sub

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有