美丽中国支教项目待遇:在XP系统中怎么EXITWINDOWSEX函数没用了?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/01 19:31:30
我用的是VB6
期待回答,Thanks very much!

要先获得权限,看下面代码,注意函数 AdjustToken
将下面内容保存为".frm"文件
------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 2490
ClientLeft = 60
ClientTop = 345
ClientWidth = 2145
LinkTopic = "Form1"
ScaleHeight = 2490
ScaleWidth = 2145
StartUpPosition = 3 'Windows Default
Begin VB.CheckBox Check1
Caption = "强制"
Height = 315
Left = 780
TabIndex = 3
Top = 1950
Width = 795
End
Begin VB.CommandButton Command2
Caption = "重启"
Height = 465
Left = 240
TabIndex = 2
Top = 1260
Width = 1635
End
Begin VB.CommandButton Command1
Caption = "注销"
Height = 465
Left = 240
TabIndex = 1
Top = 660
Width = 1635
End
Begin VB.CommandButton cmdForceShutdown
Caption = "关机"
Height = 435
Left = 240
TabIndex = 0
Top = 90
Width = 1635
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim force As Boolean
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

' Beginning of Code
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_FORCE As Long = 4
Private Const EWX_REBOOT As Long = 2
Private Const EWX_LOGOFF As Long = 0

Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long

Private Sub AdjustToken()

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Dim lngRtn As Long

hdlProcessHandle = GetCurrentProcess()
'MsgBox lngRtn
lngRtn = OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle)
'MsgBox lngRtn
' Get the LUID for shutdown privilege.
lngRtn = LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)
'MsgBox lngRtn
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED

' Enable the shutdown privilege in the access token of this
' process.
lngRtn = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
'MsgBox lngRtn
End Sub

Private Sub Check1_Click()
If Check1.Value = 0 Then
force = False
Else
force = True
End If
End Sub

Private Sub cmdForceShutdown_Click()
If force = True Then
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
Else
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
End If
End Sub

Private Sub Command1_Click()
If force = True Then
ExitWindowsEx (EWX_LOGOFF Or EWX_FORCE), &HFFFF
Else
ExitWindowsEx (EWX_LOGOFF), &HFFFF
End If
End Sub

Private Sub Command2_Click()
If force = True Then
ExitWindowsEx (EWX_REBOOT Or EWX_FORCE), &HFFFF
Else
ExitWindowsEx (EWX_REBOOT), &HFFFF
End If
End Sub

Private Sub Form_Load()
AdjustToken
End Sub