熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用VB.NET 2005編寫定時關機程序[2]

2022-06-13   來源: .NET編程 

  雙擊窗體進入常規聲明Public Class Form 事件中

  CODE:

Imports SystemRuntimeInteropServices
Imports MicrosoftVisualBasic
Public Class Form
<DllImport(kerneldll ExactSpelling:=True)> _調用系統參數
Friend Shared Function GetCurrentProcess() As IntPtr
End Function

<DllImport(advapidll ExactSpelling:=True SetLastError:=True)> _
Friend Shared Function OpenProcessToken(ByVal h As IntPtr ByVal acc As Integer ByRef phtok As IntPtr) As Boolean
End Function

<DllImport(advapidll SetLastError:=True)> _
Friend Shared Function LookupPrivilegeValue(ByVal host As String ByVal name As String ByRef pluid As Long) As Boolean
End Function

<DllImport(advapidll ExactSpelling:=True SetLastError:=True)> _
Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr ByVal disall As Boolean ByRef newst As TokPrivLuid ByVal len As Integer ByVal prev As IntPtr ByVal relen As IntPtr) As Boolean
End Function

<DllImport(userdll ExactSpelling:=True SetLastError:=True)> _
Friend Shared Function ExitWindowsEx(ByVal flg As Integer ByVal rea As Integer) As Boolean
End Function

Friend Const SE_PRIVILEGE_ENABLED As Integer = &H
Friend Const TOKEN_QUERY As Integer = &H
Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H
Friend Const SE_SHUTDOWN_NAME As String = SeShutdownPrivilege
Friend Const EWX_LOGOFF As Integer = &H 注銷計算機
Friend Const EWX_SHUTDOWN As Integer = &H關閉計算機
Friend Const EWX_REBOOT As Integer = &H重新啟動計算機
Friend Const EWX_FORCE As Integer = &H關閉所有進程注銷計算機
Friend Const EWX_POWEROFF As Integer = &H
Friend Const EWX_FORCEIFHUNG As Integer = &H

<StructLayout(LayoutKindSequential Pack:=)> _
  引用參數
 Friend Structure TokPrivLuid
 Public Count As Integer
 Public Luid As Long
 Public Attr As Integer
End Structure

Private Shared Sub DoExitWin(ByVal flg As Integer)
 Dim xc As Boolean 判斷語句
 Dim tp As TokPrivLuid
 Dim hproc As IntPtr = GetCurrentProcess()
 調用進程值
 Dim htok As IntPtr = IntPtrZero
 xc = OpenProcessToken(hproc TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY htok)
 tpCount =
 tpLuid =
 tpAttr = SE_PRIVILEGE_ENABLED
 xc = LookupPrivilegeValue(Nothing SE_SHUTDOWN_NAME tpLuid)
 xc = AdjustTokenPrivileges(htok False tp IntPtrZero IntPtrZero)
 xc = ExitWindowsEx(flg )
End Sub

Public Shared Sub Reboot()
 DoExitWin((EWX_FORCE Or EWX_REBOOT)) 重新啟動計算機
End Sub

Public Shared Sub PowerOff()
 DoExitWin((EWX_FORCE Or EWX_POWEROFF)) 關閉計算機
End Sub

Public Shared Sub LogoOff()
 DoExitWin((EWX_FORCE Or EWX_LOGOFF)) 注銷計算機
End Sub

Dim entTime As Object 保存輸入時間
Dim xianzaiTime As Object 保存實時時間
Dim startTime As Object 保存開始定時時間

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15695.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.