unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
Inifiles;
type
Tmvideo_sync =
class(TService)
procedure ServiceExecute(Sender: TService);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
procedure ServiceAfterUninstall(Sender: TService);
procedure ServiceBeforeUninstall(Sender: TService);
procedure ServiceAfterInstall(Sender: TService);
private
{ Private-Deklarationen }
public
testi :
string;
function GetServiceController: TServiceController;
override;
{ Public-Deklarationen }
end;
var
mvideo_sync: Tmvideo_sync;
videos_dir,
// Relativer Pfad
videos_neu_dir,
// Relativer Pfad
mvideo_sync_homedir :
String;
// Absoluter Pfad zur Dienst-Executable
IniFile : TIniFile;
LogFileName :
String;
// Ohne Pfadangabe
IniStringList : TStringList;
hour_of_execution : Word;
// Stunde, zu der das Update durchgeführt werden soll
UpdateExecuted : Boolean;
// Wurde das Update durchgeführt? Dient dazu, einen weiteren Durchlauf während der "Update-Stunde" zu verhindern
implementation
{$R *.DFM}
const
IniFileName = '
mvideo_sync.ini';
procedure Log(Msg:
String);
var
DTStr :
String;
LogFile : Textfile;
begin showmessage(mvideo_sync.testi + IntToStr(hour_of_execution) + '
, ' + LogFileName + '
, ' + videos_dir + '
, ' + videos_neu_dir + '
, ' + mvideo_sync_homedir);
DateTimeToString(DTStr, '
dd.mm.yy hh:mm', now);
AssignFile(LogFile, mvideo_sync_homedir + '
\' + LogFileName);
Try
if not FileExists(mvideo_sync_homedir + '
\' + LogFileName)
then //ReWrite(LogFile)
else ;
//Append(LogFile);
// Try
// WriteLn(LogFile, DTStr + ' : ' + Msg);
// except end;
finally
// CloseFile(LogFile);
end;
end;
procedure ExecNewProcess(ProgramName :
String);
// Mit relativem Pfad aufzurufen
var
StartInfo : TStartupInfo;
ProcInfo : TProcessInformation;
CreateOK : Boolean;
AbsProgramName :
String;
begin
AbsProgramName := mvideo_sync_homedir + '
\' + ProgramName;
{ fill with known state }
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateOK := CreateProcess(
nil, PChar(AbsProgramName),
nil,
nil, false, CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
nil,
nil, StartInfo, ProcInfo);
if CreateOK
then
//may or may not be needed. Usually wait for child processes
WaitForSingleObject(ProcInfo.hProcess, INFINITE)
else begin
// Log error
MessageDlg('
Scheiße!', mtError, [mbOK], 0);
exit;
end;
end;
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
mvideo_sync.Controller(CtrlCode);
end;
function Tmvideo_sync.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure Tmvideo_sync.ServiceExecute(Sender: TService);
var
h, m, s, ms : Word;
begin
while not terminated
do begin
//ServiceThread.ProcessRequests(false);
DecodeTime(now, h, m, s, ms);
if h = hour_of_execution + 1
then UpdateExecuted := false;
// Indikator rücksetzen, um in ca. 23 Stunden wieder ready zu sein
if (h = hour_of_execution)
and not UpdateExecuted
then begin
Log('
Beginning Update...');
// irgendwas...
Log('
Finished Update');
UpdateExecuted := true;
end;
end;
end;
procedure Tmvideo_sync.ServiceStart(Sender: TService;
var Started: Boolean);
begin
IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + IniFileName);
IniStringList := TStringList.Create;
IniFile.ReadSectionValues('
mvideo_sync', IniStringList);
hour_of_execution := StrToInt(IniStringList.Values ['
hour_of_execution']);
LogFileName := IniStringList.Values ['
logfile'];
videos_dir := IniStringList.Values ['
videos_dir'];
videos_neu_dir := IniStringList.Values ['
videos_neu_dir'];
mvideo_sync_homedir := IniStringList.Values ['
mvideo_sync_homedir'];
testi := '
gesetzt';
Started := true;
UpdateExecuted := false;
Log('
Service started');
end;
procedure Tmvideo_sync.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
showmessage(testi);
Log('
Service stopped');
end;
procedure Tmvideo_sync.ServiceAfterUninstall(Sender: TService);
begin
showmessage(testi);
Log('
Service has been uninstalled');
end;
procedure Tmvideo_sync.ServiceBeforeUninstall(Sender: TService);
begin
showmessage(testi);
Log('
Service is tried to be uninstalled');
end;
procedure Tmvideo_sync.ServiceAfterInstall(Sender: TService);
begin
IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + IniFileName);
IniStringList := TStringList.Create;
IniFile.ReadSectionValues('
mvideo_sync', IniStringList);
hour_of_execution := StrToInt(IniStringList.Values ['
hour_of_execution']);
LogFileName := IniStringList.Values ['
logfile'];
videos_dir := IniStringList.Values ['
videos_dir'];
videos_neu_dir := IniStringList.Values ['
videos_neu_dir'];
mvideo_sync_homedir := IniStringList.Values ['
mvideo_sync_homedir'];
showmessage(testi + IntToStr(hour_of_execution) + '
' + LogFileName + '
' + videos_dir + '
' + videos_neu_dir + '
' + mvideo_sync_homedir);
end;
end.