English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

各位大大
目前我根據各方面的資料,找到關於關機的API,在Win98底下都可以正常關機,在Win2000底下也有按照找到的資料,提昇安全權限並進行關機,但是雖然重開機可以成功,但是如果下達關機指令,電腦是會進行關機動作,但是會停留在『您可以放心關機』的畫面下,卻不能直接將電源關閉,所以我想請問到底要如何才能使Win2000正常關機呢?﹙EX_SHOWDOWN能關機,EX_FORCE卻變成登出,測試兩台電腦結果都一樣﹚
已完成程式如下:
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1
Type LUID
lowpart As Long
highpart As Long
End Type
Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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
Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

Sub AdjustToken ()
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
hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkp.PrivilegeCount = 1
tkp.Privileges(0).pLuid = tmpLuid
tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub

Private Sub Cmd_Click()
AdjustToken
ExitWindowsEx EWX_FORCE,0
End Sub

2005-12-14 07:38:32 · 2 個解答 · 發問者 Rody 5 in 電腦與網際網路 程式設計

2 個解答

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
Private Const EWX_SHUTDOWN = 1
Private Const EWX_POWEROFF = 8
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
Sub SdW()
Dim P As Long, T As Long, L As LUID, S As TOKEN_PRIVILEGES, D As TOKEN_PRIVILEGES, E As Long
P = GetCurrentProcess()
OpenProcessToken P, (&H20 Or &H8), T
LookupPrivilegeValue "", "SeShutdownPrivilege", L
S.PrivilegeCount = 1: S.TheLuid = L: S.Attributes = &H2
AdjustTokenPrivileges T, False, S, Len(D), D, E
End Sub
Private Sub Command1_Click()
SdW
ExitWindowsEx EWX_SHUTDOWN + EWX_POWEROFF, &HFFFF
End Sub

2005-12-14 10:55:09 · answer #1 · answered by W.J.S. 7 · 0 0

各位大大
我也有相同問題,電腦是會進行關機動作,但是會停留在『您可以放心關機』的畫面下,卻不能直接將電源關閉,所以我想請問到底要如何才能使Win2000正常關機呢?﹙剛重灌win2000)

2006-01-12 18:47:17 · answer #2 · answered by Dragon 1 · 0 0

fedest.com, questions and answers