你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
用VB.NET 2005编写定时关机程序(2)
 
图2

  以上界面工作基本完成现在需要输入代码了

  双击窗体进入常规-声明Public Class Form1 事件中

  CODE:

Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Public Class Form1
<DllImport("kernel32.dll", ExactSpelling:=True)> _'调用系统参数
Friend Shared Function GetCurrentProcess() As IntPtr
End Function
<DllImport("advapi32.dll", 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("advapi32.dll", SetLastError:=True)> _
Friend Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
End Function
<DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
End Function
<DllImport("user32.dll", 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 = &H2
Friend Const TOKEN_QUERY As Integer = &H8
Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Friend Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Friend Const EWX_LOGOFF As Integer = &H0 '注销计算机
Friend Const EWX_SHUTDOWN As Integer = &H1'关闭计算机
Friend Const EWX_REBOOT As Integer = &H2'重新启动计算机
Friend Const EWX_FORCE As Integer = &H4'关闭所有进程,注销计算机
Friend Const EWX_POWEROFF As Integer = &H8
Friend Const EWX_FORCEIFHUNG As Integer = &H10
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
  '引用参数
 Friend Structure TokPriv1Luid
 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 TokPriv1Luid
 Dim hproc As IntPtr = GetCurrentProcess()
 '调用进程值
 Dim htok As IntPtr = IntPtr.Zero
 xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
 tp.Count = 1
 tp.Luid = 0
 tp.Attr = SE_PRIVILEGE_ENABLED
 xc = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
 xc = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
 xc = ExitWindowsEx(flg, 0)
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 '保存开始定时时间

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089