VB在Win2000下实现关机

大家可能都知道在Win98下实现关机只须使用ExitWindows或ExitWindowsEx这两个API函数就可以,但是到了Windows2000下就不好使了,不论设置什么参数都只是注销,下面教你如何实现Win2000下关机。

'窗体中代码

Option Explicit

Dim uFlags As Long

Private Sub Command1_Click()
AdjustTokenPrivilegesForNT '在95/98中调用没作用,但为了和NT兼容,写上无妨
ExitWindowsEx EWX_REBOOT, 0 '这里将uFlgs换成以上面标记蓝色字中所提到的四个参数之一即可
End Sub

 

'模块中代码

Option Explicit
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

'ExitWindowsEx的参数uflags,有四个对应值,分别是:

Public Const EWX_LOGOFF = 0 '退出(注销)
Public Const EWX_SHUTDOWN = 1 '关机
Public Const EWX_REBOOT = 2 '重启动
Public Const EWX_FORCE = 4 '强制关机,即不通知现在活动应用程序让其先自我关闭

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

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

'这个函数就是用于NT关机中使用的
Sub AdjustTokenPrivilegesForNT()

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


 

展开阅读全文

win2000关机程序???

03-03

这是一个关机程序,但不能进行关机,只能重启,怎么会事呀rn代码:Public Const EWX_SHUTDOWN As Long = 1rnPublic Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As LongrnrnExitWindowsEx EWX_SHUTDOWN, 0rnrn在NT和2000,用程序去关机需要一定的权限。以下是MSDN里的做法:rnrnStep By Step ExamplernCreate a new standard EXE in Visual Basic. Form1 is created by default. rnrnrnView the code for Form1. In the Declarations section, add the following code:rnrnrnrn Private Type LUIDrn UsedPart As Longrn IgnoredForNowHigh32BitPart As Longrn End Typernrn Private Type TOKEN_PRIVILEGESrn PrivilegeCount As Longrn TheLuid As LUIDrn Attributes As Longrn End Typernrn Private Const EWX_SHUTDOWN As Long = 1rn Private Const EWX_FORCE As Long = 4rn Private Const EWX_REBOOT = 2rnrn Private Declare Function ExitWindowsEx Lib "user32" (ByVal _rn dwOptions As Long, ByVal dwReserved As Long) As Longrnrn Private Declare Function GetCurrentProcess Lib "kernel32" () As Longrn Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _rn ProcessHandle As Long, _rn ByVal DesiredAccess As Long, TokenHandle As Long) As Longrn Private Declare Function LookupPrivilegeValue Lib "advapi32" _rn Alias "LookupPrivilegeValueA" _rn (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _rn As LUID) As Longrn Private Declare Function AdjustTokenPrivileges Lib "advapi32" _rn (ByVal TokenHandle As Long, _rn ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES _rn , ByVal BufferLength As Long, _rn PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Longrn rnAdd a procedure called AdjustToken with the following code:rnrnrnrn Private Sub AdjustToken()rn Const TOKEN_ADJUST_PRIVILEGES = &H20rn Const TOKEN_QUERY = &H8rn Const SE_PRIVILEGE_ENABLED = &H2rn Dim hdlProcessHandle As Longrn Dim hdlTokenHandle As Longrn Dim tmpLuid As LUIDrn Dim tkp As TOKEN_PRIVILEGESrn Dim tkpNewButIgnored As TOKEN_PRIVILEGESrn Dim lBufferNeeded As Longrnrn hdlProcessHandle = GetCurrentProcess()rn OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _rn TOKEN_QUERY), hdlTokenHandlernrn ' Get the LUID for shutdown privilege.rn LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuidrnrn tkp.PrivilegeCount = 1 ' One privilege to setrn tkp.TheLuid = tmpLuidrn tkp.Attributes = SE_PRIVILEGE_ENABLEDrnrn ' Enable the shutdown privilege in the access token of this process.rn AdjustTokenPrivileges hdlTokenHandle, False, _rn tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeededrnrn End Subrn rnAdd a CommandButton to the form. In the Click event, add the following code:rnrnrnrn Private Sub Command1_Click()rn AdjustTokenrn ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFFrn End Subrnrnrnrn 论坛

没有更多推荐了,返回首页