老生常谈,win2000的重启,看看会不会?

usstdiy 2003-08-26 03:37:14
思路:通过asp调用com组件的方式来实现服务器的重启动。(如果不能实现,不要笑我)
方法:网上有好多重启的例子,随便找了一个能用的。(不是赝品,代码在下面)
结果:如果是一台已经登陆的服务器,就是说服务器的已经用administrator登陆了。在客户端执行Restart.asp,服务器成功启动。
服务器重起后不要登陆,或者将服务器设置为锁定状态。执行Restart.asp,这次就没戏了。经反复验证,在锁定状态下,此法无效。
看看了代码,可能是没登陆无法获得LUID的原因吧。在com+组件的控制面板里加入用户名和登陆密码,无效。我也不知道如何指定一个LUID,高手来给我个解吧。

Private Const EWX_LogOff As Long = 0
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8
Private wk_Restart As String


'The ExitWindowsEx function either logs off, shuts down, or shuts
'down and restarts the system.
Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

'The GetLastError function returns the calling thread's last-error
'code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
Private Declare Function GetLastError Lib "kernel32" () As Long

Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1

Public glngWhichWindows32 As Long

'The GetVersion function returns the operating system in use.
Private Declare Function GetVersion Lib "kernel32" () As Long

Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type

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

'The GetCurrentProcess function returns a pseudohandle for the
'current process.
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

'The OpenProcessToken function opens the access token associated with
'a process.
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

'The LookupPrivilegeValue function retrieves the locally unique
'identifier (LUID) used on a specified system to locally represent
'the specified privilege name.
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long

'The AdjustTokenPrivileges function enables or disables privileges
'in the specified access token. Enabling or disabling privileges
'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
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 Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)

Private Sub AdjustToken()

'********************************************************************
'* This procedure sets the proper privileges to allow a log off or a
'* shut down to occur under Windows NT.
'********************************************************************

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

'Set the error code of the last thread to zero using the
'SetLast Error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
SetLastError 0

'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
hdlProcessHandle = GetCurrentProcess()

If GetLastError <> 0 Then
MsgBox "GetCurrentProcess error==" & GetLastError
End If

OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle

If GetLastError <> 0 Then
MsgBox "OpenProcessToken error==" & GetLastError
End If

'Get the LUID for shutdown privilege
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

If GetLastError <> 0 Then
MsgBox "LookupPrivilegeValue error==" & GetLastError
End If

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
AdjustTokenPrivileges hdlTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded

If GetLastError <> 0 Then
MsgBox "AdjustTokenPrivileges error==" & GetLastError
End If

End Sub
Function RestartComputer(ByVal UserName As String, ByVal Pwd As String) As Boolean
If UserName <> "a" Or Pwd <> "b" Then
RestartComputer = False
Exit Function
Else
On Error GoTo Errorhandler
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken
End If
ExitWindowsEx EWX_REBOOT, EWX_FORCE
RestartComputer = True
End If
Exit Function

Errorhandler:
RestartComputer = False
MsgBox "ExitWindowsEx's GetLastError " & GetLastError
End Function
...全文
23 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
usstdiy 2003-08-29
  • 打赏
  • 举报
回复
我也看到这个方法了,准备试一下,成了就给分
putongren 2003-08-29
  • 打赏
  • 举报
回复
提供两个思路吧(相关代码很久没写了,所以没法给代码)。

1. 如果用 ExitWindowsEx 不行,试试 InitiateSystemShutdown,我在几年前实现过,但不是 COM,远程关闭 NT、2k Server都可以,无论目标机器是否登录或锁定。

2. 另写一个 Win2k 的服务,以系统管理员的权限启动那个服务,在那个服务中可以 ExitWindowsEx。这个我也实现过。比较麻烦的是你的 COM 与服务之间的通讯,可以写一段很短的 TCP/IP 代码来实现。
usstdiy 2003-08-27
  • 打赏
  • 举报
回复
好难啊,还没解决
iiboy 2003-08-26
  • 打赏
  • 举报
回复
看着前头痛!!!
usstdiy 2003-08-26
  • 打赏
  • 举报
回复
多谢!!等待中....
lwm1977 2003-08-26
  • 打赏
  • 举报
回复
调试ing
doudoushen 2003-08-26
  • 打赏
  • 举报
回复
下午的活太累了 一看代码就头疼
usstdiy 2003-08-26
  • 打赏
  • 举报
回复
up
usstdiy 2003-08-26
  • 打赏
  • 举报
回复
千万不要晕
chutianqi 2003-08-26
  • 打赏
  • 举报
回复
晕了~~~~~~~

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧