SHARE
TWEET

FDCRelauncher.pas

LarsFosdal Oct 15th, 2014 488 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit FDCRelauncher;
  2.  
  3. ///  Simple mechanisms for restarting a service or application on exit.
  4. ///  Intended for use after .exe upgrades, etc.
  5. ///  Instructions:
  6. ///    1. Put the unit as early as possible in the main app uses section
  7. ///    2. Call one of the Restart procedures before exiting.
  8. ///
  9. ///  Written by Lars Fosdal, Tine SA, 15 OCT 2014, http://plus.lars.fosdal.com
  10.  
  11. interface
  12. uses
  13.   Windows, SysUtils, ShellAPI, CustomDebugOut;
  14.  
  15.   /// <summary> Restarts the application on exit, optionally passing extra arguments on the command line</summary>
  16.   procedure RestartApplicationOnExit(const aExtraArguments: String = '');
  17.  
  18.   /// <summary> Restarts the service when it stops, using service name (without quotes)</summary>
  19.   procedure RestartServiceOnExit(const ServiceName: String);
  20.  
  21. implementation
  22.  
  23. type
  24.   TRestartOnExit = reference to procedure;
  25.  
  26. var
  27.   RestartOnExit: TRestartOnExit;
  28.  
  29. procedure RestartServiceOnExit(const ServiceName: String);
  30. begin
  31.   RestartOnExit := procedure
  32.   begin
  33.     OutputDebugString(SysEvent + 'Restarting ' + ServiceName);
  34.     try
  35.       ShellExecute(0, 'open', pChar('cmd.exe'),
  36.         pChar('/S /C net start "' + ServiceName + '"'), nil, SW_HIDE);
  37.     except
  38.       on E:Exception
  39.       do begin
  40.         OutputDebugString(BadShit + 'Restart Service Exception ' + E.ClassName + ' ' + E.Message);
  41.       end;
  42.     end;
  43.  
  44.   end;
  45. end;
  46.  
  47. procedure RestartApplicationOnExit(const aExtraArguments: String);
  48. var
  49.   ExtraArguments: String;
  50. begin
  51.   ExtraArguments := Trim(aExtraArguments);
  52.  
  53.   RestartOnExit := procedure
  54.   begin
  55.     OutputDebugString(SysEvent + 'Restarting ' + ExtraArguments);
  56.     try
  57.       ShellExecute(0, 'open', pChar(ParamStr(0)),
  58.         pChar('RestartApplicationOnExit='+IntToStr(GetCurrentProcessId) + ExtraArguments), nil, SW_NORMAL);
  59.     except
  60.       on E:Exception
  61.       do begin
  62.         OutputDebugString(BadShit + 'Restart Application Exception ' + E.ClassName + ' ' + E.Message);
  63.       end;
  64.     end;
  65.  
  66.   end;
  67.  
  68. end;
  69.  
  70. initialization
  71.   RestartOnExit := nil;
  72.  
  73. finalization
  74.  
  75.   if Assigned(RestartOnExit)
  76.    then RestartOnExit;
  77.  
  78. end.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top