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 / Exemple :
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...
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.