勇士总冠军颁奖典礼:怎么用程序实现电脑的自动关机?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/20 20:45:25
我想用vb或c来实现一个程序,将这个程序设置成开机自动运行,如果人为的将这个程序停止就会自动关机。那位大侠能为小弟指点迷津。感激不尽!
我只有这么多的分都给了!
我的系统是XP的!!!
谢谢大家,问题解决了,我用的shell函数,我只能给一个人加分!抱歉!

其实在 XP 下面,只要执行 SHUTDOWN 命令就可以关机了,而详细的参数可能参见 shutdown /? 你如果想要在其它程序下关机,只要用 shell ,之类的函数去调用外部的命令就OK了.如果做到"人为的将这个程序停止就会自动关机",建议可以用两个程序,相互监视对方的进程.

楼上的也真是,写了那么一大行代码,关机很简单,用API函数就可以实现
声明一个API的函数
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
建立个按纽,CLICK 事件里添加
ExitWindowsEx EWX_SHUTDOWN, 0 ' ShutDown
就可以了
GOOD LUCK
用软件改变世界,用代码书写人生,用思想创造未来!
如果有兴趣想学习可以QQ上交流86711527

下面是我愿来写的关机程序,你可以拷来用.因为你的系统是2000,所以需要特别注意的是应该先得到关机的特权:(你要想弄懂下面的程序,要先学学怎么调用api函数的知识.如果你一看就头大,哈哈,那放弃吧......嘿嘿,想关机也并不是容易的事情啊......)

其中:前面一些Public Declare都是api函数的声明.
Public Sub AdjustToken()子程序用来取得关机特权.
Public Sub Shutdown() '是关机子程序
Public Sub Reboot() '是重启子程序

*********************代码开始了:*****************
Public Structure LUID
Dim UsedPart As Integer
Dim IgnoredForNowHigh32BitPart As Integer
End Structure

Public Structure LUID_AND_ATTRIBUTES
Dim TheLuid As LUID
Dim Attributes As Integer
End Structure

Public Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim TheLuid As LUID
Dim Attributes As Integer
End Structure

'强制关机函数
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer

'GetLastError函数返回本线程的最后一次错误代码。错误代码是按照线程
'储存的,多线程也不会覆盖其他线程的错误代码。
Public Declare Function GetLastError Lib "kernel32" () As Integer

'GetCurrentProcess函数返回当前进程的一个句柄。
Public Declare Function GetCurrentProcess Lib "kernel32" () As Integer

'OpenProcessToken函数打开一个进程的访问代号。
Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Integer, ByVal DesiredAccess As Integer, ByRef TokenHandle As Integer) As Integer

'LookupPrivilegeValue函数获得本地唯一的标示符(LUID),用于在特定的系统中
'表示特定的优先权。
'UPGRADE_WARNING: 结构 LUID 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA"(ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer

'AdjustTokenPrivileges函数使能或者禁用指定访问记号的优先权。
'使能或者禁用优先权需要TOKEN_ADJUST_PRIVILEGES访问权限。
'UPGRADE_WARNING: 结构 TOKEN_PRIVILEGES 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
'UPGRADE_WARNING: 结构 TOKEN_PRIVILEGES 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
Public Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Integer, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer

Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Integer)
'********************************************************************
'* 这个过程设置正确的优先权,以允许在Windows NT下关机或者重新启动。
'********************************************************************
Public Sub AdjustToken()

Const TOKEN_ADJUST_PRIVILEGES As Short = &H20s
Const TOKEN_QUERY As Short = &H8s
Const SE_PRIVILEGE_ENABLED As Short = &H2s

Dim hdlProcessHandle As Integer
Dim hdlTokenHandle As Integer
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Integer

'使用SetLastError函数设置错误代码为0。
'这样做,GetLastError函数如果没有错误会返回0
'''''''SetLastError 0

'GetCurrentProcess函数设置 hdlProcessHandle变量
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

' 获得关机优先权的LUID
LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)

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

tkp.PrivilegeCount = 1 ' 设置一个优先权
'UPGRADE_WARNING: 未能解析对象 tkp.TheLuid 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"”
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED

' 对当前进程使能关机优先权
AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)

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

End Sub
Public Sub Shutdown() '关机子程序
'******************根据windows版本来关机************************
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken() '调用取得优先权子程序
End If

ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFFs)
'*****************************************************************
End Sub

Public Sub Reboot() '重启子程序
'******************根据windows版本来关机************************
If glngWhichWindows32 = mlngWindowsNT Then
AdjustToken() '调用取得优先权子程序
End If

ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, &HFFFFs)
'*****************************************************************
End Sub