⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 Visual.Basic.NET实用编程百例-47.6M.zip
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "控制系统"
   ClientHeight    =   2190
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   2940
   LinkTopic       =   "Form1"
   ScaleHeight     =   2190
   ScaleWidth      =   2940
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdShutdown 
      Caption         =   "关机"
      Height          =   375
      Left            =   720
      TabIndex        =   2
      Top             =   1440
      Width           =   1455
   End
   Begin VB.CommandButton cmdReboot 
      Caption         =   "重新启动"
      Height          =   375
      Left            =   720
      TabIndex        =   1
      Top             =   960
      Width           =   1455
   End
   Begin VB.CommandButton cmdLogoff 
      Caption         =   "注销"
      Height          =   375
      Left            =   720
      TabIndex        =   0
      Top             =   480
      Width           =   1455
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Const EWX_LogOff As Long = 0
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8

Private Declare Function ExitWindowsEx Lib "user32" _
                (ByVal dwOptions As Long, _
                ByVal dwReserved As Long) As Long

Private Declare Function GetLastError Lib "kernel32" () As Long

Private Type LUID
   UsedPart As Long
   IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
   TheLuid As LUID
   Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
   PrivilegeCount As Long
   TheLuid As LUID
   Attributes As Long
End Type


Private Declare Function GetCurrentProcess Lib "kernel32" () As Long


Private Declare Function OpenProcessToken Lib "advapi32" _
   (ByVal ProcessHandle As Long, _
    ByVal DesiredAccess As Long, _
    TokenHandle As Long) As Long


Private Declare Function LookupPrivilegeValue Lib "advapi32" _
   Alias "LookupPrivilegeValueA" _
   (ByVal lpSystemName As String, _
    ByVal lpName As String, _
    lpLuid As LUID) As Long


Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
   (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 Sub SetLastError Lib "kernel32" _
   (ByVal dwErrCode As Long)

Private Const mlngWindows95 = 0
Private Const mlngWindowsNT = 1

Public glngWhichWindows32 As Long


Private Declare Function GetVersion Lib "kernel32" () As Long

Private Sub AdjustToken()

   Const TOKEN_ADJUST_PRIVILEGES = &H20
   Const TOKEN_QUERY = &H8
   Const SE_PRIVILEGE_ENABLED = &H2

   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


   SetLastError 0

  
   hdlProcessHandle = GetCurrentProcess()
   OpenProcessToken hdlProcessHandle, _
            (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _
            hdlTokenHandle

  
   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

   tkp.PrivilegeCount = 1
   tkp.TheLuid = tmpLuid
   tkp.Attributes = SE_PRIVILEGE_ENABLED

  
   AdjustTokenPrivileges hdlTokenHandle, _
            False, _
            tkp, _
            Len(tkpNewButIgnored), _
            tkpNewButIgnored, _
            lBufferNeeded
End Sub

Private Sub cmdLogoff_Click()
   If glngWhichWindows32 = mlngWindowsNT Then
        AdjustToken
   End If
   
   ExitWindowsEx EWX_LogOff, 0
End Sub

Private Sub cmdShutdown_Click()
   If glngWhichWindows32 = mlngWindowsNT Then
        AdjustToken
   End If

   ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_POWEROFF), 0
End Sub

Private Sub cmdReboot_Click()
   If glngWhichWindows32 = mlngWindowsNT Then
        AdjustToken
   End If

   ExitWindowsEx EWX_REBOOT, 0
End Sub

Private Sub Form_Load()
    Dim lngVersion As Long

    lngVersion = GetVersion()

    If ((lngVersion And &H80000000) = 0) Then
        glngWhichWindows32 = mlngWindowsNT
    Else
        glngWhichWindows32 = mlngWindows95
    End If
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -