Ver Mensaje Individual
  #4 (permalink)  
Antiguo 21/12/2009, 23:13
Avatar de pkj
pkj
 
Fecha de Ingreso: julio-2006
Ubicación: Órbita sincrónica
Mensajes: 899
Antigüedad: 17 años, 10 meses
Puntos: 29
Respuesta: Reiniciar o apagar pc forzadamente

Aquí os dejo un mini-programa de reinicio y apagado que es lo más potente que he encontrado para VB. Si esto no apaga el PC en un rato, mejor que uses el botón.

Adquiere prioridad máxima al iniciarse, permite forzar el apagado y adquiere privilegios de apagado para un cierre más seguro.

Para simplificar lo he hecho sin botones ni nada. Vá por línea de comandos. Es lo más rápido para posibles bloqueos.

Lo mejor es poner el ejecutable en la carpeta de Windows y para usarlo abrir el menú de inicio/ejecutar y teclear ahí la orden. P.ej.: Apaga R

También puedes crear un acceso directo en el escritorio u otro sitio y modificar las propiedades para que esté listo para un reinicio forzado, por si hace falta.

Al ejecutarlo sin parámetros muestra la ayuda, para evirar reinicios no deseados y por si olvido los parámetros.

Código vb:
Ver original
  1. Option Explicit
  2.  
  3. Const EWX_SHUTDOWN As Long = 1
  4. Const EWX_REBOOT = 2
  5. Const EWX_FORCE As Long = 4
  6. Const EWX_POWEROFF As Long = 8
  7.  
  8. Private Type LUID
  9.   UsedPart As Long
  10.   IgnoredForNowHigh32BitPart As Long
  11. End Type
  12. Private Type TOKEN_PRIVILEGES
  13.   PrivilegeCount As Long
  14.   TheLuid As LUID
  15.   Attributes As Long
  16. End Type
  17.  
  18. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  19. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
  20.   ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  21. Private Declare Function LookupPrivilegeValue Lib "advapi32" _
  22.   Alias "LookupPrivilegeValueA" _
  23.   (ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
  24.   As LUID) As Long
  25. Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
  26.   (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
  27.   NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
  28.   PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
  29. Private Declare Function ExitWindowsEx Lib "user32" (ByVal _
  30.   dwOptions As Long, ByVal dwReserved As Long) As Long
  31.  
  32. Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityclass As Long) As Long
  33.  
  34. Sub ConsiguePrivilegios()
  35.   Const EWX_SHUTDOWN As Long = 1
  36.   Const EWX_FORCE As Long = 4
  37.   Const EWX_POWEROFF As Long = 8
  38.   Const TOKEN_ADJUST_PRIVILEGES = &H20
  39.   Const TOKEN_QUERY = &H8
  40.   Const SE_PRIVILEGE_ENABLED = &H2
  41.   Dim hdlProcessHandle As Long
  42.   Dim hdlTokenHandle As Long
  43.   Dim tmpLuid As LUID
  44.   Dim tkp As TOKEN_PRIVILEGES
  45.   Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  46.   Dim lBufferNeeded As Long
  47.   hdlProcessHandle = GetCurrentProcess()
  48.   OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  49.   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  50.   tkp.PrivilegeCount = 1
  51.   tkp.TheLuid = tmpLuid
  52.   tkp.Attributes = SE_PRIVILEGE_ENABLED
  53.   AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  54. End Sub
  55.  
  56. Sub Reiniciar(Forzar As Boolean)
  57.   If Forzar = True Then
  58.     ExitWindowsEx EWX_SHUTDOWN + EWX_REBOOT + EWX_FORCE, 0
  59.   Else
  60.     ExitWindowsEx EWX_SHUTDOWN + EWX_REBOOT, 0
  61.   End If
  62. End Sub
  63.  
  64. Sub Apagar(Forzar As Boolean)
  65.   If Forzar = True Then
  66.     ExitWindowsEx EWX_SHUTDOWN + EWX_POWEROFF + EWX_FORCE, 0
  67.   Else
  68.     ExitWindowsEx EWX_SHUTDOWN + EWX_POWEROFF, 0
  69.   End If
  70. End Sub
  71.  
  72. Private Sub Form_Initialize()
  73.   Const REALTIME_PRIORITY_CLASS = &H100
  74.   Dim sCmd As String
  75.   Dim ForzarCierre As Boolean
  76.   SetPriorityClass GetCurrentProcess, REALTIME_PRIORITY_CLASS
  77.   ConsiguePrivilegios
  78.   sCmd = UCase$(Trim$(Command))
  79.   If InStr(1, sCmd, "F") Then ForzarCierre = True
  80.   If InStr(1, sCmd, "A") Then
  81.     Apagar ForzarCierre
  82.   ElseIf InStr(1, sCmd, "R") Then
  83.     Reiniciar ForzarCierre
  84.   Else
  85.     MsgBox _
  86.     "      Apagar el equipo." & vbCrLf & _
  87.     App.EXEName & " A" & vbCrLf & _
  88.     "      Apagar el equipo y forzar el cierre de los programas." & vbCrLf & _
  89.     App.EXEName & " A F" & vbCrLf & _
  90.     "      Reiniciar el equipo." & vbCrLf & _
  91.     App.EXEName & " R" & vbCrLf & _
  92.     "      Reiniciar el equipo y forzar el cierre de los programas." & vbCrLf & _
  93.     App.EXEName & " R F"
  94.   End If
  95.   End
  96. End Sub

Por supuesto, también podeis modificarlo y que funcione como cada cual quiera que lo haga.

Espero que sea útil.

Saludos.
__________________
No hay preguntas tontas, solo gente estup..., ¡No!, ¿como era? No hay gente que pregunte a tontos... ¡Nooo!... ¡Vaya cabeza!