用VB语言如何设置一个简单的关机程序(2006-10-29 19:25:10)
如果系统是XP的话,你可以用这个语句:
shell "shutdown -r -t " % '%26 "-c " %
<就是你要设置的关机时间,单位为秒,是你的关机提示.不想关机的话,就运行"shutdown -a" 就行了.
如果你倒计时间是固定的话(比如30秒),可以用以下的:
shell "shutdown -r -t 30"
如果说别的系统的话,这里有一个模块,你自己调用吧
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
'报告API错误:
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = %26H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = %26H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = %26H800
Private Const FORMAT_MESSAGE_FROM_STRING = %26H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = %26H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = %26H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = %26HFF
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 "LookupPrivilegeVa
lueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED = %26H2
Private Const READ_CONTROL = %26H20000
Private Const STANDARD_RIGHTS_ALL = %26H1F0000
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_REQUIRED = %26HF0000
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const TOKEN_ASSIGN_PRIMARY = %26H1
Private Const TOKEN_DUPLICATE = (%26H2)
Private Const TOKEN_IMPERSONATE = (%26H4)
Private Const TOKEN_QUERY = (%26H8)
Private Const TOKEN_QUERY_SOURCE = (%26H10)
Private Const TOKEN_ADJUST_PRIVILEGES = (%26H20)
Private Const TOKEN_ADJUST_GROUPS = (%26H40)
Private Const TOKEN_ADJUST_DEFAULT = (%26H80)
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
Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (
ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
Private Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long
' ================================================================
Public Function WinError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long
'返回与LastDLLError关联的错误消息:
sBuff = String$(256, 0)
lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0%26, 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
'返回系统
是否为NT:
If Not (bOnce) Then
Dim tVI As OSVERSIONINFO
tVI.dwOSVersionInfoSize = Len(tVI)
If (GetVersionEx(tVI) %26lt; 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
'在NT下,我们必须给试图关闭系统的进程SE_SHUTDOWN_NAME特权
'否则,所有企图关闭系统的调用都会无效!
'寻Shoudown特权令牌的LUID:
lR = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tLUID)
'如果我们到了
If (lR %26lt; 0) Then
'取得当前进程的句柄:
hProcess = GetCurrentProcess()
If (hProcess %26lt; 0) Then
'打开令牌来Adjust和Query(用户可能没有权限)
lR = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
If (lR %26lt; 0) Then
'好,我们现在可以调整Shutdown特权了:
With tTP
.PrivilegeCount = 1
With .Privileges(0)
.Attributes = SE_PRIVILEGE_ENABLED
.
pLuid.HighPart = tLUID.HighPart
.pLuid.LowPart = tLUID.LowPart
End With
End With
'现在允许这个进程关闭系统:
lR = AdjustTokenPrivileges(hToken, 0, tTP, Len(tTP), tTPOld, lTpOld)
If (lR %26lt; 0) Then
NTEnableShutDown = True
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName %26 ".mShutDown", "不能shutdown:你没有关闭本系统的权限。[" %26 WinError(Err.LastDllError) %26 "]"
End If
'记得用完后关闭这个句柄:
CloseHandle hToken
Else
Err.Raise eeSSDErrorBase + 6, App.EXEName %26 ".mShutDown", "不能shutdown:你没有关闭本系统的权限。[" %26 WinError(Err.LastDllError) %26 "]"
End If
Else
Err.Raise eeSSDErrorBase + 5, App.EXEName %26 ".mShutDown", "不能shutdown:不能终止当前进程。[" %26 WinError(Err.LastDllError) %26 "]"
End If
Else
Err.Raise eeSSDErrorBase + 4, App.EXEName %26 ".mShutDown", "不能shutdown:不到SE_SHU
TDOWN_NAME特权值。[" %26 WinError(Err.LastDllError) %26 "]"
End If
End Function
Public Function NTForceTimedShutdown( _
Optional ByVal lTimeOut As Long = -1, _
Optional ByVal sMsg As String = "", _
Optional ByVal sMachineNetworkName As String = vbNullString, _
Optional ByVal bForceAppsToClose As Boolean = False, _
Optional ByVal bReboot As Boolean = False _
) As Boolean
Dim lR As Long
If IsNT Then
'如果我们在NT下,确信我们已经给了这个进程关闭系统的特权:
If Not (NTEnableShutDown(sMsg)) Then
Exit Function
End If
'这是定时关闭系统的代码:
lR = InitiateSystemShutdown(sMachineNetworkName, sMsg, lTimeOut, bForceAppsToClose, bReboot)
If (lR = 0) Then
Err.Raise eeSSDErrorBase + 2, App.EXEName %26 ".mShutDown", "InitiateSystemShutdown failed: " %26 WinError(Err.LastDllError)
End If
Else
Err.Raise eeSSDErrorBase + 1, App.EXEName %26 ".mShutDown", "函数仅在Windows NT下有效。"
End If
End Function
Public Function NTAbortTimedShutdown(Optional ByVal sMachineNetworkName As String = vbNullString)
AbortSystemShutdown sMachineNetworkName
End Function
用vb关闭与打开显示器
在窗口中放置两个按扭名为command1和command2,将下面的代码放到程序中
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion(1 To 128) As Byte
End Type
Private Declare Function SendScreenMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const MONITOR_ON = -1&
Private Const MONITOR_LOWPOWER = 1&
Private Const MONITOR_OFF = 2&
Private Const SC_MONITORPOWER = &HF170&
Private Const WM_SYSCOMMAND = &H112
Private Sub Command1_Click()
If IsNT Then
Call SendScreenMessage(Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal MONITOR_LOWPOWER)
Else
Call SendScreenMessage(Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 0)
End If
End Sub
Private Sub Command2_Click()
Call SendScreenMessage(Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal MONITOR_ON)
End Sub
Public Function IsNT() As Boolean  '判断系统
Dim OSVer As OSVERSIONINFO
OSVer.dwOSVersionInfoSize = LenB(OSVer)
GetVersionEx OSVer
'IsNT = OSVer.dwPlatformId = VER_PLATFORM_WIN32_NT
If OSVer.dwMajorVersion < 5 Then
IsNT = False
ElseIf OSVer.dwMajorVersion >= 5 Then
IsNT = True
End If
End Function

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。