unit uLogService;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;
const
PipeName = '
\\.\pipe\CtrPipe';
type
TService1 =
class(TService)
procedure ServiceShutdown(Sender: TService);
procedure ServiceExecute(Sender: TService);
procedure WriteToLogfile(
const aText:
String);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
private
PipeFileHandle : THandle;
public
function GetServiceController: TServiceController;
override;
end;
var
Service1: TService1;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
Service1.Controller(CtrlCode);
end;
function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService1.ServiceShutdown(Sender: TService);
begin
WriteToLogfile('
Beendet');
end;
procedure TService1.ServiceExecute(Sender: TService);
var
msg : ShortString;
dw : DWORD;
begin
msg := '
'; dw := 0;
WriteToLogfile('
Begin Execute');
while not Terminated
do
begin
ServiceThread.ProcessRequests(False);
Try
ReadFile(PipeFileHandle, msg, sizeof(msg), dw,
nil);
if msg <> '
'
then
begin
WriteToLogfile(msg);
end;
Except
WriteToLogfile('
ReadFile - '+SysErrorMessage(GetLastError));
end;
end;
WriteToLogfile('
End Execute');
end;
procedure TService1.WriteToLogfile(
const aText:
String);
const
logfile = '
\log\log.txt';
var
List : TStringList;
Time :
String;
begin
List := TStringList.Create;
Time := TimeToStr(GetTime);
try
List.LoadFromFile(logfile);
except
List.SaveToFile(logfile);
List.LoadFromFile(logfile);
end;
List.Add(format('
%s Zeit: %s', [aText, Time]));
List.SaveToFile(logfile);
List.Destroy;
end;
procedure TService1.ServiceStart(Sender: TService;
var Started: Boolean);
var
FSA : SECURITY_ATTRIBUTES;
FSD : SECURITY_DESCRIPTOR;
begin
WriteToLogfile('
Started');
InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@FSD, True,
nil, False);
FSA.lpSecurityDescriptor := @FSD;
FSA.nLength := sizeof(SECURITY_ATTRIBUTES);
FSA.bInheritHandle := True;
try
CreateNamedPipe(PipeName, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE
or PIPE_READMODE_MESSAGE
or PIPE_NOWAIT, PIPE_UNLIMITED_INSTANCES, 4096, 4096, 50, @FSA);
try
PipeFileHandle := CreateFile(PipeName, GENERIC_READ, 0, @FSA, OPEN_EXISTING, 0, 0);
except
WriteToLogfile('
CreateFile - '+SysErrorMessage(GetLastError));
end;
except
WriteToLogfile('
CreateNamedPipe - '+SysErrorMessage(GetLastError));
end;
Started := True;
end;
end.