Pour écrire cette source, je me suis inspiré d'un article de x-smurf
http://www.vbfrance.com/article.aspx?ID=3170 et d'un article de fabiin
http://www.delphifr.com/article.aspx?Val=208
Les fonctions ont été testées sous Windows XP (familiale) et sous Windows 95 (sauf la fonction LogOffNT).
En fait j'ai réécrit en Delphi 3 les fonctions VB de x-smurf : ça évitera à ceux qui voulaient le faire de perdre du temps à pianoter...
Mise à jour du 16/09/2003 : ajout de la procédure de mise en veille du PC.
Si vous avez des remarques ou des suggestions n'hésitez pas à m'en faire part.
Source / Exemple :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
uses Uboot;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsWinNT then ShowMessage('OK') else ShowMessage('Pas OK');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShutDownNT(true);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
PowerDownNT(true);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
RebootNT(true);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
LogOffNT(true);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
MiseEnVeillePC(True, True);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;
end;
end.
//====================================================
unit Uboot;
interface
uses
Windows, Dialogs;
Function IsWinNT : Boolean;
Procedure EnableShutDown;
Procedure ShutDownNT (Force : Bool);
Procedure PowerDownNT (Force : Bool);
Procedure RebootNT (Force : Bool);
Procedure LogOffNT (Force : Bool);
Procedure MiseEnVeillePC(fSuspend, fForce : bool);
implementation
Function IsWinNT : Boolean;
var
myOS: TOSVersionInfo;
begin
myOS.dwOSVersionInfoSize := Sizeof(myOS);
GetVersionEx(myOS);
result := (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT);
end;
procedure EnableShutDown;
var mPriv,mNewPriv : TTokenPrivileges;
ReturnLen : DWORD ;
hProc,hToken : THANDLE ;
mLUID : TLargeInteger ;
begin
hProc := GetCurrentProcess ;
OpenProcessToken (hProc,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',mLUID);
mPriv.PrivilegeCount := 1;
mPriv.Privileges[0].Luid := mLUID;
mPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,mPriv,sizeof(TTokenPrivileges),mNewPriv,ReturnLen);
CloseHandle (hToken);
end;
//fonction qui éteint le PC (Force=true oblige le traitement)
Procedure ShutDownNT (Force : Bool);
var ret: bool;
FLAGS : Integer;
begin
FLAGS := EWX_SHUTDOWN;
If Force Then FLAGS := FLAGS or EWX_FORCE;
If IsWinNT Then EnableShutDown;
ret := ExitWindowsEx (FLAGS, 0);
If ret = False then ShowMessage ('Impossible de fermer Windows');
end;
//fonction qui éteint totalement la tour (Force=true oblige le traitement)
Procedure PowerDownNT (Force : Bool);
var ret: bool;
FLAGS : Integer;
begin
FLAGS := EWX_POWEROFF;
If Force Then FLAGS := FLAGS or EWX_FORCE;
If IsWinNT Then EnableShutDown;
ret := ExitWindowsEx (FLAGS, 0);
If ret = False then ShowMessage ('Impossible de fermer Windows');
end;
// fonction qui fait rebooter l'OS (Force=true oblige le traitement)
Procedure RebootNT (Force : Bool);
var ret: bool;
FLAGS : Integer;
begin
FLAGS := EWX_REBOOT;
If Force Then FLAGS := FLAGS or EWX_FORCE;
If IsWinNT Then EnableShutDown;
ret := ExitWindowsEx (FLAGS, 0);
If ret = False then ShowMessage ('Impossible de fermer Windows');
end;
//fonction qui délogue l'user (Force=true oblige le traitement)
Procedure LogOffNT (Force : Bool);
var ret: bool;
FLAGS : Integer;
begin
FLAGS := EWX_LOGOFF;
If Force Then FLAGS := FLAGS or EWX_FORCE;
If IsWinNT Then EnableShutDown;
ret := ExitWindowsEx (FLAGS, 0);
If ret = False then ShowMessage ('Impossible de fermer Windows');
end;
//Procédure de mise en veille du PC
Procedure MiseEnVeillePC(fSuspend, fForce : bool);
var
succes : bool;
begin
If IsWinNT Then EnableShutDown;
succes := SetSystemPowerState(fSuspend, fForce);
if not succes then showmessage ('Impossible de mettre le PC en veille');
end;
end.
Conclusion :
Merci à x-smurf et à fabiin.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.