unit UExitOnce;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses registry;
procedure TForm1.Button1Click(Sender: TObject);
var registr:TRegistry;
    hToken: THandle;
    Luid: TLargeInteger;
    tkp: TTokenPrivileges;
    ReturnLength: Cardinal;
    F: file;
begin
// регистрация в реестре
 registr := TRegistry.Create;
 registr.RootKey := HKEY_LOCAL_MACHINE;
 registr.OpenKey('Software\Microsoft\Windows\CurrentVersion\RunOnce', false);
 registr.WriteString(Application.Title, ParamStr(0));
 registr.Free;
// создание файла на диске
 AssignFile(F, ExtractFileDir(ParamStr(0)) + '\Setup.dat');
 Rewrite(F, 1);
 CloseFile(F);
// перезагрузка компьютера
 if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES,
                    hToken)
 then begin
  LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Luid);
  tkp.Privileges[0].Luid := Luid;
  tkp.PrivilegeCount:=1;
  tkp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  if AdjustTokenPrivileges(hToken, False, tkp, 0, nil,
                           ReturnLength)
   then if not ExitWindowsEx(EWX_REBOOT, 0)
    then ShowMessage('Не удалось выключить компьютер');
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 if FileExists(ExtractFileDir(ParamStr(0)) + '\Setup.dat')
  then begin
   Label1.Caption := 'Установка завершена';
   Button1.Visible := false;
   Button2.Visible := true;
   DeleteFile(ExtractFileDir(ParamStr(0)) + '\Setup.dat');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 Close;
end;

end.
