by Jawahar (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 4th May 2005
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
System Drive and Shuting Down
Dim bWindowsNT As Boolean
'
' Operating System Constants, Types and Declares
'
Const VER_PLATFORM_WIN32s = 0
Const VER_PLATFORM_WIN32_WINDOWS = 1
Const VER_PLATFORM_WIN32_NT = 2
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
'
' Shutdown/, Types and Declares
'
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
'
' Using this option to shutdown windows does not send
' the WM_QUERYENDSESSION and WM_ENDSESSION messages to
' the open applications. Thus, those apps may loose
' any unsaved data.
'
Const EWX_FORCE = 4
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_BITSPERPEL = &H40000
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const CDS_UPDATEREGISTRY = &H1
Const CDS_TEST = &H2
Const CDS_FULLSCREEN = &H4
Const DISP_CHANGE_SUCCESSFUL = 0
Const DISP_CHANGE_RESTART = 1
Const HWND_BROADCAST = &HFFFF&
Const WM_DISPLAYCHANGE = &H7E&
Const SPI_SETNONCLIENTMETRICS = 42
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'
' The following are required to shutdown NT.
'
Const ERROR_NOT_ALL_ASSIGNED = 1300
Const SE_PRIVILEGE_ENABLED = 2
Const TOKEN_QUERY = &H8
Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Type LUID
lowpart As Long
highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges As LUID_AND_ATTRIBUTES
End Type
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPriv As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpUid As LUID) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub cmdReboot_Click()
Dim tLuid As LUID
Dim tTokenPriv As TOKEN_PRIVILEGES
Dim tPrevTokenPriv As TOKEN_PRIVILEGES
Dim lResult As Long
Dim lToken As Long
Dim lLenBuffer As Long
Dim lMode As Long
'
' Determine the shutdown mode.
'
' EWX_LOGOFF
' Shuts down all processes running and
' logs off the user.
'
' EWX_REBOOT
' Shuts down and restarts the system.
'
' EWX_SHUTDOWN
' Shuts down the system to a point where
' it is safe to turn off the system.
'
' EWX_POWEROFF
' Shuts down the system and turns off power.
' The system must support this feature.
'
' EWX_FORCE
' Forcibly shuts down the system. Files are not closed,...
' data may be lost.
'
If optShut(0) Then
lMode = EWX_LOGOFF
ElseIf optShut(1) Then
lMode = EWX_REBOOT
ElseIf optShut(2) Then
lMode = EWX_SHUTDOWN
Else: lMode = EWX_FORCE
End If
If Not bWindowsNT Then
Call ExitWindowsEx(lMode, 0)
Else
'
' Get the access token of the current process. Get it
' with the privileges of querying the access token and
' adjusting its privileges.
'
lResult = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lToken)
If lResult = 0 Then
Exit Sub 'Failed
End If
'
' Get the locally unique identifier (LUID) which
' represents the shutdown privilege.
'
lResult = LookupPrivilegeValue(0&, "SeShutdownPrivilege", tLuid)
If lResult = 0 Then Exit Sub 'Failed
'
' Populate the new TOKEN_PRIVILEGES values with the LUID
' and allow your current process to shutdown the computer.
'
With tTokenPriv
.PrivilegeCount = 1
.Privileges.Attributes = SE_PRIVILEGE_ENABLED
.Privileges.pLuid = tLuid
lResult = AdjustTokenPrivileges(lToken, False, tTokenPriv, Len(tPrevTokenPriv), tPrevTokenPriv, lLenBuffer)
End With
If lResult = 0 Then
Exit Sub 'Failed
Else
If Err.LastDllError = ERROR_NOT_ALL_ASSIGNED Then Exit Sub 'Failed
End If
'
' Shutdown Windows.
'
Call ExitWindowsEx(lMode, 0)
End If
End Sub
Private Sub Form_Load()
Dim OSInfo As OSVERSIONINFO
'
' See if we are running Windows 9x or NT.
'
OSInfo.dwOSVersionInfoSize = Len(OSInfo)
Call GetVersionEx(OSInfo)
bWindowsNT = (OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Sub
Private Sub dirdriectory_change()
filfile.Path = dirdriectory
End Sub
Private Sub drvdrive_change()
dirdriectory.Path = drvdrive.Drive
End Sub
Private Sub filfile_click()
txtpath.Text = filfile.FileName
End Sub