How to log off, power off, sleep, reboot, hibernate or shutdown Windows in Delphi

It's very simple. Just drop TWindowsShutdown component onto your form and call appropriate method.

procedure TfrmShutdownMain.btnTurnOffDialogClick(Sender: TObject);
begin
  cmpWindowsShutdown.TurnOffDialog;
end;

procedure TfrmShutdownMain.btnSleepClick(Sender: TObject);
begin
  cmpWindowsShutdown.Sleep;
end;

procedure TfrmShutdownMain.btnHibernateClick(Sender: TObject);
begin
  cmpWindowsShutdown.Hibernate;
end;

procedure TfrmShutdownMain.btnLogOffClick(Sender: TObject);
begin
  cmpWindowsShutdown.LogOff;
end;

procedure TfrmShutdownMain.btnPowerOffClick(Sender: TObject);
begin
  cmpWindowsShutdown.PowerOff;
end;

procedure TfrmShutdownMain.btnRebootClick(Sender: TObject);
begin
  cmpWindowsShutdown.Reboot;
end;

procedure TfrmShutdownMain.btnShutdownClick(Sender: TObject);
begin
  cmpWindowsShutdown.Shutdown;
end;

Be careful with Force property, because if this property is True, your data in the open programs can be lost.

See also
Windows Shutdown
Components for Delphi / C++ Builder
Howtos for Programmers