Extinction du moniteur

Résolu
Warium Messages postés 36 Date d'inscription jeudi 27 mars 2008 Statut Membre Dernière intervention 27 janvier 2010 - 25 sept. 2008 à 17:29
Nicolas___ Messages postés 992 Date d'inscription jeudi 2 novembre 2000 Statut Membre Dernière intervention 24 avril 2013 - 26 sept. 2008 à 22:01
Bonjour.
Je voudrais avec un simple clique sur un boutton, eteindre mon ordinateur.
 
j'ai vu dans l'aide une méthode apelée ShutDown mais ça ne marche pas ou je ne sais pas l'utiliser

Merci pour votre attention.

15 réponses

Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
25 sept. 2008 à 18:50
Ah mais oui mais non attends :

unit Unit1;

interface

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

type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
TForm1 = class(TForm)
Button1: TButton;
// Ne pas déclarer GetWinType ici, inutile
procedure Button1Click(Sender: TObject);
procedure ExitWindowsProc(ExitType: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

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 ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin

if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
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);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;

Voilà : si tu déclares GetWinType dans l'interface de ta fiche, il faut que l'implémentation soit précédée de l'instance de ta fiche (ici TForm1). Tu pourrais le faire, mais c'est inutile.

Cordialement, Bacterius !
3
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
25 sept. 2008 à 17:39
procedure ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin

if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
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);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;

_____________________________________


Paramètres à passer dans ExitType :

Eteindre ordinateur : EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF

Redémarrer ordinateur : EWX_REBOOT

Fermer la session : EWX_LOGOFF

Et y'a d'autres flags je te laisse chercher.

Cordialement, Bacterius !
0
Warium Messages postés 36 Date d'inscription jeudi 27 mars 2008 Statut Membre Dernière intervention 27 janvier 2010
25 sept. 2008 à 17:44
Merci, mais comment puis-je faire cela avec un clique sur un button???
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
25 sept. 2008 à 17:46
Alors,
tu ajoutes ça dans ton unité (avant le code du bouton, ou alors déclares la dans l'interface).

Ensuite, dans ton ButtonClick tu fais (pour éteindre) :

begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;

Voilà !

Cordialement, Bacterius !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
25 sept. 2008 à 17:48
oups petit problème ^^
Voilà la version corrigée

...

interface

type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);

...

implementation


function GetWinType: TWinType; // Le type de Windows, mais plus simplement
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 ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin

if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
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);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;

Voilà !

Cordialement, Bacterius !
0
Warium Messages postés 36 Date d'inscription jeudi 27 mars 2008 Statut Membre Dernière intervention 27 janvier 2010
25 sept. 2008 à 18:01
Désolé bacterius esque SVP tu peux me corriger ça ::
Il me signal l'erreur suivante :
[Error] Unit1.pas(13): Unsatisfied forward or external declaration: 'TForm1.GetWinType'

unit Unit1;


interface


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


type
  TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
  TForm1 = class(TForm)
    Button1: TButton;
    function GetWinType(): TWinType;
    procedure Button1Click(Sender: TObject);
    procedure ExitWindowsProc(ExitType: Cardinal);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;


implementation


{$R *.dfm}
function GetWinType(): TWinType; // Le type de Windows, mais plus simplement
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 ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin


if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error;     // On fixe nos privilèges
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);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error;  // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
    ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;


end.
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
25 sept. 2008 à 18:48
unit Unit1;

interface

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

type
TWinType = (wtWindows95, wtWindowsNT, wtWin32s, wtUnknown);
TForm1 = class(TForm)
Button1: TButton;
function GetWinType(): TWinType;
procedure Button1Click(Sender: TObject);
procedure ExitWindowsProc(ExitType: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

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 ExitWindowsProc(ExitType: Cardinal);
var
TokenHandle: THandle;
NewState, PreviousState: TTokenPrivileges;
ReturnLength: DWORD;
begin

if GetWinType = wtWindowsNT then begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY, TokenHandle) then
RaiseLastWin32Error; // On fixe nos privilèges
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);
if not ExitWindowsEx(ExitType, 0) then
RaiseLastWin32Error; // On ferme
end;
end
else begin
if not ExitWindowsEx(ExitType,0) then begin
RaiseLastWin32Error;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ExitWindowsProc(EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF);
end;

Si tu ne fais que ça ça devrait marcher.

Cordialement, Bacterius !
0
Warium Messages postés 36 Date d'inscription jeudi 27 mars 2008 Statut Membre Dernière intervention 27 janvier 2010
25 sept. 2008 à 23:28
Merci Bacterius ça a marché
0
Nicolas___ Messages postés 992 Date d'inscription jeudi 2 novembre 2000 Statut Membre Dernière intervention 24 avril 2013 1
26 sept. 2008 à 11:01
Sous windows ...

uses  ShellAPI;

procedure TForm1.btEteindreClick(Sender: TObject);
begin
  ShellExecute(Handle,'open',PChar('shutdown'),'-s -t 5 -c "Extinction des feux dans 5 sec !"',nil,SW_SHOW);
end;

procedure TForm1.btAnnuleClick(Sender: TObject);
begin
  ShellExecute(Handle,'open',PChar('shutdown'),'-a',nil,SW_SHOW);
end;

Nico
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
26 sept. 2008 à 17:00
Ouais ... mais c'est moins stylé ^^

Cordialement, Bacterius !
0
Nicolas___ Messages postés 992 Date d'inscription jeudi 2 novembre 2000 Statut Membre Dernière intervention 24 avril 2013 1
26 sept. 2008 à 19:55
 bof ...
je préfère de loin ma solution à une ligne que ta function ...
de plus il voulait comprendre ShutDown ben voila , c'est chose faire maintenant !

Nico
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
26 sept. 2008 à 21:01
Ouais mais ton truc c'est pas propre ... tu règles même pas les privilèges !
Imagine que les droits de l'utilisateur de ta "function" soient au niveau Esclave (le droit de se connecter et d'aller sur PriceMinister.com). Enfin de manière générale de ne pas pouvoir appeler cette commande ShutDown ! Erreur, pas le droit de privilèges ...

Evidemment 1 ligne = plus facile à comprendre, et à mettre en oeuvre.

Mais je préfère de loin avoir un gros code, qui soit fonctionnel, plutôt qu'un petit code de 2 ou 3 lignes, qui peut avoir des occasions de de foutre sur la gueule ^^

Mais nous n'allons pas en faire un débat philosophique, sinon on va basculer dans les grosses API et l'asm ... et j'ai pas envie ^^

[HORS SUJET]
Sinon, dans ton jeu space-shoot, je n'ai pas vu ma fonction pour créer du cosmos ... aurais-tu oublié de l'ajouter ? Ou n'as-tu pas eu envie ? :'(
En passant, il manque la DLL FMod ^^
SUJET

Cordialement, Bacterius !
0
Nicolas___ Messages postés 992 Date d'inscription jeudi 2 novembre 2000 Statut Membre Dernière intervention 24 avril 2013 1
26 sept. 2008 à 21:34
En passant, il manque la DLL FMod ^^
euh déso de te contredire mais elle y est !



J'ai pas encore mis a jour depuis le 15 donc ton cosmos n'est pas encore pris en compte ...

j'ai posté des commentaires par rapport aux jeux ...

A mon avis je ferais encore 1 maj et puis basta ... manque de temps , d'interêt et d'autre projet en vue ...  

Explication de ma méthode "pas propre " de shutdown

Bonjour.
Je voudrais avec un simple clique sur un bouton, éteindre mon ordinateur.
j'ai vu dans l'aide une méthode appelée ShutDown mais ça ne marche pas ou je ne sais pas l'utiliser
Merci pour votre attention.

Voila , j'ai répondu à ça requête ... Ca ne sert à rien de sortir le bazooka quand la tapette est suffisante...
Si plus tard tu veux bosser dans l'info , je t'encourage à baisser ton enthousiasme ... il faut répondre a la demande, ni plus, ni moins !
En plus ici, il n'y a pas de rémunérations alors ...
the lower, the better !

Nico
0
Bacterius Messages postés 3792 Date d'inscription samedi 22 décembre 2007 Statut Membre Dernière intervention 3 juin 2016 10
26 sept. 2008 à 21:49
Je ne suis pas d'accord : "the lower the better" "pas de rémunérations". Justement : celui qui a du temps fournit une réponse plus structurée et tout.
Je suppose que celui qui utilise ta méthode, quand il s'apercevra que ça ne marche pas dans certains environnements, il se dira "mince alors, ou est la méthode de l'autre ?"
Alors bon, toi tu dois sûrement être grand, connaître le boulot d'un développeur mieux que moi ...
Je sais que sur un truc il faut répondre à la demande, mais c'est tout. Mais, si tu lui fournis une réponse incomplète (qui ne marchera pas dans toutes les conditions), tu ne réponds pas à la demande.

Et d'ailleurs, si tu regardes bien, il ne demandait pas une réponse simple.
Il voulait que son clic de bouton se fasse simplement (il voulait peut-être un fluidificateur de souris ^^) avec un shutdown en plus.

Bref, n'en parlons plus. Après tout, il prendra celle qu'il préfère (la tienne je pense).

Sinon, pour FMod, je te jure qu'elle n'y était pas ...

Cordialement, Bacterius !
0
Nicolas___ Messages postés 992 Date d'inscription jeudi 2 novembre 2000 Statut Membre Dernière intervention 24 avril 2013 1
26 sept. 2008 à 22:01
% a Fmod ; tu m'as mis un commentaire  le 10/09/2008 19:34:35 que La Dll n'était pas présente et j'ai répondu
<dt>le 10 septembre 2008 19:46:55  : dll manquante ... T'as vu la réactivité ^^
%Au sujet : On va arrêter , tu ne comprends pas ... Et on dévie assez

Topic clôt , comme dirait les admins ^^

Nico
0
Rejoignez-nous