0

I wrote a small service that basically just writes some text into a .txt file which it just doesnt do. I tested it without the service and writing the file works.

procedure TMyService.ServiceExecute(Sender: TService);
const
  SecBetweenRuns = 10;
var
  Count: Integer;
begin
  Count := 0;
  MyFileWriter:= TMyFileWriter.Create;

  while not Terminated do
  begin
    Inc(Count);
    if Count >= SecBetweenRuns then
    begin
      Count := 0;

      //Here i just try to write a .txt file
      MyFileWriter.WriteFile();

    end;
    Sleep(1000);
    ServiceThread.ProcessRequests(False);
  end;
end;

Do i need special rights for the service?

Kuden
  • 11
  • 1
    Services can easily create and write files. There's a defect in your code most likely. Do some debugging and see why it fails. You'll likely need to debug with something other than interactive debugger. – David Heffernan Apr 27 '20 at 14:55
  • 2
    First, make sure you use an absolute path for the file. And yes, depending on the account under which the service is running, you can have permission issues. – Olivier Apr 27 '20 at 15:06
  • if your service runs under SYSTEM account, it should not have problems writing the file unless the ACL for the folder you are writing to has restrictions for SYSTEM. Make a small console application and verify your filewriter is working as expected. – whosrdaddy Apr 27 '20 at 18:45
  • Hint: never implement OnServiceExecute, spin up a thread on service start and terminate the thread on service shutdown. Because you use sleep in the service loop, it will take 1 second to shutdown the service, never do that. – whosrdaddy Apr 27 '20 at 18:47
  • [here is a service skeleton example](https://stackoverflow.com/questions/10537267/delphi-windows-service-design/10538102#10538102) – whosrdaddy Apr 27 '20 at 19:08
  • @Kuden,when I design services, I always make a TThread descendant class in a separate unit and use this class in a console application, this way you can easily debug your code, when everything works as intended use the same class in your service application... – whosrdaddy Apr 29 '20 at 12:02

0 Answers0