- 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.