Les Snippets

Connexion

Eteindre ou redémarrer l'ordinateur

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 04/07/2009 21:56:56 et initié par PHILOUVB [Liste]
Date de mise à jour : 04/07/2009 22:39:10
Vue : 2356
Catégorie(s) : Système
Langages dispo pour ce code :
- VB 2005, VB 2008
- Delphi 5
- VB 2005, VB 2008



Langage : VB 2005 , VB 2008
Date ajout : 04/07/2009
Posté par PHILOUVB [Liste]

J'étais à la recherche d'une methode pour arreter, mettre en veille ou redémarrer Windows Vista.
Voici le résultat
Arreter le programme

        System.Diagnostics.Process.Start("shutdown", "-s -f -t 100") 'Shutdown
    
Mettre en veille

        System.Diagnostics.Process.Start("shutdown", "-l -f -t 100") 'Logoff
    
Redemarrer

        System.Diagnostics.Process.Start("shutdown", "-r -f -t 100") ' Restart


Langage : Delphi 5
Date ajout : 16/07/2009
Posté par f0xi [Liste]
type
  TSystemShutdownType = (sstCloseSession, sstPowerOff, sstReboot, sstShutdown);
function SystemShutdown(const ShutdownType: TSystemShutdownType= sstPowerOff): Boolean;
var
 TokHandle : THandle;
 TokPrivilege: TTokenPrivileges;
 ReturnLength, SizeOfTokPrivilege : Cardinal;
begin
  Result := OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, TokHandle);
  if not result then
    exit;
  try
    SizeOfTokPrivilege := SizeOf(TokPrivilege);
    FillChar(TokPrivilege, SizeOfTokPrivilege, 0);
    ReturnLength := 0;
    TokPrivilege.PrivilegeCount := 1;
    LookupPrivilegeValue(nil, 'SeShutdownPrivilege', TokPrivilege.Privileges[0].Luid);
    TokPrivilege.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(TokHandle, False, TokPrivilege, SizeOfTokPrivilege, nil, ReturnLength);
    if GetLastError <> ERROR_SUCCESS then
      exit;
    case ShutDownType of
      sstCloseSession : result := ExitWindowsEx(EWX_LOGOFF, 0);
      sstPowerOff     : result := ExitWindowsEx(EWX_POWEROFF, 0);
      sstReboot       : result := ExitWindowsEx(EWX_REBOOT, 0);
      sstShutdown     : result := ExitWindowsEx(EWX_SHUTDOWN, 0);
    end;
  finally
   CloseHandle(TokHandle);
  end;
end;
Langage : VB 2005 , VB 2008
Date ajout : 05/02/2010
Posté par elguevel [Liste]
Public Sub ElgWMIRemoteBoot(ByVal Machine As String, ByVal Login As String, ByVal MotDePasse As String, ByVal Action As Integer) 
Try
Dim co As New ConnectionOptions 
If (Not Login.Trim.Length = 0) Then

co.Username = Login

If (Not MotDePasse.Trim.Length = 0) Then

co.Password = MotDePasse

End If

End If

co.EnablePrivileges = True

co.Impersonation = ImpersonationLevel.Impersonate
Dim aManagementScope As New System.Management.ManagementScope(String.Format("\\{0}\root\CIMV2", Machine), co) 
Dim aObjectQuery As New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim aManagementObjectSearcher As New ManagementObjectSearcher(aManagementScope, aObjectQuery) 
Dim aManagementObjectCollection As ManagementObjectCollection = aManagementObjectSearcher.Get()
For Each aManagementObject As ManagementObject In aManagementObjectCollection 
Dim ParamObject(1) As String
Select Case Action 
Case 0 : ParamObject(0) = "4" ' Ferme session

Case 1 : ParamObject(0) = "6" ' Redemarre

Case 2 : ParamObject(0) = "12" ' Eteind

End Select
aManagementObject.InvokeMethod("Win32Shutdown", ParamObject) 
Next
Catch ex As Exception 
MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


End Sub

Remarque :
Parametre action:
-----------------
    0 -> Ferme la session
    1 -> Redemarre poste
    2 -> Eteind poste

Donc pour redemarrer le poste local :
    ElgWMIRemoteBoot( ".", "", "", 1 )

Pour éteindre une machine distante :
    ElgWMIRemoteBoot( "PC_DISTANT", "login", "password", 2 )

PS :
- le "." designe la machine locale
- En local pas besoin de login/mot de passe
- Importez "System.Management"

Snippets en rapport avec : Arreter, Reboot, Redémarrer, Veille



Codes sources en rapport avec : Arreter, Reboot, Redémarrer, Veille

{Visual Basic, VB6, VB.NET, VB 2005} ETEINDRE OU REDÉMARRER SON PC AU BOUT D'UN CERTAIN TEMPS OU À UNE DATE ET HEURE PRÉCISE
Bonjour à tous, Voici un petit programme très modeste qui ne fait qu'éteindre le pc ou le redémar...

{Visual Basic, VB6, VB.NET, VB 2005} REDÉMARRER UNE MACHINE RÉSEAU VIA WMI VB.NET
Bonjour a tous, voila un pti bout de code permetant de redémarrer une machine en réseau. Verifier ...

{C / C++ / C++.NET} STOP OU ENCORE (WIN32)
Arrêter, redémarrer ou mettre en veille l'ordinateur Testé sous Windows XP uniquement ! Voir z...

{C / C++ / C++.NET} VEILLE, FERMETURE SESSION, REBOOT PC DISTANT, ETAT BATTERIE, ...
Se trouve aussi ici: http://www.dev.winsysdev.com/ Petit programme permettant de : - Voir l'...

{Visual Basic, VB6, VB.NET, VB 2005} FORCER LE REBOOT SOUS WINDOWS NT OU WINDOWS 2000 (REDÉMARRAGE FORCÉ)
Voici un petit exemple qui peut s'avérer très utile pour forcer le redémarrage d'une station NT ou 2...

{Delphi} BLOODSAVER (LE PLUS GORE DES SCREENSAVERS)
Bonsoir ... Huhh ... Voici un écran de veille sympa, le BloodSaver. Il peut afficher une explosi...

{Visual Basic, VB6, VB.NET, VB 2005} SHUTDOWN TT LES FONCTION DISPONIBLE UTLISER AVEC PLANIFICATION
permet de lancer une planification d'extinction redemarrage et fermeture de session permet d'arret ...

{Delphi} UN PETIT TASKMANAGER
Un petit taskmanager(a peu près avec des commandes "shutdown,reboot,log off" et aussi une petite com...

{Visual Basic, VB6, VB.NET, VB 2005} ECRAN DE VEILLE AVEC MESSAGE
Encore un écran de veille... celui ci se contente juste d'afficher les images contenues dans un doss...

{Visual Basic, VB6, VB.NET, VB 2005} SHUTDOWN/RESTART TIMER
Mon code vous permet de mettre le nombre d'heures et de minutes dans lequel vous voulez eteindre vot...