一个菜鸟的求助

Lzh_lee 2002-01-14 03:51:20
请问在WIN2000上怎么编程实现关机.
...全文
51 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
chillman 2002-09-11
  • 打赏
  • 举报
回复
我也找了很久,以上代码 不能关电源 更改过 reg 值 也不行。只能到windows ”可以安全关闭电源了“
Lzh_lee 2002-04-29
  • 打赏
  • 举报
回复
to wumingcn(wuming)& pasl(阿龙1):权限问题上面两位大哥已经解决了,他们的程序在98和NT下都是能正常关机的,不过不知为什么在win2000下就是不能关机,我猜可能是电源管理的问题,因为开始菜单上的关机用的API和上述的API是不同的。不过也很谢谢你们两位。wumingcn(wuming)能不能把你写的vc代码贴出来?虽然我平常用的是VB,但是C++还是略懂多少的,不要寄给我,我一般都不收的,因为有一次收到据说是csdn一个网友寄来帮我解决问题的信件,结果导致系统崩溃。
pasl 2002-04-28
  • 打赏
  • 举报
回复
Public Function WinError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long

' Return the error message associated with LastDLLError:
sBuff = String$(256, 0)
lCount = FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
If lCount Then
WinError = Left$(sBuff, lCount)
End If

End Function

Public Function IsNT() As Boolean
Static bOnce As Boolean
Static bValue As Boolean

' Return whether the system is running NT or not:
If Not (bOnce) Then
Dim tVI As OSVERSIONINFO
tVI.dwOSVersionInfoSize = Len(tVI)
If (GetVersionEx(tVI) <> 0) Then
bValue = (tVI.dwPlatformId = VER_PLATFORM_WIN32_NT)
bOnce = True
End If
End If
IsNT = bValue

End Function

Private Function NTEnableShutDown(ByRef sMsg As String) As Boolean
Dim tLUID As LUID
Dim hProcess As Long
Dim hToken As Long
Dim tTP As TOKEN_PRIVILEGES, tTPOld As TOKEN_PRIVILEGES
Dim lTpOld As Long
Dim lR As Long

' Under NT we must enable the SE_SHUTDOWN_NAME privilege in the
' process we're trying to shutdown from, otherwise a call to
' try to shutdown has no effect!

' Find the LUID of the Shutdown privilege token:
lR = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tLUID)

' If we get it:
If (lR <> 0) Then

' Get the current process handle:
hProcess = GetCurrentProcess()
If (hProcess <> 0) Then
' Open the token for adjusting and querying (if we can - user may not have rights):
lR = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
If (lR <> 0) Then

' Ok we can now adjust the shutdown priviledges:
With tTP
.PrivilegeCount = 1
With .Privileges(0)
.Attributes = SE_PRIVILEGE_ENABLED
.pLuid.HighPart = tLUID.HighPart
.pLuid.LowPart = tLUID.LowPart
End With
End With

' Now allow this process to shutdown the system:
lR = AdjustTokenPrivileges(hToken, 0, tTP, Len(tTP), tTPOld, lTpOld)

If (lR <> 0) Then
NTEnableShutDown = True
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName & ".mShutDown", "Can't enable shutdown: You do not have the privileges to shutdown this system. [" & WinError(Err.LastDllError) & "]"
End If

' Remember to close the handle when finished with it:
CloseHandle hToken
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName & ".mShutDown", "Can't enable shutdown: You do not have the privileges to shutdown this system. [" & WinError(Err.LastDllError) & "]"
End If
Else
Err.Raise eeSSDErrorBase + 5, App.EXEName & ".mShutDown", "Can't enable shutdown: Can't determine the current process. [" & WinError(Err.LastDllError) & "]"
End If
Else
Err.Raise eeSSDErrorBase + 4, App.EXEName & ".mShutDown", "Can't enable shutdown: Can't find the SE_SHUTDOWN_NAME privilege value. [" & WinError(Err.LastDllError) & "]"
End If

End Function

Public Function ShutdownSystem( _
Optional ByVal eType As EShutDownTypes = EWX_RESET _
) As Boolean
Dim lR As Long
Dim sMsg As String

' Validate shutdown type:
If (eType < EShutDownTypes.[_First] And eType > EShutDownTypes.[_Last]) Then
Err.Raise eeSSDErrorBase + 7, App.EXEName & ".mShutDown", "Invalid parameter to ShutdownSystem: " & eType, vbInformation
Exit Function
End If

' Make sure we have enabled the privilege to shutdown
' for this process if we're running NT:
If (IsNT) Then
If Not (NTEnableShutDown(sMsg)) Then
Exit Function
End If
End If

' This is the code to shut down
lR = ExitWindowsEx(eType, &HFFFFFFFF)
If (lR = 0) Then
Err.Raise eeSSDErrorBase + 3, App.EXEName & ".mShutDown", "ShutdownSystem failed: " & WinError(Err.LastDllError)
Else
' Remember that shutdown will proceed on another
' thread to this one, so code may continue to
' execute after this.
ShutdownSystem = True
End If

End Function



To try out a shutdown, add a Command button to the project's form and then paste in the following code. Note you should save your work before running it, because the shutdown will kill VB without asking you to save any changes! Clicking on the Command button will cause the system to shutdown and reboot.

Private Sub Command1_Click()
' Don't do this unless you've saved the code!
If (MsgBox("Are you sure you want to shutdown?", vbYesNo Or vbQuestion) = vbYes) Then
ShutdownSystem
End If
End Sub

这是很久以前从一个国外的网页中下载的,代码比较长,可以在98’NT下实现关机你可以试一试,如果管用,别忘了给分
pasl 2002-04-28
  • 打赏
  • 举报
回复
This tip demonstrates how to shutdown, logoff and/or reboot a system. Shutting down a Windows 9x system is very straightforward - just one API call to ExitWindowsEx is all that is required. However, under NT it is a little more tricky. By default, no application processes under Windows NT have the privilege to shut the system down, and the call to ExitWindowsEx simply fails on calling it. The trick to make it work is to call the API AdjustTokenPrivileges function to enable the calling process to shut the system down. Note that under NT the user may not have sufficient system privileges to shut down anyway.

Start a new project in VB. Add a new module, then add the following code to it:

' To Shutdown Windows:
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const ENDSESSION_LOGOFF = &H80000000

Public Enum EShutDownTypes
[_First] = 0
EWX_LOGOFF = 0
EWX_SHUTDOWN = 1&
EWX_REBOOT = 2&
EWX_FORCE = 4&
EWX_POWEROFF = 8&
EWX_FORCEIFHUNG = 10& ' NT5 only

EWX_RESET = EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT
[_Last] = &H20& - 1
End Enum
Public Enum EShutDownErrorBaseConstant
eeSSDErrorBase = vbObjectError Or (1048 + &H210)
End Enum

' To Determine if we are running NT or not:
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32s = 0

' To Report API errors:
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long


' ============================================================================================
' NT Only
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Type LUID
LowPart As Long
HighPart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(0 To 0) As LUID_AND_ATTRIBUTES
End Type
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, TokenInformationClass As Integer, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private 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
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2

Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)

Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = (&H2)
Private Const TOKEN_IMPERSONATE = (&H4)
Private Const TOKEN_QUERY = (&H8)
Private Const TOKEN_QUERY_SOURCE = (&H10)
Private Const TOKEN_ADJUST_PRIVILEGES = (&H20)
Private Const TOKEN_ADJUST_GROUPS = (&H40)
Private Const TOKEN_ADJUST_DEFAULT = (&H80)
Private Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
TOKEN_ASSIGN_PRIMARY Or _
TOKEN_DUPLICATE Or _
TOKEN_IMPERSONATE Or _
TOKEN_QUERY Or _
TOKEN_QUERY_SOURCE Or _
TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_ADJUST_GROUPS Or _
TOKEN_ADJUST_DEFAULT)
Private Const TOKEN_READ = (STANDARD_RIGHTS_READ Or _
TOKEN_QUERY)
Private Const TOKEN_WRITE = (STANDARD_RIGHTS_WRITE Or _
TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_ADJUST_GROUPS Or _
TOKEN_ADJUST_DEFAULT)
Private Const TOKEN_EXECUTE = (STANDARD_RIGHTS_EXECUTE)

Private Const TokenDefaultDacl = 6
Private Const TokenGroups = 2
Private Const TokenImpersonationLevel = 9
Private Const TokenOwner = 4
Private Const TokenPrimaryGroup = 5
Private Const TokenPrivileges = 3
Private Const TokenSource = 7
Private Const TokenStatistics = 10
Private Const TokenType = 8
Private Const TokenUser = 1
' ============================================================================================
BestAns1 2002-04-28
  • 打赏
  • 举报
回复
知道了 api 函数用 vb 写也就没问题了吧?
要是答案还不满意就给我发信。

------------------------------
http://wumingcn.yeah.net
BestAns1 2002-04-28
  • 打赏
  • 举报
回复
前面帖子的一些关机方法只能在 9x 下使用。在 nt 内核的系统上(nt/2k/xp)使用,需要用 API 函数 AdjustTokenPrivileges() 为进程设置SE_SHUTDOWN_NAME(分配关机权限),然后调用 API 函数 ExitWindowsEx 关机即可。我常用的是 vc,这里不能给出 vb 代码了,十分抱歉。

不用第三方软件的方法:
win2k 的 Rkit 中自带 shutdown,用 shutdown /l 实现关机。
用命令 at 22:00 shutdown /l 就可以实现定时关机了。
用命令 %windows%\rundll32 user.exe, exitwindows 也能实现关机。也可以把命令加在 计划任务 里面,同样方便。

------------------------------
http://wumingcn.yeah.net
Lzh_lee 2002-04-28
  • 打赏
  • 举报
回复
恐怕这个问题真的没人能答了吧?有点失望,不过想再等几天,半年都等过来了,是不?五一回来结贴,别给人说我不会结贴了,反正分有的是。
Lzh_lee 2002-04-26
  • 打赏
  • 举报
回复
谁说不会结贴,只是没人给正确答案罢了。
cornerxu 2002-04-25
  • 打赏
  • 举报
回复
别跟了,看日期就知道不会结帖了
happybeyond 2002-04-24
  • 打赏
  • 举报
回复
学习!
Lzh_lee 2002-04-18
  • 打赏
  • 举报
回复
有人愿意回答吗?或者什么“微软支持”的出来讲解下答案好不好?
liuzhanwen 2002-04-17
  • 打赏
  • 举报
回复
up
cornerxu 2002-04-17
  • 打赏
  • 举报
回复
UPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUP
KAIBate 2002-04-15
  • 打赏
  • 举报
回复
UPUP
shuzzz 2002-04-12
  • 打赏
  • 举报
回复
等待
snowmany 2002-04-10
  • 打赏
  • 举报
回复
later,up
gump2000 2002-04-10
  • 打赏
  • 举报
回复
来晚了啊

加上 FORCE 看看了
lou_df 2002-04-05
  • 打赏
  • 举报
回复
学习一下。
Lzh_lee 2002-03-26
  • 打赏
  • 举报
回复
这也是可以考虑的,不过也太那个了吧,嘻嘻,如果关机时我控制着鼠标呢?
sippey 2002-03-22
  • 打赏
  • 举报
回复
用程序控制鼠标点关机
加载更多回复(13)

7,762

社区成员

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

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