如何编写一个定时关机的程序?

解决方案 »

  1.   

    '模块定义
    Enum HowExitConst
        EWX_FORCE = 4 ' 强制关机
        EWX_LOGOFF = 0 ' 登出
        EWX_REBOOT = 2 ' 重开机
        EWX_SHUTDOWN = 1 ' 关机
        EWX_POWEROFF = 8
    End EnumConst TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
    Const ANYSIZE_ARRAY = 1Private Type LUID
        lowpart As Long
        highpart As Long
    End TypePrivate Type LUID_AND_ATTRIBUTES
        pLuid As LUID
        Attributes As Long
    End TypePrivate Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    End TypePublic Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () 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 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 OpenProcessToken Lib "advapi32.dll" _
        (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
        TokenHandle As Long) As LongPublic Sub AdjustToken()
        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
        'Get the LUID for shutdown privilege.
        LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
        tkp.PrivilegeCount = 1 ' One privilege to set
        tkp.Privileges(0).pLuid = tmpLuid
        tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
        'Enable the shutdown privilege in the access token of this process.
        AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), _
        tkpNewButIgnored, lBufferNeeded
    End Sub'窗体代码
    '放一个定时器
    Private Sub Timer1_Timer()
        If Hour(Now) = i Then
            'win2000
            AdjustToken
            ExitWindowsEx EWX_POWEROFF, 0
            'win98
            ExitWindowsEx EWX_SHUTDOWN, 0
        End If
    End Sub
      

  2.   

    if 太晚了then  '关机
    end if