begin process at 2008 05 16 06:55:23
1 173 219 membres
61 nouveaux aujourd'hui
13 970 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

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


Information sur la source

Catégorie :Système Niveau : Débutant Date de création : 02/11/2004 Vu : 2 993

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

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...
  • signaler à un administrateur
    Commentaire de MAURICIO le 10/11/2004 13:22:15

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

  • signaler à un administrateur
    Commentaire de s_baudet_3 le 03/12/2004 11:49:25

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

  • signaler à un administrateur
    Commentaire de Choumoumou le 28/12/2004 21:49:07

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

  • signaler à un administrateur
    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.

  • signaler à un administrateur
    Commentaire de Chyokyka le 09/04/2008 19:14:31

    OUAIS Geniale ^^
    Merci !!

Ajouter un commentaire

Appels d'offres

Pub



CalendriCode

Mai 2008
LMMJVSD
   1234
567891011
12131415161718
19202122232425
262728293031 

Boutique

Boutique de goodies CodeS-SourceS