Apagar el PC con un botón

(1/1)

Meta:
Hola:

Tengo un Form y un button1 con Visual C# Express 2010 Beta 2. ¿Existe la manera de que al pulsar el botón se apague el equipo completo?



A veces puede ocurrir de que te salga una ventana que te dice Finalizar tarea y el equipo no se apaga. Si es posible hacerlo lo de apagar el equipo con un botón. ¿Hay una forma de evitar la posible ventana de Finalizar tarea?

Salu2.

seba123neo:
Hola, en visual basic 6 hay una forma de forzar el apagado con la api AdjustTokenPrivileges, fijate si hay algo parecido en C#, debe haber seguro...

saludos.

raul338:
Cita de: seba123neo en 29 Diciembre 2009, 02:47 am

Hola, en visual basic 6 hay una forma de forzar el apagado con la api AdjustTokenPrivileges, fijate si hay algo parecido en C#, debe haber seguro...

saludos.


Exacto, tienes que usar la API, y apagar "a la fuerza", te paso parte de una aplicacion que hize recopilando codigos de internet (la aplicacion era para apagar la PC a determinada hora, con el shutdown.exe era dificil calcular en cuantos segundos queria q se apage :P)

Código
#region "- Delcaraciones API -"
<DllImport("user32.dll", SetLastError:=True)> _
   Function ExitWindowsEx( _
    ByVal uFlags As ExitWindows, _
    ByVal dwReason As ShutdownReason) As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function
 
   <Flags()> _
   Enum ExitWindows As UInteger
       LogOff = &H0
       ShutDown = &H1
       Reboot = &H2
       PowerOff = &H8
       RestartApps = &H40
       ' plus AT MOST ONE of the following two:
       Force = &H4
       ForceIfHung = &H10
   End Enum
 
   <Flags()> _
Enum ShutdownReason As UInteger
       MajorApplication = &H40000
       MajorHardware = &H10000
       MajorLegacyApi = &H70000
       MajorOperatingSystem = &H20000
       MajorOther = &H0
       MajorPower = &H60000
       MajorSoftware = &H30000
       MajorSystem = &H50000
 
       MinorBlueScreen = &HF
       MinorCordUnplugged = &HB
       MinorDisk = &H7
       MinorEnvironment = &HC
       MinorHardwareDriver = &HD
       MinorHotfix = &H11
       MinorHung = &H5
       MinorInstallation = &H2
       MinorMaintenance = &H1
       MinorMMC = &H19
       MinorNetworkConnectivity = &H14
       MinorNetworkCard = &H9
       MinorOther = &H0
       MinorOtherDriver = &HE
       MinorPowerSupply = &HA
       MinorProcessor = &H8
       MinorReconfig = &H4
       MinorSecurity = &H13
       MinorSecurityFix = &H12
       MinorSecurityFixUninstall = &H18
       MinorServicePack = &H10
       MinorServicePackUninstall = &H16
       MinorTermSrv = &H20
       MinorUnstable = &H6
       MinorUpgrade = &H3
       MinorWMI = &H15
 
       FlagUserDefined = &H40000000
       FlagPlanned = &H80000000&
   End Enum
 
   'This routine enables the Shutdown privilege for the current process,
   'which is necessary if you want to call ExitWindowsEx.
 
   Const ANYSIZE_ARRAY As Integer = 1
   Const TOKEN_QUERY As Integer = &H8
   Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
   Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
   Const SE_PRIVILEGE_ENABLED As Integer = &H2
 
   <StructLayout(LayoutKind.Sequential)> _
   Private Structure LUID
       Public LowPart As UInt32
       Public HighPart As UInt32
   End Structure
 
   <StructLayout(LayoutKind.Sequential)> _
   Private Structure LUID_AND_ATTRIBUTES
       Public Luid As LUID
       Public Attributes As UInt32
   End Structure
 
   <StructLayout(LayoutKind.Sequential)> _
   Private Structure TOKEN_PRIVILEGES
       Public PrivilegeCount As UInt32
       <MarshalAs(UnmanagedType.ByValArray, SizeConst:=ANYSIZE_ARRAY)> _
       Public Privileges() As LUID_AND_ATTRIBUTES
   End Structure
 
   <DllImport("advapi32.dll", SetLastError:=True)> _
   Private Function LookupPrivilegeValue( _
    ByVal lpSystemName As String, _
    ByVal lpName As String, _
    ByRef lpLuid As LUID _
     ) As Boolean
   End Function
 
   <DllImport("advapi32.dll", SetLastError:=True)> _
   Private Function OpenProcessToken( _
    ByVal ProcessHandle As IntPtr, _
    ByVal DesiredAccess As Integer, _
    ByRef TokenHandle As IntPtr _
     ) As Boolean
   End Function
 
   <DllImport("kernel32.dll", SetLastError:=True)> _
   Private Function CloseHandle(ByVal hHandle As IntPtr) As Boolean
   End Function
 
   <DllImport("advapi32.dll", SetLastError:=True)> _
   Private Function AdjustTokenPrivileges( _
      ByVal TokenHandle As IntPtr, _
      ByVal DisableAllPrivileges As Boolean, _
      ByRef NewState As TOKEN_PRIVILEGES, _
      ByVal BufferLength As Integer, _
      ByRef PreviousState As TOKEN_PRIVILEGES, _
      ByRef ReturnLength As IntPtr _
    ) As Boolean
   End Function
 
   Public Sub AcquireShutdownPrivilege()
 
       Dim lastWin32Error As Integer = 0
 
       'Get the LUID that corresponds to the Shutdown privilege, if it exists.
       Dim luid_Shutdown As LUID
       If Not LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, luid_Shutdown) Then
           lastWin32Error = Marshal.GetLastWin32Error()
           Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
            "LookupPrivilegeValue failed with error " & lastWin32Error.ToString & ".")
       End If
 
       'Get the current process's token.
       Dim hProc As IntPtr = Process.GetCurrentProcess().Handle
       Dim hToken As IntPtr
       If Not OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
           lastWin32Error = Marshal.GetLastWin32Error()
           Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
            "OpenProcessToken failed with error " & lastWin32Error.ToString & ".")
       End If
 
       Try
 
           'Set up a LUID_AND_ATTRIBUTES structure containing the Shutdown privilege, marked as enabled.
           Dim luaAttr As New LUID_AND_ATTRIBUTES
           luaAttr.Luid = luid_Shutdown
           luaAttr.Attributes = SE_PRIVILEGE_ENABLED
 
           'Set up a TOKEN_PRIVILEGES structure containing only the shutdown privilege.
           Dim newState As New TOKEN_PRIVILEGES
           newState.PrivilegeCount = 1
           newState.Privileges = New LUID_AND_ATTRIBUTES() {luaAttr}
 
           'Set up a TOKEN_PRIVILEGES structure for the returned (modified) privileges.
           Dim prevState As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES
           ReDim prevState.Privileges(CInt(newState.PrivilegeCount))
 
           'Apply the TOKEN_PRIVILEGES structure to the current process's token.
           Dim returnLength As IntPtr
           If Not AdjustTokenPrivileges(hToken, False, newState, Marshal.SizeOf(prevState), prevState, returnLength) Then
               lastWin32Error = Marshal.GetLastWin32Error()
               Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
                "AdjustTokenPrivileges failed with error " & lastWin32Error.ToString & ".")
           End If
 
       Finally
           CloseHandle(hToken)
       End Try
 
   End Sub
#End Region
 
Sub ApagarWindows()
           Call AcquireShutdownPrivilege()
           Call ExitWindowsEx(ExitWindows.ShutDown, ShutdownReason.MajorApplication)
End Sub
 

Ahora encargate de pasar eso a C#  ;D

PInvoke te puede ayudar para las API, guarda el link  ;)

Meta:
Muchas gracias, es mucho código para lo que es. Da la casualdiad que hay estos:

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

http://www.elguille.info/colabora/2007/phanthom2k_ApagadoPC.htm

Saludo.

nico56:
E c/c++ tenia la funcion system("comando"), donde comando era cualquier comando del sistema operativo, ahi podrias poner un shutdown con las opciones de forzado apagado. Para mas info de ese comando en un cmd pone "shutdown /?"

Navegación

[0] Índice de Mensajes