begin process at 2012 02 11 12:17:50
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Système

 > ETEINDRE TOUS LES WINDOWS (MÊME NT...)

ETEINDRE TOUS LES WINDOWS (MÊME NT...)


 Information sur la source

Note :
9,2 / 10 - par 5 personnes
9,20 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Système Niveau :Débutant Date de création :02/11/2004 Vu :4 285

Auteur : s_baudet_3

Ecrire un message privé
Commentaire sur cette source (5)
Ajouter un commentaire et/ou une note

 Description

Ce code est tout simple...
Un clic sur un bouton permet d'éteindre l'ordinateur.
Aucun message n'apparaît à la fin comme "Vous pouvez éteindre votre ordinateur maintenant".
J'ai testé ce programme sous XP, 2000, Millenium, NT, 98 et 95. Il marchait parfaitement...

Source

  • unit Unit1;
  • interface
  • uses
  • Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  • Dialogs, StdCtrls;
  • type
  • TForm1 = class(TForm)
  • Button1: TButton;
  • procedure Button1Click(Sender: TObject);
  • private
  • { Déclarations privées }
  • public
  • { Déclarations publiques }
  • end;
  • type
  • // Liste des différents OS
  • TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
  • var
  • Form1: TForm1;
  • implementation
  • {$R *.dfm}
  • // fonction récupérant l'OS de votre ordinateur
  • function GetWinType: TWinType;
  • var
  • VersionInfo: TOSVersionInfo;
  • begin
  • Result := wtUnknown;
  • VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
  • GetVersionEx(VersionInfo);
  • case VersionInfo.dwPlatformId of
  • VER_PLATFORM_WIN32S : Result := wtWin32s;
  • VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
  • VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
  • end;
  • end;
  • // Procedure permettant la fermeture de windows selon l'OS
  • procedure DoCloseWindow;
  • var
  • TokenHandle: THandle;
  • NewState, PreviousState: TTokenPrivileges;
  • ReturnLength: DWORD;
  • begin
  • // on différencie Windows NT des autres OS
  • if GetWinType = wtWindowsNT then begin
  • if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
  • TOKEN_QUERY, TokenHandle) then
  • RaiseLastWin32Error;
  • try
  • NewState.PrivilegeCount := 1;
  • if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
  • NewState.Privileges[0].LUID) then begin
  • RaiseLastWin32Error;
  • end;
  • NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  • ReturnLength := 0;
  • if not AdjustTokenPrivileges(TokenHandle, False, NewState,
  • SizeOf(NewState), PreviousState, ReturnLength) then begin
  • RaiseLastWin32Error;
  • end;
  • finally
  • CloseHandle(TokenHandle);
  • // éteindre Windows
  • if not ExitWindowsEx(EWX_FORCE or EWX_shutdown or EWX_POWEROFF, 0) then
  • RaiseLastWin32Error;
  • end;
  • end
  • else begin
  • if not ExitWindowsEx(EWX_FORCE or EWX_shutdown or EWX_POWEROFF,0) then begin
  • RaiseLastWin32Error;
  • end;
  • end;
  • end;
  • procedure TForm1.Button1Click(Sender: TObject);
  • begin
  • DoCloseWindow;
  • end;
  • end.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

type
  // Liste des différents OS
  TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);

var
  Form1: TForm1;

implementation

{$R *.dfm}

// fonction récupérant l'OS de votre ordinateur
function GetWinType: TWinType;
var
  VersionInfo: TOSVersionInfo;
begin
  Result := wtUnknown;
  VersionInfo.dwOSVersionInfoSize := SizeOf(VersionInfo);
  GetVersionEx(VersionInfo);
  case VersionInfo.dwPlatformId of
    VER_PLATFORM_WIN32S : Result := wtWin32s;
    VER_PLATFORM_WIN32_WINDOWS : Result := wtWindows95;
    VER_PLATFORM_WIN32_NT : Result := wtWindowsNT;
  end;
end;

// Procedure permettant la fermeture de windows selon l'OS
procedure DoCloseWindow;
var
  TokenHandle: THandle;
  NewState, PreviousState: TTokenPrivileges;
  ReturnLength: DWORD;
begin
  // on différencie Windows NT des autres OS
  if GetWinType = wtWindowsNT then begin
    if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
                            TOKEN_QUERY, TokenHandle) then
      RaiseLastWin32Error;
    try
      NewState.PrivilegeCount := 1;
      if not LookupPrivilegeValue(nil, 'SeshutdownPrivilege',
                                  NewState.Privileges[0].LUID) then begin
        RaiseLastWin32Error;
      end;
      NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      ReturnLength := 0;
      if not AdjustTokenPrivileges(TokenHandle, False, NewState,
                                   SizeOf(NewState), PreviousState, ReturnLength) then begin
        RaiseLastWin32Error;
      end;
    finally
      CloseHandle(TokenHandle);
    // éteindre Windows
    if not ExitWindowsEx(EWX_FORCE or EWX_shutdown or EWX_POWEROFF, 0) then
      RaiseLastWin32Error;
    end;
  end
  else begin
    if not ExitWindowsEx(EWX_FORCE or EWX_shutdown or EWX_POWEROFF,0) then begin
      RaiseLastWin32Error;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoCloseWindow;
end;

end.

 Conclusion

Si jamais vous avez des idées, n'hésitez pas à me les faire parvenir... je débute dc...


 Sources de la même categorie

Source avec Zip LECTURE DE LA MEMOIRE D'UN AUTRE PROCESSUS par Mokost
Source avec Zip Source avec une capture LIBÉRER LA TAILLE MAXIMALE D'UNE FENÊTRE PAR SUBCLASSING par rt15
Source avec Zip Source avec une capture OBSERVATEUR DE PROCESSUS ACTIFS; VPROCESS 1,0 par Neftali
UN SELECTDIRECTORY QUI SE PLACE AU BON ENDROIT par ThWilliam
Source avec une capture VOTRE PROGRAMME DE MAIL COMME CLIENT MAIL PAR DÉFAUT SOUS WI... par MAURICIO

Commentaires et avis

Commentaire de MAURICIO le 10/11/2004 13:22:15 administrateur CS

Je te mets 9/10! Moi je connaissais déjà mais ça merite cette note quand meme!!!

Commentaire de s_baudet_3 le 03/12/2004 11:49:25

Merci Mauricio pour la note...
Je mettrais d'autres sources bientôt...

Commentaire de Choumoumou le 28/12/2004 21:49:07

Wow ! Super ! Je voulai justement un code comme sa pour delphi ! 9/10

Commentaire de freetai le 10/07/2005 18:01:37

pratique ton code, j'étais moi aussi en train de plancher sur un truc du genre, ça m'est bien utile! en plus le code est bien clair, indenté... rien à redire pour cette petite fonctionnalité ô combien pratique.

Commentaire de Chyokyka le 09/04/2008 19:14:31

OUAIS Geniale ^^
Merci !!

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,312 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales