清泪伊人醉,是什么意思:VB.Net远程控制编程实例[注销关机重启]

来源:百度文库 编辑:偶看新闻 时间:2024/09/28 23:24:57
VB.Net远程控制编程实例[注销关机重启] 作者:IVA    来源:乐博网转化     更新时间:2007-12-16

原代码为C#编写,由L3'Studio团队开发;乐博网将其转化为VB.Net代码

功能描述:管理类(主要有关机重启等功能,API函数实现)

相关事件 :注销 关机 重启

命名空间:Lob.Windows

子类:WindowsManager

转化后代码如下:
    
Imports System
Imports System.Runtime.InteropServices
Namespace Lob.Windows

    Public Class WindowsManager

        Friend Const SE_PRIVILEGE_ENABLED As Integer = 2

        Friend Const TOKEN_QUERY As Integer = 8

        Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = 32

        Friend Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"

        Friend Const EWX_LOGOFF As Integer = 0

        Friend Const EWX_SHUTDOWN As Integer = 1

        Friend Const EWX_REBOOT As Integer = 2

        Friend Const EWX_FORCE As Integer = 4

        Friend Const EWX_POWEROFF As Integer = 8

        Friend Const EWX_FORCEIFHUNG As Integer = 16

        Friend Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr

        Friend Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean

        Friend Declare Function LookupPrivilegeValue Lib "advapi32.dll" (ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean

        Friend Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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

        Friend Declare Function ExitWindowsEx Lib "user32.dll" (ByVal flg As Integer, ByVal rea As Integer) As Boolean

        Private Shared Sub DoExitWin(ByVal flg As Integer)
            Dim ok As Boolean
            Dim tp As TokPriv1Luid
            Dim hproc As IntPtr = GetCurrentProcess
            Dim htok As IntPtr = IntPtr.Zero
            ok = OpenProcessToken(hproc, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), htok)
            tp.Count = 1
            tp.Luid = 0
            tp.Attr = SE_PRIVILEGE_ENABLED
            ok = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
            ok = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
            ok = ExitWindowsEx(flg, 0)
        End Sub

        Public Shared Sub ShutDown()
            DoExitWin(EWX_SHUTDOWN)
        End Sub

        Public Shared Sub Reboot()
            DoExitWin(EWX_REBOOT)
        End Sub

        _
        Structure TokPriv1Luid

            Public Count As Integer

            Public Luid As Long

            Public Attr As Integer
        End Structure
    End Class
End Namespace