I don't know what you really mean by "systems messages" but services don't uses messages as for the standard windows applications (in windows gui mode, I mean).
Anyway, the only working solution I've found until now is to patch directly the concerned fpc source code (which is of course not really recommended for maintenance purposes).
I've tried to redirect the callback function to my own one:
...
T:=RegisterServiceCtrlHandlerEx(MYDAEMON_NAME,@MyServiceControlHandlerEntry,Controller);
...
function MyServiceControlHandlerEntry(dwControl,dwEventType: DWord; lpEventData,lpContext : Pointer) : DWord; StdCall;
const
SErrNoControlContext = 'Not handling Control message without control context: (%d %d %d).';
begin
If (Nil<>lpContext) then
begin
CallLog('MyServiceController: '+Format('ControlCode - EventType : %d - %d',[dwControl,dwEventType]));
TDaemonController(lpContext).Controller(dwControl,dwEventType,lpEventData)
end
else
Application.Log(etError,Format(SerrNoControlContext,[dwControl,dwEventType,lpEventData]));
result:=NO_ERROR;
end;
This is OK. But I can't modify the reportstatus for it after that, in order to add the SERVICE_ACCEPT_SESSIONCHANGE option using the SetServiceStatus API.
So, either I'm doing it incorrectly (witch is quite possible as Windows seems to be "sensible" to the parameters in the SERVICE_STATUS structure); or it's impossible to modify twice the reportstatus for a given service when it's started (the first call being done into the fpc source code).
Attached a demonstration test with the patched fpc source code, as indicated above. I've just copied the source code from the fpc 2.6.4 version and put it directly into the test project directory (for demonstration purposes only). Only daemonapp.inc has been modified (see comment "Patch 001 ...").
First of all, modify the log filename and path inside the CallLog function in MyDaemon.lpr (see CLOG_FILENAME) according to your own need. I'm not using the application event log any more, because it's off when a shutdown is in process (it took me a while to figure that).
The name of this test service is "Test daemon" (as indicated in D.DisplayName): search for this name in the service manager to interact with it (i.e. start, stop, pause, continue) after you've installed it (i.e. mydaeamon -i).
All the main actions are logged, even the shutdown and the session change calls. The last ones are processed trough the standard HandleCustomCode function (with code=$0B). But you get only the main code here, not the EventType ones (like WTS_SESSION_LOGON and WTS_SESSION_LOGOFF, for instance).