Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Spegnieto automatico - ShutDown.bas

ShutDown.bas

Caricato da: Antometal
Scarica il programma completo

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. '***************
  4. '*Shutdown part*
  5. '***************
  6. Public Const EWX_LOGOFF = 0       'fa il LOG-OFF dell'utente
  7. Public Const EWX_SHUTDOWN = 1     'spenge il PC non completamente (con la schermata "Ora è possibile spegnere il computer")
  8. Public Const EWX_REBOOT = 2       'riavvia il PC
  9. Public Const EWX_FORCE = 4        'forza lo spengimento (può causare perdita di dati)
  10. Public Const EWX_POWEROFF = 8     'spenge completamente il PC (se la scheda madre lo permette)
  11. 'The ExitWindowsEx function either logs off, shutsdown, or shutsdown and restarts the system.
  12. Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  13. 'The GetLastError function returns the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
  14. 'Multiple threads do not overwrite each other's last-error code.
  15. Public GetLasrError As Long
  16. Public Declare Function GetLastError Lib "kernel32" () As Long
  17. 'OS constants
  18. Public Const mlngWindows95 = 0
  19. Public Const mlngWindowsNT = 1
  20. Public glngWhichWindows32 As Long
  21. 'The GetVersion function returns the operating system in use.
  22. Public Declare Function GetVersion Lib "kernel32" () As Long
  23. Public Type LUID
  24.   UsedPart As Long
  25.   IgnoredForNowHigh32BitPart As Long
  26. End Type
  27. Public Type LUID_AND_ATTRIBUTES
  28.   TheLuid As LUID
  29.   Attributes As Long
  30. End Type
  31. Public Type TOKEN_PRIVILEGES
  32.   PrivilegeCount As Long
  33.   TheLuid As LUID
  34.   Attributes As Long
  35. End Type
  36. 'The GetCurrentProcess function returns a pseudohandle for the current process.
  37. Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
  38. 'The OpenProcessToken function opens the access token associated with a process.
  39. Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  40. 'The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
  41. Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  42. 'The AdjustTokenPrivileges function enables or disables privileges in the specified access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access.
  43. Public 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
  44. Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
  45.  
  46. '***************************
  47. '*Internet connections part*
  48. '***************************
  49. Public Const RAS_MaxEntryName As Integer = 256
  50. Public Const RAS_MaxDeviceType As Integer = 16
  51. Public Const RAS_MaxDeviceName As Integer = 128
  52. Public Const RAS_RASCONNSIZE As Integer = 412
  53. Public Const ERROR_SUCCESS = 0&
  54. Public Type RASCONN
  55.     dwSize As Long
  56.     hRasConn As Long
  57.     szEntryName(RAS_MaxEntryName) As Byte
  58.     szDeviceType(RAS_MaxDeviceType) As Byte
  59.     szDeviceName(RAS_MaxDeviceName) As Byte
  60. End Type
  61. Public Type RASENTRYNAME
  62.     dwSize As Long
  63.     szEntryName(RAS_MaxEntryName) As Byte
  64. End Type
  65. 'Enumerate the active connections
  66. Public Declare Function RasEnumConnections Lib "RasApi32.DLL" Alias "RasEnumConnectionsA" (lprasconn As Any, lpcb As Long, lpcConnections As Long) As Long
  67. 'HangUp the modem
  68. Public Declare Function RasHangUp Lib "RasApi32.DLL" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long
  69. 'icon tray
  70. Public nid As NOTIFYICONDATA
  71. Public Type NOTIFYICONDATA
  72.     cbSize As Long
  73.     hwnd As Long
  74.     uId As Long
  75.     uFlags As Long
  76.     uCallBackMessage As Long
  77.     hIcon As Long
  78.     szTip As String * 64
  79. End Type
  80. Public Const NIM_ADD = &H0
  81. Public Const NIM_MODIFY = &H1
  82. Public Const NIM_DELETE = &H2
  83. Public Const WM_MOUSEMOVE = &H200
  84. Public Const NIF_MESSAGE = &H1
  85. Public Const NIF_ICON = &H2
  86. Public Const NIF_TIP = &H4
  87. Public Const WM_LBUTTONDBLCLK = &H203
  88. Public Const WM_LBUTTONDOWN = &H201
  89. Public Const WM_LBUTTONUP = &H202
  90. Public Const WM_RBUTTONDBLCLK = &H206
  91. Public Const WM_RBUTTONDOWN = &H204
  92. Public Const WM_RBUTTONUP = &H205
  93. Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
  94. '******************************************************
  95. '*This procedure sets the proper privileges to allow a*
  96. '*log off or a shutdown to occur under Windows NT.    *
  97. '******************************************************
  98. Public Sub AdjustToken()
  99.   Const TOKEN_ADJUST_PRIVILEGES = &H20
  100.   Const TOKEN_QUERY = &H8
  101.   Const SE_PRIVILEGE_ENABLED = &H2
  102.   Dim hdlProcessHandle, hdlTokenHandle, lBufferNeeded As Long
  103.   Dim tmpLuid As LUID
  104.   Dim tkp As TOKEN_PRIVILEGES
  105.   Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  106.   'Set the error code of the last thread to zero using the SetLast Error function. Do this so that the GetLastError function does not return a value other than zero for no apparent reason.
  107.   SetLastError 0
  108.   'Use the GetCurrentProcess function to set the hdlProcessHandle variable.
  109.   hdlProcessHandle = GetCurrentProcess()
  110.   If GetLastError <> 0 Then MsgBox "GetCurrentProcess error==" & GetLastError
  111.   OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  112.   If GetLastError <> 0 Then MsgBox "OpenProcessToken error==" & GetLastError
  113.   'Get the LUID for shutdown privilege.
  114.   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  115.   If GetLastError <> 0 Then MsgBox "LookupPrivilegeValue error==" & GetLastError
  116.   tkp.PrivilegeCount = 1    ' One privilege to set.
  117.   tkp.TheLuid = tmpLuid
  118.   tkp.Attributes = SE_PRIVILEGE_ENABLED
  119.   'Enable the shutdown privilege in the access token of this process.
  120.   AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  121.   If GetLastError <> 0 Then MsgBox "AdjustTokenPrivileges error==" & GetLastError
  122. End Sub
  123.  
  124. Public Sub Control_windows()
  125.   Dim lngVersion As Long
  126.   'tipo di OS
  127.   lngVersion = GetVersion()
  128.   If ((lngVersion And &H80000000) = 0) Then
  129.     'OS = Windows NT/2000/XP
  130.     glngWhichWindows32 = mlngWindowsNT
  131.   Else
  132.     'OS = Windows 9x
  133.     glngWhichWindows32 = mlngWindows95
  134.   End If
  135. End Sub
  136.  
  137. Public Sub Shutdown(ByVal tipo As String)
  138.   'Procedura che provoca lo spengimento del PC:
  139.   'se l'OS è Win NT/2000/XP allora prima di spengere setta i privilegi
  140.   If glngWhichWindows32 = mlngWindowsNT Then
  141.     'aggiusta i privilegi per poter spengere il PC
  142.     AdjustToken
  143.     'se AdjustToken va in errore allora visualizzo il tipo di errore
  144.     If GetLasrError <> 0 Then MsgBox "Post-AdjustToken's GetLastError " & GetLastError
  145.   End If
  146.   'spengo il PC: se la scheda madre lo permette il PC si spenge completamente,
  147.   'altrimenti apparira la schermata che è possibile spengere il computer (manualmente)
  148.   ExitWindowsEx tipo, &HFFFF
  149.   'se ExitWindowsEx va in errore allora visualizzo il tipo di errore
  150.   If GetLasrError <> 0 Then MsgBox "ExitWindowsEx's GetLastError " & GetLastError
  151. End Sub