Recent

Author Topic: Daemon realization on Windows  (Read 1320 times)

Molochnik

  • Jr. Member
  • **
  • Posts: 76
Daemon realization on Windows
« on: April 10, 2024, 10:59:14 am »
I tested the daemon realization in Lazarus under Windows and found that TDaemonDef uses UTF-8 strings (Name, Description and others) and they are directly passed to CreateService and ChangeServiceConfig2 winapi functions. Of course it works well only with Ascii symbols. I tried to change them manually using Move but failed probably because they are properties, not strings. Is it possible to make it work without rewriting all daemonapp unit?

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Daemon realization on Windows
« Reply #1 on: April 10, 2024, 11:15:44 am »
You should not use that. Use the example that comes with FPC in rtl-extra.
Years ago Michael wrote a very good article about it. And it is still valid:
https://www.freepascal.org/~michael/articles/daemons/daemons.pdf

That should make it clear?
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Molochnik

  • Jr. Member
  • **
  • Posts: 76
Re: Daemon realization on Windows
« Reply #2 on: April 10, 2024, 11:26:34 am »
Thaddy
Thanks! But I have already read it two times and found no key differences with the daemonapp, maybe I missed something? As I said it works well on Windows (except when non Ascii symbols in the parameters). Under Linux there are some difficulties with SELinux that refuses to run my app as a daemon (I am investigating that yet)
« Last Edit: April 10, 2024, 11:31:43 am by Molochnik »

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Daemon realization on Windows
« Reply #3 on: April 10, 2024, 11:32:29 am »
Well, there's UTF8 again.
On windows, the strings should be unicode16 string and on linux utf8. (Ansi cp_utf8). These are compatible for FPC, so try to use unicodestring as string type in your windows service and UTF8 in your unix daemon.
That should solve the issue.
(Note on Windows, UTF16 has been the native api string type for decades, not years, Ansi is merely proxied... people tend to forget that or ignore it)
And don't use the component, but the class. The component is nonsense in this particular case.
Particularly today, I really do not understand the benefit of visual design for something that is non-visual.
Only for the controller it makes any sense.
This is nothing new: I hold this opinion for decades too. With maybe a small exception for the database access controls, that is.
« Last Edit: April 10, 2024, 11:47:11 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Molochnik

  • Jr. Member
  • **
  • Posts: 76
Re: Daemon realization on Windows
« Reply #4 on: April 10, 2024, 11:51:14 am »
I dont use the component either, just TCustomDaemon and TCustomDaemonMapper. but TCustomDaemonMapper uses the collection of TDaemonDef-s which in turn use UTF-8 strings as parameters. Should I throw it away and rewrite it using UnicodeStrings?
PS And I agree with you against visual components but it doesn't work with Delphi - its TService component cannot be used without a form, at lease i couldnt manage to get rid of it.
« Last Edit: April 10, 2024, 11:56:17 am by Molochnik »

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Daemon realization on Windows
« Reply #5 on: April 10, 2024, 12:08:09 pm »
There is no way that it returns UTF8 strings, since it is part of FPC and not part of Lazarus.
It is Lazarus that mixes things up again with a half-hearted implementation of string as UTF8. Try to explicitly define the string type, either Ansi or UnicodeString, so AnsiString or UnicodeString. UTF8 is not a native string type to the compiler!
« Last Edit: April 10, 2024, 12:11:17 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

cdbc

  • Hero Member
  • *****
  • Posts: 1224
    • http://www.cdbc.dk
Re: Daemon realization on Windows
« Reply #6 on: April 10, 2024, 12:24:01 pm »
Hi
Could you /praysee/ get away with just using:
Code: Pascal  [Select][+][-]
  1. Xxx.SomeUnicodeProperty:= UTF8Decode(SomeLazUtf8String);
  2. // &
  3. SomeLazUtf8String:= UTF8Encode(Xxx.SomeUnicodeProperty);
  4.  
edit: ...on the windows side ofc.
Regards Benny
« Last Edit: April 10, 2024, 12:26:36 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Daemon realization on Windows
« Reply #7 on: April 10, 2024, 12:56:18 pm »
Especially on Windows, that would introduce another level of indirection, Benny.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Molochnik

  • Jr. Member
  • **
  • Posts: 76
Re: Daemon realization on Windows
« Reply #8 on: April 10, 2024, 12:59:38 pm »
cdbc
Could you explain please the code?
As far as I see it the CreateService used in FPC is of an A-version which accepts Ansi strings which of course discregards all non european languages. So what can be done is only for european non English languages - to pass to UTF-8 strings Ansi strings with symbols of 128-255 code encoded in Windows non-Unicode code page.

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Daemon realization on Windows
« Reply #9 on: April 10, 2024, 01:06:30 pm »
In this case you should ignore Benny's answer, as I explained to him.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

paweld

  • Hero Member
  • *****
  • Posts: 1045
Re: Daemon realization on Windows
« Reply #10 on: April 10, 2024, 01:09:11 pm »
DemonMapper content in my projects is something like this:
Code: Pascal  [Select][+][-]
  1. unit SrvMapperU;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, DaemonApp, LazUTF8;
  9.  
  10. type
  11.  
  12.   { TTestServiceMapper }
  13.  
  14.   TTestServiceMapper = class(TDaemonMapper)
  15.   published
  16.     constructor Create(AOwner: TComponent); override;
  17.   end;
  18.  
  19. const
  20.   SrvName = 'TestService';
  21.  
  22. var
  23.   TestServiceMapper: TTestServiceMapper;
  24.  
  25. implementation
  26.  
  27. procedure RegisterMapper;
  28. begin
  29.   RegisterDaemonMapper(TTestServiceMapper)
  30. end;
  31.  
  32. {$R *.lfm}
  33.  
  34. { TTestServiceMapper }
  35.  
  36. constructor TTestServiceMapper.Create(AOwner: TComponent);
  37. var
  38.   ddef: TDaemonDef;
  39. begin
  40.   inherited Create(AOwner);
  41.   ddef := Self.DaemonDefs.Add as TDaemonDef;
  42.   ddef.DaemonClassName := 'TTestService';
  43.   ddef.Name := SrvName;
  44.   ddef.DisplayName := UTF8ToWinCP('Usługa "Test"');
  45.   ddef.Description := UTF8ToWinCP('Usługa systemowa o nazwie Test, łążĆŃÓĘŁĄłąą ');
  46.   ddef.Options := [doAllowStop];
  47.   ddef.WinBindings.StartType := stBoot;
  48.   ddef.WinBindings.WaitHint := 0;
  49.   ddef.WinBindings.IDTag := 0;
  50.   ddef.WinBindings.ServiceType := stWin32;
  51.   ddef.WinBindings.ErrorSeverity := esIgnore;
  52.   ddef.LogStatusReport := False;
  53. end;
  54.  
  55.  
  56. initialization
  57.   RegisterMapper;
  58. end.
  59.  
  60.  
content is displayed correctly in the list of services
« Last Edit: April 10, 2024, 01:15:58 pm by paweld »
Best regards / Pozdrawiam
paweld

Molochnik

  • Jr. Member
  • **
  • Posts: 76
Re: Daemon realization on Windows
« Reply #11 on: April 10, 2024, 01:27:37 pm »
paweld
Thank you very much! That totally resolved my problem :)

 

TinyPortal © 2005-2018