关机/重启/注销
2015-09-04 17:32
218 查看
Imports System.Runtime.InteropServices
<DllImport("kernel32.dll", ExactSpelling:=True)> _
Friend
Shared Function GetCurrentProcess() As IntPtr
End Function
<DllImport("advapi32.dll", ExactSpelling:=True,
SetLastError:=True)> _
Friend Shared Function OpenProcessToken(ByVal h
As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
End
Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Friend
Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String,
ByRef pluid As Long) As Boolean
End Function
<DllImport("advapi32.dll", ExactSpelling:=True,
SetLastError:=True)> _
Friend Shared Function
AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst
As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As
IntPtr) As Boolean
End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)>
_
Friend Shared Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As
Integer) As Boolean
End Function
Friend Const SE_PRIVILEGE_ENABLED As Integer = &H2
Friend
Const TOKEN_QUERY As Integer = &H8
Friend Const
TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Friend Const
SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Friend Const
EWX_LOGOFF As Integer = &H0 '注销计算机
Friend Const EWX_SHUTDOWN As
Integer = &H1 '关闭计算机
Friend Const EWX_REBOOT As Integer = &H2
'重新启动计算机
Friend Const EWX_FORCE As Integer = &H4 '关闭所有进程,注销计算机
Friend Const EWX_POWEROFF As Integer = &H8
Friend Const
EWX_FORCEIFHUNG As Integer = &H10
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Friend
Structure TokPriv1Luid
Public Count As Integer
Public Luid
As Long
Public Attr As Integer
End Structure
Private Shared Sub DoExitWin(ByVal flg As Integer)
Dim xc As
Boolean '判断语句
Dim tp As TokPriv1Luid
Dim hproc As IntPtr =
GetCurrentProcess()
'调用进程值
Dim htok As IntPtr =
IntPtr.Zero
xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or
TOKEN_QUERY, htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
xc = LookupPrivilegeValue(Nothing,
SE_SHUTDOWN_NAME, tp.Luid)
xc = AdjustTokenPrivileges(htok, False,
tp, 0, IntPtr.Zero, IntPtr.Zero)
xc = ExitWindowsEx(flg, 0)
End Sub
Public Shared Sub Reboot()
DoExitWin((EWX_FORCE Or
EWX_REBOOT)) '重新启动计算机
End Sub
Public Shared Sub PowerOff()
DoExitWin((EWX_FORCE Or
EWX_POWEROFF)) '关闭计算机
End Sub
Public Shared Sub LogoOff()
DoExitWin((EWX_FORCE Or
EWX_LOGOFF)) '注销计算机
End Sub
<DllImport("kernel32.dll", ExactSpelling:=True)> _
Friend
Shared Function GetCurrentProcess() As IntPtr
End Function
<DllImport("advapi32.dll", ExactSpelling:=True,
SetLastError:=True)> _
Friend Shared Function OpenProcessToken(ByVal h
As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
End
Function
<DllImport("advapi32.dll", SetLastError:=True)> _
Friend
Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String,
ByRef pluid As Long) As Boolean
End Function
<DllImport("advapi32.dll", ExactSpelling:=True,
SetLastError:=True)> _
Friend Shared Function
AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst
As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As
IntPtr) As Boolean
End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)>
_
Friend Shared Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As
Integer) As Boolean
End Function
Friend Const SE_PRIVILEGE_ENABLED As Integer = &H2
Friend
Const TOKEN_QUERY As Integer = &H8
Friend Const
TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Friend Const
SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Friend Const
EWX_LOGOFF As Integer = &H0 '注销计算机
Friend Const EWX_SHUTDOWN As
Integer = &H1 '关闭计算机
Friend Const EWX_REBOOT As Integer = &H2
'重新启动计算机
Friend Const EWX_FORCE As Integer = &H4 '关闭所有进程,注销计算机
Friend Const EWX_POWEROFF As Integer = &H8
Friend Const
EWX_FORCEIFHUNG As Integer = &H10
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Friend
Structure TokPriv1Luid
Public Count As Integer
Public Luid
As Long
Public Attr As Integer
End Structure
Private Shared Sub DoExitWin(ByVal flg As Integer)
Dim xc As
Boolean '判断语句
Dim tp As TokPriv1Luid
Dim hproc As IntPtr =
GetCurrentProcess()
'调用进程值
Dim htok As IntPtr =
IntPtr.Zero
xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or
TOKEN_QUERY, htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
xc = LookupPrivilegeValue(Nothing,
SE_SHUTDOWN_NAME, tp.Luid)
xc = AdjustTokenPrivileges(htok, False,
tp, 0, IntPtr.Zero, IntPtr.Zero)
xc = ExitWindowsEx(flg, 0)
End Sub
Public Shared Sub Reboot()
DoExitWin((EWX_FORCE Or
EWX_REBOOT)) '重新启动计算机
End Sub
Public Shared Sub PowerOff()
DoExitWin((EWX_FORCE Or
EWX_POWEROFF)) '关闭计算机
End Sub
Public Shared Sub LogoOff()
DoExitWin((EWX_FORCE Or
EWX_LOGOFF)) '注销计算机
End Sub
相关文章推荐
- [Android Pro] android 混淆文件project.properties和proguard-project.txt
- 【Python学习日记】 第四天
- HDU 4545 魔法串
- 网上看的一个SQL需求写的SQL(有点难度哦)
- JPA
- AI(Adobe Illustrator)简单入门——小熊
- 音乐播放器-DAO之MusicUtils
- python相关的一些站点
- Topcoder SRM630,DIV1,500,后缀数组
- hdu3397(线段树区间合并)
- js中json字符串转成js对象
- matlab 全部的随机数函数
- 【Android】简单详细的安卓开发环境配置及运行第一个程序Helloworld
- Codeforces 274A k-Multiple Free Set
- 二叉树遍历-----前序后序迭代遍历的新思路
- 搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)
- python发邮件
- 提高
- python的注释
- 内省和BeanUtils框架