转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.NET中实现关机/休眠/重启/注销的类         ★★★★

VB.NET中实现关机/休眠/重启/注销的类

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2181 更新时间:2009/4/23 19:01:49

Imports System
Imports System.Text
Imports System.Diagnostics
Imports System.Runtime.InteropServices

    Public Class WindowsController
        Public Enum RestartOptions
            LogOff = 0
            PowerOff = 8
            Reboot = 2
            ShutDown = 1
            Suspend = -1
            Hibernate = -2
        End Enum

        Public Structure LUID
            Dim LowPart As Integer
            Dim HighPart As Integer
        End Structure

        Public Structure LUID_AND_ATTRIBUTES

            Dim pLuid As LUID

            Dim Attributes As Integer
        End Structure


        Public Structure TOKEN_PRIVILEGES

            Dim PrivilegeCount As Integer

            Dim Privileges As LUID_AND_ATTRIBUTES
        End Structure

        Private Const TOKEN_ADJUST_PRIVILEGES = &H20
        Private Const TOKEN_QUERY = &H8
        Private Const SE_PRIVILEGE_ENABLED = &H2
        Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
        Private Const EWX_FORCE = 4
        Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As IntPtr
        Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As IntPtr) As Integer
        Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
        Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
        Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
        Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
        Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
        Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
        Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

        Public Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
            Select Case how
                Case RestartOptions.Suspend
                    SuspendSystem(False, force)
                Case RestartOptions.Hibernate
                    SuspendSystem(True, force)
                Case Else
                    ExitWindows(Convert.ToInt32(how), force)
            End Select
        End Sub

        Protected Sub ExitWindows(ByVal how As Integer, ByVal force As Boolean)
            EnableToken("SeShutdownPrivilege")
            If force Then how = how Or EWX_FORCE
            If (ExitWindowsEx(how, 0) = 0) Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
        End Sub

        Protected Sub EnableToken(ByVal privilege As String)
            If Not CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges") Then Return
            Dim tokenHandle As IntPtr = IntPtr.Zero
            Dim privilegeLUID = New LUID()
            Dim newPrivileges = New TOKEN_PRIVILEGES()
            Dim tokenPrivileges As TOKEN_PRIVILEGES
            If (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
            If (LookupPrivilegeValue("", privilege, privilegeLUID)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
            tokenPrivileges.PrivilegeCount = 1
            tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
            tokenPrivileges.Privileges.pLuid = privilegeLUID
            Dim Size As Integer = 4
            If (AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
        End Sub

        Protected Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean)
            If Not CheckEntryPoint("powrprof.dll", "SetSuspendState") Then Throw New PlatformNotSupportedException("The SetSuspendState method is not supported on this system!")
            SetSuspendState(Convert.ToInt32(IIf(hibernate, 1, 0)), Convert.ToInt32(IIf(force, 1, 0)), 0)
        End Sub

        Protected Function CheckEntryPoint(ByVal library As String, ByVal method As String) As Boolean
            Dim libPtr As IntPtr = LoadLibrary(library)
            If Not libPtr.Equals(IntPtr.Zero) Then
                If Not GetProcAddress(libPtr, method).Equals(IntPtr.Zero) Then
                    FreeLibrary(libPtr)
                    Return True
                End If
                FreeLibrary(libPtr)
            End If
            Return False
        End Function

        Protected Function FormatError(ByVal number As Integer) As String
            Dim Buffer = New StringBuilder(255)
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, Buffer, Buffer.Capacity, 0)
  

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台