Sordie.co.uk

CrashRestartDemo.pas

Raw

program CrashRestartDemo;

{$APPTYPE CONSOLE}

uses
  Windows;

// Prototype the WinAPI
function RegisterApplicationRestart(pwzCommandline: PChar; dwFlags: DWORD): HRESULT; stdcall; external kernel32;

var
  p: ^Integer;
begin
  // Register for a restart if something goes wrong, pass the parameter 'crashed' if it does
  RegisterApplicationRestart(PChar('crashed'), 0);

  // Check the parameter
  if ParamStr(1) = 'crashed' then
    WriteLn('Recovered from a crash')
  else
    WriteLn('Running normally');

  // Wait for the user to press Enter (should be longer than 60 seconds to cause a restart)
  ReadLn;

  // Cause an access violation
  p := nil;
  p^ := 1;
end.