Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Reiniciar o apagar pc forzadamente

Estas en el tema de Reiniciar o apagar pc forzadamente en el foro de Visual Basic clásico en Foros del Web. Bueno, en esta ocacion lo que el la forma para apagar o reiniciar de manera forzada la pc. El siguiente codigo hace lo que la ...
  #1 (permalink)  
Antiguo 19/12/2009, 09:49
 
Fecha de Ingreso: diciembre-2009
Mensajes: 1
Antigüedad: 14 años, 4 meses
Puntos: 0
Pregunta Reiniciar o apagar pc forzadamente

Bueno, en esta ocacion lo que el la forma para apagar o reiniciar de manera forzada la pc.
El siguiente codigo hace lo que la mayoria piensa que yo quiero:

Private Sub Command1_Click()
Shell "shutdown -s -f -t 00"
End Sub

Private Sub Command2_Click()
Shell "shutdown -r -f -t 00"
End Sub

Estas son ordenes de apagado y reinicio normal, donde te cierran los programas, luego la sesión, guarda la configuración y luego se apaga.

Lo que quiero es un código que me apague como si se desconectara el cable de la corriente, y u código que reinicie como si presionara el botón de reiniciar, que se te reinicia y punto.

Alguna idea? Eso es todo.
  #2 (permalink)  
Antiguo 20/12/2009, 16:44
Avatar de tepitenio  
Fecha de Ingreso: noviembre-2008
Mensajes: 1.188
Antigüedad: 15 años, 5 meses
Puntos: 88
Respuesta: Reiniciar o apagar pc forzadamente

No se, pero tengo curiosidad: Por qué queres eso??? Podes arruinar la PC!!??
__________________
Tepi
(Si te gusto mi opinion... por que no me das karma???)
  #3 (permalink)  
Antiguo 21/12/2009, 15:57
Avatar de tepitenio  
Fecha de Ingreso: noviembre-2008
Mensajes: 1.188
Antigüedad: 15 años, 5 meses
Puntos: 88
Respuesta: Reiniciar o apagar pc forzadamente

Se me ocurre que lo que quieres es que no se detenga a salvar archivos. Pues el siguiente codigo puede ayudarte:

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const EWX_FORCE = 4
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2
Private Const EWX_SHUTDOWN = 1

Private Sub cmdShutDown()
ExitWindowsEx EWX_SHUTDOWN, 0
End Sub


Creo que para forzar el cierre CAIGA QUIEN CAIGA, debes usar el FORCE como parametro.
__________________
Tepi
(Si te gusto mi opinion... por que no me das karma???)
  #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, 9 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!
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:19.